Oleksii Molchanov | 7664357 | 2021-07-21 21:54:59 +0300 | [diff] [blame] | 1 | import logging |
| 2 | |
| 3 | logger = logging.getLogger(__name__) |
| 4 | |
| 5 | test_timestamper_groovy = """\ |
| 6 | import hudson.plugins.timestamper.TimestamperConfig |
| 7 | |
| 8 | def timestamper = TimestamperConfig.get() |
| 9 | if (timestamper.isAllPipelines()) { |
| 10 | print("ENABLED") |
| 11 | } else { |
| 12 | print("DISABLED") |
| 13 | } |
| 14 | """ |
| 15 | |
| 16 | enable_timestamper_groovy = """\ |
| 17 | import hudson.plugins.timestamper.TimestamperConfig |
| 18 | |
| 19 | def timestamper = TimestamperConfig.get() |
| 20 | if (!timestamper.isAllPipelines()) { |
| 21 | timestamper.setAllPipelines(true) |
| 22 | print("ENABLED") |
| 23 | } else { |
| 24 | print("ALREADY ENABLED") |
| 25 | } |
| 26 | """ |
| 27 | |
| 28 | disable_timestamper_groovy = """\ |
| 29 | import hudson.plugins.timestamper.TimestamperConfig |
| 30 | |
| 31 | def timestamper = TimestamperConfig.get() |
| 32 | if (timestamper.isAllPipelines()) { |
| 33 | timestamper.setAllPipelines(false) |
| 34 | print("DISABLED") |
| 35 | } else { |
| 36 | print("ALREADY DISABLED") |
| 37 | } |
| 38 | """ |
| 39 | |
| 40 | def __virtual__(): |
| 41 | ''' |
| 42 | Only load if jenkins_common module exist. |
| 43 | ''' |
| 44 | if 'jenkins_common.call_groovy_script' not in __salt__: |
| 45 | return ( |
| 46 | False, |
| 47 | 'The jenkins_timestamper state module cannot be loaded: ' |
| 48 | 'jenkins_common not found') |
| 49 | return True |
| 50 | |
| 51 | |
| 52 | def present(name='timestamper'): |
| 53 | test = __opts__['test'] # noqa |
| 54 | ret = { |
| 55 | 'name': name, |
| 56 | 'changes': {}, |
| 57 | 'result': False, |
| 58 | 'comment': '', |
| 59 | } |
| 60 | result = False |
| 61 | if test: |
| 62 | call_result = __salt__['jenkins_common.call_groovy_script']( |
| 63 | test_timestamper_groovy, {}) |
| 64 | if call_result["code"] == 200 and call_result["msg"] in [ |
| 65 | "DISABLED"]: |
| 66 | ret['comment'] = 'Jenkins timestamper will be enabled' |
| 67 | else: |
| 68 | call_result = __salt__['jenkins_common.call_groovy_script']( |
| 69 | enable_timestamper_groovy, {}) |
| 70 | if call_result["code"] == 200 and call_result["msg"] in [ |
| 71 | "ENABLED", "ALREADY ENABLED"]: |
| 72 | status = call_result["msg"] |
| 73 | if status == "ENABLED": |
| 74 | ret['changes'][name] = status |
| 75 | ret['comment'] = 'Jenkins timestamper config %s %s' % ( |
| 76 | name, status.lower()) |
| 77 | result = True |
| 78 | else: |
| 79 | status = 'FAILED' |
| 80 | logger.error( |
| 81 | "Jenkins timestamper config API call failure: %s", call_result["msg"]) |
| 82 | ret['comment'] = 'Jenkins timestamper config API call failure: %s' % (call_result[ |
| 83 | "msg"]) |
| 84 | ret['result'] = None if test else result |
| 85 | return ret |
| 86 | |
| 87 | |
| 88 | def absent(name='timestamper'): |
| 89 | test = __opts__['test'] # noqa |
| 90 | ret = { |
| 91 | 'name': name, |
| 92 | 'changes': {}, |
| 93 | 'result': False, |
| 94 | 'comment': '', |
| 95 | } |
| 96 | result = False |
| 97 | if test: |
| 98 | call_result = __salt__['jenkins_common.call_groovy_script']( |
| 99 | test_timestamper_groovy, {}) |
| 100 | if call_result["code"] == 200 and call_result["msg"] in [ |
| 101 | "ENABLED"]: |
| 102 | ret['comment'] = 'Jenkins timestamper will be disabled' |
| 103 | else: |
| 104 | call_result = __salt__['jenkins_common.call_groovy_script']( |
| 105 | disable_timestamper_groovy, {}) |
| 106 | if call_result["code"] == 200 and call_result["msg"] in [ |
| 107 | "DISABLED", "ALREADY DISABLED"]: |
| 108 | status = call_result["msg"] |
| 109 | if status == "DISABLED": |
| 110 | ret['changes'][name] = status |
| 111 | ret['comment'] = 'Jenkins timestamper config %s %s' % ( |
| 112 | name, status.lower()) |
| 113 | result = True |
| 114 | else: |
| 115 | status = 'FAILED' |
| 116 | logger.error( |
| 117 | "Jenkins timestamper config API call failure: %s", call_result["msg"]) |
| 118 | ret['comment'] = 'Jenkins timestamper config API call failure: %s' % (call_result[ |
| 119 | "msg"]) |
| 120 | ret['result'] = None if test else result |
| 121 | return ret |
| 122 | |