Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 1 | import logging |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 2 | |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 3 | logger = logging.getLogger(__name__) |
| 4 | |
| 5 | install_plugin_groovy = """\ |
| 6 | import jenkins.model.* |
| 7 | import java.util.logging.Logger |
| 8 | |
| 9 | def logger = Logger.getLogger("") |
| 10 | def installed = false |
| 11 | def exists = false |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 12 | def pluginName="${plugin}" |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 13 | def instance = Jenkins.getInstance() |
| 14 | def pm = instance.getPluginManager() |
| 15 | def uc = instance.getUpdateCenter() |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 16 | def needUpdateSites(maxOldInSec = 1800){ |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 17 | long oldestTs = 0 |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 18 | for (UpdateSite s : Jenkins.instance.updateCenter.siteList) { |
| 19 | if(oldestTs == 0 || s.getDataTimestamp()<oldestTs){ |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 20 | oldestTs = s.getDataTimestamp() |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 21 | } |
| 22 | } |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 23 | return (System.currentTimeMillis()-oldestTs)/1000 > maxOldInSec |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 24 | } |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 25 | |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 26 | if (!pm.getPlugin(pluginName)) { |
| 27 | if(needUpdateSites()) { |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 28 | uc.updateAllSites() |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 29 | } |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 30 | def plugin = uc.getPlugin(pluginName) |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 31 | if (plugin) { |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 32 | plugin.deploy() |
| 33 | installed = true |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 34 | } |
| 35 | }else{ |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 36 | exists = true |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 37 | print("EXISTS") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 38 | } |
| 39 | if (installed) { |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 40 | instance.save() |
Jakub Josef | a87f8de | 2017-10-24 13:33:13 +0200 | [diff] [blame^] | 41 | if(${restart}){ |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 42 | instance.doSafeRestart() |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 43 | } |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 44 | print("INSTALLED") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 45 | }else if(!exists){ |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 46 | print("FAILED") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 47 | } |
| 48 | """ # noqa |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 49 | |
| 50 | remove_plugin_groovy = """ |
| 51 | import jenkins.model.* |
| 52 | import java.util.logging.Logger |
| 53 | |
| 54 | def logger = Logger.getLogger("") |
| 55 | def installed = false |
| 56 | def initialized = false |
| 57 | |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 58 | def pluginName="${plugin}" |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 59 | def instance = Jenkins.getInstance() |
| 60 | def pm = instance.getPluginManager() |
| 61 | |
| 62 | def actPlugin = pm.getPlugin(pluginName) |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 63 | if (!actPlugin) { |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 64 | def pluginToInstall = Jenkins.instance.updateCenter.getPlugin(pluginName) |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 65 | if(!pluginToInstall){ |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 66 | print("FAILED") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 67 | }else{ |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 68 | print("NOT PRESENT") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 69 | } |
| 70 | } else { |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 71 | actPlugin.disable() |
| 72 | actPlugin.archive.delete() |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 73 | if({restart}){ |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 74 | instance.doSafeRestart() |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 75 | } |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 76 | print("REMOVED") |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 77 | } |
| 78 | """ # noqa |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 79 | |
| 80 | |
Ilya Kharin | 3d8bffe | 2017-06-22 17:40:31 +0400 | [diff] [blame] | 81 | def __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 Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 93 | def 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 Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 101 | return _plugin_call(name, restart, install_plugin_groovy, [ |
| 102 | "INSTALLED", "EXISTS"]) |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 103 | |
| 104 | |
| 105 | def 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 Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 113 | return _plugin_call(name, restart, remove_plugin_groovy, [ |
| 114 | "REMOVED", "NOT PRESENT"]) |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 115 | |
| 116 | |
| 117 | def _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 Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 137 | ret['comment'] = 'Jenkins plugin %s %s%s' % (name, status.lower( |
| 138 | ), ", jenkins restarted" if status == success_msgs[0] and restart else "") |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 139 | 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 Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 145 | "msg"]) |
Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame] | 146 | ret['result'] = None if test else result |
| 147 | return ret |