Jakub Josef | b0dd773 | 2017-11-16 14:57:04 +0100 | [diff] [blame] | 1 | import logging |
| 2 | |
| 3 | logger = logging.getLogger(__name__) |
| 4 | |
| 5 | add_prop_groovy = """\ |
| 6 | instance = Jenkins.getInstance() |
| 7 | globalNodeProperties = instance.getGlobalNodeProperties() |
| 8 | envVarsNodePropertyList = globalNodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class) |
| 9 | |
| 10 | newEnvVarsNodeProperty = null |
| 11 | envVars = null |
| 12 | |
| 13 | if ( envVarsNodePropertyList == null || envVarsNodePropertyList.size() == 0 ) { |
| 14 | newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty(); |
| 15 | globalNodeProperties.add(newEnvVarsNodeProperty) |
| 16 | envVars = newEnvVarsNodeProperty.getEnvVars() |
| 17 | } else { |
| 18 | envVars = envVarsNodePropertyList.get(0).getEnvVars() |
| 19 | } |
| 20 | if(!envVars.containsKey("${key}") || !envVars.get("${key}").equals("${value}")){ |
| 21 | envVars.put("${key}", "${value}") |
| 22 | instance.save() |
| 23 | print("ADDED/CHANGED") |
| 24 | }else{ |
| 25 | print("EXISTS") |
| 26 | } |
| 27 | """ # noqa |
| 28 | |
| 29 | remove_prop_groovy = """\ |
| 30 | instance = Jenkins.getInstance() |
| 31 | globalNodeProperties = instance.getGlobalNodeProperties() |
| 32 | envVarsNodePropertyList = globalNodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class) |
| 33 | |
| 34 | newEnvVarsNodeProperty = null |
| 35 | envVars = null |
| 36 | |
| 37 | if ( envVarsNodePropertyList == null || envVarsNodePropertyList.size() == 0 ) { |
| 38 | print("NOT PRESENT") |
| 39 | } else { |
| 40 | envVars = envVarsNodePropertyList.get(0).getEnvVars() |
| 41 | if(envVars.containsKey("${key}"){ |
| 42 | envVars.remove("${key}") |
| 43 | print("REMOVED") |
| 44 | instance.save() |
| 45 | }else{ |
| 46 | print("NOT PRESENT") |
| 47 | } |
| 48 | } |
| 49 | """ # noqa |
| 50 | |
| 51 | |
| 52 | def __virtual__(): |
| 53 | ''' |
| 54 | Only load if jenkins_common module exist. |
| 55 | ''' |
| 56 | if 'jenkins_common.call_groovy_script' not in __salt__: |
| 57 | return ( |
| 58 | False, |
| 59 | 'The jenkins_globalenvprop state module cannot be loaded: ' |
| 60 | 'jenkins_common not found') |
| 61 | return True |
| 62 | |
| 63 | |
| 64 | def present(name, value, **kwargs): |
| 65 | """ |
| 66 | Jenkins global env property present state method |
| 67 | |
| 68 | :param name: global env property name |
| 69 | :param value: env property velue |
| 70 | :returns: salt-specified state dict |
| 71 | """ |
| 72 | return _plugin_call(name, value, add_prop_groovy, [ |
| 73 | "ADDED/CHANGED", "EXISTS"], **kwargs) |
| 74 | |
| 75 | |
| 76 | def absent(name, **kwargs): |
| 77 | """ |
| 78 | Jenkins global env property absent state method |
| 79 | |
| 80 | :param name: global env property name |
| 81 | :returns: salt-specified state dict |
| 82 | """ |
| 83 | return _plugin_call(name, None, remove_prop_groovy, [ |
| 84 | "REMOVED", "NOT PRESENT"], **kwargs) |
| 85 | |
| 86 | |
| 87 | def _plugin_call(name, value, template, success_msgs, **kwargs): |
| 88 | test = __opts__['test'] # noqa |
| 89 | ret = { |
| 90 | 'name': name, |
| 91 | 'changes': {}, |
| 92 | 'result': False, |
| 93 | 'comment': '', |
| 94 | } |
| 95 | result = False |
| 96 | if test: |
| 97 | status = success_msgs[0] |
| 98 | ret['changes'][name] = status |
| 99 | ret['comment'] = 'Jenkins global enviroment property %s %s' % (name, status.lower()) |
| 100 | else: |
| 101 | call_result = __salt__['jenkins_common.call_groovy_script']( |
| 102 | template, {"key": name, "value": value}) |
| 103 | if call_result["code"] == 200 and call_result["msg"] in success_msgs: |
| 104 | status = call_result["msg"] |
| 105 | if status == success_msgs[0]: |
| 106 | ret['changes'][name] = status |
| 107 | ret['comment'] = 'Jenkins global enviroment property %s %s' % (name, status.lower()) |
| 108 | result = True |
| 109 | else: |
| 110 | status = 'FAILED' |
| 111 | logger.error( |
| 112 | "Jenkins global enviroment property API call failure: %s", call_result["msg"]) |
| 113 | ret['comment'] = 'Jenkins global enviroment property API call failure: %s' % (call_result[ |
| 114 | "msg"]) |
| 115 | ret['result'] = None if test else result |
| 116 | return ret |