jenkins自动化脚本整理

发布jar文件
规范化Jenkins,编译服务器编译目录设定为:
  • 编译脚本目录:bin
  • 发布脚本目录:deploy
  • 编译代码目录:source
1、项目代码目录
  • 创建source/dmmclist.txt文件
kc_tmplt_tmpltsvc_msa-master
kc_tmplt_catalogsvc_msa-master
kc_tmplt_atomsvc_msa-master
kc_task_job_msa-master
2、编译脚本
  • 创建bin/build-crmkc.sh
#!/bin/bash
svnpath=/app/aideploy/work/crmck/source
deploypath=/app/aideploy/work/crmck/deploy
#env=dev2yw
dmmc=`cat ./dmmclist`
echo "##########开始删除旧代码#####################"
rm -rf $svnpath/*
echo "##########开始更新代码#####################"
cd $svnpath/
svn co https://192.168.228.46:18080/svn/CRM_CODE/trunk/crm-v0.3-trunk-dev/crmkc
echo "##########开始install 安装 kc_common_sso_msa-master代码#####################"
cd $svnpath/crmkc/kc_common_sso_msa-master/
mvn clean install -Dmaven.test.skip=true
echo "##########开始将指定环境变量  $env 配置文件更新到代码/src/main/resources/目录下#####################"


for dm in $dmmc
do
  cp -rf $svnpath/crmkc/environment/$env/* $svnpath/crmkc/$dm/src/main/resources/
  echo "##########开始进入代码 "$dm" 目录更新代码#####################"
  cd $svnpath/crmkc/$dm
  echo "##########开始build "$dm" 编译更新代码#####################"
  mvn clean package -Dmaven.test.skip=true
if [ $? -eq 0 ]
then   
  echo "the $dm is build successfully"
else
  echo "the $dm is build failed,please check......build...log..........."
  exit 1;
fi
done
cd $svnpath/crmkc/
mkdir -p $deploypath/$env/
\cp -rvf */target/*.jar $deploypath/$env/
echo "##########代码编译成功#####################"
echo "##########请检查deploy发布目录 $deploypath  是否编译成功#####################"

### 执行脚本后查看jar包
3、发布脚本
  • 创建deploy/deploy-crmkc.sh
crmkc5ip=192.168.229.9
ml5='/data/crmkc'
xmml5='/app/aideploy/work/crmck/deploy'
bak5='/data/crmkc/crmkcbak/'
echo "将文件包发送到$crmkc5ip$ml5 目录下"
echo "将"$xmml5"目录下的对应jar包发送到目标root@"$crmkc5ip"服务器"
#ssh -p 22 root@"$crmkc4ip" " \cp -r "$ml4"/*.jar "$bak4""
#传输需要的部署jar包
scp -P 22 -r "$xmml5"/$env/kc_doc_catalogsvc_msa-1.0.0.jar root@"$crmkc5ip":"$ml5"
scp -P 22 -r "$xmml5"/$env/kc_doc_multimediasvc_msa-1.0.0.jar root@"$crmkc5ip":"$ml5"
scp -P 22 -r "$xmml5"/$env/kc_manage_wordsvc_msa-1.0.0.jar root@"$crmkc5ip":"$ml5"
scp -P 22 -r "$xmml5"/$env/kc_task_job_msa-1.0.0.jar root@"$crmkc5ip":"$ml5"
scp -P 22 -r /app/aideploy/work/crmck/source/crmkc/environment/$env/* root@"$crmkc5ip":"$ml5"
ssh -p 22 [email protected] "cd /data/crmkc/ && sh start.sh>nohup.out"
echo "################################ root@"$crmkc5ip" 服务器发布成功"

通过python来编译和发布

python编译代码
  • build.py 编译代码
# -*- coding: utf-8 -*-
import sys
import time
import pandas as pd
from utils.edas_utils import EdasUtils
from utils.oss_utils import OssUtils

def main(action, version, module_name, env_name, app_name):
    """
    阿里云edas/oss的API发布主函数
    :param action: 执行的动作
    :param version: 版本号
    :param module_name: 模块名称
    :param env_name: 环境名称
    :param app_name: EDAS的app_name
    :return:
    """
#https://edas.console.res.postcloud.cn/
    edas_config = {'access_key_id': 'sp************',
                   'access_key_secret': 'uoly************',
                   'endpoint': 'edas-api.console************',
                   'region_id': 'cn-be************',
                   'api_version': '2017-08-01'}
    oss_config = {'access_key_id': 'spi6************',
                  'access_key_secret': 'uolyHgaq************',
                  'endpoint': 'http://oss-cn-beijing-************'}
    deploy_excel_data = get_all_deploy_info()
    # 增加对表格信息的稽核
    if action == 'check':
        status = do_check(edas_config, deploy_excel_data)
        if not status:
            exit(1)
        else:
            exit(0)


    deploy_info_list = []
    bucket_list = []


    for item in deploy_excel_data:
        # 假如状态不为“U”,直接退出
        if 'U' != item['state']:
            continue
        # 不是当前环境名,跳出
        if env_name != item['env_name']:
            continue
        if module_name != item['module_name'] and module_name != 'all':
            continue
        if app_name != item['app_name'] and app_name != 'all':
            continue


        # 本地文件目录
        local_file = '%s/%s' % (item['env_name'], item['pkg_name'])
        # OSS对象名称
        object_name = '%s/%s/%s/%s' % (item['module_name'], item['env_name'], version, item['pkg_name'])
        # OSS对象下载地址
        war_url = '%s/%s/%s' % (oss_config['endpoint'], item['bucket_name'], object_name)
        # 获取edas对应部署OSS的bucket_list
        if item['bucket_name'] not in bucket_list:
            bucket_list.append(item['bucket_name'])
        # EDAS和OSS操作需要的信息
        deploy_info = {'app_id': item['app_id'],
                       'app_name': item['app_name'],
                       'local_file': local_file,
                       'object_name': object_name,
                       'bucket_name': item['bucket_name'],
                       'war_url': war_url}
        deploy_info_list.append(deploy_info)


    if len(bucket_list) == 0 or len(deploy_info_list) == 0:
        print('[info]:没有需要发布的应用')
        exit(0)


    if action == 'stop':
        print('##############################################')
        print('[info]: 开始执行停止Edas服务指令!')
        do_stop(edas_config, deploy_info_list)
        print('[info]: 停止指令执行完毕!')
        print('##############################################\n')
    elif action == 'start':
        print('##############################################')
        print('[info]: 开始执行启动Edas服务指令!')
        do_start(edas_config, deploy_info_list)
        print('[info]: 启动指令执行完毕!')
        print('##############################################\n')
    elif action == 'deploy':
        print('##############################################')
        print('[info]: 开始执行部署Edas服务指令!')
        do_deploy(edas_config, deploy_info_list, version)
        print('[info]: 部署指令执行完毕!')
        print('##############################################\n')
    elif action == 'info':
        print('##############################################')
        print('[info]: 开始执行查询Edas服务信息指令!')
        do_info(edas_config, deploy_info_list)
        print('[info]: 查询指令执行完毕!')
        print('##############################################\n')
    elif action == 'ftp':
        print('##############################################')
        print('[info]: 开始上传包到OSS!')
        do_ftp(oss_config, deploy_info_list, bucket_list)
        print('[info]: 上传完毕!')
        print('##############################################\n')
    else:
        print('[error]请输入正确的参数!')
        exit(1)




def do_ftp(oss_config, edas_info_list, bucket_list):
    """
    根据edas_info_list上传包到oss
    :param oss_config: oss api配置信息
    :param edas_info_list: 需要操作的edas信息list
    :param bucket_list: edas对应bucket的list
    :return: None
    """
    for bucket_name in bucket_list:
        oss_utils = OssUtils(oss_config, bucket_name)
        for edas_info in edas_info_list:
            if edas_info['bucket_name'] == bucket_name:
                print('[info]: 开始上传!')
                response = oss_utils.put_object(edas_info['object_name'], edas_info['local_file'])
                print('AppName:' + edas_info['app_name'])
                print('local_file:' + edas_info['local_file'])
                print('war_url:' + edas_info['war_url'])
                print('request_id:' + response.request_id)
                if response.status == 200:
                    print('[info]: 上传成功!\n')
                else:
                    print('[error]: 上传失败!\n')




def print_edas_info(edas_info, response, action):
    print('AppId:' + edas_info['app_id'])
    print('AppName:' + edas_info['app_name'])
    print('response:' + str(response))
    if response['Code'] == 200:
        print('[info]: %s成功!\n' % action)
    else:
        print('[error]: %s失败!\n' % action)




def do_stop(edas_config, edas_info_list):
    """
    根据edas_info_list停止edas服务
    :param edas_config: edas api配置信息
    :param edas_info_list: 需要操作的edas信息list
    :return: None
    """
    edas_utils = EdasUtils(edas_config)
    for edas_info in edas_info_list:
        response = edas_utils.stop_app(edas_info['app_id'])
        print_edas_info(edas_info, response, '停止')
    # 获取此次部署edas部署的状态,全部为0时退出
    still_edas_state(edas_config, edas_info_list, 0)




def do_start(edas_config, edas_info_list):
    """
    根据edas_info_list启动edas服务
    :param edas_config: edas api配置信息
    :param edas_info_list: 需要操作的edas信息list
    :return: None
    """
    edas_utils = EdasUtils(edas_config)
    for edas_info in edas_info_list:
        response = edas_utils.start_app(edas_info['app_id'])
        print_edas_info(edas_info, response, '启动')
    # 获取此次部署edas部署的状态,全部为1时退出
    still_edas_state(edas_config, edas_info_list, 1)




def do_deploy(edas_config, edas_info_list, version):
    """
    根据edas_info_list部署edas服务
    :param edas_config: edas api配置信息
    :param edas_info_list: 需要操作的edas信息list
    :param version: 版本信息
    :return: None
    """
    edas_utils = EdasUtils(edas_config)
    for edas_info in edas_info_list:
        i = 1
        while True:
            response = edas_utils.deploy_app(app_id=edas_info['app_id'], package_version=version, deploy_type='url',
                                             war_url=edas_info['war_url'], group_id='all')
            if response['Code'] != 200:
                print('[info]:5s后重新检查%s 的状态,请稍等。' % edas_info['app_name'])
                time.sleep(5)
            else:
                break
            # 重试超过三次退出
            if i > 3:
                print('[error]:%s 已重试3次,部署异常,请重新部署!!!' % edas_info['app_name'])
                print(edas_info['war_url'])
                print(edas_info['app_id'])
                print(str(response))
                break
            i += 1
        print_edas_info(edas_info, response, '部署')
    # 获取此次部署edas部署的状态,全部为1时退出
    still_edas_state(edas_config, edas_info_list, 1)




def do_info(edas_config, edas_info_list):
    """
    根据edas_info_list获取das服务信息
    :param edas_config: edas api配置信息
    :param edas_info_list: 需要操作的edas信息list
    :return: None
    """
    edas_utils = EdasUtils(edas_config)
    for edas_info in edas_info_list:
        response = edas_utils.get_app_info(edas_info['app_id'])
        if response['Code'] == 200:
            print('app_name :%s' % response['Applcation']['Name'])
            print('app_id   :%s' % response['Applcation']['AppId'])
            if 0 == response['Applcation']['RunningInstanceCount']:
                print('app_state: %s' % 'False')
            else:
                print('app_state: %s' % 'True')




def do_check(edas_config, deploy_excel_data):
    edas_utils = EdasUtils(edas_config)
    status = True
    for edas_info in deploy_excel_data:
        if edas_info['state'] not in ['U', 'E']:
            status = False
            print('[error]: %s 的state字段不是U和E!' % app_name)
        app_name = edas_info['app_name']
        if not isinstance(app_name, str):
            break
        app_id = edas_info['app_id']
        response = edas_utils.get_app_info(app_id)
        if response['Code'] == 200:
            if app_name != response['Applcation']['Name']:
                status = False
                print('[error]: %s 和app_id不一致!' % app_name)
        else:
            print('[error]: %s 请求失败!\n %s' % (app_name, str(response)))
    return status




def get_all_deploy_info():
    column_info = [('module_name', 0), ('env_name', 1), ('app_name', 2), ('app_id', 3), ('pkg_name', 4), ('bucket_name', 5),
                   ('state', 6), ('remark', 7), ]
    return read_excel('config/deploy.xlsx', 'deploy', column_info)




def still_edas_state(edas_config, edas_info_list, state):
    edas_utils = EdasUtils(edas_config)
    # 获取此次部署edas部署的状态,全部为 state 时退出
    for edas_info in edas_info_list:
        i = 1
        while True:
            response = edas_utils.get_app_info(app_id=edas_info['app_id'])
            if response['Applcation']['RunningInstanceCount'] != state and state == 0:
                print('[info]:5s后重新检查%s 的状态,请稍等。' % edas_info['app_name'])
                time.sleep(5)
            elif response['Applcation']['RunningInstanceCount'] < state :
                print('[info]:5s后重新检查%s 的状态,请稍等。' % edas_info['app_name'])
                time.sleep(5)
            else:
                break
            # 重试超过三次退出
            if i > 12:
                print(str(response))
                print('[error]:%s 已重试12次,应用状态异常,请登陆控制台检查!!!' % edas_info['app_name'])
                break
            i += 1




def read_excel(file, sheet_name, column_info):
    """
    读取Excel表格内容
    :param file: path/**.xls[x]
    :param sheet_name: sheet页名字
    :param column_info: [(columnA, 0), (columnA, 1)]
    :return: list_dict
    """
    excel_data = pd.DataFrame(pd.read_excel(file, sheet_name=sheet_name))
    list_dict = []
    for item in excel_data.values:
        line_data = {}
        for column in column_info:
            name = column[0]
            location = column[1]
            line_data[name] = item[location]
        list_dict.append(line_data)
    return list_dict




def get_params(params):
    """
    处理传给脚本的参数
    :param params: 参数信息
    :return:
    """
    action = params['action']
    version = params['version']
    env_name = params['env_name']


    # 对参数组合进行校验
    if action not in ['stop', 'start', 'deploy', 'info', 'ftp', 'check']:
        print('[error]:不存在该action')
        get_help()
        exit(1)
    if env_name == '':
        print('[error]:env_name不可为空')
        get_help()
        exit(1)
    if action in ['ftp', 'deploy'] and version == '':
        print('[error]:action为ftp和deploy时,version不可为空')
        get_help()
        exit(1)




def get_help():
    help_message = """
阿里云V3部署工具使用说明:
${PYTHON_HOME}/bin/python3 deploy.py '{"action":"action","version":"version","env_name":"env_name","module_name":"module_name","app_name":"app_name"}'
    action  : 可选择action为stop、start、info、deploy、ftp。(不可为空)
    version : action为ftp和deploy时,需要指定version。(不可为空)
    env_name: 环境名称。(不可为空)
    module  : 模块名称/all。
    app_name: EDAS的app_name/all。
【注意】:不是单个部署时,app_name为all即可。
    """
    print(help_message)




if __name__ == '__main__':
    # 无参数报错提示
    if len(sys.argv[0:]) <= 1:
        get_help()
        exit(1)
    # 校验参数
    param_dict = eval(sys.argv[1])
    print('###################参数信息###################')
    print(sys.argv[1])
    get_params(param_dict)
    main(param_dict['action'], param_dict['version'], param_dict['module_name'], param_dict['env_name'], param_dict['app_name'])
python发布脚本
  • deploy.py
# -*- coding: utf-8 -*-
# @Author  : pengge
# @Time    : 2018/5/8 15:19
import sys
import time
import pandas as pd
from utils.edas_utils import EdasUtils
from utils.oss_utils import OssUtils




def main(action, version, module_name, env_name, app_name):
    """
    阿里云edas/oss的API发布主函数
    :param action: 执行的动作
    :param version: 版本号
    :param module_name: 模块名称
    :param env_name: 环境名称
    :param app_name: EDAS的app_name
    :return:
    """
#https://edas.console.res.postcloud.cn/
    edas_config = {'access_key_id': 'sp************',
                   'access_key_secret': 'uoly************',
                   'endpoint': 'edas-api.console************',
                   'region_id': 'cn-be************',
                   'api_version': '2017-08-01'}
    oss_config = {'access_key_id': 'spi6************',
                  'access_key_secret': 'uolyHgaq************',
                  'endpoint': 'http://oss-cn-beijing-************'}
    deploy_excel_data = get_all_deploy_info()
    # 增加对表格信息的稽核
    if action == 'check':
        status = do_check(edas_config, deploy_excel_data)
        if not status:
            exit(1)
        else:
            exit(0)


    deploy_info_list = []
    bucket_list = []


    for item in deploy_excel_data:
        # 假如状态不为“U”,直接退出
        if 'U' != item['state']:
            continue
        # 不是当前环境名,跳出
        if env_name != item['env_name']:
            continue
        if module_name != item['module_name'] and module_name != 'all':
            continue
        if app_name != item['app_name'] and app_name != 'all':
            continue


        # 本地文件目录
        local_file = '%s/%s' % (item['env_name'], item['pkg_name'])
        # OSS对象名称
        object_name = '%s/%s/%s/%s' % (item['module_name'], item['env_name'], version, item['pkg_name'])
        # OSS对象下载地址
        war_url = '%s/%s/%s' % (oss_config['endpoint'], item['bucket_name'], object_name)
        # 获取edas对应部署OSS的bucket_list
        if item['bucket_name'] not in bucket_list:
            bucket_list.append(item['bucket_name'])
        # EDAS和OSS操作需要的信息
        deploy_info = {'app_id': item['app_id'],
                       'app_name': item['app_name'],
                       'local_file': local_file,
                       'object_name': object_name,
                       'bucket_name': item['bucket_name'],
                       'war_url': war_url}
        deploy_info_list.append(deploy_info)


    if len(bucket_list) == 0 or len(deploy_info_list) == 0:
        print('[info]:没有需要发布的应用')
        exit(0)


    if action == 'stop':
        print('##############################################')
        print('[info]: 开始执行停止Edas服务指令!')
        do_stop(edas_config, deploy_info_list)
        print('[info]: 停止指令执行完毕!')
        print('##############################################\n')
    elif action == 'start':
        print('##############################################')
        print('[info]: 开始执行启动Edas服务指令!')
        do_start(edas_config, deploy_info_list)
        print('[info]: 启动指令执行完毕!')
        print('##############################################\n')
    elif action == 'deploy':
        print('##############################################')
        print('[info]: 开始执行部署Edas服务指令!')
        do_deploy(edas_config, deploy_info_list, version)
        print('[info]: 部署指令执行完毕!')
        print('##############################################\n')
    elif action == 'info':
        print('##############################################')
        print('[info]: 开始执行查询Edas服务信息指令!')
        do_info(edas_config, deploy_info_list)
        print('[info]: 查询指令执行完毕!')
        print('##############################################\n')
    elif action == 'ftp':
        print('##############################################')
        print('[info]: 开始上传包到OSS!')
        do_ftp(oss_config, deploy_info_list, bucket_list)
        print('[info]: 上传完毕!')
        print('##############################################\n')
    else:
        print('[error]请输入正确的参数!')
        exit(1)




def do_ftp(oss_config, edas_info_list, bucket_list):
    """
    根据edas_info_list上传包到oss
    :param oss_config: oss api配置信息
    :param edas_info_list: 需要操作的edas信息list
    :param bucket_list: edas对应bucket的list
    :return: None
    """
    for bucket_name in bucket_list:
        oss_utils = OssUtils(oss_config, bucket_name)
        for edas_info in edas_info_list:
            if edas_info['bucket_name'] == bucket_name:
                print('[info]: 开始上传!')
                response = oss_utils.put_object(edas_info['object_name'], edas_info['local_file'])
                print('AppName:' + edas_info['app_name'])
                print('local_file:' + edas_info['local_file'])
                print('war_url:' + edas_info['war_url'])
                print('request_id:' + response.request_id)
                if response.status == 200:
                    print('[info]: 上传成功!\n')
                else:
                    print('[error]: 上传失败!\n')




def print_edas_info(edas_info, response, action):
    print('AppId:' + edas_info['app_id'])
    print('AppName:' + edas_info['app_name'])
    print('response:' + str(response))
    if response['Code'] == 200:
        print('[info]: %s成功!\n' % action)
    else:
        print('[error]: %s失败!\n' % action)




def do_stop(edas_config, edas_info_list):
    """
    根据edas_info_list停止edas服务
    :param edas_config: edas api配置信息
    :param edas_info_list: 需要操作的edas信息list
    :return: None
    """
    edas_utils = EdasUtils(edas_config)
    for edas_info in edas_info_list:
        response = edas_utils.stop_app(edas_info['app_id'])
        print_edas_info(edas_info, response, '停止')
    # 获取此次部署edas部署的状态,全部为0时退出
    still_edas_state(edas_config, edas_info_list, 0)




def do_start(edas_config, edas_info_list):
    """
    根据edas_info_list启动edas服务
    :param edas_config: edas api配置信息
    :param edas_info_list: 需要操作的edas信息list
    :return: None
    """
    edas_utils = EdasUtils(edas_config)
    for edas_info in edas_info_list:
        response = edas_utils.start_app(edas_info['app_id'])
        print_edas_info(edas_info, response, '启动')
    # 获取此次部署edas部署的状态,全部为1时退出
    still_edas_state(edas_config, edas_info_list, 1)




def do_deploy(edas_config, edas_info_list, version):
    """
    根据edas_info_list部署edas服务
    :param edas_config: edas api配置信息
    :param edas_info_list: 需要操作的edas信息list
    :param version: 版本信息
    :return: None
    """
    edas_utils = EdasUtils(edas_config)
    for edas_info in edas_info_list:
        i = 1
        while True:
            response = edas_utils.deploy_app(app_id=edas_info['app_id'], package_version=version, deploy_type='url',
                                             war_url=edas_info['war_url'], group_id='all')
            if response['Code'] != 200:
                print('[info]:5s后重新检查%s 的状态,请稍等。' % edas_info['app_name'])
                time.sleep(5)
            else:
                break
            # 重试超过三次退出
            if i > 3:
                print('[error]:%s 已重试3次,部署异常,请重新部署!!!' % edas_info['app_name'])
                print(edas_info['war_url'])
                print(edas_info['app_id'])
                print(str(response))
                break
            i += 1
        print_edas_info(edas_info, response, '部署')
    # 获取此次部署edas部署的状态,全部为1时退出
    still_edas_state(edas_config, edas_info_list, 1)




def do_info(edas_config, edas_info_list):
    """
    根据edas_info_list获取das服务信息
    :param edas_config: edas api配置信息
    :param edas_info_list: 需要操作的edas信息list
    :return: None
    """
    edas_utils = EdasUtils(edas_config)
    for edas_info in edas_info_list:
        response = edas_utils.get_app_info(edas_info['app_id'])
        if response['Code'] == 200:
            print('app_name :%s' % response['Applcation']['Name'])
            print('app_id   :%s' % response['Applcation']['AppId'])
            if 0 == response['Applcation']['RunningInstanceCount']:
                print('app_state: %s' % 'False')
            else:
                print('app_state: %s' % 'True')




def do_check(edas_config, deploy_excel_data):
    edas_utils = EdasUtils(edas_config)
    status = True
    for edas_info in deploy_excel_data:
        if edas_info['state'] not in ['U', 'E']:
            status = False
            print('[error]: %s 的state字段不是U和E!' % app_name)
        app_name = edas_info['app_name']
        if not isinstance(app_name, str):
            break
        app_id = edas_info['app_id']
        response = edas_utils.get_app_info(app_id)
        if response['Code'] == 200:
            if app_name != response['Applcation']['Name']:
                status = False
                print('[error]: %s 和app_id不一致!' % app_name)
        else:
            print('[error]: %s 请求失败!\n %s' % (app_name, str(response)))
    return status




def get_all_deploy_info():
    column_info = [('module_name', 0), ('env_name', 1), ('app_name', 2), ('app_id', 3), ('pkg_name', 4), ('bucket_name', 5),
                   ('state', 6), ('remark', 7), ]
    return read_excel('config/deploy.xlsx', 'deploy', column_info)




def still_edas_state(edas_config, edas_info_list, state):
    edas_utils = EdasUtils(edas_config)
    # 获取此次部署edas部署的状态,全部为 state 时退出
    for edas_info in edas_info_list:
        i = 1
        while True:
            response = edas_utils.get_app_info(app_id=edas_info['app_id'])
            if response['Applcation']['RunningInstanceCount'] != state and state == 0:
                print('[info]:5s后重新检查%s 的状态,请稍等。' % edas_info['app_name'])
                time.sleep(5)
            elif response['Applcation']['RunningInstanceCount'] < state :
                print('[info]:5s后重新检查%s 的状态,请稍等。' % edas_info['app_name'])
                time.sleep(5)
            else:
                break
            # 重试超过三次退出
            if i > 12:
                print(str(response))
                print('[error]:%s 已重试12次,应用状态异常,请登陆控制台检查!!!' % edas_info['app_name'])
                break
            i += 1




def read_excel(file, sheet_name, column_info):
    """
    读取Excel表格内容
    :param file: path/**.xls[x]
    :param sheet_name: sheet页名字
    :param column_info: [(columnA, 0), (columnA, 1)]
    :return: list_dict
    """
    excel_data = pd.DataFrame(pd.read_excel(file, sheet_name=sheet_name))
    list_dict = []
    for item in excel_data.values:
        line_data = {}
        for column in column_info:
            name = column[0]
            location = column[1]
            line_data[name] = item[location]
        list_dict.append(line_data)
    return list_dict




def get_params(params):
    """
    处理传给脚本的参数
    :param params: 参数信息
    :return:
    """
    action = params['action']
    version = params['version']
    env_name = params['env_name']


    # 对参数组合进行校验
    if action not in ['stop', 'start', 'deploy', 'info', 'ftp', 'check']:
        print('[error]:不存在该action')
        get_help()
        exit(1)
    if env_name == '':
        print('[error]:env_name不可为空')
        get_help()
        exit(1)
    if action in ['ftp', 'deploy'] and version == '':
        print('[error]:action为ftp和deploy时,version不可为空')
        get_help()
        exit(1)




def get_help():
    help_message = """
阿里云V3部署工具使用说明:
${PYTHON_HOME}/bin/python3 deploy.py '{"action":"action","version":"version","env_name":"env_name","module_name":"module_name","app_name":"app_name"}'
    action  : 可选择action为stop、start、info、deploy、ftp。(不可为空)
    version : action为ftp和deploy时,需要指定version。(不可为空)
    env_name: 环境名称。(不可为空)
    module  : 模块名称/all。
    app_name: EDAS的app_name/all。
【注意】:不是单个部署时,app_name为all即可。
    """
    print(help_message)




if __name__ == '__main__':
    # 无参数报错提示
    if len(sys.argv[0:]) <= 1:
        get_help()
        exit(1)
    # 校验参数
    param_dict = eval(sys.argv[1])
    print('###################参数信息###################')
    print(sys.argv[1])
    get_params(param_dict)
    main(param_dict['action'], param_dict['version'], param_dict['module_name'], param_dict['env_name'], param_dict['app_name'])
运行Python需要准备的lib包
  • view.json文件
[
{
    "name": "view-api",
    "grade": 0,
    "svn_url": "https://192.168.228.46:18080/svn/CRM_CODE/branches/crm-v0.3-branch/view/view-api",
    "svn_path": "/app/aideploy/work/view/source/agv-api",
    "maven_cmd": "mvn clean package install -Dmaven.test.skip=true"
  },
  {
    "name": "view-server",
    "grade": 1,
    "svn_url": "https://192.168.228.46:18080/svn/CRM_CODE/branches/crm-v0.3-branch/view/view-server",
    "svn_path": "/app/aideploy/work/view/source/view-server",
    "deploy_path": "/app/aideploy/work/view/deploy",
    "war_info_list": [
      {
        "war_path": "/app/aideploy/work/view/source/view-server/target/view-server.war",
        "dist_war_name": "view-server.war"
      }
    ]
  }
]

  • python的 requirement.txt
alibaba-cloud-python-sdk-v2==1.0.5
aliyun-python-sdk-core==2.13.4
aliyun-python-sdk-core-v3==2.13.3
aliyun-python-sdk-ecs==4.16.7
aliyun-python-sdk-edas==2.43.0
aliyun-python-sdk-kms==2.6.0
asn1crypto==0.24.0
certifi==2019.3.9
cffi==1.12.3
chardet==3.0.4
crcmod==1.7
cryptography==2.6.1
idna==2.8
jenkins==1.0.2
jmespath==0.9.4
Markdown==3.1
mq-http-sdk==1.0.1
numpy==1.16.3
oss2==2.6.1
pandas==0.24.2
psutil==5.6.3
pycparser==2.19
pycryptodome==3.8.1
pymongo==3.8.0
pyOpenSSL==19.0.0
python-dateutil==2.8.0
pytz==2019.1
requests==2.21.0
SDK==1.0.0
six==1.12.0
urllib3==1.24.2
xlrd==1.2.0
xlwt==1.3.0

发布搅拌
module_name='view'
work_home=${HOME}/work/${module_name}
python_home=${HOME}/python3.6.5


cd $work_home/deploy
svn up /app/aideploy/work/view/bin/config
\cp -rvf /app/aideploy/work/view/bin/config/view.xls /app/aideploy/work/view/deploy/config/deploy.xlsx


# 处理参数
dt=`date +%Y%m%d%T`
echo "$dt"
params='{"action":"${action}","version":"'$dt'","module_name":"${module_name}","env_name":"${env_name}","app_name":"${app_name}"}'
params=${params/'${module_name}'/${module_name}}
params=${params/'${version}'/"'$dt'"}
params=${params/'${env_name}'/${env_name}}
params=${params/'${app_name}'/${app_name}}


ftp_pamrams=${params/'${action}'/'ftp'}
stop_pamrams=${params/'${action}'/'stop'}
deploy_pamrams=${params/'${action}'/'deploy'}
#start_pamrams=${params/'${action}'/'start'}
# 传包到OSS
python3 -u deploy.py ${ftp_pamrams}
# 停止EDAS服务
python3 -u deploy.py ${stop_pamrams}
# sleep 20s
# 部署EDAS服务
python3 -u deploy.py ${deploy_pamrams}
# sleep 30s
# 启动EDAS服务
#python3 -u deploy.py ${start_pamrams}

你可能感兴趣的:(服务端运维技术整理,jenkins,自动化,运维)