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

2024-09-27 07:17:40 发布

您现在位置: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脚本

^{pr2}$

我的_文件.json有以下数据

^{3}$

当我运行bash脚本时,slave被创建。 但是当我运行python脚本时,我得到的错误是

<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>

编辑:我可以使用pythonjenkins模块创建slave,但我不想使用它


Tags: keyname脚本bashnodejsonhomebin

热门问题