Jakub Josef | 123be7a | 2016-12-12 16:02:36 +0100 | [diff] [blame] | 1 | import logging |
| 2 | logger = logging.getLogger(__name__) |
| 3 | |
| 4 | |
| 5 | create_node_groovy = u"""\ |
| 6 | import jenkins.model.* |
| 7 | import hudson.model.* |
| 8 | import hudson.slaves.* |
| 9 | import hudson.plugins.sshslaves.* |
Jakub Josef | 123be7a | 2016-12-12 16:02:36 +0100 | [diff] [blame] | 10 | |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 11 | def result=Jenkins.instance.slaves.find{{ |
Jakub Josef | 1bb7f44 | 2017-05-26 17:02:56 +0200 | [diff] [blame] | 12 | it.name == '{name}' && |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 13 | it.numExecutors == {num_executors} && |
Jakub Josef | 1bb7f44 | 2017-05-26 17:02:56 +0200 | [diff] [blame] | 14 | it.nodeDescription == "{desc}" && |
| 15 | it.remoteFS == "{remote_home}" && |
| 16 | it.labelString == "{label}" && |
| 17 | it.mode == Node.Mode.{node_mode} && |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 18 | it.launcher.getClass().getName().equals({launcher}.getClass().getName()) && |
| 19 | it.retentionStrategy.getClass().getName().equals(new hudson.slaves.RetentionStrategy.{ret_strategy}().getClass().getName())}} |
| 20 | if(result){{ |
| 21 | print("EXISTS") |
| 22 | }}else{{ |
Jakub Josef | 123be7a | 2016-12-12 16:02:36 +0100 | [diff] [blame] | 23 | 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 Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 34 | print("CREATED") |
| 35 | }} |
Jakub Josef | 123be7a | 2016-12-12 16:02:36 +0100 | [diff] [blame] | 36 | """ # noqa |
| 37 | |
| 38 | create_lbl_groovy = u"""\ |
| 39 | hudson = hudson.model.Hudson.instance |
| 40 | updated = false |
| 41 | hudson.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 | }} |
| 50 | if(!updated){{ |
| 51 | print "FAILED" |
| 52 | }} |
| 53 | hudson.save() |
| 54 | """ # noqa |
| 55 | |
Jakub Josef | 1bb7f44 | 2017-05-26 17:02:56 +0200 | [diff] [blame] | 56 | configure_master_groovy = u"""\ |
| 57 | def instance = Jenkins.instance |
| 58 | def changed = false |
| 59 | |
| 60 | if(Jenkins.instance.numExecutors != {num_executors}){{ |
| 61 | Jenkins.instance.setNumExecutors({num_executors}) |
| 62 | changed = true |
| 63 | }} |
| 64 | |
| 65 | if(!Jenkins.instance.mode.name.equals(new String("{node_mode}").toUpperCase())){{ |
| 66 | Jenkins.instance.setMode(Node.Mode.{node_mode}) |
| 67 | changed = true |
| 68 | }} |
| 69 | |
| 70 | if(!Jenkins.instance.labelString.equals("{labels}")){{ |
| 71 | Jenkins.instance.setLabelString("{labels}") |
| 72 | changed = true |
| 73 | }} |
| 74 | if(changed){{ |
| 75 | Jenkins.instance.save() |
| 76 | println("SUCCESS") |
| 77 | }}else{{ |
| 78 | println("EXISTS") |
| 79 | }} |
| 80 | """ |
Jakub Josef | 123be7a | 2016-12-12 16:02:36 +0100 | [diff] [blame] | 81 | |
| 82 | def label(name, lbl_text, append=False): |
| 83 | """ |
| 84 | Jenkins node label state method |
| 85 | |
| 86 | :param name: node name |
| 87 | :param lbl_text: label text |
| 88 | :returns: salt-specified state dict |
| 89 | """ |
| 90 | test = __opts__['test'] # noqa |
| 91 | ret = { |
| 92 | 'name': name, |
| 93 | 'changes': {}, |
| 94 | 'result': False, |
| 95 | 'comment': '', |
| 96 | } |
| 97 | result = False |
| 98 | if test: |
| 99 | status = 'CREATED' |
| 100 | ret['changes'][name] = status |
| 101 | ret['comment'] = 'Label %s %s' % (name, status.lower()) |
| 102 | else: |
| 103 | call_result = __salt__['jenkins_common.call_groovy_script']( |
| 104 | create_lbl_groovy, {'name': name, 'lbl_text': lbl_text, 'append': "true" if append else "false"}) |
| 105 | if call_result["code"] == 200 and call_result["msg"].strip() == lbl_text: |
| 106 | status = "CREATED" |
| 107 | ret['changes'][name] = status |
| 108 | ret['comment'] = 'Label %s %s ' % (name, status.lower()) |
| 109 | result = True |
| 110 | else: |
| 111 | status = 'FAILED' |
| 112 | logger.error( |
| 113 | "Jenkins label API call failure: %s", call_result["msg"]) |
| 114 | ret['comment'] = 'Jenkins label API call failure: %s' % ( |
| 115 | call_result["msg"]) |
| 116 | ret['result'] = None if test else result |
| 117 | return ret |
| 118 | |
| 119 | |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 120 | def present(name, remote_home, launcher, num_executors="1", node_mode="Normal", desc="", labels=[], ret_strategy="Always"): |
Jakub Josef | 123be7a | 2016-12-12 16:02:36 +0100 | [diff] [blame] | 121 | """ |
| 122 | Jenkins node state method |
| 123 | |
| 124 | :param name: node name |
| 125 | :param remote_home: node remote home path |
| 126 | :param launcher: launcher dict with type, name, port, username, password |
| 127 | :param num_executors: number of node executurs (optional, default 1) |
| 128 | :param node_mode: node mode (optional, default Normal) |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 129 | :param desc: node description (optional) |
| 130 | :param labels: node labels list (optional) |
Jakub Josef | 123be7a | 2016-12-12 16:02:36 +0100 | [diff] [blame] | 131 | :param ret_strategy: node retention strategy from RetentionStrategy class |
| 132 | :returns: salt-specified state dict |
| 133 | """ |
| 134 | test = __opts__['test'] # noqa |
| 135 | ret = { |
| 136 | 'name': name, |
| 137 | 'changes': {}, |
| 138 | 'result': False, |
| 139 | 'comment': '', |
| 140 | } |
| 141 | result = False |
| 142 | if test: |
| 143 | status = 'CREATED' |
| 144 | ret['changes'][name] = status |
| 145 | ret['comment'] = 'Node %s %s' % (name, status.lower()) |
| 146 | else: |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 147 | label_string = " ".join(labels) |
| 148 | launcher_string = "new hudson.slaves.JNLPLauncher()" |
Jakub Josef | 123be7a | 2016-12-12 16:02:36 +0100 | [diff] [blame] | 149 | if launcher: |
| 150 | if launcher["type"] == "ssh": |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 151 | launcher_string = 'new hudson.plugins.sshslaves.SSHLauncher("{}",{},"{}","{}","","","","","")'.format( |
Jakub Josef | 123be7a | 2016-12-12 16:02:36 +0100 | [diff] [blame] | 152 | launcher["host"], launcher["port"], launcher["username"], launcher["password"]) |
| 153 | elif launcher["type"] == "jnlp": |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 154 | launcher_string = "new hudson.slaves.JNLPLauncher()" |
Jakub Josef | 123be7a | 2016-12-12 16:02:36 +0100 | [diff] [blame] | 155 | |
| 156 | call_result = __salt__['jenkins_common.call_groovy_script']( |
| 157 | create_node_groovy, |
| 158 | {"name": name, |
Jakub Josef | 95ad980 | 2017-01-17 15:59:00 +0100 | [diff] [blame] | 159 | "desc": desc if desc else "", |
| 160 | "label": label_string if label_string else "", |
| 161 | "remote_home": remote_home if remote_home else "", |
| 162 | "num_executors": num_executors if num_executors else "1", |
Jakub Josef | 123be7a | 2016-12-12 16:02:36 +0100 | [diff] [blame] | 163 | "launcher": launcher_string, |
| 164 | "node_mode": node_mode.upper(), |
Jakub Josef | 95ad980 | 2017-01-17 15:59:00 +0100 | [diff] [blame] | 165 | "ret_strategy": ret_strategy if ret_strategy else "Always"}) |
Jakub Josef | 98123ab | 2016-12-14 14:05:01 +0100 | [diff] [blame] | 166 | if call_result["code"] == 200 and call_result["msg"] in ["CREATED", "EXISTS"]: |
| 167 | status = call_result["msg"] |
| 168 | if call_result["msg"] == "CREATED": |
| 169 | ret['changes'][name] = status |
Jakub Josef | 123be7a | 2016-12-12 16:02:36 +0100 | [diff] [blame] | 170 | ret['comment'] = 'Node %s %s' % (name, status.lower()) |
| 171 | result = True |
| 172 | else: |
| 173 | status = 'FAILED' |
| 174 | logger.error( |
| 175 | "Jenkins node API call failure: %s", call_result["msg"]) |
| 176 | ret['comment'] = 'Jenkins node API call failure: %s' % ( |
| 177 | call_result["msg"]) |
| 178 | ret['result'] = None if test else result |
| 179 | return ret |
Jakub Josef | 1bb7f44 | 2017-05-26 17:02:56 +0200 | [diff] [blame] | 180 | |
Jakub Josef | d2a6203 | 2017-06-06 17:53:21 +0200 | [diff] [blame^] | 181 | |
| 182 | def setup_master(name, num_executors="1", node_mode="Normal", labels=[]): |
Jakub Josef | 1bb7f44 | 2017-05-26 17:02:56 +0200 | [diff] [blame] | 183 | """ |
| 184 | Jenkins setup master state method |
| 185 | |
| 186 | :param name: node name (master) |
| 187 | :param num_executors: number of executors (optional, default 1) |
| 188 | :param node_mode: Node mode (Normal or Exclusive) |
Jakub Josef | d2a6203 | 2017-06-06 17:53:21 +0200 | [diff] [blame^] | 189 | :param labels: array of labels |
Jakub Josef | 1bb7f44 | 2017-05-26 17:02:56 +0200 | [diff] [blame] | 190 | :returns: salt-specified state dict |
| 191 | """ |
| 192 | test = __opts__['test'] # noqa |
| 193 | ret = { |
| 194 | 'name': name, |
| 195 | 'changes': {}, |
| 196 | 'result': False, |
| 197 | 'comment': '', |
| 198 | } |
| 199 | result = False |
| 200 | if test: |
| 201 | status = 'CREATED' |
| 202 | ret['changes'][name] = status |
Jakub Josef | d2a6203 | 2017-06-06 17:53:21 +0200 | [diff] [blame^] | 203 | ret['comment'] = 'Master node %s' % (status.lower()) |
Jakub Josef | 1bb7f44 | 2017-05-26 17:02:56 +0200 | [diff] [blame] | 204 | else: |
| 205 | call_result = __salt__['jenkins_common.call_groovy_script']( |
Jakub Josef | d2a6203 | 2017-06-06 17:53:21 +0200 | [diff] [blame^] | 206 | configure_master_groovy, {'num_executors': num_executors, 'lbl_text': " ".join(labels), 'node_mode': node_mode}) |
Jakub Josef | 1bb7f44 | 2017-05-26 17:02:56 +0200 | [diff] [blame] | 207 | if call_result["code"] == 200 and call_result["msg"] in ["CREATED", "EXISTS"]: |
| 208 | status = "CREATED" |
| 209 | ret['changes'][name] = status |
| 210 | ret['comment'] = 'Master node %s ' % (status.lower()) |
| 211 | result = True |
| 212 | else: |
| 213 | status = 'FAILED' |
| 214 | logger.error( |
| 215 | "Jenkins master configure API call failure: %s", call_result["msg"]) |
| 216 | ret['comment'] = 'Jenkins master configure API call failure: %s' % ( |
| 217 | call_result["msg"]) |
| 218 | ret['result'] = None if test else result |
Jakub Josef | d2a6203 | 2017-06-06 17:53:21 +0200 | [diff] [blame^] | 219 | return ret |