blob: 5e41e79fb7da89530162dd63526db02929f06448 [file] [log] [blame]
Oleksii Molchanov76643572021-07-21 21:54:59 +03001import logging
2
3logger = logging.getLogger(__name__)
4
5test_timestamper_groovy = """\
6import hudson.plugins.timestamper.TimestamperConfig
7
8def timestamper = TimestamperConfig.get()
9if (timestamper.isAllPipelines()) {
10 print("ENABLED")
11} else {
12 print("DISABLED")
13}
14"""
15
16enable_timestamper_groovy = """\
17import hudson.plugins.timestamper.TimestamperConfig
18
19def timestamper = TimestamperConfig.get()
20if (!timestamper.isAllPipelines()) {
21 timestamper.setAllPipelines(true)
22 print("ENABLED")
23} else {
24 print("ALREADY ENABLED")
25}
26"""
27
28disable_timestamper_groovy = """\
29import hudson.plugins.timestamper.TimestamperConfig
30
31def timestamper = TimestamperConfig.get()
32if (timestamper.isAllPipelines()) {
33 timestamper.setAllPipelines(false)
34 print("DISABLED")
35} else {
36 print("ALREADY DISABLED")
37}
38"""
39
40def __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
52def 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
88def 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