python脚本批量上传mvn私服

为完善公司对于运维的devops化

脚本是点jira流程钩子自动触发jenkins的job来执行python脚本来完成自动上传!也可以改成本地批量上传!

代码如下:

#!/usr/bin/env python3
# coding=utf-8

import os
from jira import JIRA

import requests, paramiko
'''
脚本是坐标文件内容和需要上传的文件对应一致
'''

#下载jira流程里需要上传的附件jar或者pom
def download():
    url = 'http://jira.com.cn/secure/attachment/'
    for i in files:
        URL = url + i.id + '/' + i.filename
        res = requests.get(URL, auth=('jira', 'jira'))
        with open(i.filename, "wb") as f:
            f.write(res.content)

#获取jira流程上传坐标相关信息
def get_dependency():
    groupId_list = []
    artifactId_list = []
    version_list = []
    for i in jar_msg.split():
        # tmp_str = i.split('>')[1].split('<')[0]
        if 'groupId' in i:
            id = i.split('>')[1].split('<')[0]
            groupId_list.append(id)
        elif 'artifactId' in i:
            id = i.split('>')[1].split('<')[0]
            artifactId_list.append(id)
        elif 'version' in i:
            id = i.split('>')[1].split('<')[0]
            version_list.append(id)

    return groupId_list, artifactId_list, version_list

#删除私服已存在jar或者pom
def remote_rm(file_path):
    ssh_client = paramiko.SSHClient()
    ssh_client.load_system_host_keys()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh_client.connect('10.10.10.10', 22, username='root', password='123456', timeout=5)
    rm_list = file_path.split('/')[:-1]
    rm_dir = '/'.join(rm_list)

    stdin, stdout, stderr = ssh_client.exec_command('ls %s' % rm_dir)
    if stdout.readline() != '':
        if file_path.endswith('jar'):
            print("jar文件存在!删除%s目录下的所有文件..." % rm_dir)
            ssh_client.exec_command('rm -fr %s/*' % rm_dir)
        else:
            print("pom相关文件存在!删除%s目录下的pom相关文件" % rm_dir)
            ssh_client.exec_command('rm -fr %s/*.pom*' % rm_dir)
    else:
        print("File not exist")
    ssh_client.close()

#检查上传是否成功并输出到jira注释里
def check_status(result,issue,filename):
    if result == 0:
        print('%s 上传成功' % filename)
        jira.add_comment(issue, '%s Uploaded Successfully' % filename)
    else:
        print('%s 上传失败' % filename)
        jira.add_comment(issue, '%s Upload Failed' % filename)




#上传jar或者pom
def cmd_shell(filename):
    base_path = '/var/lib/docker/volumes/5a469ce21acca42b2f2eff0a5e207e138bb87994fda6a34663a54c8071ae7038/_data/storage/'
    for i in range(len(artifactId_list)):
        DgroupId = groupId_list[i]
        DartifactId = artifactId_list[i]
        Dversion = version_list[i]
        file = DartifactId + '-' + Dversion
        if file == filename[:-4]:  # and filename.endswith('jar'):
            if 'SNAPSHOT' in Dversion:
                Durl = 'http://10.8.8.8/nexus/content/repositories/snapshots/'
                DrepositoryId = 'Snapshots'
            elif 'RELEASE' in Dversion:
                Durl = 'http://10.8.8.8/nexus/content/repositories/releases/'
                DrepositoryId = 'Releases'
            else:
                Durl = 'http://10.8.8.8/nexus/content/repositories/thirdparty/'
                DrepositoryId = 'thirdparty'

            file_path = base_path + DrepositoryId.lower() + '/' + DgroupId.replace('.',
                                                                                   '/') + '/' + DartifactId + '/' + Dversion + '/' + filename
            # print('/'.join(file_path.split('/')[:-1]))
            if filename.endswith('jar'):
                remote_rm(file_path)
                jar_shell = 'mvn deploy:deploy-file -DgroupId=%s -DartifactId=%s -Dversion=%s -Dpackaging=jar -Dfile=./%s -Durl=%s -DrepositoryId=%s' % (
                    DgroupId, DartifactId, Dversion, filename, Durl, DrepositoryId)
                print(jar_shell)
                result = os.system(jar_shell)
                check_status(result,issue,filename)

            elif filename.endswith('pom'):
                remote_rm(file_path)
                pom_shell = 'mvn deploy:deploy-file -DgroupId=%s -DartifactId=%s -Dversion=%s -Dpackaging=pom -Dfile=./%s -Durl=%s -DrepositoryId=%s' % (
                    DgroupId, DartifactId, Dversion, filename, Durl, DrepositoryId)
                print(pom_shell)
                result = os.system(pom_shell)
                check_status(result,issue,filename)



if __name__ == '__main__':
    jira = JIRA("http://jira.com.cn", basic_auth=('jira', 'jira'))
    #jira的issue_key值变量
    scm_key = os.environ['JIRA_ISSUE_KEY']
    issue = jira.issue(scm_key)
    jar_msg = issue.fields.customfield_11103
    files = issue.fields.attachment
    dependency = get_dependency()
    groupId_list = dependency[0]
    artifactId_list = dependency[1]
    version_list = dependency[2]

    download()
    
    for i in files:
        filename = i.filename
        cmd_shell(filename)
        print("*****************************文件 %s 完成上传*****************************" % filename)
    rm_cache()
    

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值