Пример #1
0
def init_pkg(app_folder, options=None):
    """init package config"""
    pkgCom = PkgCommon()
    if options and options.pkg_id: # 首先从参数找
        pkg_id = options.pkg_id
    else:
        pkg_info = pkgCom.getPkgId(app_folder)
        if pkg_info: # 然后尝试从pkg目录找
            pkg_id = pkg_info['packageId']
        else: # 最后取安装路径的目录名称
            _, pkg_id = os.path.split(app_folder)
            if not pkg_id:
                return 1, 'can not get app folder, install path should not "/"'
    from create_pkg_conf import main as create_conf
    create_conf(app_folder)
    from install import updateConf
    code, msg = updateConf(app_folder, pkg_id, '0.0', '0.0', type="pkg")
    if code != 0:
        return code, msg
    # 将log软链到应用目录
    conf_path = pkgCom.getConfPath(app_folder, pkg_id)
    log_admin = os.path.join(app_folder, 'log_admin')
    if os.path.islink(log_admin):
        os.unlink(log_admin)
    os.symlink(os.path.join(conf_path, 'log'), log_admin)
    return 0, 'ok'
Пример #2
0
def main(argv):
    package_path = os.path.dirname(os.path.abspath(sys.argv[0]))
    parser = argparse.ArgumentParser()
    parser.add_argument("--installPath", help="install path", required=True)
    parser.add_argument("--packageId", help="package id", required=False)
    parser.add_argument("--configPackageId", help="config package id", required=False)
    parser.add_argument("--withConfig", help="with config", required=True)
    parser.add_argument("--type", help="package type", required=False)
    parser.add_argument("--preStop", help="stop before update", required=False)
    parser.add_argument("--postRestart", help="restart after update", required=False)
    parser.add_argument("--forceUpdate", help=" update force", required=False)
    args, unknown = parser.parse_known_args()
    pkg_path = os.path.join(package_path, 'main')
    conf_pkg_path = os.path.join(package_path, 'conf')
    conf_pkg_info = getConfPkgInfo(args.installPath, conf_pkg_path)
    conf_file_list = conf_pkg_info['fileList']
    start_stop_err = ""

    #获取锁
    pkgCom = PkgCommon()
    local_conf_path = pkgCom.getConfPath(args.installPath,args.packageId)
    ret = pkgCom.getLock(local_conf_path)
    if not ret:
        exit_proc(2008, "get lock error")
    # get package id
    if args.type == "2":
        real_pkg_id = getPkgId(args.installPath)
    else:
        real_pkg_id = args.packageId
    # get user
    pkg_user, pkg_group =pkgCom.getPkgUser(args.installPath, real_pkg_id, package_path)

    # check package
    if os.path.isdir(pkg_path):
        code, msg = checkPkg(args.installPath, pkg_path)
        if code < 0:
            exit_proc(code, msg)
        elif code > 0 and args.forceUpdate != "true":
            exit_proc(code, msg)
    if os.path.isdir(conf_pkg_path):
        if os.path.exists(os.path.join(conf_pkg_path, "package.conf.yaml")):
            os.remove(os.path.join(conf_pkg_path, "package.conf.yaml"))
        code, msg = checkPkg(args.installPath, conf_pkg_path)
        if code < 0:
            exit_proc(code, msg)
        elif code > 0 and args.forceUpdate != "true":
            exit_proc(code, msg)
    # stop before update
    if args.preStop == "true":
        code, msg = stop(real_pkg_id, args.installPath, pkg_user)
        if code != 0:
            start_stop_err += msg + "\n"


    # pre script
    code, msg = preScript(args.installPath, real_pkg_id, pkg_user)
    if code != 0:
        start_stop_err += msg
        # exit_proc(code,msg)
        # return

    # do update
    if os.path.isdir(pkg_path):
        if os.path.isdir(conf_pkg_path):
            code, msg = combinePkg(conf_pkg_path, pkg_path)
            if code != 0:
                exit_proc(code, msg)
        code, msg = updatePkg(args.installPath, pkg_path, conf_file_list, pkg_user, pkg_group, 'pkg')
        if code != 0:
            exit_proc(code, msg)
    if os.path.isdir(conf_pkg_path):
        code, msg = updatePkg(args.installPath, conf_pkg_path, conf_file_list, pkg_user, pkg_group, 'conf')
        if code != 0:
            exit_proc(code, msg)

    # update config
    if os.path.isdir(pkg_path):
        code, msg = updateConf(args.installPath, pkg_path, args.packageId, 'pkg')
        if code != 0:
            exit_proc(code, msg)
    if os.path.isdir(conf_pkg_path):
        if (not args.configPackageId) and (args.type == "2"):
            args.configPackageId = args.packageId
        code, msg = updateConf(args.installPath, conf_pkg_path, args.configPackageId, 'conf')
        if code != 0:
            exit_proc(code, msg)

    # post script
    code, msg = postScript(args.installPath, real_pkg_id, pkg_user)
    if code != 0:
        exit_proc(code, msg)
        return
    if args.postRestart == "true":
        code, msg = restart(real_pkg_id, args.installPath, pkg_user)
        if code != 0:
            start_stop_err += msg
    if start_stop_err != '':
        msg = "文件升级成功,升级后重启失败,ret=%s"%(start_stop_err)
        exit_proc(1105, msg)
    exit_proc(0, 'OK')