blob: cb88fa8671c09d22bdb8ad12571cfa280cbed385 [file] [log] [blame]
Jakub Josef123be7a2016-12-12 16:02:36 +01001import logging
Jakub Josef123be7a2016-12-12 16:02:36 +01002
Adam Tengler70763e02017-08-21 16:50:32 +00003logger = logging.getLogger(__name__)
Jakub Josef123be7a2016-12-12 16:02:36 +01004
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
Adam Tengler70763e02017-08-21 16:50:32 +000011def 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} &&
Adam Tengler70763e02017-08-21 16:50:32 +000014 it.nodeDescription == "${desc}" &&
15 it.remoteFS == "${remote_home}" &&
16 it.labelString == "${label}" &&
Jakub Josef1bb7f442017-05-26 17:02:56 +020017 it.mode == Node.Mode.{node_mode} &&
Jakub Josef98123ab2016-12-14 14:05:01 +010018 it.launcher.getClass().getName().equals({launcher}.getClass().getName()) &&
Adam Tengler70763e02017-08-21 16:50:32 +000019 it.retentionStrategy.getClass().getName().equals(new hudson.slaves.RetentionStrategy.${ret_strategy}().getClass().getName())}
20if(result){
Jakub Josef98123ab2016-12-14 14:05:01 +010021 print("EXISTS")
Adam Tengler70763e02017-08-21 16:50:32 +000022}else{
Jakub Josef123be7a2016-12-12 16:02:36 +010023 Slave slave = new DumbSlave(
Adam Tengler70763e02017-08-21 16:50:32 +000024 "${name}",
25 "${desc}",
26 "${remote_home}",
27 "${num_executors}",
28 Node.Mode.${node_mode},
29 "${label}",
30 ${launcher},
31 new RetentionStrategy.${ret_strategy}(),
Jakub Josef123be7a2016-12-12 16:02:36 +010032 new LinkedList())
33 Jenkins.instance.addNode(slave)
Jakub Josef98123ab2016-12-14 14:05:01 +010034 print("CREATED")
Adam Tengler70763e02017-08-21 16:50:32 +000035}
Jakub Josef123be7a2016-12-12 16:02:36 +010036""" # noqa
37
38create_lbl_groovy = u"""\
39hudson = hudson.model.Hudson.instance
40updated = false
Adam Tengler70763e02017-08-21 16:50:32 +000041hudson.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 }
Jakub Josef123be7a2016-12-12 16:02:36 +010047 updated = true
Adam Tengler70763e02017-08-21 16:50:32 +000048 print "${lbl_text}"
49}
50if(!updated){
Jakub Josef123be7a2016-12-12 16:02:36 +010051 print "FAILED"
Adam Tengler70763e02017-08-21 16:50:32 +000052}
Jakub Josef123be7a2016-12-12 16:02:36 +010053hudson.save()
54""" # noqa
55
Jakub Josef1bb7f442017-05-26 17:02:56 +020056configure_master_groovy = u"""\
57def instance = Jenkins.instance
58def changed = false
59
Adam Tengler70763e02017-08-21 16:50:32 +000060if(Jenkins.instance.numExecutors != ${num_executors}){
61 Jenkins.instance.setNumExecutors(${num_executors})
Jakub Josef1bb7f442017-05-26 17:02:56 +020062 changed = true
Adam Tengler70763e02017-08-21 16:50:32 +000063}
Jakub Josef1bb7f442017-05-26 17:02:56 +020064
Adam Tengler70763e02017-08-21 16:50:32 +000065if(!Jenkins.instance.mode.name.equals(new String("${node_mode}").toUpperCase())){
66 Jenkins.instance.setMode(Node.Mode.${node_mode})
Jakub Josef1bb7f442017-05-26 17:02:56 +020067 changed = true
Adam Tengler70763e02017-08-21 16:50:32 +000068}
Jakub Josef1bb7f442017-05-26 17:02:56 +020069
Adam Tengler70763e02017-08-21 16:50:32 +000070if(!Jenkins.instance.labelString.equals("${labels}")){
71 Jenkins.instance.setLabelString("${labels}")
Jakub Josef1bb7f442017-05-26 17:02:56 +020072 changed = true
Adam Tengler70763e02017-08-21 16:50:32 +000073}
74if(changed){
Jakub Josef1bb7f442017-05-26 17:02:56 +020075 Jenkins.instance.save()
Jakub Josef7339a002017-06-09 11:33:02 +020076 print("CREATED")
Adam Tengler70763e02017-08-21 16:50:32 +000077}else{
Jakub Josef58787542017-06-08 16:59:47 +020078 print("EXISTS")
Adam Tengler70763e02017-08-21 16:50:32 +000079}
Jakub Josef1bb7f442017-05-26 17:02:56 +020080"""
Jakub Josef123be7a2016-12-12 16:02:36 +010081
Ilya Kharin3d8bffe2017-06-22 17:40:31 +040082
83def __virtual__():
84 '''
85 Only load if jenkins_common module exist.
86 '''
87 if 'jenkins_common.call_groovy_script' not in __salt__:
88 return (
89 False,
90 'The jenkins_node state module cannot be loaded: '
91 'jenkins_common not found')
92 return True
93
94
Jakub Josef123be7a2016-12-12 16:02:36 +010095def label(name, lbl_text, append=False):
96 """
97 Jenkins node label state method
98
99 :param name: node name
100 :param lbl_text: label text
101 :returns: salt-specified state dict
102 """
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 = 'CREATED'
113 ret['changes'][name] = status
114 ret['comment'] = 'Label %s %s' % (name, status.lower())
115 else:
116 call_result = __salt__['jenkins_common.call_groovy_script'](
117 create_lbl_groovy, {'name': name, 'lbl_text': lbl_text, 'append': "true" if append else "false"})
Adam Tengler70763e02017-08-21 16:50:32 +0000118 if call_result["code"] == 200 and call_result["msg"].strip(
119 ) == lbl_text:
Jakub Josef123be7a2016-12-12 16:02:36 +0100120 status = "CREATED"
121 ret['changes'][name] = status
122 ret['comment'] = 'Label %s %s ' % (name, status.lower())
123 result = True
124 else:
125 status = 'FAILED'
126 logger.error(
127 "Jenkins label API call failure: %s", call_result["msg"])
128 ret['comment'] = 'Jenkins label API call failure: %s' % (
129 call_result["msg"])
130 ret['result'] = None if test else result
131 return ret
132
133
Adam Tengler70763e02017-08-21 16:50:32 +0000134def present(name, remote_home, launcher, num_executors="1",
135 node_mode="Normal", desc="", labels=[], ret_strategy="Always"):
Jakub Josef123be7a2016-12-12 16:02:36 +0100136 """
137 Jenkins node state method
138
139 :param name: node name
140 :param remote_home: node remote home path
141 :param launcher: launcher dict with type, name, port, username, password
142 :param num_executors: number of node executurs (optional, default 1)
143 :param node_mode: node mode (optional, default Normal)
Jakub Josef98123ab2016-12-14 14:05:01 +0100144 :param desc: node description (optional)
145 :param labels: node labels list (optional)
Jakub Josef123be7a2016-12-12 16:02:36 +0100146 :param ret_strategy: node retention strategy from RetentionStrategy class
147 :returns: salt-specified state dict
148 """
149 test = __opts__['test'] # noqa
150 ret = {
151 'name': name,
152 'changes': {},
153 'result': False,
154 'comment': '',
155 }
156 result = False
157 if test:
158 status = 'CREATED'
159 ret['changes'][name] = status
160 ret['comment'] = 'Node %s %s' % (name, status.lower())
161 else:
Jakub Josef98123ab2016-12-14 14:05:01 +0100162 label_string = " ".join(labels)
163 launcher_string = "new hudson.slaves.JNLPLauncher()"
Jakub Josef123be7a2016-12-12 16:02:36 +0100164 if launcher:
165 if launcher["type"] == "ssh":
Jakub Josef98123ab2016-12-14 14:05:01 +0100166 launcher_string = 'new hudson.plugins.sshslaves.SSHLauncher("{}",{},"{}","{}","","","","","")'.format(
Jakub Josef123be7a2016-12-12 16:02:36 +0100167 launcher["host"], launcher["port"], launcher["username"], launcher["password"])
168 elif launcher["type"] == "jnlp":
Jakub Josef98123ab2016-12-14 14:05:01 +0100169 launcher_string = "new hudson.slaves.JNLPLauncher()"
Jakub Josef123be7a2016-12-12 16:02:36 +0100170
171 call_result = __salt__['jenkins_common.call_groovy_script'](
172 create_node_groovy,
173 {"name": name,
Jakub Josef95ad9802017-01-17 15:59:00 +0100174 "desc": desc if desc else "",
175 "label": label_string if label_string else "",
176 "remote_home": remote_home if remote_home else "",
177 "num_executors": num_executors if num_executors else "1",
Jakub Josef123be7a2016-12-12 16:02:36 +0100178 "launcher": launcher_string,
179 "node_mode": node_mode.upper(),
Jakub Josef95ad9802017-01-17 15:59:00 +0100180 "ret_strategy": ret_strategy if ret_strategy else "Always"})
Adam Tengler70763e02017-08-21 16:50:32 +0000181 if call_result["code"] == 200 and call_result["msg"] in [
182 "CREATED", "EXISTS"]:
Jakub Josef98123ab2016-12-14 14:05:01 +0100183 status = call_result["msg"]
184 if call_result["msg"] == "CREATED":
185 ret['changes'][name] = status
Jakub Josef123be7a2016-12-12 16:02:36 +0100186 ret['comment'] = 'Node %s %s' % (name, status.lower())
187 result = True
188 else:
189 status = 'FAILED'
190 logger.error(
191 "Jenkins node API call failure: %s", call_result["msg"])
192 ret['comment'] = 'Jenkins node API call failure: %s' % (
193 call_result["msg"])
194 ret['result'] = None if test else result
195 return ret
Jakub Josef1bb7f442017-05-26 17:02:56 +0200196
Jakub Josefd2a62032017-06-06 17:53:21 +0200197
198def setup_master(name, num_executors="1", node_mode="Normal", labels=[]):
Jakub Josef1bb7f442017-05-26 17:02:56 +0200199 """
200 Jenkins setup master state method
201
202 :param name: node name (master)
203 :param num_executors: number of executors (optional, default 1)
204 :param node_mode: Node mode (Normal or Exclusive)
Jakub Josefd2a62032017-06-06 17:53:21 +0200205 :param labels: array of labels
Jakub Josef1bb7f442017-05-26 17:02:56 +0200206 :returns: salt-specified state dict
207 """
208 test = __opts__['test'] # noqa
209 ret = {
210 'name': name,
211 'changes': {},
212 'result': False,
213 'comment': '',
214 }
215 result = False
216 if test:
217 status = 'CREATED'
218 ret['changes'][name] = status
Jakub Josefd2a62032017-06-06 17:53:21 +0200219 ret['comment'] = 'Master node %s' % (status.lower())
Jakub Josef1bb7f442017-05-26 17:02:56 +0200220 else:
221 call_result = __salt__['jenkins_common.call_groovy_script'](
Jakub Josef7339a002017-06-09 11:33:02 +0200222 configure_master_groovy, {'num_executors': num_executors, 'labels': " ".join(labels), 'node_mode': node_mode.upper()})
Adam Tengler70763e02017-08-21 16:50:32 +0000223 if call_result["code"] == 200 and call_result["msg"] in [
224 "CREATED", "EXISTS"]:
Jakub Josef58787542017-06-08 16:59:47 +0200225 status = call_result["msg"]
226 if status == "CREATED":
227 ret['changes'][name] = status
228 ret['comment'] = 'Master node configuration %s ' % (status.lower())
Jakub Josef1bb7f442017-05-26 17:02:56 +0200229 result = True
230 else:
231 status = 'FAILED'
232 logger.error(
233 "Jenkins master configure API call failure: %s", call_result["msg"])
234 ret['comment'] = 'Jenkins master configure API call failure: %s' % (
235 call_result["msg"])
236 ret['result'] = None if test else result
Jakub Josefd2a62032017-06-06 17:53:21 +0200237 return ret