Ilya Kharin | 3d8bffe | 2017-06-22 17:40:31 +0400 | [diff] [blame] | 1 | def __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 Josef | 123be7a | 2016-12-12 16:02:36 +0100 | [diff] [blame] | 13 | def 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 Burmistrov | b4416ef | 2018-04-13 11:22:02 +0400 | [diff] [blame] | 21 | 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 Josef | 123be7a | 2016-12-12 16:02:36 +0100 | [diff] [blame] | 32 | |
| 33 | |
Adam Tengler | 70763e0 | 2017-08-21 16:50:32 +0000 | [diff] [blame] | 34 | def present(name, remote_home, launcher, num_executors="1", |
| 35 | node_mode="Normal", desc="", labels=[], ret_strategy="Always"): |
Jakub Josef | 123be7a | 2016-12-12 16:02:36 +0100 | [diff] [blame] | 36 | """ |
| 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 Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 44 | :param desc: node description (optional) |
| 45 | :param labels: node labels list (optional) |
Jakub Josef | 123be7a | 2016-12-12 16:02:36 +0100 | [diff] [blame] | 46 | :param ret_strategy: node retention strategy from RetentionStrategy class |
| 47 | :returns: salt-specified state dict |
| 48 | """ |
Dmitry Burmistrov | b4416ef | 2018-04-13 11:22:02 +0400 | [diff] [blame] | 49 | template = __salt__['jenkins_common.load_template']( |
| 50 | 'salt://jenkins/files/groovy/node.template', |
| 51 | __env__) |
Jakub Josef | 123be7a | 2016-12-12 16:02:36 +0100 | [diff] [blame] | 52 | |
Dmitry Burmistrov | b4416ef | 2018-04-13 11:22:02 +0400 | [diff] [blame] | 53 | label_string = " ".join(labels) |
| 54 | launcher_string = "new hudson.slaves.JNLPLauncher()" |
| 55 | tunnel_string = "" |
| 56 | jvmopts_string = "" |
Dmitry Burmistrov | b4416ef | 2018-04-13 11:22:02 +0400 | [diff] [blame] | 57 | if launcher: |
Ivan Berezovskiy | c041c3f | 2019-06-11 17:15:51 +0400 | [diff] [blame] | 58 | if "jvmopts" in launcher: |
| 59 | jvmopts_string = launcher["jvmopts"] |
Dmitry Burmistrov | b4416ef | 2018-04-13 11:22:02 +0400 | [diff] [blame] | 60 | if launcher["type"] == "ssh": |
Ivan Berezovskiy | c041c3f | 2019-06-11 17:15:51 +0400 | [diff] [blame] | 61 | 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 Burmistrov | b4416ef | 2018-04-13 11:22:02 +0400 | [diff] [blame] | 70 | 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 Josef | 1bb7f44 | 2017-05-26 17:02:56 +0200 | [diff] [blame] | 75 | |
Dmitry Burmistrov | b4416ef | 2018-04-13 11:22:02 +0400 | [diff] [blame] | 76 | 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 Josef | d2a6203 | 2017-06-06 17:53:21 +0200 | [diff] [blame] | 91 | |
| 92 | def setup_master(name, num_executors="1", node_mode="Normal", labels=[]): |
Jakub Josef | 1bb7f44 | 2017-05-26 17:02:56 +0200 | [diff] [blame] | 93 | """ |
| 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 Josef | d2a6203 | 2017-06-06 17:53:21 +0200 | [diff] [blame] | 99 | :param labels: array of labels |
Jakub Josef | 1bb7f44 | 2017-05-26 17:02:56 +0200 | [diff] [blame] | 100 | :returns: salt-specified state dict |
| 101 | """ |
Dmitry Burmistrov | b4416ef | 2018-04-13 11:22:02 +0400 | [diff] [blame] | 102 | 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 | |