blob: 29e9c2c9e6eadaeff216dd587a3fc1149d4caf11 [file] [log] [blame]
Ilya Kharin3d8bffe2017-06-22 17:40:31 +04001def __virtual__():
2 '''
3 Only load if jenkins_common module exist.
4 '''
5 if 'jenkins_common.call_groovy_script' not in __salt__:
6 return (
7 False,
8 'The jenkins_node state module cannot be loaded: '
9 'jenkins_common not found')
10 return True
11
12
Jakub Josef123be7a2016-12-12 16:02:36 +010013def label(name, lbl_text, append=False):
14 """
15 Jenkins node label state method
16
17 :param name: node name
18 :param lbl_text: label text
19 :returns: salt-specified state dict
20 """
Dmitry Burmistrovb4416ef2018-04-13 11:22:02 +040021 template = __salt__['jenkins_common.load_template'](
22 'salt://jenkins/files/groovy/node_label.template',
23 __env__)
24 return __salt__['jenkins_common.api_call'](name, template,
25 ["CREATED", lbl_text],
26 {
27 'name': name,
28 'lbl_text': lbl_text,
29 'append': "true" if append else "false"
30 },
31 'Node Label')
Jakub Josef123be7a2016-12-12 16:02:36 +010032
33
Adam Tengler70763e02017-08-21 16:50:32 +000034def present(name, remote_home, launcher, num_executors="1",
35 node_mode="Normal", desc="", labels=[], ret_strategy="Always"):
Jakub Josef123be7a2016-12-12 16:02:36 +010036 """
37 Jenkins node state method
38
39 :param name: node name
40 :param remote_home: node remote home path
41 :param launcher: launcher dict with type, name, port, username, password
42 :param num_executors: number of node executurs (optional, default 1)
43 :param node_mode: node mode (optional, default Normal)
Jakub Josef98123ab2016-12-14 14:05:01 +010044 :param desc: node description (optional)
45 :param labels: node labels list (optional)
Jakub Josef123be7a2016-12-12 16:02:36 +010046 :param ret_strategy: node retention strategy from RetentionStrategy class
47 :returns: salt-specified state dict
48 """
Dmitry Burmistrovb4416ef2018-04-13 11:22:02 +040049 template = __salt__['jenkins_common.load_template'](
50 'salt://jenkins/files/groovy/node.template',
51 __env__)
Jakub Josef123be7a2016-12-12 16:02:36 +010052
Dmitry Burmistrovb4416ef2018-04-13 11:22:02 +040053 label_string = " ".join(labels)
54 launcher_string = "new hudson.slaves.JNLPLauncher()"
55 tunnel_string = ""
56 jvmopts_string = ""
57 if "jvmopts" in launcher:
58 jvmopts_string = launcher["jvmopts"]
59 if launcher:
60 if launcher["type"] == "ssh":
61 launcher_string = 'new hudson.plugins.sshslaves.SSHLauncher("{}",{},"{}","{}","","{}","","","")'.format(
62 launcher["host"], launcher["port"], launcher["username"],
63 launcher["password"], jvmopts_string)
64 elif launcher["type"] == "jnlp":
65 if "tunnel" in launcher:
66 tunnel_string = launcher["tunnel"]
67 launcher_string = 'new hudson.slaves.JNLPLauncher("{}","{}")'.format(
68 tunnel_string, jvmopts_string)
Jakub Josef1bb7f442017-05-26 17:02:56 +020069
Dmitry Burmistrovb4416ef2018-04-13 11:22:02 +040070 return __salt__['jenkins_common.api_call'](name, template,
71 ["CREATED", "EXISTS"],
72 {
73 "name": name,
74 "desc": desc if desc else "",
75 "label": label_string if label_string else "",
76 "remote_home": remote_home if remote_home else "",
77 "num_executors": num_executors if num_executors else "1",
78 "launcher": launcher_string,
79 "tunnel": tunnel_string,
80 "jvmopts": jvmopts_string,
81 "node_mode": node_mode.upper(),
82 "ret_strategy": ret_strategy if ret_strategy else "Always"
83 },
84 'Node')
Jakub Josefd2a62032017-06-06 17:53:21 +020085
86def setup_master(name, num_executors="1", node_mode="Normal", labels=[]):
Jakub Josef1bb7f442017-05-26 17:02:56 +020087 """
88 Jenkins setup master state method
89
90 :param name: node name (master)
91 :param num_executors: number of executors (optional, default 1)
92 :param node_mode: Node mode (Normal or Exclusive)
Jakub Josefd2a62032017-06-06 17:53:21 +020093 :param labels: array of labels
Jakub Josef1bb7f442017-05-26 17:02:56 +020094 :returns: salt-specified state dict
95 """
Dmitry Burmistrovb4416ef2018-04-13 11:22:02 +040096 template = __salt__['jenkins_common.load_template'](
97 'salt://jenkins/files/groovy/master_node.template',
98 __env__)
99 return __salt__['jenkins_common.api_call'](name, template,
100 ["CREATED", "EXISTS"],
101 {
102 'num_executors': num_executors,
103 'labels': " ".join(labels),
104 'node_mode': node_mode.upper()
105 },
106 'Master node configuration')
107