Skip to content

Instantly share code, notes, and snippets.

@omerxx
Last active May 6, 2016 12:09
Show Gist options
  • Save omerxx/8ed7e0da03ba9e02952d4cc78114fa58 to your computer and use it in GitHub Desktop.
Save omerxx/8ed7e0da03ba9e02952d4cc78114fa58 to your computer and use it in GitHub Desktop.
Jenkins SSH node creation with auto registration using python jenkinsapi
from jenkinsapi.jenkins import Jenkins
from jenkinsapi.utils.requester import Requester
from subprocess import check_output
import requests
requests.packages.urllib3.disable_warnings()
# Using AWS AMI allows using the private IP and AWS instance name for the node's registration
# Change these value if you're using other AMI or not using AWS at all
instance_name = check_output(["ec2-metadata", "-i"]).split()[1]
instance_ip = check_output(["ec2-metadata", "-o"]).split()[1]
# Default values, change according to requirements
jenkins_url = "http://127.0.0.1:8080"
username = "default_user"
password = "default_pass"
# In case you're not behind SSL or any auth method you can simply use: api = Jenkins(jenkins_url)
api = Jenkins(jenkins_url, requester=Requester(username, password, baseurl=jenkins_url, ssl_verify=False))
node_dict = {
'num_executors': 4,
'node_description': 'Automation Slave',
'remote_fs': '/tmp',
'labels': 'new_node',
'exclusive': True,
'host': instance_ip, # Can be 'localhost'
'port': 22,
'credential_description': 'Example description', # Mandatory for SSH nodes!
'jvm_options': '-Xmx2000M',
'java_path': '/usr/bin/java',
'prefix_start_slave_cmd': '',
'suffix_start_slave_cmd': '',
'max_num_retries': 0,
'retry_wait_time': 0,
'retention': 'Always', # Can be changes to OnDemand too
'ondemand_delay': 1,
'ondemand_idle_delay': 5
}
slave_name = 'Automation-Slave-({0})'.format(instance_name) # Remove `({0})'.format(instance_name)` if not on AWS
slave_node = api.nodes.create_node(slave_name, node_dict) # Creation of node
print "Done setting node"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment