zoukankan      html  css  js  c++  java
  • jenkins任务带参数迁移并修改参数

    #!/usr/bin/env python
    #coding=utf8
    import os
    import sys
    import getpass
    from subprocess import Popen, PIPE
    import requests
    import re
    import xml.etree.ElementTree as ET
    
    def convert_xml_file_to_str(path_to_config_file):
        tree = ET.parse(path_to_config_file)
        root = tree.getroot()
        return ET.tostring(root, encoding='utf8', method='xml').decode()
    
    dest_env="dev"
    if dest_env=='test':
        password=" "
        remote_ip=" "
    elif dest_env=='dev':
        password = " "
        remote_ip = " "
    file_name='product_list.txt'
    
    with open(file_name, 'r') as f:
        out = f.readlines()
    
    product_names = []
    for i in out:
        if not i.startswith('='):
            product_names.append(i.strip())
    
    def run_cmd(cmd, run_as=None):
        if run_as and os.name != 'nt' and run_as != getpass.getuser():
            cmd = '''su - {} -c '{}' '''.format(run_as, cmd)
        print cmd
        close_fds = os.name != 'nt'
        p = Popen(
            cmd,
            stdout=PIPE,
            stdin=PIPE,
            stderr=PIPE,
            shell=True,
            close_fds=close_fds)
        stdout, stderr = p.communicate()
        if p.returncode and stderr:
            print 'Run cmd: {} 
    Code: {}, Stderr: {}'.format(cmd, p.returncode, stderr)
        return p.returncode, stdout.strip()
    
    
    def move_jobs(job_name):
        global password,remote_ip
        name=job_name.strip()
        cmd="cd /home/jenkins/jobs;sshpass -p {} rsync -av {} root@{}:/home/jenkins/jobs/".format(password,name,remote_ip)
        print(cmd)
        code,res=run_cmd(cmd)
        if code!=0:
            print(res)
            sys.exit(1)
        else:
            print('{}迁移成功'.format(name))
    
    
    def change_env(job_name):
        global remote_ip
        username = ' '
        password = ' '
        server_ip='{}:8080'.format(remote_ip)
        headers={'Content-Type':'application/json;UTF-8'}
        url = 'http://{}/job/{}/config.xml'.format(server_ip, job_name)
        print url
        req = requests.get(url, headers=headers, auth=(username, password))
        res=req.content
        print res
        # res = res.replace('if (setEnv =="test"){','if (setEnv == "test"){
        test_url=" "
        host_ips=" "')
        if remote_ip==' ':
            res=res.replace('setEnv="dev"','setEnv="test"')
        elif remote_ip==' ':
            res=res.replace('setEnv="test"','setEnv="dev"')
        with open('/tmp/config.xml','w') as f:
            f.writelines(res)
    
        res=requests.post('http://{}/job/{}/config.xml'.format(server_ip, job_name),
                          data=file('/tmp/config.xml','rb').read(),
                          auth=(username, password),headers=headers)
        if res.status_code==200:
            print('{}修改成功'.format(job_name))
        else:
            print('{}修改失败'.format(job_name))
            print(res.content)
            sys.exit(1)
    
    
    for job_name in product_names:
        # move_jobs(job_name)
        change_env(job_name)
    

      

  • 相关阅读:
    UVa 1354 天平难题 (枚举二叉树)
    广西邀请赛总结
    UVa 12118 检查员的难题 (dfs判连通, 构造欧拉通路)
    UVA
    Uva 127 "Accordian" Patience (模拟)
    UVA 10539 Almost Prime Numbers( 素数因子)
    HDU 1272 小希的迷宫(并查集)
    HDU 1213 How Many Tables (并查集)
    POJ 2236 Wireless Network(并查集)
    HDU 1233 还是畅通工程 ( Kruskal或Prim)
  • 原文地址:https://www.cnblogs.com/slqt/p/13529120.html
Copyright © 2011-2022 走看看