Python Jenkins-Jenkins 批量更新Job开启浅克隆

一、Job更新测试阶段

【Alpha】所有CD流水线开启浅克隆为1,只获取最近一次commit
     第一步:获取Jenkins的所有jobname
     第二步:  遍历jobname,获取每个job的配置文件config.xml
     第三步:将获取到的xml类型字符串转化为document对象,然后修改机器节点的值,然后将修改的document对象写入一个新的xml文件
     第四步:将新的修改后的xml文件作为参数传给job
  • 1、将标准和非标准的示例Job拷贝至Other视图中:

image

  • 2、问题总结
1、shallow与depth关系 :
# 只有开启shallow时才会相应的开启depth,默认的depth值为空,如下config.xml
      <extensions>
        <hudson.plugins.git.extensions.impl.CloneOption>
          <shallow>true</shallow>
          <noTags>true</noTags>
          <reference></reference>
          <depth></depth>
          <honorRefspec>false</honorRefspec>
        </hudson.plugins.git.extensions.impl.CloneOption>
      </extensions>
2、期望开启shallow和depth的值
      <extensions>
        <hudson.plugins.git.extensions.impl.CloneOption>
          <shallow>true</shallow>       # 开启浅克隆
          <noTags>true</noTags>
          <reference></reference>
          <depth>1</depth>              # 克隆级别为1
          <honorRefspec>false</honorRefspec>
        </hudson.plugins.git.extensions.impl.CloneOption>
      </extensions>
  • 3、编写代码如下:
    • 使用re正则模块过滤出CD Job,并在CD Job中过滤出测试Job进行测试。
    • 将执行失败操作的Job 的config.xml 记录到临时本机目录中。
    • 为了保证不重启Jenkins server ,采用启动/禁用 Jenkins job方式
import jenkins
import xml.etree.ElementTree as ET
import re
 
def add_config_job(username, password):
    # server获取信息
    server = jenkins.Jenkins('http://jenkins.JenkinsDemon.live', username=username, password=password)
    view_info = server.get_info()
    print(view_info)
    count = 0
    list = []
    #依次获取对应视图里面的jobs名称及配置信息
    for num in range(0,len(view_info['jobs'])):
        job_name = view_info['jobs'][num]['name']   # .endswitch("CD-job")
        # print(job_name)
        result = re.search('CD', job_name)
 
        # 测试Job
        test_name1 = 'standard_job_CD'
        test_name2 = 'non-standard_job_CD'
 
        if result:
            # print(job_name)
            # list.append(job_name)
        # print(list)
            if job_name == "non-standard_job_CD":
                jobs_config = server.get_job_config(job_name)
                print(jobs_config)
 
                # 检查是否开启浅拷贝
                if '<shallow>true</shallow>' not in jobs_config:
                    print(job_name + "shallow is not true")
                    if '<depth>1</depth>' not in jobs_config:
                        print(job_name + '\tdepth is not set...')
                else:
                        print(job_name + '\tshallow is true and depth is  set 1...')
 
                # 将配置写入xml文件中
                tmp_xml = '/tmp/job_config.xml'
                with open(tmp_xml, 'w') as f:
                    f.write(jobs_config)
                    f.write('\n')
 
                try:
                    # 更新第一次
                    modify_xml1(tmp_xml)
                #     # 配置xml文件,添加对应的标签
                #     # modify_xml(tmp_xml)
                    with open(tmp_xml, 'r') as f:
                        jobs_config_new = f.read()
                #       # 此处的server用于修改配置,配置的名称在上面定义好
                        server = jenkins.Jenkins('http://jenkins.pingcode.live', username=username, password=password)
                        server.disable_job(job_name)
                        server.reconfig_job(job_name, jobs_config_new)
 
                        server.enable_job(job_name)
 
                    # 更新第二次
                    modify_xml2(tmp_xml)
                    with open(tmp_xml, 'r') as f:
                        jobs_config_new = f.read()
                        #       # 此处的server用于修改配置,配置的名称在上面定义好
                        server = jenkins.Jenkins('http://jenkins.pingcode.live', username=username,
                                                 password=password)
                        server.disable_job(job_name)
                        server.reconfig_job(job_name, jobs_config_new)
 
                        server.enable_job(job_name)
 
                except Exception as e:
                    # print(e)
                    # 将配置失败的job名称保存到/tmp/config_failed.list列表中
                    fail_list = '/tmp/config_failed.list'
                    if 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')
                            count = count + 1
 
def modify_xml1(tmp_xml):
    tree = ET.parse(tmp_xml)
    root = tree.getroot()
 
    # 更新选项
    for shallow in root.iter('shallow'):
        shallow.text = 'true'
        print('shallow is true........')
    tree.write(tmp_xml)
 
def modify_xml2(tmp_xml):
    tree = ET.parse(tmp_xml)
    root = tree.getroot()
    # 深浅需要另行添加
    for depth in root.text('depth'):
        depth.text = '1'
    tree.write(tmp_xml)
 
if __name__ == '__main__':
    username = 'daizhe 账号'
    password = 'XXXXXXXXX密码'
    add_config_job(username, password)

二、Job更新实战阶段

  • 1、备份Jenkins server端 所有 Job的 config.xml
# 临时备份一份config.xml
/yc/data/jenkins/data/jobs# mkdir back_2020.12.24
/yc/data/jenkins/data/jobs# cp -ravf * back_2020.12.24/
  • 2、编写代码如下:
    • 正则匹配所有CD流水线增加浅克隆及克隆深度
import sys
import jenkins
import xml.etree.ElementTree as ET
import re
import logging
 
logging.basicConfig(level=logging.DEBUG,
          format='%(asctime)s %(filename)s %(levelname)s %(message)s',
          datefmt='%a, %d %b %Y %H:%M:%S',
          filename='/tmp/alpha_jenkins.log',
          filemode='w')
 
def add_config_job(username, password):
    # server获取信息
    server = jenkins.Jenkins('http://jenkins.Jenkins Demon.live', username=username, password=password)
    view_info = server.get_info()
    print(view_info)
    count = 0
    list = []
    #依次获取对应视图里面的jobs名称及配置信息
    for num in range(0,len(view_info['jobs'])):
        job_name = view_info['jobs'][num]['name']   # .endswitch("CD-job")
        # print(job_name)
        result = re.search('CD', job_name)
        if result:
            jobs_config = server.get_job_config(job_name)
            print(jobs_config)
 
            # 检查是否开启浅拷贝
            if '<shallow>true</shallow>' not in jobs_config:
                print(job_name + "shallow is not true")
                if '<depth>1</depth>' not in jobs_config:
                    logging.info('{} --- depth is not set...'.format(job_name))
            else:
                    logging.info('{} --- shallow is true and depth is  set 1...'.format(job_name))
 
            # 将配置写入xml文件中
            tmp_xml = '/tmp/job_config.xml'
            with open(tmp_xml, 'w') as f:
                f.write(jobs_config)
                f.write('\n')
 
            try:
                # 更新第一次
                modify_xml1(tmp_xml)
                with open(tmp_xml, 'r') as f:
                    jobs_config_new = f.read()
            #       # 此处的server用于修改配置,配置的名称在上面定义好
                    server = jenkins.Jenkins('http://jenkins.Jenkins Demon.live', username=username, password=password)
                    server.disable_job(job_name)
                    server.reconfig_job(job_name, jobs_config_new)
 
                    server.enable_job(job_name)
 
                # 更新第二次
                modify_xml2(tmp_xml)
                with open(tmp_xml, 'r') as f:
                    jobs_config_new = f.read()
                    #       # 此处的server用于修改配置,配置的名称在上面定义好
                    server = jenkins.Jenkins('http://jenkins.pingcode.live', username=username,
                                             password=password)
                    server.disable_job(job_name)
                    server.reconfig_job(job_name, jobs_config_new)
 
                    server.enable_job(job_name)
 
            except Exception as e:
                # 将配置失败的job名称保存到/tmp/config_failed.list列表中
                fail_list = '/tmp/config_failed.list'
                if 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')
                        count = count + 1
 
def modify_xml1(tmp_xml):
    tree = ET.parse(tmp_xml)
    root = tree.getroot()
 
    # 更新选项
    for shallow in root.iter('shallow'):
        shallow.text = 'true'
        print('shallow is true........')
 
    tree.write(tmp_xml)
 
def modify_xml2(tmp_xml):
    tree = ET.parse(tmp_xml)
    root = tree.getroot()
    # 深浅需要另行添加
    for depth in root.text('depth'):
        depth.text = '1'
    tree.write(tmp_xml)
 
if __name__ == '__main__':
    username = 'daizhe-账号'
    password = 'XXXX密码'
    add_config_job(username, password)
  • 3、验证Jenkins Job CD流水线配置 :无误

image

posted @ 2021-05-30 14:19  SRE运维充电站  阅读(694)  评论(0编辑  收藏  举报