blob: 8b3a4b9b96a61d5f382c234322c2aa59af51bd6b [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 = ""
Dmitry Burmistrovb4416ef2018-04-13 11:22:02 +040057 if launcher:
Ivan Berezovskiyc041c3f2019-06-11 17:15:51 +040058 if "jvmopts" in launcher:
59 jvmopts_string = launcher["jvmopts"]
Dmitry Burmistrovb4416ef2018-04-13 11:22:02 +040060 if launcher["type"] == "ssh":
Ivan Berezovskiyc041c3f2019-06-11 17:15:51 +040061 if "credentials" in launcher:
62 # Constructor based on ssh-slaves plugin of 1.29+ versions
63 launcher_string = 'new hudson.plugins.sshslaves.SSHLauncher("{}",{},"{}","{}","","","",null,null,null,null)'.format(
64 launcher["host"], launcher["port"], launcher["credentials"], jvmopts_string)
65 else:
66 # Deprecated: Constructor based on old ssh-slaves plugin versions (~1.17)
67 launcher_string = 'new hudson.plugins.sshslaves.SSHLauncher("{}",{},"{}","{}","","{}","","","")'.format(
68 launcher["host"], launcher["port"], launcher["username"],
69 launcher["password"], jvmopts_string)
Dmitry Burmistrovb4416ef2018-04-13 11:22:02 +040070 elif launcher["type"] == "jnlp":
71 if "tunnel" in launcher:
72 tunnel_string = launcher["tunnel"]
73 launcher_string = 'new hudson.slaves.JNLPLauncher("{}","{}")'.format(
74 tunnel_string, jvmopts_string)
Jakub Josef1bb7f442017-05-26 17:02:56 +020075
Dmitry Burmistrovb4416ef2018-04-13 11:22:02 +040076 return __salt__['jenkins_common.api_call'](name, template,
77 ["CREATED", "EXISTS"],
78 {
79 "name": name,
80 "desc": desc if desc else "",
81 "label": label_string if label_string else "",
82 "remote_home": remote_home if remote_home else "",
83 "num_executors": num_executors if num_executors else "1",
84 "launcher": launcher_string,
85 "tunnel": tunnel_string,
86 "jvmopts": jvmopts_string,
87 "node_mode": node_mode.upper(),
88 "ret_strategy": ret_strategy if ret_strategy else "Always"
89 },
90 'Node')
Jakub Josefd2a62032017-06-06 17:53:21 +020091
92def setup_master(name, num_executors="1", node_mode="Normal", labels=[]):
Jakub Josef1bb7f442017-05-26 17:02:56 +020093 """
94 Jenkins setup master state method
95
96 :param name: node name (master)
97 :param num_executors: number of executors (optional, default 1)
98 :param node_mode: Node mode (Normal or Exclusive)
Jakub Josefd2a62032017-06-06 17:53:21 +020099 :param labels: array of labels
Jakub Josef1bb7f442017-05-26 17:02:56 +0200100 :returns: salt-specified state dict
101 """
Dmitry Burmistrovb4416ef2018-04-13 11:22:02 +0400102 template = __salt__['jenkins_common.load_template'](
103 'salt://jenkins/files/groovy/master_node.template',
104 __env__)
105 return __salt__['jenkins_common.api_call'](name, template,
106 ["CREATED", "EXISTS"],
107 {
108 'num_executors': num_executors,
109 'labels': " ".join(labels),
110 'node_mode': node_mode.upper()
111 },
112 'Master node configuration')
113