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 = "" |
| 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 Josef | 1bb7f44 | 2017-05-26 17:02:56 +0200 | [diff] [blame] | 69 | |
Dmitry Burmistrov | b4416ef | 2018-04-13 11:22:02 +0400 | [diff] [blame] | 70 | 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 Josef | d2a6203 | 2017-06-06 17:53:21 +0200 | [diff] [blame] | 85 | |
| 86 | def setup_master(name, num_executors="1", node_mode="Normal", labels=[]): |
Jakub Josef | 1bb7f44 | 2017-05-26 17:02:56 +0200 | [diff] [blame] | 87 | """ |
| 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 Josef | d2a6203 | 2017-06-06 17:53:21 +0200 | [diff] [blame] | 93 | :param labels: array of labels |
Jakub Josef | 1bb7f44 | 2017-05-26 17:02:56 +0200 | [diff] [blame] | 94 | :returns: salt-specified state dict |
| 95 | """ |
Dmitry Burmistrov | b4416ef | 2018-04-13 11:22:02 +0400 | [diff] [blame] | 96 | 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 | |