blob: c8afacc782acb0b9626349c93066f706edb06ecc [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{{
12 it.name == '{name}' &&
13 it.numExecutors == {num_executors} &&
14 it.nodeDescription == "{desc}" &&
15 it.remoteFS == "{remote_home}" &&
16 it.labelString == "{label}" &&
17 it.mode == Node.Mode.{node_mode} &&
18 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
56
57def label(name, lbl_text, append=False):
58 """
59 Jenkins node label state method
60
61 :param name: node name
62 :param lbl_text: label text
63 :returns: salt-specified state dict
64 """
65 test = __opts__['test'] # noqa
66 ret = {
67 'name': name,
68 'changes': {},
69 'result': False,
70 'comment': '',
71 }
72 result = False
73 if test:
74 status = 'CREATED'
75 ret['changes'][name] = status
76 ret['comment'] = 'Label %s %s' % (name, status.lower())
77 else:
78 call_result = __salt__['jenkins_common.call_groovy_script'](
79 create_lbl_groovy, {'name': name, 'lbl_text': lbl_text, 'append': "true" if append else "false"})
80 if call_result["code"] == 200 and call_result["msg"].strip() == lbl_text:
81 status = "CREATED"
82 ret['changes'][name] = status
83 ret['comment'] = 'Label %s %s ' % (name, status.lower())
84 result = True
85 else:
86 status = 'FAILED'
87 logger.error(
88 "Jenkins label API call failure: %s", call_result["msg"])
89 ret['comment'] = 'Jenkins label API call failure: %s' % (
90 call_result["msg"])
91 ret['result'] = None if test else result
92 return ret
93
94
Jakub Josef98123ab2016-12-14 14:05:01 +010095def present(name, remote_home, launcher, num_executors="1", node_mode="Normal", desc="", labels=[], ret_strategy="Always"):
Jakub Josef123be7a2016-12-12 16:02:36 +010096 """
97 Jenkins node state method
98
99 :param name: node name
100 :param remote_home: node remote home path
101 :param launcher: launcher dict with type, name, port, username, password
102 :param num_executors: number of node executurs (optional, default 1)
103 :param node_mode: node mode (optional, default Normal)
Jakub Josef98123ab2016-12-14 14:05:01 +0100104 :param desc: node description (optional)
105 :param labels: node labels list (optional)
Jakub Josef123be7a2016-12-12 16:02:36 +0100106 :param ret_strategy: node retention strategy from RetentionStrategy class
107 :returns: salt-specified state dict
108 """
109 test = __opts__['test'] # noqa
110 ret = {
111 'name': name,
112 'changes': {},
113 'result': False,
114 'comment': '',
115 }
116 result = False
117 if test:
118 status = 'CREATED'
119 ret['changes'][name] = status
120 ret['comment'] = 'Node %s %s' % (name, status.lower())
121 else:
Jakub Josef98123ab2016-12-14 14:05:01 +0100122 label_string = " ".join(labels)
123 launcher_string = "new hudson.slaves.JNLPLauncher()"
Jakub Josef123be7a2016-12-12 16:02:36 +0100124 if launcher:
125 if launcher["type"] == "ssh":
Jakub Josef98123ab2016-12-14 14:05:01 +0100126 launcher_string = 'new hudson.plugins.sshslaves.SSHLauncher("{}",{},"{}","{}","","","","","")'.format(
Jakub Josef123be7a2016-12-12 16:02:36 +0100127 launcher["host"], launcher["port"], launcher["username"], launcher["password"])
128 elif launcher["type"] == "jnlp":
Jakub Josef98123ab2016-12-14 14:05:01 +0100129 launcher_string = "new hudson.slaves.JNLPLauncher()"
Jakub Josef123be7a2016-12-12 16:02:36 +0100130
131 call_result = __salt__['jenkins_common.call_groovy_script'](
132 create_node_groovy,
133 {"name": name,
Jakub Josef95ad9802017-01-17 15:59:00 +0100134 "desc": desc if desc else "",
135 "label": label_string if label_string else "",
136 "remote_home": remote_home if remote_home else "",
137 "num_executors": num_executors if num_executors else "1",
Jakub Josef123be7a2016-12-12 16:02:36 +0100138 "launcher": launcher_string,
139 "node_mode": node_mode.upper(),
Jakub Josef95ad9802017-01-17 15:59:00 +0100140 "ret_strategy": ret_strategy if ret_strategy else "Always"})
Jakub Josef98123ab2016-12-14 14:05:01 +0100141 if call_result["code"] == 200 and call_result["msg"] in ["CREATED", "EXISTS"]:
142 status = call_result["msg"]
143 if call_result["msg"] == "CREATED":
144 ret['changes'][name] = status
Jakub Josef123be7a2016-12-12 16:02:36 +0100145 ret['comment'] = 'Node %s %s' % (name, status.lower())
146 result = True
147 else:
148 status = 'FAILED'
149 logger.error(
150 "Jenkins node API call failure: %s", call_result["msg"])
151 ret['comment'] = 'Jenkins node API call failure: %s' % (
152 call_result["msg"])
153 ret['result'] = None if test else result
154 return ret