blob: aa54efd84b566bea1dfb3b9f9448240a41b864e1 [file] [log] [blame]
import logging
logger = logging.getLogger(__name__)
create_node_groovy = u"""\
import jenkins.model.*
import hudson.model.*
import hudson.slaves.*
import hudson.plugins.sshslaves.*
def result=Jenkins.instance.slaves.find{{
it.name == '{name}' &&
it.numExecutors == {num_executors} &&
it.nodeDescription == "{desc}" &&
it.remoteFS == "{remote_home}" &&
it.labelString == "{label}" &&
it.mode == Node.Mode.{node_mode} &&
it.launcher.getClass().getName().equals({launcher}.getClass().getName()) &&
it.retentionStrategy.getClass().getName().equals(new hudson.slaves.RetentionStrategy.{ret_strategy}().getClass().getName())}}
if(result){{
print("EXISTS")
}}else{{
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("CREATED")
}}
""" # 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
configure_master_groovy = u"""\
def instance = Jenkins.instance
def changed = false
if(Jenkins.instance.numExecutors != {num_executors}){{
Jenkins.instance.setNumExecutors({num_executors})
changed = true
}}
if(!Jenkins.instance.mode.name.equals(new String("{node_mode}").toUpperCase())){{
Jenkins.instance.setMode(Node.Mode.{node_mode})
changed = true
}}
if(!Jenkins.instance.labelString.equals("{labels}")){{
Jenkins.instance.setLabelString("{labels}")
changed = true
}}
if(changed){{
Jenkins.instance.save()
print("CREATED")
}}else{{
print("EXISTS")
}}
"""
def __virtual__():
'''
Only load if jenkins_common module exist.
'''
if 'jenkins_common.call_groovy_script' not in __salt__:
return (
False,
'The jenkins_node state module cannot be loaded: '
'jenkins_common not found')
return True
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="", labels=[], 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 desc: node description (optional)
:param labels: node labels list (optional)
: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:
label_string = " ".join(labels)
launcher_string = "new hudson.slaves.JNLPLauncher()"
if launcher:
if launcher["type"] == "ssh":
launcher_string = 'new hudson.plugins.sshslaves.SSHLauncher("{}",{},"{}","{}","","","","","")'.format(
launcher["host"], launcher["port"], launcher["username"], launcher["password"])
elif launcher["type"] == "jnlp":
launcher_string = "new hudson.slaves.JNLPLauncher()"
call_result = __salt__['jenkins_common.call_groovy_script'](
create_node_groovy,
{"name": name,
"desc": desc if desc else "",
"label": label_string if label_string else "",
"remote_home": remote_home if remote_home else "",
"num_executors": num_executors if num_executors else "1",
"launcher": launcher_string,
"node_mode": node_mode.upper(),
"ret_strategy": ret_strategy if ret_strategy else "Always"})
if call_result["code"] == 200 and call_result["msg"] in ["CREATED", "EXISTS"]:
status = call_result["msg"]
if call_result["msg"] == "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
def setup_master(name, num_executors="1", node_mode="Normal", labels=[]):
"""
Jenkins setup master state method
:param name: node name (master)
:param num_executors: number of executors (optional, default 1)
:param node_mode: Node mode (Normal or Exclusive)
:param labels: array of labels
: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'] = 'Master node %s' % (status.lower())
else:
call_result = __salt__['jenkins_common.call_groovy_script'](
configure_master_groovy, {'num_executors': num_executors, 'labels': " ".join(labels), 'node_mode': node_mode.upper()})
if call_result["code"] == 200 and call_result["msg"] in ["CREATED", "EXISTS"]:
status = call_result["msg"]
if status == "CREATED":
ret['changes'][name] = status
ret['comment'] = 'Master node configuration %s ' % (status.lower())
result = True
else:
status = 'FAILED'
logger.error(
"Jenkins master configure API call failure: %s", call_result["msg"])
ret['comment'] = 'Jenkins master configure API call failure: %s' % (
call_result["msg"])
ret['result'] = None if test else result
return ret