Exemple #1
0
def clean_remote_server(task):
    project = task.project
    cmd = ['cd %s/%s/%s' % (project.release_library, git_utils.get_project_name(project.repo_url), task.link_id),
           'rm -f %s/%s/*.tar.gz' % (
               project.release_library, git_utils.get_project_name(project.repo_url)),
           'ls -1 | sort -r | awk \'FNR > %d  {printf("rm -rf %%s\n", $0);}\' | bash' % project.keep_version_num
           ]

    for host in project.hosts.split('\n'):
        utils.command_with_result(hostname=host, command=' && '.join(cmd))
Exemple #2
0
def init_workspace(task):
    from_time = int(1000 * time.time())
    project = task.project
    project_name = git_utils.get_project_name(project.repo_url)
    build_path = "%s/%s-%s-%s" % (project.deploy_from, project_name, project.level, task.link_id)
    # 拷贝本地文件
    subprocess.Popen('cp -rf  %s %s' % (git_utils.get_source_path(project), build_path), shell=True)
    # 拷贝远程文件
    for host in project.hosts.split('\n'):
        version = '%s/%s/%s' % (project.release_library, project_name, task.link_id)
        utils.command_with_result(hostname=host, command='mkdir -p %s' % version)

    record = Record(user_id=current_user.id, task_id=task.id, action=24, duration=int(1000 * time.time()) - from_time)
    db.session.add(record)
    return
Exemple #3
0
def un_package_file(task):
    project = task.project
    version = task.link_id
    release_file = get_release_file(project, task)
    web_root_path = project.release_to
    release_path = '%s/%s/%s' % (project.release_library, git_utils.get_project_name(project.repo_url), version)
    cmd = []
    if task.file_transmission_mode == 2:
        cmd.append('cp -arf %s/. %s' % web_root_path % release_path)

    cmd.append(
        'cd %s$s && tar --no-same-owner -pm -C %s$s -xz -f %s$s' % (release_path, release_path, release_file))
    command = ' && '.join(cmd)
    for host in project.hosts.split('\n'):
        utils.command_with_result(hostname=host, command=command)
Exemple #4
0
def un_package_file(task):
    """
    解压文件
    """
    project = task.project
    version = task.link_id
    release_file = get_release_file(project, task)
    web_root_path = project.release_to
    release_path = '%s/%s/%s' % (project.release_library,
                                 git_utils.get_project_name(
                                     project.repo_url), version)
    cmd = []
    if task.file_transmission_mode == 2:
        cmd.append('cp -a %s/. %s' % web_root_path % release_path)

    cmd.append('cd %s$s && tar --no-same-owner -pm -C %s$s -xz -f %s$s' %
               (release_path, release_path, release_file))
    command = ' && '.join(cmd)
    for host in project.hosts.split('\n'):
        port = 22 if host.find(':') == -1 else int(host[host.rfind(":") + 1:])
        host = host if host.find(':') == -1 else str(host[:host.rfind(":")])
        resutl, error_msg = utils.command_with_result(
            hostname=host,
            username=project.release_user,
            command=command,
            port=port)
        # 如果远端出现异常直接退出
        if error_msg:
            app.logger.error(error_msg)
            return 1, error_msg
Exemple #5
0
def update_remote_server(task):
    project = task.project
    cmd = [
        get_remote_command(project.pre_release, task.link_id, project),
        get_linked_command(task),
        get_remote_command(project.post_release, task.link_id, project)
    ]
    from_time = int(1000 * time.time())
    command = ' && '.join(cmd)
    memo = []
    for host in project.hosts.split('\n'):
        port = 22 if host.find(':') == -1 else int(host[host.rfind(":") + 1:])
        host = host if host.find(':') == -1 else str(host[:host.rfind(":")])
        result, error = utils.command_with_result(
            hostname=host,
            username=project.release_user,
            command=command,
            port=port)
        if error:
            return 1, "%s\t%s" % (error, result)
        memo.append(result)

    record = Record(user_id=current_user.id,
                    task_id=task.id,
                    action=100,
                    memo=''.join(memo),
                    duration=int(1000 * time.time()) - from_time,
                    step=6,
                    command=command)

    db.session.add(record)
    db.session.commit()
    return 0, memo
Exemple #6
0
def getDiskOfIp(ip):
    if ip != "101.200.196.229":
        result, error_msg = utils.command_with_result(hostname=ip,
                                                      username="******",
                                                      port=22,
                                                      command="df -h")
        if error_msg:
            app.logger.error(error_msg)
        return result
    else:
        result, error_msg = utils.command_with_result(
            hostname='101.200.196.229',
            username="******",
            port=49720,
            command="df -h")
        if error_msg:
            app.logger.error(error_msg)
        return result
Exemple #7
0
def update_remote_server(task):
    project = task.project
    cmd = [get_remote_command(project.pre_release, task.link_id, project),
           get_linked_command(task),
           get_remote_command(project.post_release, task.link_id, project)]
    from_time = int(1000 * time.time())
    command = ' && '.join(cmd)
    for host in project.hosts.split('\n'):
        utils.command_with_result(hostname=host, command=command)

    record = Record(user_id=current_user.id,
                    task_id=task.id,
                    action=100,
                    duration=int(1000 * time.time()) - from_time,
                    command=command)

    db.session.add(record)
    db.session.commit()
Exemple #8
0
def init_workspace(task):
    """
    初始化工作目录 本地和远程的
    :param task: 发布任务
    :return: Void
    """
    from_time = int(1000 * time.time())
    project = task.project
    project_name = git_utils.get_project_name(project.repo_url)
    build_path = get_build_path(project, project_name, task)
    command = ['mkdir -p  %s' % build_path]
    p_open = subprocess.Popen('mkdir -p  %s' % build_path,
                              stderr=subprocess.PIPE,
                              stdout=subprocess.PIPE,
                              shell=True)
    p_open.wait()
    return_code = p_open.returncode
    if return_code != 0:
        return return_code, ''.join(p_open.stderr.readlines())

    # 拷贝本地文件

    memo = [''.join(v.decode("utf-8") for v in p_open.stdout.readlines())]

    # 拷贝远程文件
    for host in project.hosts.split('\n'):
        version = '%s/%s/%s' % (project.release_library, project_name,
                                task.link_id)
        port = 22 if host.find(':') == -1 else int(host[host.rfind(":") + 1:])
        host = host if host.find(':') == -1 else str(host[:host.rfind(":")])
        remote_cmd = 'mkdir -p %s' % version
        result, error_msg = utils.command_with_result(
            hostname=host,
            username=project.release_user,
            port=port,
            command=remote_cmd)
        memo.append('r:%s' % result)
        command.append('r: %s' % remote_cmd)

    record = Record(user_id=current_user.id,
                    task_id=task.id,
                    action=TASK_PROGRESS[0],
                    command=' && '.join(command),
                    memo=''.join(memo),
                    step=1,
                    duration=int(1000 * time.time()) - from_time)
    db.session.add(record)
    db.session.commit()
    return 0, record.memo
Exemple #9
0
def clean_remote_server(task):
    """

    """
    project = task.project
    from_time = int(1000 * time.time())
    cmd = [
        'cd %s/%s' % (project.release_library,
                      git_utils.get_project_name(project.repo_url)),
        'rm -f %s/%s/*.tar.gz' %
        (project.release_library, git_utils.get_project_name(
            project.repo_url)),
        'ls -lt| awk "FNR > %d  {print $1;}" | xargs rm -rf ' %
        (project.keep_version_num + 1)
    ]
    command = ' && '.join(cmd)
    for host in project.hosts.split('\n'):
        port = 22 if host.find(':') == -1 else int(host[host.rfind(":") + 1:])
        host = host if host.find(':') == -1 else str(host[:host.rfind(":")])
        result, error = utils.command_with_result(
            hostname=host,
            username=project.release_user,
            command=command,
            port=port)
        if error:
            return 1, "%s\t%s" % (error, result)

    record = Record(user_id=current_user.id,
                    task_id=task.id,
                    action=100,
                    memo=''.join(command),
                    duration=int(1000 * time.time()) - from_time,
                    step=7,
                    command=command)
    db.session.add(record)
    db.session.commit()
    app.logger.info("task id:[%d],msg:clean_server finished", task.id)