gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:00:43 31 4
gpt4 key购买 nike

我正在使用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)

关于python - 使用rest api调用使用python创建一个新的jenkins slave,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57970708/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com