load_handle.py 2.4 KB
Newer Older
金凯强's avatar
金凯强 committed
1 2 3 4 5 6
# -*- coding:utf-8 -*-

import os
import re

if __name__ == '__main__':
金凯强's avatar
金凯强 committed
7 8 9
    # 同步配置nginx配置
    app_conf_path = '/huansi/upgrade/app.conf'
    rpc_conf_path = '/huansi/upgrade/rpc.conf'
金凯强's avatar
金凯强 committed
10 11 12 13 14 15 16 17 18 19
    compose_path = '/huansi/upgrade/docker-compose.yml'

    with open(compose_path, 'r') as f:
        compose_content = f.read()

    # 取出内容中ip " # ip: "192.168.0.1" "
    ip_str_list = re.findall('# ip: "(.*)"', compose_content)
    if not ip_str_list:
        raise RuntimeError('未在docker-compose.yml找到ip的配置')
    ip = str(ip_str_list[0])
金凯强's avatar
金凯强 committed
20 21 22 23 24 25

    os.system('mkdir -p /data/nginx/conf/')
    if os.path.exists(app_conf_path):
        with open(app_conf_path, 'r')as f:
            app_conf_content = f.read()

金凯强's avatar
金凯强 committed
26
        app_conf_content = app_conf_content.replace('$service_ip', ip)
金凯强's avatar
金凯强 committed
27 28 29 30 31 32 33 34

        with open('/data/nginx/conf/app.conf', 'w')as f:
            f.write(app_conf_content)

    if os.path.exists(rpc_conf_path):
        with open(rpc_conf_path, 'r')as f:
            rpc_conf_content = f.read()

金凯强's avatar
金凯强 committed
35
            rpc_conf_content = rpc_conf_content.replace('$service_ip', ip)
金凯强's avatar
金凯强 committed
36 37 38 39

        with open('/data/nginx/conf/rpc.conf', 'w')as f:
            f.write(rpc_conf_content)

金凯强's avatar
金凯强 committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    with os.popen('ls | grep .tar') as f:
        cmd_txt = f.read()

    if not cmd_txt:
        raise RuntimeError('请检查文件,tar文件为空')

    app_name_list = cmd_txt.split('\n')

    for app_name in app_name_list:
        if not app_name:
            continue
        print('加载镜像:{}'.format(app_name[:-4].replace('___', "/").replace("__", ":")))
        cmd_res = os.system('docker load -i {}'.format(app_name))
        if cmd_res == 1:
            raise RuntimeError('加载失败')

    print('镜像加载完毕')
    print('开始升级程序')

    app_list_str = re.findall('# app_list: ".*"', compose_content)
    if app_list_str:
        app_list_str = app_list_str[0]
    else:
        raise RuntimeError('未设置app_list')

    app_str = app_list_str[len('# app_list: "'):-1]

    if app_str == '*':
        res = os.system('''export HUANSI_REGISTRY_URL=47.110.145.204:8084 &&\
69 70
source /huansi/upgrade/huansi.sh &&\
docker-compose -p deploy up -d --force-recreate''')
金凯强's avatar
金凯强 committed
71 72
    else:
        res = os.system('''export HUANSI_REGISTRY_URL=47.110.145.204:8084 &&\
73
source /huansi/upgrade/huansi.sh &&\
金凯强's avatar
金凯强 committed
74
docker-compose -p deploy up -d --force-recreate --no-deps {}'''.format(app_str))
金凯强's avatar
金凯强 committed
75 76

    if res == 1: raise RuntimeError('程序升级失败')