繁体   English   中英

使用rest api调用使用python创建一个新的jenkins slave

[英]Use rest api call to create a new jenkins slave using python

我正在使用 python 自动化我们的 jenkins slave 创建过程,我正在设计一个函数,该函数将 slave 名称和 json 文件作为输入,并在我的 jenkins 实例中创建一个新的 jenkins slave。

我遇到了一个 jenkins 文档,它讨论了这个问题,我有一个可用的 bash 脚本来做到这一点。 但我的要求是一个python脚本。 我的 bash 脚本有效,但是当我将其转换为 python 时却无效。 有人可以帮助我吗?

这是我的bashpython脚本。

#!/bin/bash

export JENKINS_URL=https://jenkins.domain.com
export JENKINS_USER=<user>
export JENKINS_API_TOKEN=<api>
export NODE_NAME=testnode_sep17
export JSON_OBJECT="{ 'name':+'${NODE_NAME}',+'nodeDescription':+'Linux+slave',+'numExecutors':+'5',+'remoteFS':+'/home/jenkins/agent',+'labelString':+'SLAVE-DOCKER+linux',+'mode':+'EXCLUSIVE',+'':+['hudson.slaves.JNLPLauncher',+'hudson.slaves.RetentionStrategy\$Always'],+'launcher':+{'stapler-class':+'hudson.slaves.JNLPLauncher',+'\$class':+'hudson.slaves.JNLPLauncher',+'workDirSettings':+{'disabled':+true,+'workDirPath':+'',+'internalDir':+'remoting',+'failIfWorkDirIsMissing':+false},+'tunnel':+'',+'vmargs':+'-Xmx1024m'},+'retentionStrategy':+{'stapler-class':+'hudson.slaves.RetentionStrategy\$Always',+'\$class':+'hudson.slaves.RetentionStrategy\$Always'},+'nodeProperties':+{'stapler-class-bag':+'true',+'hudson-slaves-EnvironmentVariablesNodeProperty':+{'env':+[{'key':+'JAVA_HOME',+'value':+'/docker-java-home'},+{'key':+'JENKINS_HOME',+'value':+'/home/jenkins'}]},+'hudson-tools-ToolLocationNodeProperty':+{'locations':+[{'key':+'hudson.plugins.git.GitTool\$DescriptorImpl@Default',+'home':+'/usr/bin/git'},+{'key':+'hudson.model.JDK\$DescriptorImpl@JAVA-8',+'home':+'/usr/bin/java'},+{'key':+'hudson.tasks.Maven\$MavenInstallation\$DescriptorImpl@MAVEN-3.5.2',+'home':+'/usr/bin/mvn'}]}}}"
curl -L -s -o /dev/null -v -k -w "%{http_code}" -u "${JENKINS_USER}:${JENKINS_API_TOKEN}" -H "Content-Type:application/x-www-form-urlencoded" -X POST -d "json=${JSON_OBJECT}" "${JENKINS_URL}/computer/doCreateItem?name=${NODE_NAME}&type=hudson.slaves.DumbSlave"

这是python脚本

#!/usr/bin/python

import requests

headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
}

params = (
    ('name', 'test_sep17_TBR'),
    ('type', 'hudson.slaves.DumbSlave'),
)

#data = {
 # 'json': 'file.json'
#}
data = open('my_file.json', 'rb').read()

response = requests.post('http://jenkins.domain.com/computer/doCreateItem', headers=headers, params=params, data=data, verify=False, auth=('user', 'xxxxxxxxxxxxxx'))
print(response.text)

my_file.json 有以下数据

{ 'name':+'${NODE_NAME}',+'nodeDescription':+'Linux+slave',+'numExecutors':+'5',+'remoteFS':+'/home/jenkins/agent',+'labelString':+'SLAVE-DOCKER+linux',+'mode':+'EXCLUSIVE',+'':+['hudson.slaves.JNLPLauncher',+'hudson.slaves.RetentionStrategy\$Always'],+'launcher':+{'stapler-class':+'hudson.slaves.JNLPLauncher',+'\$class':+'hudson.slaves.JNLPLauncher',+'workDirSettings':+{'disabled':+true,+'workDirPath':+'',+'internalDir':+'remoting',+'failIfWorkDirIsMissing':+false},+'tunnel':+'',+'vmargs':+'-Xmx1024m'},+'retentionStrategy':+{'stapler-class':+'hudson.slaves.RetentionStrategy\$Always',+'\$class':+'hudson.slaves.RetentionStrategy\$Always'},+'nodeProperties':+{'stapler-class-bag':+'true',+'hudson-slaves-EnvironmentVariablesNodeProperty':+{'env':+[{'key':+'JAVA_HOME',+'value':+'/docker-java-home'},+{'key':+'JENKINS_HOME',+'value':+'/home/jenkins'}]},+'hudson-tools-ToolLocationNodeProperty':+{'locations':+[{'key':+'hudson.plugins.git.GitTool\$DescriptorImpl@Default',+'home':+'/usr/bin/git'},+{'key':+'hudson.model.JDK\$DescriptorImpl@JAVA-8',+'home':+'/usr/bin/java'},+{'key':+'hudson.tasks.Maven\$MavenInstallation\$DescriptorImpl@MAVEN-3.5.2',+'home':+'/usr/bin/mvn'}]}}}

当我运行 bash 脚本时,会创建从站。 但是当我运行我的 python scrit 时,我得到的错误是

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Error 400 This page expects a form submission</title>
</head>
<body><h2>HTTP ERROR 400</h2>
<p>Problem accessing /computer/doCreateItem. Reason:
<pre>    This page expects a form submission</pre></p><hr><i><small>Powered by Jetty://</small></i><hr/>

</body>
</html>

编辑:我可以使用 python jenkins 模块来创建从站,但我不想使用它

花了几个小时后,我发现了我们都错过的东西:

-d "json=${JSON_OBJECT}"

这意味着它发布了一个字符串,所以在 python 中你需要:

requests.post(url, data=f'json={json.dumps(postdata)}', headers=headers)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM