Last Updated: February 25, 2016
·
547
· wehappyfew

Building Jenkins on multiple servers

<figure class="tmblr-full" data-orig-height="128" data-orig-width="398">image</figure>

During load-testing with Locustio , I found myself having spawned 10 Locust slaves on AWS. </p>

Unfortunately, Locustio does not have a web interface (or something equivalent) to centrally manage it’s slaves. <br />And the thing with the slaves is that each and every one of them must have the latest and updated code that is going to run on the master.<br />How is that possible? Oh well, with Jenkins CI installed in each slave.<br />So, I had to build the same code in 10 different AWS instances.<br />The first and immediately most productive solution is open 10 Chrome/Firefox tabs and go to the Jenkins urls. But as you can imagine any good engineer that gets bored easily ... well,is bored of the amount of clicks included.</p>

So , I sat down and crafted a small piece of code to build all of them at once.<br />As someone can see I didn’t used their IPs, but if someone wants to do it that way is pretty straightforward.</p>

    def build_jenkins(j_user, j_pass, j_url, job_name):
    """
        # https://github.com/stackforge/python-jenkins
        # http://python-jenkins.readthedocs.org/en/latest/index.html
    """
    import time
    from jenkins import Jenkins

    # set the jenkins object
    j = Jenkins(j_url, j_user, j_pass)

    if j.job_exists(job_name) is True:
        j.build_job(job_name)
        print("\n Build command sent to Jenkins: %s \n"%j_url)
        print("Wait for the build to complete....")
        time.sleep(15)

        last_b = j.get_job_info(job_name)['lastCompletedBuild']['number']
        last_successful_b = j.get_job_info(job_name)['lastSuccessfulBuild']['number']
        # print last_b , last_successful_b #for debugging
        # print "Take extra build info from this:\n" , j.get_job_info(job_name) # for debugging

        print "Checking last build ... "
        print "Last build : %s" % last_b
        print "Last successful build : %s" % last_successful_b, "\n"

        # time.sleep(10)
        if last_b == last_successful_b:
            print "-- Build success --"
        else:
            print "Failed build :["

# Build all locust slaves
jenkins_urls = []
for i in range(1,11):
    # create the jenkins url
    jenkins_url = "http://host%s.site.com:8686/"%i
    # add the jenkins url to the list
    jenkins_urls.append(jenkins_url)
    # print jenkins_urls #for debugging

for url in jenkins_urls:
    build_jenkins(j_user="username", j_pass="password", j_url=url, job_name='JobName')

Well, the thing is that it sends the commands serially. I leave here a note to myself to make it open threads and do it in parallel.</p><a href="https://coderwall.com/wehappyfew">Endorse wehappyfew on Coderwall</a>