blob: ca0bbaed9b40d87d73b97fa770dc0526d6e3e8ef [file] [log] [blame]
Jakub Josef123be7a2016-12-12 16:02:36 +01001import logging
2logger = logging.getLogger(__name__)
3
4
5create_node_groovy = u"""\
6import jenkins.model.*
7import hudson.model.*
8import hudson.slaves.*
9import hudson.plugins.sshslaves.*
Jakub Josef123be7a2016-12-12 16:02:36 +010010
Jakub Josef98123ab2016-12-14 14:05:01 +010011def result=Jenkins.instance.slaves.find{{
Jakub Josef1bb7f442017-05-26 17:02:56 +020012 it.name == '{name}' &&
Jakub Josef98123ab2016-12-14 14:05:01 +010013 it.numExecutors == {num_executors} &&
Jakub Josef1bb7f442017-05-26 17:02:56 +020014 it.nodeDescription == "{desc}" &&
15 it.remoteFS == "{remote_home}" &&
16 it.labelString == "{label}" &&
17 it.mode == Node.Mode.{node_mode} &&
Jakub Josef98123ab2016-12-14 14:05:01 +010018 it.launcher.getClass().getName().equals({launcher}.getClass().getName()) &&
19 it.retentionStrategy.getClass().getName().equals(new hudson.slaves.RetentionStrategy.{ret_strategy}().getClass().getName())}}
20if(result){{
21 print("EXISTS")
22}}else{{
Jakub Josef123be7a2016-12-12 16:02:36 +010023 Slave slave = new DumbSlave(
24 "{name}",
25 "{desc}",
26 "{remote_home}",
27 "{num_executors}",
28 Node.Mode.{node_mode},
29 "{label}",
30 {launcher},
31 new RetentionStrategy.{ret_strategy}(),
32 new LinkedList())
33 Jenkins.instance.addNode(slave)
Jakub Josef98123ab2016-12-14 14:05:01 +010034 print("CREATED")
35}}
Jakub Josef123be7a2016-12-12 16:02:36 +010036""" # noqa
37
38create_lbl_groovy = u"""\
39hudson = hudson.model.Hudson.instance
40updated = false
41hudson.slaves.find {{ slave -> slave.nodeName.equals("{name}")
42 if({append}){{
43 slave.labelString = slave.labelString + " " + "{lbl_text}"
44 }}else{{
45 slave.labelString = "{lbl_text}"
46 }}
47 updated = true
48 print "{lbl_text}"
49}}
50if(!updated){{
51 print "FAILED"
52}}
53hudson.save()
54""" # noqa
55
Jakub Josef1bb7f442017-05-26 17:02:56 +020056configure_master_groovy = u"""\
57def instance = Jenkins.instance
58def changed = false
59
60if(Jenkins.instance.numExecutors != {num_executors}){{
61 Jenkins.instance.setNumExecutors({num_executors})
62 changed = true
63}}
64
65if(!Jenkins.instance.mode.name.equals(new String("{node_mode}").toUpperCase())){{
66 Jenkins.instance.setMode(Node.Mode.{node_mode})
67 changed = true
68}}
69
70if(!Jenkins.instance.labelString.equals("{labels}")){{
71 Jenkins.instance.setLabelString("{labels}")
72 changed = true
73}}
74if(changed){{
75 Jenkins.instance.save()
76 println("SUCCESS")
77}}else{{
78 println("EXISTS")
79}}
80"""
Jakub Josef123be7a2016-12-12 16:02:36 +010081
82def label(name, lbl_text, append=False):
83 """
84 Jenkins node label state method
85
86 :param name: node name
87 :param lbl_text: label text
88 :returns: salt-specified state dict
89 """
90 test = __opts__['test'] # noqa
91 ret = {
92 'name': name,
93 'changes': {},
94 'result': False,
95 'comment': '',
96 }
97 result = False
98 if test:
99 status = 'CREATED'
100 ret['changes'][name] = status
101 ret['comment'] = 'Label %s %s' % (name, status.lower())
102 else:
103 call_result = __salt__['jenkins_common.call_groovy_script'](
104 create_lbl_groovy, {'name': name, 'lbl_text': lbl_text, 'append': "true" if append else "false"})
105 if call_result["code"] == 200 and call_result["msg"].strip() == lbl_text:
106 status = "CREATED"
107 ret['changes'][name] = status
108 ret['comment'] = 'Label %s %s ' % (name, status.lower())
109 result = True
110 else:
111 status = 'FAILED'
112 logger.error(
113 "Jenkins label API call failure: %s", call_result["msg"])
114 ret['comment'] = 'Jenkins label API call failure: %s' % (
115 call_result["msg"])
116 ret['result'] = None if test else result
117 return ret
118
119
Jakub Josef98123ab2016-12-14 14:05:01 +0100120def present(name, remote_home, launcher, num_executors="1", node_mode="Normal", desc="", labels=[], ret_strategy="Always"):
Jakub Josef123be7a2016-12-12 16:02:36 +0100121 """
122 Jenkins node state method
123
124 :param name: node name
125 :param remote_home: node remote home path
126 :param launcher: launcher dict with type, name, port, username, password
127 :param num_executors: number of node executurs (optional, default 1)
128 :param node_mode: node mode (optional, default Normal)
Jakub Josef98123ab2016-12-14 14:05:01 +0100129 :param desc: node description (optional)
130 :param labels: node labels list (optional)
Jakub Josef123be7a2016-12-12 16:02:36 +0100131 :param ret_strategy: node retention strategy from RetentionStrategy class
132 :returns: salt-specified state dict
133 """
134 test = __opts__['test'] # noqa
135 ret = {
136 'name': name,
137 'changes': {},
138 'result': False,
139 'comment': '',
140 }
141 result = False
142 if test:
143 status = 'CREATED'
144 ret['changes'][name] = status
145 ret['comment'] = 'Node %s %s' % (name, status.lower())
146 else:
Jakub Josef98123ab2016-12-14 14:05:01 +0100147 label_string = " ".join(labels)
148 launcher_string = "new hudson.slaves.JNLPLauncher()"
Jakub Josef123be7a2016-12-12 16:02:36 +0100149 if launcher:
150 if launcher["type"] == "ssh":
Jakub Josef98123ab2016-12-14 14:05:01 +0100151 launcher_string = 'new hudson.plugins.sshslaves.SSHLauncher("{}",{},"{}","{}","","","","","")'.format(
Jakub Josef123be7a2016-12-12 16:02:36 +0100152 launcher["host"], launcher["port"], launcher["username"], launcher["password"])
153 elif launcher["type"] == "jnlp":
Jakub Josef98123ab2016-12-14 14:05:01 +0100154 launcher_string = "new hudson.slaves.JNLPLauncher()"
Jakub Josef123be7a2016-12-12 16:02:36 +0100155
156 call_result = __salt__['jenkins_common.call_groovy_script'](
157 create_node_groovy,
158 {"name": name,
Jakub Josef95ad9802017-01-17 15:59:00 +0100159 "desc": desc if desc else "",
160 "label": label_string if label_string else "",
161 "remote_home": remote_home if remote_home else "",
162 "num_executors": num_executors if num_executors else "1",
Jakub Josef123be7a2016-12-12 16:02:36 +0100163 "launcher": launcher_string,
164 "node_mode": node_mode.upper(),
Jakub Josef95ad9802017-01-17 15:59:00 +0100165 "ret_strategy": ret_strategy if ret_strategy else "Always"})
Jakub Josef98123ab2016-12-14 14:05:01 +0100166 if call_result["code"] == 200 and call_result["msg"] in ["CREATED", "EXISTS"]:
167 status = call_result["msg"]
168 if call_result["msg"] == "CREATED":
169 ret['changes'][name] = status
Jakub Josef123be7a2016-12-12 16:02:36 +0100170 ret['comment'] = 'Node %s %s' % (name, status.lower())
171 result = True
172 else:
173 status = 'FAILED'
174 logger.error(
175 "Jenkins node API call failure: %s", call_result["msg"])
176 ret['comment'] = 'Jenkins node API call failure: %s' % (
177 call_result["msg"])
178 ret['result'] = None if test else result
179 return ret
Jakub Josef1bb7f442017-05-26 17:02:56 +0200180
Jakub Josefd2a62032017-06-06 17:53:21 +0200181
182def setup_master(name, num_executors="1", node_mode="Normal", labels=[]):
Jakub Josef1bb7f442017-05-26 17:02:56 +0200183 """
184 Jenkins setup master state method
185
186 :param name: node name (master)
187 :param num_executors: number of executors (optional, default 1)
188 :param node_mode: Node mode (Normal or Exclusive)
Jakub Josefd2a62032017-06-06 17:53:21 +0200189 :param labels: array of labels
Jakub Josef1bb7f442017-05-26 17:02:56 +0200190 :returns: salt-specified state dict
191 """
192 test = __opts__['test'] # noqa
193 ret = {
194 'name': name,
195 'changes': {},
196 'result': False,
197 'comment': '',
198 }
199 result = False
200 if test:
201 status = 'CREATED'
202 ret['changes'][name] = status
Jakub Josefd2a62032017-06-06 17:53:21 +0200203 ret['comment'] = 'Master node %s' % (status.lower())
Jakub Josef1bb7f442017-05-26 17:02:56 +0200204 else:
205 call_result = __salt__['jenkins_common.call_groovy_script'](
Jakub Josefd2a62032017-06-06 17:53:21 +0200206 configure_master_groovy, {'num_executors': num_executors, 'lbl_text': " ".join(labels), 'node_mode': node_mode})
Jakub Josef1bb7f442017-05-26 17:02:56 +0200207 if call_result["code"] == 200 and call_result["msg"] in ["CREATED", "EXISTS"]:
208 status = "CREATED"
209 ret['changes'][name] = status
210 ret['comment'] = 'Master node %s ' % (status.lower())
211 result = True
212 else:
213 status = 'FAILED'
214 logger.error(
215 "Jenkins master configure API call failure: %s", call_result["msg"])
216 ret['comment'] = 'Jenkins master configure API call failure: %s' % (
217 call_result["msg"])
218 ret['result'] = None if test else result
Jakub Josefd2a62032017-06-06 17:53:21 +0200219 return ret