First version of jenkins nodes enforcing.
Fixed python-bcrypt dependency definition.
Fixed plurals in state file names.
diff --git a/_states/jenkins_node.py b/_states/jenkins_node.py
new file mode 100644
index 0000000..b80fd4d
--- /dev/null
+++ b/_states/jenkins_node.py
@@ -0,0 +1,139 @@
+import logging
+logger = logging.getLogger(__name__)
+
+
+create_node_groovy = u"""\
+import jenkins.model.*
+import hudson.model.*
+import hudson.slaves.*
+import hudson.plugins.sshslaves.*
+import java.util.ArrayList;
+import hudson.slaves.EnvironmentVariablesNodeProperty.Entry;
+
+ Slave slave = new DumbSlave(
+ "{name}",
+ "{desc}",
+ "{remote_home}",
+ "{num_executors}",
+ Node.Mode.{node_mode},
+ "{label}",
+ {launcher},
+ new RetentionStrategy.{ret_strategy}(),
+ new LinkedList())
+ Jenkins.instance.addNode(slave)
+ print "{name}"
+""" # noqa
+
+create_lbl_groovy = u"""\
+hudson = hudson.model.Hudson.instance
+updated = false
+hudson.slaves.find {{ slave -> slave.nodeName.equals("{name}")
+ if({append}){{
+ slave.labelString = slave.labelString + " " + "{lbl_text}"
+ }}else{{
+ slave.labelString = "{lbl_text}"
+ }}
+ updated = true
+ print "{lbl_text}"
+}}
+if(!updated){{
+ print "FAILED"
+}}
+hudson.save()
+""" # noqa
+
+
+def label(name, lbl_text, append=False):
+ """
+ Jenkins node label state method
+
+ :param name: node name
+ :param lbl_text: label text
+ :returns: salt-specified state dict
+ """
+ test = __opts__['test'] # noqa
+ ret = {
+ 'name': name,
+ 'changes': {},
+ 'result': False,
+ 'comment': '',
+ }
+ result = False
+ if test:
+ status = 'CREATED'
+ ret['changes'][name] = status
+ ret['comment'] = 'Label %s %s' % (name, status.lower())
+ else:
+ call_result = __salt__['jenkins_common.call_groovy_script'](
+ create_lbl_groovy, {'name': name, 'lbl_text': lbl_text, 'append': "true" if append else "false"})
+ if call_result["code"] == 200 and call_result["msg"].strip() == lbl_text:
+ status = "CREATED"
+ ret['changes'][name] = status
+ ret['comment'] = 'Label %s %s ' % (name, status.lower())
+ result = True
+ else:
+ status = 'FAILED'
+ logger.error(
+ "Jenkins label API call failure: %s", call_result["msg"])
+ ret['comment'] = 'Jenkins label API call failure: %s' % (
+ call_result["msg"])
+ ret['result'] = None if test else result
+ return ret
+
+
+def present(name, remote_home, launcher, num_executors="1", node_mode="Normal", desc="", label="", ret_strategy="Always"):
+ """
+ Jenkins node state method
+
+ :param name: node name
+ :param remote_home: node remote home path
+ :param launcher: launcher dict with type, name, port, username, password
+ :param num_executors: number of node executurs (optional, default 1)
+ :param node_mode: node mode (optional, default Normal)
+ :param ret_strategy: node retention strategy from RetentionStrategy class
+ :returns: salt-specified state dict
+ """
+ test = __opts__['test'] # noqa
+ ret = {
+ 'name': name,
+ 'changes': {},
+ 'result': False,
+ 'comment': '',
+ }
+ result = False
+ if test:
+ status = 'CREATED'
+ ret['changes'][name] = status
+ ret['comment'] = 'Node %s %s' % (name, status.lower())
+ else:
+ launcher_string = "new JNLPLauncher()"
+ if launcher:
+ if launcher["type"] == "ssh":
+ launcher_string = 'new SSHLauncher("{}",{},"{}","{}","","","","","")'.format(
+ launcher["host"], launcher["port"], launcher["username"], launcher["password"])
+ elif launcher["type"] == "jnlp":
+ launcher_string = "new JNLPLauncher()"
+
+ call_result = __salt__['jenkins_common.call_groovy_script'](
+ create_node_groovy,
+ {"name": name,
+ "desc": desc,
+ "label": label,
+ "remote_home": remote_home,
+ "num_executors": num_executors,
+ "launcher": launcher_string,
+ "node_mode": node_mode.upper(),
+ "ret_strategy": ret_strategy})
+ if call_result["code"] == 200 and call_result["msg"].strip() == name:
+ status = "CREATED"
+ ret['changes'][name] = status
+ ret['comment'] = 'Node %s %s' % (name, status.lower())
+ result = True
+ else:
+ status = 'FAILED'
+ logger.error(
+ "Jenkins node API call failure: %s", call_result["msg"])
+ ret['comment'] = 'Jenkins node API call failure: %s' % (
+ call_result["msg"])
+ ret['result'] = None if test else result
+ return ret