python jenkins api

#!/usr/bin/python
import sys, time
import shutil, commands
#coding=utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import jenkins

config_file = 'jenkins_list.txt'
jenkins_url='http://qc-xxx.xxxx.com/'
user_name='user'
pass_word='password'
server = jenkins.Jenkins(jenkins_url, username=user_name, password=pass_word)
temp_job_name='saas-kf-aal-online'
tem_xml_file='tem.xml'


#print server.get_whoami()

#server.copy_job("xx-xx-xx-online", "xx-xx-copy-xxx-online")
#print server.get_job_info("saas-kf-copy-aal-online", depth=2, fetch_all_builds=True)
#print server.get_job_config("saas-kf-copy-aal-online")
#with open('aa.xml', 'r') as xml_file:
# config_data = xml_file.read()
#server.reconfig_job("saas-kf-copy-aal-online",config_data)

def commands_shell(cmd):
(status, output) = commands.getstatusoutput(cmd)
print status, output
return status, output


file = open(config_file)


while 1:
line = file.readline()
if not line:
break
line_str = line.split(',')
job_name = line_str[0].strip()
git_url = line_str[1].strip()
target_dir = line_str[2].strip()
job_name_xml='job_name/' + job_name + '.xml'
print job_name,git_url
##copy tem file
shutil.copy(tem_xml_file, job_name_xml)
cmd = "sed -i 's#TMPGITURL#%s#g' %s" % (git_url, job_name_xml)
print cmd
ret = commands_shell(cmd)

print ret
cmd = "sed -i 's#TARGET_DIR#%s#g' %s" % (target_dir, job_name_xml)

ret = commands_shell(cmd)
print ret

config_data = ''
with open(job_name_xml, 'r') as xml_file:
config_data = xml_file.read()
#server.reconfig_job("saas-kf-copytests-aal-online",config_data)
server.create_job(job_name, config_data)


file.close()

 

posted @ 2018-07-09 19:25  清风木  阅读(1056)  评论(0编辑  收藏  举报