Ejemplo n.º 1
0
    def receive(self, text_data=None, bytes_data=None):
        info = json.loads(text_data)
        self.config = ProjectConfig.objects.select_related('project').get(
            id=info.get('config_id'))

        unique_key = self.config.project.project_name + self.config.project.project_env

        if self.redis_instance.get(unique_key):
            self.send('有相同的任务正在进行!请稍后重试!')
            self.close()
        else:
            self.redis_instance.set(unique_key, 1)
            timeline_header = '<li><i class="fa fa-flag bg-blue"></i><div class="timeline-item"><h3 class="timeline-header"><a href="javascript:void(0)">{}</a></h3><div class="timeline-body"></div></div></li>'
            cmd_detail = '<p style="font-style: italic; color: grey;">{}</p>'
            timeline_body_green = '<p style="color: #008000">{}</p>'
            timeline_body_red = '<p style="color: #FF0000">{}</p>'

            self.branch_tag = info.get('branch_tag')
            rollback = info.get('rollback', False)
            self.d_type = 'rollback' if rollback else 'deploy'
            commit = info.get('commit', None)
            self.release_name = commit if commit else self.branch_tag

            # 初始化ansible
            server_objs = self.config.deploy_server.all()
            host_ids = [server.id for server in server_objs]
            resource = gen_resource.GenResource().gen_host_list(
                host_ids=host_ids)
            self.host_list = [
                server.assets.asset_management_ip for server in server_objs
            ]
            ans = ansible_api_v2.ANSRunner(resource, sock=self)

            if self.config.repo == 'git':
                tool = GitTools(repo_url=self.config.repo_url,
                                path=self.config.src_dir,
                                env=self.config.project.project_env)

                tool.checkout(self.branch_tag)
                if commit:
                    tool.checkout(commit)
                    self.release_desc = tool.get_commit_msg(
                        self.branch_tag, commit)
                else:
                    self.release_desc = self.release_name

                self.deploy(rollback, timeline_header, cmd_detail,
                            timeline_body_green, timeline_body_red, ans, info,
                            tool)
            elif self.config.repo == 'svn':
                tool = SVNTools(repo_url=self.config.repo_url,
                                path=self.config.src_dir,
                                env=self.config.project.project_env,
                                username=self.config.repo_user,
                                password=self.config.repo_password)

                if commit:
                    model_name = '' if self.config.repo_model == 'trunk' else self.branch_tag
                    self.release_desc = tool.get_commit_msg(
                        int(commit),
                        self.config.repo_model,
                        model_name=model_name)
                else:
                    self.release_desc = self.release_name

                c = int(commit) if commit else None
                self.deploy(rollback,
                            timeline_header,
                            cmd_detail,
                            timeline_body_green,
                            timeline_body_red,
                            ans,
                            info,
                            tool,
                            repo='svn',
                            commit=c)

            self.close()
            self.redis_instance.delete(unique_key)
Ejemplo n.º 2
0
def read_branch(request, pk):
    config = ProjectConfig.objects.get(id=pk)
    if request.method == 'GET':
        key = request.GET.get('key', None)
        mode = request.GET.get('mode', 'deploy')
        env = request.GET.get('env','noenv')
        if config.repo == 'git':
            git_tool = GitTools(repo_url=config.repo_url, path=config.src_dir, env=env)
            if key:
                if key == 'model':
                    try:
                        git_tool.clone(prev_cmds=config.prev_deploy)
                        if config.repo_model == 'branch':
                            branches = git_tool.remote_branches
                            return JsonResponse({'code': 200, 'models': branches, 'msg': '获取成功!'})
                        elif config.repo_model == 'tag':
                            tags = git_tool.tags(versions=config.versions.split(','), mode=mode)
                            return JsonResponse({'code': 200, 'models': tags, 'msg': '获取成功!'})
                    except Exception as e:
                        return JsonResponse({'code': 500, 'msg': '获取失败:{}'.format(e)})
                elif key == 'commit':
                    try:
                        branch = request.GET.get('branch')
                        mode = request.GET.get('mode')
                        if request.GET.get('new_commit'):
                            git_tool.pull(branch)
                        commits = git_tool.get_commits(branch, versions=config.versions.split(','), mode=mode,
                                                       max_count=20)
                        return JsonResponse({'code': 200, 'data': commits, 'msg': '获取成功!'})
                    except Exception as e:
                        return JsonResponse({'code': 500, 'msg': '获取失败:{}'.format(e)})
            else:
                if os.path.exists(git_tool.proj_path):
                    local_branches = git_tool.local_branches
                    local_tags = tags = git_tool.tags(versions=config.versions.split(','), mode=mode)
                return render(request, 'projs/deploy.html', locals())

        elif config.repo == 'svn':
            svn_tool = SVNTools(repo_url=config.repo_url, path=config.src_dir, env=config.project.project_env,
                                username=config.repo_user, password=config.repo_password)
            if key:
                if key == 'model':
                    try:
                        if config.repo_model == 'branch':
                            branches = svn_tool.branches
                            return JsonResponse({'code': 200, 'models': branches, 'msg': '获取成功!'})
                        elif config.repo_model == 'tag':
                            tags = svn_tool.tags(versions=config.versions.split(','), mode=mode)
                            return JsonResponse({'code': 200, 'models': tags, 'msg': '获取成功!'})
                    except Exception as e:
                        return JsonResponse({'code': 500, 'msg': '获取失败:{}'.format(e)})
                elif key == 'commit':
                    branch = request.GET.get('branch')
                    try:
                        if branch == 'trunk':
                            commits = svn_tool.get_commits(versions=config.versions.split(','), mode=mode, limit=30)
                        else:
                            commits = svn_tool.get_commits(versions=config.versions.split(','), repo_model='branch',
                                                           model_name=branch, mode=mode, limit=30)
                        return JsonResponse({'code': 200, 'data': commits, 'msg': '获取成功!'})
                    except Exception as e:
                        return JsonResponse({'code': 500, 'msg': '获取失败:{}'.format(e)})
            else:
                return render(request, 'projs/deploy.html', locals())
Ejemplo n.º 3
0
Archivo: views.py Proyecto: vtatv/Ops
def deploy(request, pk):
    config = ProjectConfig.objects.select_related('project').get(id=pk)
    if request.method == 'GET':
        key = request.GET.get('key', None)
        if config.repo == 'git':
            git_tool = GitTools(repo_url=config.repo_url,
                                path=config.src_dir,
                                env=config.project.project_env)
            if key:
                if key == 'model':
                    try:
                        git_tool.clone(prev_cmds=config.prev_deploy)
                        if config.repo_model == 'branch':
                            branches = git_tool.remote_branches
                            return JsonResponse({
                                'code': 200,
                                'models': branches,
                                'msg': '获取成功!'
                            })
                        elif config.repo_model == 'tag':
                            tags = git_tool.tags
                            return JsonResponse({
                                'code': 200,
                                'models': tags,
                                'msg': '获取成功!'
                            })
                    except Exception as e:
                        return JsonResponse({
                            'code': 500,
                            'msg': '获取失败:{}'.format(e)
                        })
                elif key == 'commit':
                    try:
                        branch = request.GET.get('branch')
                        mode = request.GET.get('mode')
                        if request.GET.get('new_commit'):
                            git_tool.pull(branch)
                        commits = git_tool.get_commits(
                            branch,
                            versions=config.versions.split(','),
                            mode=mode,
                            max_count=20)
                        return JsonResponse({
                            'code': 200,
                            'data': commits,
                            'msg': '获取成功!'
                        })
                    except Exception as e:
                        return JsonResponse({
                            'code': 500,
                            'msg': '获取失败:{}'.format(e)
                        })
            else:
                if os.path.exists(git_tool.proj_path):
                    local_branches = git_tool.local_branches
                    local_tags = git_tool.tags
                mode = request.GET.get('mode', None)
                if mode:
                    return render(request, 'projs/deploy.html', locals())
                else:
                    mode = 'deploy'
                    return render(request, 'projs/deploy.html', locals())

        elif config.repo == 'svn':
            svn_tool = SVNTools(repo_url=config.repo_url,
                                path=config.src_dir,
                                env=config.project.project_env,
                                username=config.repo_user,
                                password=config.repo_password)
            if key:
                if key == 'model':
                    try:
                        if config.repo_model == 'branch':
                            branches = svn_tool.branches
                            return JsonResponse({
                                'code': 200,
                                'models': branches,
                                'msg': '获取成功!'
                            })
                        elif config.repo_model == 'tag':
                            tags = svn_tool.tags
                            return JsonResponse({
                                'code': 200,
                                'models': tags,
                                'msg': '获取成功!'
                            })
                    except Exception as e:
                        return JsonResponse({
                            'code': 500,
                            'msg': '获取失败:{}'.format(e)
                        })
                elif key == 'commit':
                    branch = request.GET.get('branch')
                    mode = request.GET.get('mode')
                    try:
                        if branch == 'trunk':
                            commits = svn_tool.get_commits(
                                versions=config.versions.split(','),
                                mode=mode,
                                limit=30)
                        else:
                            commits = svn_tool.get_commits(
                                versions=config.versions.split(','),
                                repo_model='branch',
                                model_name=branch,
                                mode=mode,
                                limit=30)
                        return JsonResponse({
                            'code': 200,
                            'data': commits,
                            'msg': '获取成功!'
                        })
                    except Exception as e:
                        return JsonResponse({
                            'code': 500,
                            'msg': '获取失败:{}'.format(e)
                        })
            else:
                mode = request.GET.get('mode', None)
                if mode is not None:
                    return render(request, 'projs/deploy.html', locals())
                else:
                    mode = 'deploy'
                    return render(request, 'projs/deploy.html', locals())

    elif request.method == 'POST':
        commit = request.POST.get('commit')
        mode = request.POST.get('mode')
        version = commit if commit else request.POST.get('branch_tag')
        db_versions = config.versions.split(',')

        if mode == 'rollback':
            if version in db_versions:
                return JsonResponse({'code': 200, 'msg': '查询版本存在,可以执行回滚操作!'})
            else:
                return JsonResponse({
                    'code': 403,
                    'msg': '查询版本不存在,无法执行回滚操作,请使用部署功能进行部署'
                })
        elif mode == 'deploy':
            if version not in db_versions:
                return JsonResponse({'code': 200, 'msg': '查询版本不存在,可以执行部署操作!'})
            else:
                return JsonResponse({
                    'code': 403,
                    'msg': '查询版本已存在,请使用回滚功能进行回滚操作'
                })
Ejemplo n.º 4
0
    def receive(self, text_data=None, bytes_data=None):

        # {"tid": "19", "pid": "3", "dserver": ["2", "3"], "d_type": "deploy"}
        info = json.loads(text_data)
        self.config = Project_Deploy_Ticket.objects.get(id=info.get('tid'))

        unique_key = self.config.ticket_config.proj_uuid
        self.d_type = info.get('d_type').capitalize()

        step_msg = "[{} " "{}] <font size=\"2\" color=\"blue\">{}</font></br>".format(
            time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), self.d_type,
            {})
        ok_msg = "[{} " "{}] <font size=\"2\" color=\"blue\">{}...OK</font></br>".format(
            time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), self.d_type,
            {})
        fail_msg = "[{} " "{}] <font size=\"2\" color=\"color: #FF0000\">{}...Error</font></br>".format(
            time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), self.d_type,
            {})
        warn_msg = "[{} " "{}] <font size=\"2\" color=\"orange\">{}...warning</font></br>".format(
            time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), self.d_type,
            {})

        if self.redis_instance.get(unique_key):
            self.send_save(fail_msg.format("有相同的任务正在进行,请稍后重试!"))
            self.close()
        else:
            self.redis_instance.set(unique_key, 1)
            ticket_no = self.config.ticket_no
            if self.d_type == 'Dcheck':
                self.deploy_check(ok_msg)
            elif self.config.ticket_status == 3 or self.config.ticket_status == 4:
                self.send_save(fail_msg.format("工单已关闭,请联系管理员!"), close=True)
                return
            elif self.d_type == 'Deploy':

                self.branch_tag = self.config.ticket_config.proj_branch_tag
                commit = self.config.ticket_commit
                self.release_name = commit if commit else self.branch_tag

                if self.config.ticket_config.proj_role.repo == 'git':
                    tool = GitTools(
                        repo_url=self.config.ticket_config.proj_role.repo_url,
                        path=self.config.ticket_config.proj_role.src_dir,
                        env=self.config.ticket_config.proj_env.projenv_name)
                    try:
                        tool.checkout(self.branch_tag)
                        self.send_save(ok_msg.format("切换分支:" +
                                                     self.branch_tag))
                        if commit:
                            self.send_save(
                                ok_msg.format("Commit ID:" + commit[:7]))
                            tool.checkout(commit)
                            self.release_desc = tool.get_commit_msg(
                                self.branch_tag, commit)
                        else:
                            self.release_desc = self.release_name
                        self.send_save(
                            ok_msg.format("Commit Msg: " + self.release_desc))
                    except Exception as e:
                        self.send_save(fail_msg("检出代码"), close=True)

                    self.deploy(ticket_no, info, tool, step_msg, ok_msg,
                                fail_msg, warn_msg)

                elif self.config.ticket_config.proj_role.repo == 'svn':
                    tool = SVNTools(
                        repo_url=self.config.ticket_config.proj_role.repo_url,
                        path=self.config.ticket_config.proj_role.src_dir,
                        env=self.config.ticket_config.proj_env.projenv_name,
                        username=self.config.ticket_config.proj_role.repo_user,
                        password=self.config.ticket_config.proj_role.
                        repo_password)
                    self.send_save(step_msg.format("【检出SVN代码】"))
                    if commit:
                        self.send_save(
                            ok_msg.format("Commit ID: " + commit[:7]))
                        model_name = '' if self.config.ticket_config.proj_role.repo_model == 'trunk' else self.brahch_tag
                        self.release_desc = tool.get_commit_msg(
                            int(commit),
                            self.config.ticket_config.proj_role.repo_model,
                            model_name=model_name)
                    else:
                        self.release_desc = self.release_name
                    self.send_save(
                        ok_msg.format("Commit Msg: " + self.release_desc))

                    c = int(commit) if commit else None

                    # 执行检出代码之前的命令,比如安装依赖等
                    if self.config.ticket_config.proj_role.prev_deploy:
                        try:
                            code = tool.run_cmd(self.config.ticket_config.
                                                proj_role.prev_deploy)
                            if code == 0:
                                self.send_save(ok_msg.format("执行检出代码之前的命令"))
                            else:
                                self.send_save(fail_msg("执行检出代码之前的命令"),
                                               close=True)
                        except Exception as e:
                            self.send_save(fail_msg("执行检出代码之前的命令"), close=True)

                    # 执行检出代码任务
                    try:
                        if self.config.ticket_config.proj_role.repo_model == 'trunk':
                            tool.checkout(
                                self.config.ticket_config.proj_role.repo_model,
                                revision=c)
                            self.send_save(ok_msg.format("检出代码Trunk版本号: " + c))
                        else:
                            tool.checkout(
                                self.config.ticket_config.proj_role.repo_model,
                                self.branch_tag,
                                revision=commit)
                            self.send_save(
                                ok_msg.format("检出分支: " + self.branch_tag +
                                              "版本号: "))
                    except Exception as e:
                        self.send_save(fail_msg("执行检出"), close=True)

                    self.deploy(ticket_no, info, tool, step_msg, ok_msg,
                                fail_msg, warn_msg)

                self.close()
                self.redis_instance.delete(unique_key)

            elif self.d_type == 'Rollback':
                if self.config.ticket_config.proj_role.repo == 'git':
                    tool = GitTools(
                        repo_url=self.config.ticket_config.proj_role.repo_url,
                        path=self.config.ticket_config.proj_role.src_dir,
                        env=self.config.ticket_config.proj_env.projenv_name)
                    self.deploy(ticket_no, info, tool, step_msg, ok_msg,
                                fail_msg, warn_msg)
                elif self.config.ticket_config.proj_role.repo == 'svn':
                    tool = SVNTools(
                        repo_url=self.config.ticket_config.proj_role.repo_url,
                        path=self.config.ticket_config.proj_role.src_dir,
                        env=self.config.ticket_config.proj_env.projenv_name,
                        username=self.config.ticket_config.proj_role.repo_user,
                        password=self.config.ticket_config.proj_role.
                        repo_password)
                    self.deploy(ticket_no, info, tool, step_msg, ok_msg,
                                fail_msg, warn_msg)
                else:
                    pass
            else:
                pass