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.* |
| 10 | import java.util.ArrayList; |
| 11 | import hudson.slaves.EnvironmentVariablesNodeProperty.Entry; |
| 12 | |
| 13 | Slave slave = new DumbSlave( |
| 14 | "{name}", |
| 15 | "{desc}", |
| 16 | "{remote_home}", |
| 17 | "{num_executors}", |
| 18 | Node.Mode.{node_mode}, |
| 19 | "{label}", |
| 20 | {launcher}, |
| 21 | new RetentionStrategy.{ret_strategy}(), |
| 22 | new LinkedList()) |
| 23 | Jenkins.instance.addNode(slave) |
| 24 | print "{name}" |
| 25 | """ # noqa |
| 26 | |
| 27 | create_lbl_groovy = u"""\ |
| 28 | hudson = hudson.model.Hudson.instance |
| 29 | updated = false |
| 30 | hudson.slaves.find {{ slave -> slave.nodeName.equals("{name}") |
| 31 | if({append}){{ |
| 32 | slave.labelString = slave.labelString + " " + "{lbl_text}" |
| 33 | }}else{{ |
| 34 | slave.labelString = "{lbl_text}" |
| 35 | }} |
| 36 | updated = true |
| 37 | print "{lbl_text}" |
| 38 | }} |
| 39 | if(!updated){{ |
| 40 | print "FAILED" |
| 41 | }} |
| 42 | hudson.save() |
| 43 | """ # noqa |
| 44 | |
| 45 | |
| 46 | def label(name, lbl_text, append=False): |
| 47 | """ |
| 48 | Jenkins node label state method |
| 49 | |
| 50 | :param name: node name |
| 51 | :param lbl_text: label text |
| 52 | :returns: salt-specified state dict |
| 53 | """ |
| 54 | test = __opts__['test'] # noqa |
| 55 | ret = { |
| 56 | 'name': name, |
| 57 | 'changes': {}, |
| 58 | 'result': False, |
| 59 | 'comment': '', |
| 60 | } |
| 61 | result = False |
| 62 | if test: |
| 63 | status = 'CREATED' |
| 64 | ret['changes'][name] = status |
| 65 | ret['comment'] = 'Label %s %s' % (name, status.lower()) |
| 66 | else: |
| 67 | call_result = __salt__['jenkins_common.call_groovy_script']( |
| 68 | create_lbl_groovy, {'name': name, 'lbl_text': lbl_text, 'append': "true" if append else "false"}) |
| 69 | if call_result["code"] == 200 and call_result["msg"].strip() == lbl_text: |
| 70 | status = "CREATED" |
| 71 | ret['changes'][name] = status |
| 72 | ret['comment'] = 'Label %s %s ' % (name, status.lower()) |
| 73 | result = True |
| 74 | else: |
| 75 | status = 'FAILED' |
| 76 | logger.error( |
| 77 | "Jenkins label API call failure: %s", call_result["msg"]) |
| 78 | ret['comment'] = 'Jenkins label API call failure: %s' % ( |
| 79 | call_result["msg"]) |
| 80 | ret['result'] = None if test else result |
| 81 | return ret |
| 82 | |
| 83 | |
| 84 | def present(name, remote_home, launcher, num_executors="1", node_mode="Normal", desc="", label="", ret_strategy="Always"): |
| 85 | """ |
| 86 | Jenkins node state method |
| 87 | |
| 88 | :param name: node name |
| 89 | :param remote_home: node remote home path |
| 90 | :param launcher: launcher dict with type, name, port, username, password |
| 91 | :param num_executors: number of node executurs (optional, default 1) |
| 92 | :param node_mode: node mode (optional, default Normal) |
| 93 | :param ret_strategy: node retention strategy from RetentionStrategy class |
| 94 | :returns: salt-specified state dict |
| 95 | """ |
| 96 | test = __opts__['test'] # noqa |
| 97 | ret = { |
| 98 | 'name': name, |
| 99 | 'changes': {}, |
| 100 | 'result': False, |
| 101 | 'comment': '', |
| 102 | } |
| 103 | result = False |
| 104 | if test: |
| 105 | status = 'CREATED' |
| 106 | ret['changes'][name] = status |
| 107 | ret['comment'] = 'Node %s %s' % (name, status.lower()) |
| 108 | else: |
| 109 | launcher_string = "new JNLPLauncher()" |
| 110 | if launcher: |
| 111 | if launcher["type"] == "ssh": |
| 112 | launcher_string = 'new SSHLauncher("{}",{},"{}","{}","","","","","")'.format( |
| 113 | launcher["host"], launcher["port"], launcher["username"], launcher["password"]) |
| 114 | elif launcher["type"] == "jnlp": |
| 115 | launcher_string = "new JNLPLauncher()" |
| 116 | |
| 117 | call_result = __salt__['jenkins_common.call_groovy_script']( |
| 118 | create_node_groovy, |
| 119 | {"name": name, |
| 120 | "desc": desc, |
| 121 | "label": label, |
| 122 | "remote_home": remote_home, |
| 123 | "num_executors": num_executors, |
| 124 | "launcher": launcher_string, |
| 125 | "node_mode": node_mode.upper(), |
| 126 | "ret_strategy": ret_strategy}) |
| 127 | if call_result["code"] == 200 and call_result["msg"].strip() == name: |
| 128 | status = "CREATED" |
| 129 | ret['changes'][name] = status |
| 130 | ret['comment'] = 'Node %s %s' % (name, status.lower()) |
| 131 | result = True |
| 132 | else: |
| 133 | status = 'FAILED' |
| 134 | logger.error( |
| 135 | "Jenkins node API call failure: %s", call_result["msg"]) |
| 136 | ret['comment'] = 'Jenkins node API call failure: %s' % ( |
| 137 | call_result["msg"]) |
| 138 | ret['result'] = None if test else result |
| 139 | return ret |