blob: 0f80d518655ee3ddaaa7924d3973ac723702b061 [file] [log] [blame]
Jakub Josef10b4e102017-01-05 18:18:41 +01001import logging
Adam Tengler70763e02017-08-21 16:50:32 +00002
Jakub Josef10b4e102017-01-05 18:18:41 +01003logger = logging.getLogger(__name__)
4
5install_plugin_groovy = """\
6import jenkins.model.*
7import java.util.logging.Logger
8
9def logger = Logger.getLogger("")
10def installed = false
11def exists = false
Adam Tengler70763e02017-08-21 16:50:32 +000012def pluginName="${plugin}"
Jakub Josef10b4e102017-01-05 18:18:41 +010013def instance = Jenkins.getInstance()
14def pm = instance.getPluginManager()
15def uc = instance.getUpdateCenter()
Adam Tengler70763e02017-08-21 16:50:32 +000016def needUpdateSites(maxOldInSec = 1800){
Jakub Josef10b4e102017-01-05 18:18:41 +010017 long oldestTs = 0
Adam Tengler70763e02017-08-21 16:50:32 +000018 for (UpdateSite s : Jenkins.instance.updateCenter.siteList) {
19 if(oldestTs == 0 || s.getDataTimestamp()<oldestTs){
Jakub Josef10b4e102017-01-05 18:18:41 +010020 oldestTs = s.getDataTimestamp()
Adam Tengler70763e02017-08-21 16:50:32 +000021 }
22 }
Jakub Josef10b4e102017-01-05 18:18:41 +010023 return (System.currentTimeMillis()-oldestTs)/1000 > maxOldInSec
Adam Tengler70763e02017-08-21 16:50:32 +000024}
Jakub Josef10b4e102017-01-05 18:18:41 +010025
Adam Tengler70763e02017-08-21 16:50:32 +000026if (!pm.getPlugin(pluginName)) {
27 if(needUpdateSites()) {
Jakub Josef10b4e102017-01-05 18:18:41 +010028 uc.updateAllSites()
Adam Tengler70763e02017-08-21 16:50:32 +000029 }
Jakub Josef10b4e102017-01-05 18:18:41 +010030 def plugin = uc.getPlugin(pluginName)
Adam Tengler70763e02017-08-21 16:50:32 +000031 if (plugin) {
Jakub Josef10b4e102017-01-05 18:18:41 +010032 plugin.deploy()
33 installed = true
Adam Tengler70763e02017-08-21 16:50:32 +000034 }
35}else{
Jakub Josef10b4e102017-01-05 18:18:41 +010036 exists = true
Jakub Josef10b4e102017-01-05 18:18:41 +010037 print("EXISTS")
Adam Tengler70763e02017-08-21 16:50:32 +000038}
39if (installed) {
Jakub Josef10b4e102017-01-05 18:18:41 +010040 instance.save()
Jakub Josefa87f8de2017-10-24 13:33:13 +020041 if(${restart}){
Jakub Josef10b4e102017-01-05 18:18:41 +010042 instance.doSafeRestart()
Adam Tengler70763e02017-08-21 16:50:32 +000043 }
Jakub Josef10b4e102017-01-05 18:18:41 +010044 print("INSTALLED")
Adam Tengler70763e02017-08-21 16:50:32 +000045}else if(!exists){
Jakub Josef10b4e102017-01-05 18:18:41 +010046 print("FAILED")
Adam Tengler70763e02017-08-21 16:50:32 +000047}
48""" # noqa
Jakub Josef10b4e102017-01-05 18:18:41 +010049
50remove_plugin_groovy = """
51import jenkins.model.*
52import java.util.logging.Logger
53
54def logger = Logger.getLogger("")
55def installed = false
56def initialized = false
57
Adam Tengler70763e02017-08-21 16:50:32 +000058def pluginName="${plugin}"
Jakub Josef10b4e102017-01-05 18:18:41 +010059def instance = Jenkins.getInstance()
60def pm = instance.getPluginManager()
61
62def actPlugin = pm.getPlugin(pluginName)
Adam Tengler70763e02017-08-21 16:50:32 +000063if (!actPlugin) {
Jakub Josef10b4e102017-01-05 18:18:41 +010064 def pluginToInstall = Jenkins.instance.updateCenter.getPlugin(pluginName)
Adam Tengler70763e02017-08-21 16:50:32 +000065 if(!pluginToInstall){
Jakub Josef10b4e102017-01-05 18:18:41 +010066 print("FAILED")
Adam Tengler70763e02017-08-21 16:50:32 +000067 }else{
Jakub Josef10b4e102017-01-05 18:18:41 +010068 print("NOT PRESENT")
Adam Tengler70763e02017-08-21 16:50:32 +000069 }
70} else {
Jakub Josef10b4e102017-01-05 18:18:41 +010071 actPlugin.disable()
72 actPlugin.archive.delete()
Adam Tengler70763e02017-08-21 16:50:32 +000073 if({restart}){
Jakub Josef10b4e102017-01-05 18:18:41 +010074 instance.doSafeRestart()
Adam Tengler70763e02017-08-21 16:50:32 +000075 }
Jakub Josef10b4e102017-01-05 18:18:41 +010076 print("REMOVED")
Adam Tengler70763e02017-08-21 16:50:32 +000077}
78""" # noqa
Jakub Josef10b4e102017-01-05 18:18:41 +010079
80
Ilya Kharin3d8bffe2017-06-22 17:40:31 +040081def __virtual__():
82 '''
83 Only load if jenkins_common module exist.
84 '''
85 if 'jenkins_common.call_groovy_script' not in __salt__:
86 return (
87 False,
88 'The jenkins_plugin state module cannot be loaded: '
89 'jenkins_common not found')
90 return True
91
92
Jakub Josef10b4e102017-01-05 18:18:41 +010093def present(name, restart=False):
94 """
95 Jenkins plugin present state method, for installing plugins
96
97 :param name: plugin name
98 :param restart: do you want to restart jenkins after plugin install?
99 :returns: salt-specified state dict
100 """
Adam Tengler70763e02017-08-21 16:50:32 +0000101 return _plugin_call(name, restart, install_plugin_groovy, [
102 "INSTALLED", "EXISTS"])
Jakub Josef10b4e102017-01-05 18:18:41 +0100103
104
105def absent(name, restart=False):
106 """
107 Jenkins plugin absent state method, for removing plugins
108
109 :param name: plugin name
110 :param restart: do you want to restart jenkins after plugin remove?
111 :returns: salt-specified state dict
112 """
Adam Tengler70763e02017-08-21 16:50:32 +0000113 return _plugin_call(name, restart, remove_plugin_groovy, [
114 "REMOVED", "NOT PRESENT"])
Jakub Josef10b4e102017-01-05 18:18:41 +0100115
116
117def _plugin_call(name, restart, template, success_msgs):
118 test = __opts__['test'] # noqa
119 ret = {
120 'name': name,
121 'changes': {},
122 'result': False,
123 'comment': '',
124 }
125 result = False
126 if test:
127 status = success_msgs[0]
128 ret['changes'][name] = status
129 ret['comment'] = 'Jenkins plugin %s %s' % (name, status.lower())
130 else:
131 call_result = __salt__['jenkins_common.call_groovy_script'](
132 template, {"plugin": name, "restart": "true" if restart else "false"})
133 if call_result["code"] == 200 and call_result["msg"] in success_msgs:
134 status = call_result["msg"]
135 if status == success_msgs[0]:
136 ret['changes'][name] = status
Adam Tengler70763e02017-08-21 16:50:32 +0000137 ret['comment'] = 'Jenkins plugin %s %s%s' % (name, status.lower(
138 ), ", jenkins restarted" if status == success_msgs[0] and restart else "")
Jakub Josef10b4e102017-01-05 18:18:41 +0100139 result = True
140 else:
141 status = 'FAILED'
142 logger.error(
143 "Jenkins plugin API call failure: %s", call_result["msg"])
144 ret['comment'] = 'Jenkins plugin API call failure: %s' % (call_result[
Adam Tengler70763e02017-08-21 16:50:32 +0000145 "msg"])
Jakub Josef10b4e102017-01-05 18:18:41 +0100146 ret['result'] = None if test else result
147 return ret