本文主要是介绍Python-jenkins模块批量配置jobs,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 新增配置
- 配置修改
- 配置删除
新增配置
以“丢弃旧的构建”配置选项为例,先在web页面手动配置一下相关配置项
使用python-jenkins获取配置信息如下
在web页面删掉配置选项,重新获取配置信息
分析python-jenkins两次获取的配置信息,通过对比发现此次配置添加会在<properties/>标签下面添加以下标签内容:
<properties><jenkins.model.BuildDiscarderProperty><strategy class="hudson.tasks.LogRotator"><daysToKeep>2</daysToKeep><numToKeep>3</numToKeep><artifactDaysToKeep>-1</artifactDaysToKeep><artifactNumToKeep>-1</artifactNumToKeep></strategy></jenkins.model.BuildDiscarderProperty></properties>
实现思路:
- 先获取jobs配置信息,config信息的格式是xml类型的
- 将获取到的config信息保存到xml文件中
- 使用xml.etree.ElementTree模块对xml文件进行修改
- 读取修改后的xml文件信息,使用此信息重新配置jobs
批量添加的代码如下,这里配置的范围是2019文件夹2019-1视图下所有jobs:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Sudley
# ctime: 2020/01/19import sys
import jenkins
import xml.etree.ElementTree as ETdef add_config_to_jobs(username,password):#此处的server用来获取信息,部分修改操作无法在视图URL下面执行server = jenkins.Jenkins('http://%s:%s@192.168.37.134:8081/job/2019/view/2019-1/',username, password)view_info = server.get_info()fail_count = 0#依次获取对应视图里面的jobs名称及配置信息for num in range(0,len(view_info['jobs'])):job_name = view_info['jobs'][num]['name']print(job_name)#print('aaaaaaaaa')jobs_config = server.get_job_config(job_name)#如果job已经配置过BuildDiscarderProperty则直接进入下一迭代if 'jenkins.model.BuildDiscarderProperty' in jobs_config:print('BuildDiscarderProperty configured')continue#将配置写入xml文件中tmp_xml = '/tmp/job_config.xml'with open(tmp_xml,'w') as f:f.write(jobs_config)f.write('\n')try:#配置xml文件,添加对应的标签add_tag2xml(tmp_xml)with open(tmp_xml,'r') as f:jobs_config_new = f.read()#此处的server用于修改配置,配置的名称在上面定义好server_1 = jenkins.Jenkins('http://%s:%s@192.168.37.134:8081/',username, password)job_name_new = '2019/' + job_name#print(job_name_new)#print(jobs_config_new)#修改job配置前需要先对其执行disable操作#print('bbbbbbbbbbb')server_1.disable_job(job_name_new)server_1 = jenkins.Jenkins('http://%s:%s@192.168.37.134:8081/',username, password)server_1.reconfig_job(job_name_new,jobs_config_new)#print('cccccccccccccccc')server_1.enable_job(job_name_new)except Exception as e:#print(e)#将配置失败的job名称保存到/tmp/config_failed.list列表中fail_list = '/tmp/config_failed.list'if fail_count == 0:with open(fail_list,'w') as f:f.write(job_name + '\n')else:with open(fail_list,'a') as f:f.write(job_name + '\n')fail_count = fail_count + 1def add_tag2xml(tmp_xml):#print(tmp_xml)#使用ET模块解析xml文件tree = ET.parse(tmp_xml)root = tree.getroot()#在properties标签后面添加新标签for properties in root.iter('properties'):BuildDiscarderProperty = ET.SubElement(properties,'jenkins.model.BuildDiscarderProperty')strategy = ET.SubElement(BuildDiscarderProperty,'strategy',attrib={'class':'hudson.tasks.LogRotator'})daysToKeep = ET.SubElement(strategy,'daysToKeep')daysToKeep.text = '2'numToKeep = ET.SubElement(strategy,'numToKeep')numToKeep.text = '3'artifactDaysToKeep = ET.SubElement(strategy,'artifactDaysToKeep')artifactDaysToKeep.text = '-1'artifactNumToKeep = ET.SubElement(strategy,'artifactNumToKeep')artifactNumToKeep.text = '-1'#美化xml格式,添加缩进prettyXml(root, ' ', '\n')tree.write(tmp_xml)def prettyXml(element, indent, newline, level = 0): # elemnt为传进来的Elment类,参数indent用于缩进,newline用于换行if element: # 判断element是否有子元素if element.text == None or element.text.isspace(): # 如果element的text没有内容element.text = newline + indent * (level + 1)else:element.text = newline + indent * (level + 1) + element.text.strip() + newline + indent * (level + 1)#else: # 此处两行如果把注释去掉,Element的text也会另起一行#element.text = newline + indent * (level + 1) + element.text.strip() + newline + indent * leveltemp = list(element) # 将elemnt转成listfor subelement in temp:if temp.index(subelement) < (len(temp) - 1): # 如果不是list的最后一个元素,说明下一个行是同级别元素的起始,缩进应一致subelement.tail = newline + indent * (level + 1)else: # 如果是list的最后一个元素, 说明下一行是母元素的结束,缩进应该少一个subelement.tail = newline + indent * levelprettyXml(subelement, indent, newline, level = level + 1) # 对子元素进行递归操作if __name__ == '__main__' :username = sys.argv[1]password = sys.argv[2]add_config_to_jobs(username,password)
运行方式:
[root@CSDN /home/Sudley/python-jenkins]#python3 add_config_to_jobs.py ‘username’ ‘passwd’
配置修改
配置的修改和配置添加实现思路类似,比如将原来的“保持构建的最大个数”这一项由3改成30
上述新增配置添加成功后web页面下jobs的配置如下:
此次的代码在新增配置代码的基础下进行适当的修改,修改了if判断条件以及将add_tag2xml()替换为modify_xml(),详细脚本如下
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Sudley
# ctime: 2020/02/09import sys
import jenkins
import xml.etree.ElementTree as ETdef add_config_to_jobs(username,password):#此处的server用来获取信息,部分修改操作无法在视图URL下面执行server = jenkins.Jenkins('http://%s:%s@192.168.37.134:8081/job/2019/view/2019-1/',username, password)view_info = server.get_info()fail_count = 0#依次获取对应视图里面的jobs名称及配置信息for num in range(0,len(view_info['jobs'])):job_name = view_info['jobs'][num]['name']print(job_name)#print('aaaaaaaaa')jobs_config = server.get_job_config(job_name)#只有numToKeep的配置是3时菜进行修改if '<numToKeep>3</numToKeep>' not in jobs_config:print('numToKeep is not equal 3')continue#将配置写入xml文件中tmp_xml = '/tmp/job_config.xml'with open(tmp_xml,'w') as f:f.write(jobs_config)f.write('\n')try:#配置xml文件,添加对应的标签modify_xml(tmp_xml)with open(tmp_xml,'r') as f:jobs_config_new = f.read()#此处的server用于修改配置,配置的名称在上面定义好server_1 = jenkins.Jenkins('http://%s:%s@192.168.37.134:8081/',username, password)job_name_new = '2019/' + job_name#print(job_name_new)#print(jobs_config_new)#修改job配置前需要先对其执行disable操作#print('bbbbbbbbbbb')server_1.disable_job(job_name_new)server_1 = jenkins.Jenkins('http://%s:%s@192.168.37.134:8081/',username, password)server_1.reconfig_job(job_name_new,jobs_config_new)#print('cccccccccccccccc')server_1.enable_job(job_name_new)except Exception as e:#print(e)#将配置失败的job名称保存到/tmp/config_failed.list列表中fail_list = '/tmp/config_failed.list'if fail_count == 0:with open(fail_list,'w') as f:f.write(job_name + '\n')else:with open(fail_list,'a') as f:f.write(job_name + '\n')fail_count = fail_count + 1def modify_xml(tmp_xml):#print(tmp_xml)#使用ET模块解析xml文件tree = ET.parse(tmp_xml)root = tree.getroot()#修改numToKeep标签文本#方式一:for numToKeep in root.iter('numToKeep'):numToKeep.text = '30'#方式二:#root[3][0][0][1].text = '30'tree.write(tmp_xml)if __name__ == '__main__' :username = sys.argv[1]password = sys.argv[2]add_config_to_jobs(username,password)
配置后的效果
配置删除
删除上面添加的配置,即需要删除xml文件中properties标签下的jenkins.model.BuildDiscarderProperty标签,代码在配置修改的基础上面修改modify_xml()
详细代码如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Sudley
# ctime: 2020/02/09import sys
import jenkins
import xml.etree.ElementTree as ETdef add_config_to_jobs(username,password):#此处的server用来获取信息,部分修改操作无法在视图URL下面执行server = jenkins.Jenkins('http://%s:%s@192.168.37.134:8081/job/2019/view/2019-1/',username, password)view_info = server.get_info()fail_count = 0#依次获取对应视图里面的jobs名称及配置信息for num in range(0,len(view_info['jobs'])):job_name = view_info['jobs'][num]['name']print(job_name)jobs_config = server.get_job_config(job_name)#只有numToKeep的配置是3时菜进行修改if 'jenkins.model.BuildDiscarderProperty' not in jobs_config:print('jenkins.model.BuildDiscarderProperty not configured')continue#将配置写入xml文件中tmp_xml = '/tmp/job_config.xml'with open(tmp_xml,'w') as f:f.write(jobs_config)f.write('\n')try:#配置xml文件,添加对应的标签modify_xml(tmp_xml)with open(tmp_xml,'r') as f:jobs_config_new = f.read()#此处的server用于修改配置,配置的名称在上面定义好server_1 = jenkins.Jenkins('http://%s:%s@192.168.37.134:8081/',username, password)job_name_new = '2019/' + job_name#修改job配置前需要先对其执行disable操作server_1.disable_job(job_name_new)server_1 = jenkins.Jenkins('http://%s:%s@192.168.37.134:8081/',username, password)server_1.reconfig_job(job_name_new,jobs_config_new)server_1.enable_job(job_name_new)except Exception as e:#print(e)#将配置失败的job名称保存到/tmp/config_failed.list列表中fail_list = '/tmp/config_failed.list'if fail_count == 0:with open(fail_list,'w') as f:f.write(job_name + '\n')else:with open(fail_list,'a') as f:f.write(job_name + '\n')fail_count = fail_count + 1def modify_xml(tmp_xml):#print(tmp_xml)#使用ET模块解析xml文件tree = ET.parse(tmp_xml)root = tree.getroot()#删除所有properties标签下的jenkins.model.BuildDiscarderProperty标签for properties in root.findall('properties'):for BuildDiscarderProperty in properties.findall('jenkins.model.BuildDiscarderProperty'):properties.remove(BuildDiscarderProperty)tree.write(tmp_xml)if __name__ == '__main__' :username = sys.argv[1]password = sys.argv[2]add_config_to_jobs(username,password)
主要用到的python模块为jenkins和xml.etree.ElementTre
参考文献:
https://python-jenkins.readthedocs.io/en/latest/examples.html
【Python】如何对xml文件进行新增、修改和删除等操作
这篇关于Python-jenkins模块批量配置jobs的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!