Jenkins – 创建一个带有 Python 脚本的代理

huangapple go评论73阅读模式
英文:

Jenkins - Create an agent with a python script

问题

根据您的请求,以下是翻译好的部分:

"as the title suggests, I am willing to automate the creation of a jenkins agent using python!" -> "正如标题所示,我愿意使用Python自动创建Jenkins代理!"

"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!" -> "我对Jenkins一无所知,也不知道它究竟用来做什么,我只是在实习中得到了一个任务,不能跳过它,所以我刚刚安装了Jenkins,但还没有做任何事情!"

"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:" -> "我所拥有的只是这个Django视图函数:"

"@login_required" -> "@login_required"

"def create(request):" -> "def create(request):"

"if request.method == 'POST':" -> "if request.method == 'POST':"

"name = request.POST.get('form-submit')" -> "name = request.POST.get('form-submit')"

"description = request.POST.get('employe-description')" -> "description = request.POST.get('employe-description')"

"employee = Employee(user=request.user, name=name, description=description)" -> "employee = Employee(user=request.user, name=name, description=description)"

"employee.save()" -> "employee.save()"

"return redirect('create')" -> "return redirect('create')"

"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." -> "我想要的是当表单提交时,将使用来自请求的员工姓名创建一个Jenkins代理。"

"Thanks in advance." -> "提前感谢您。"

英文:

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.

答案1

得分: 1

你需要澄清一下需求。它应该是什么类型的代理?Windows、Linux?或者你需要在Kubernetes集群或Docker中创建它吗?无论如何,这都会有一定的复杂性。

作为一个起点,由于你在使用Python,可以查看 python-jenkins package。你可以使用它来从Python中与Jenkins交互。有详细的 文档 可供参考。

具体来说,查看 创建节点 部分,因为这是你分配的任务。你可以在这里看到创建Linux节点的示例:

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)

即使在这个代码块中,仍然有许多幕后工作需要完成,所以你还需要了解一些关于Jenkins的知识。

主机: 这是你的实际代理。如果你正在运行一个Linux虚拟机作为代理,那么该机器应该存在并具有IP地址。你将把这个IP地址放在这里。

凭据: 你需要单独为代理设置凭据,使用凭据插件,并在这里使用它。关于凭据以及如何使用它的教程可以在 这里 找到。例如,在上述情况下,如果你正在使用Linux虚拟机作为代理,凭据将是该Linux虚拟机的用户名和密码。

端口: 这是SSH端口,通常始终是22。

至于 server.create_node 中的参数,你可以保持它们不变。至于在哪里获取这些信息,你需要向你的团队提问。虚拟机需要被预配并准备好供使用,但这可能不是你任务的一部分。

英文:

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.

huangapple
  • 本文由 发表于 2023年6月8日 08:22:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定