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