Jenkins – Create an agent with a python script

Question:

as the title suggests, I am willing to automate the creation of a jenkins agent using python!

I don’t have any idea about jenkins or what is it even used for, I just got a task to do in my internship and I can’t skip it, so I just installed Jenkins and didn’t anything yet!

I’m sorry for not giving a code sample because I’m literally stuck in that task!
All I have is this django view function:

@login_required
def create(request):
    if request.method == 'POST':
        name = request.POST.get('form-submit')
        description = request.POST.get('employe-description')

        employee = Employee(user=request.user, name=name, description=description)
        employee.save()

        return redirect('create')

    return render(request, 'create.html')

And all I want is when the form is submitted, a jenkins agent will be created using the same name as the employee’s from the request.

Thanks in advance.

Asked By: Nova

||

Answers:

You need to clarify the requirements a bit. What type of agent should it be? Windows, Linux? Or would you have to create it in a Kubernetes cluster or with Docker? In any case, it’s going to be somewhat complex.

As a starting point, since you are using Python, take a look at python-jenkins package. You can use it to interact with Jenkins from within Python. There is detailed documentation for it.

Specifically, look at the section on creating nodes, since that is the task you have been given. You can see a Linux node being created here:

params = {
    'port': '22',
    'username': 'juser',
    'credentialsId': '10f3a3c8-be35-327e-b60b-a3e5edb0e45f',
    'host': 'my.jenkins.slave1'
}
server.create_node(
    'slave1',
    nodeDescription='my test slave',
    remoteFS='/home/juser',
    labels='precise',
    exclusive=True,
    launcher=jenkins.LAUNCHER_SSH,
    launcher_params=params)

There is still a lot going behind the scenes even with this code block, so you will also have to learn a little about Jenkins.

Host: This is your actual agent. If you are running a Linux VM as the agent, the machine should exist with an IP address. It is this IP address you will put here.

Credentials: You need to set up the credentials for your agent separately with the credentials plugin and use it here. A tutorial on credentials and how you use it can be found here. For example, as in the above case, if you are using a Linux VM as the agent, the credentials would be the username and password of that Linux VM.

Port: This is the SSH port, it is usually always 22.

As for the parameters in server.create_node, you can keep them as they are. And as for where you can get the information, you will have to ask your team. The VMs need to be provisioned and ready for use, but that likely isn’t part of your task.

Answered By: M B
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.