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
def transmission(task): from_time = int(1000 * time.time()) project = task.project package_file_full_name = package_file(task) remote_file_name = get_release_file(project, task) 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(":")]) utils.trans_data(hostname=host, remote_path=remote_file_name, local_path=package_file_full_name, port=port, username=project.release_user) un_package_file(task) record = Record(user_id=current_user.id, task_id=task.id, action=78, duration=int(1000 * time.time()) - from_time, step=5, command='scp %s to %s:%s' % (package_file_full_name, project.hosts, remote_file_name)) db.session.add(record) db.session.commit()
def get_sourcecode(task): """ 下载最新源代码 :param task: :return: """ 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) project_gitlab = gl.projects.get( git_utils.get_project_full_name(project.repo_url)) # 从gitlab下载指定分支的源代码 if project.level == DeployEnv.DEV.value: rc = project_gitlab.branches.get(task.branch) commit_id = rc.commit['id'] else: tag = project_gitlab.tags.get(task.branch) commit_id = tag.commit.id tgz = project_gitlab.repository_archive(sha=commit_id) zip_file_name = '%s/source.tar.gz' % build_path with open(zip_file_name, 'wb') as t: t.write(tgz) commit_file_name = "%s/%s-%s-%s" % ( build_path, git_utils.get_project_name( project.repo_url), commit_id, commit_id) cmd = [ 'tar -xf %s -C %s' % (zip_file_name, build_path), 'rsync -a %s/ %s && rm -rf %s && rm -rf %s' % (commit_file_name, build_path, commit_file_name, zip_file_name) ] command = ' && '.join(cmd) p_open = subprocess.Popen(command, 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()) else: record = Record( user_id=current_user.id, task_id=task.id, action=TASK_PROGRESS[2], command=command, memo=''.join(v.decode("utf-8") for v in p_open.stdout.readlines()), step=3, duration=int(1000 * time.time()) - from_time) db.session.add(record) db.session.commit() return 0, record.memo
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
def clean_local(task): project = task.project from_time = int(1000 * time.time()) cmd = 'rm -rf %s*' % project.get_deploy_workspace(task.link_id) subprocess.Popen(cmd, shell=True).wait() record = Record(user_id=current_user.id, task_id=task.id, action=100, memo=''.join(cmd), duration=int(1000 * time.time()) - from_time, step=7, command=cmd) db.session.add(record) db.session.commit() return
def pre_deploy(task): """ 发布前置准备 安装npm maven之类 :param task: :return: Void """ project = task.project from_time = int(1000 * time.time()) pre_deploy_split = task.project.pre_deploy.split('\n') if pre_deploy_split is None: return cmd = [ '. /etc/profile', 'cd %s' % project.get_deploy_workspace(task.link_id) ] for pre_deploy_cmd in pre_deploy_split: if len(pre_deploy_cmd.strip('\r').strip('\n').strip(' ')) > 0: cmd.append(pre_deploy_cmd) command = ' && '.join(cmd) p_open = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p_open.wait() memo = ''.join(v.decode("utf-8") for v in p_open.stdout.readlines()) return_code = p_open.returncode != 0 if return_code: return p_open.returncode, ''.join( v.decode("utf-8") for v in p_open.stderr.readlines()) + memo record = Record(user_id=current_user.id, task_id=task.id, action=TASK_PROGRESS[1], memo=memo, duration=int(1000 * time.time()) - from_time, step=2, command=command) db.session.add(record) db.session.commit() return 0, memo
def post_deploy(task): """ 打包命令 """ from_time = int(1000 * time.time()) project = task.project tasks = project.post_deploy.split('\n') # 本地可能要做一些依赖环境变量的命令操作 cmd = ['. /etc/profile'] workspace = project.get_deploy_workspace(task.link_id) cmd.append("cd %s" % workspace) for task_command in tasks: if task_command: cmd.append(task_command.strip('\n').strip('\r')) command = ' && '.join(cmd) p_open = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p_open.wait() return_code = p_open.returncode memo = ''.join(v.decode("utf-8") for v in p_open.stdout.readlines()) if return_code != 0: return return_code, ''.join( v.decode("utf-8") for v in p_open.stderr.readlines()) + memo record = Record(user_id=current_user.id, task_id=task.id, action=64, duration=int(1000 * time.time()) - from_time, memo=memo, step=4, command=command) db.session.add(record) db.session.commit() return 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)