blob: 49567519743747fd3c465459078201ecbc007e40 [file] [log] [blame]
Jakub Josef10b4e102017-01-05 18:18:41 +01001import logging
2logger = logging.getLogger(__name__)
3
4install_plugin_groovy = """\
5import jenkins.model.*
6import java.util.logging.Logger
7
8def logger = Logger.getLogger("")
9def installed = false
10def exists = false
11def pluginName="{plugin}"
12def instance = Jenkins.getInstance()
13def pm = instance.getPluginManager()
14def uc = instance.getUpdateCenter()
15def needUpdateSites(maxOldInSec = 1800){{
16 long oldestTs = 0
17 for (UpdateSite s : Jenkins.instance.updateCenter.siteList) {{
18 if(oldestTs == 0 || s.getDataTimestamp()<oldestTs){{
19 oldestTs = s.getDataTimestamp()
20 }}
21 }}
22 return (System.currentTimeMillis()-oldestTs)/1000 > maxOldInSec
23}}
24
25if (!pm.getPlugin(pluginName)) {{
26 if(needUpdateSites()) {{
27 uc.updateAllSites()
28 }}
29 def plugin = uc.getPlugin(pluginName)
30 if (plugin) {{
31 plugin.deploy()
32 installed = true
33 }}
34}}else{{
35 exists = true
36 print("EXISTS")
37}}
38if (installed) {{
39 instance.save()
40 if({restart}){{
41 instance.doSafeRestart()
42 }}
43 print("INSTALLED")
44}}else if(!exists){{
45 print("FAILED")
46}}
47""" # noqa
48
49remove_plugin_groovy = """
50import jenkins.model.*
51import java.util.logging.Logger
52
53def logger = Logger.getLogger("")
54def installed = false
55def initialized = false
56
57def pluginName="{plugin}"
58def instance = Jenkins.getInstance()
59def pm = instance.getPluginManager()
60
61def actPlugin = pm.getPlugin(pluginName)
62if (!actPlugin) {{
63 def pluginToInstall = Jenkins.instance.updateCenter.getPlugin(pluginName)
64 if(!pluginToInstall){{
65 print("FAILED")
66 }}else{{
67 print("NOT PRESENT")
68 }}
69}} else {{
70 actPlugin.disable()
71 actPlugin.archive.delete()
72 if({restart}){{
73 instance.doSafeRestart()
74 }}
75 print("REMOVED")
76}}
77""" # noqa
78
79
80def present(name, restart=False):
81 """
82 Jenkins plugin present state method, for installing plugins
83
84 :param name: plugin name
85 :param restart: do you want to restart jenkins after plugin install?
86 :returns: salt-specified state dict
87 """
88 return _plugin_call(name, restart, install_plugin_groovy, ["INSTALLED", "EXISTS"])
89
90
91def absent(name, restart=False):
92 """
93 Jenkins plugin absent state method, for removing plugins
94
95 :param name: plugin name
96 :param restart: do you want to restart jenkins after plugin remove?
97 :returns: salt-specified state dict
98 """
99 return _plugin_call(name, restart, remove_plugin_groovy, ["REMOVED", "NOT PRESENT"])
100
101
102def _plugin_call(name, restart, template, success_msgs):
103 test = __opts__['test'] # noqa
104 ret = {
105 'name': name,
106 'changes': {},
107 'result': False,
108 'comment': '',
109 }
110 result = False
111 if test:
112 status = success_msgs[0]
113 ret['changes'][name] = status
114 ret['comment'] = 'Jenkins plugin %s %s' % (name, status.lower())
115 else:
116 call_result = __salt__['jenkins_common.call_groovy_script'](
117 template, {"plugin": name, "restart": "true" if restart else "false"})
118 if call_result["code"] == 200 and call_result["msg"] in success_msgs:
119 status = call_result["msg"]
120 if status == success_msgs[0]:
121 ret['changes'][name] = status
122 ret['comment'] = 'Jenkins plugin %s %s%s' % (name, status.lower(), ", jenkins restarted" if status == success_msgs[0] and restart else "")
123 result = True
124 else:
125 status = 'FAILED'
126 logger.error(
127 "Jenkins plugin API call failure: %s", call_result["msg"])
128 ret['comment'] = 'Jenkins plugin API call failure: %s' % (call_result[
129 "msg"])
130 ret['result'] = None if test else result
131 return ret