Jakub Josef | 10b4e10 | 2017-01-05 18:18:41 +0100 | [diff] [blame^] | 1 | import logging |
| 2 | logger = logging.getLogger(__name__) |
| 3 | |
| 4 | install_plugin_groovy = """\ |
| 5 | import jenkins.model.* |
| 6 | import java.util.logging.Logger |
| 7 | |
| 8 | def logger = Logger.getLogger("") |
| 9 | def installed = false |
| 10 | def exists = false |
| 11 | def pluginName="{plugin}" |
| 12 | def instance = Jenkins.getInstance() |
| 13 | def pm = instance.getPluginManager() |
| 14 | def uc = instance.getUpdateCenter() |
| 15 | def 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 | |
| 25 | if (!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 | }} |
| 38 | if (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 | |
| 49 | remove_plugin_groovy = """ |
| 50 | import jenkins.model.* |
| 51 | import java.util.logging.Logger |
| 52 | |
| 53 | def logger = Logger.getLogger("") |
| 54 | def installed = false |
| 55 | def initialized = false |
| 56 | |
| 57 | def pluginName="{plugin}" |
| 58 | def instance = Jenkins.getInstance() |
| 59 | def pm = instance.getPluginManager() |
| 60 | |
| 61 | def actPlugin = pm.getPlugin(pluginName) |
| 62 | if (!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 | |
| 80 | def 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 | |
| 91 | def 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 | |
| 102 | def _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 |