예제 #1
0
파일: common.py 프로젝트: zmyer/teletraan
def clone_from_stage_name(request, env_name, stage_name, from_env_name,
                          from_stage_name, description):
    from_stage = environs_helper.get_env_by_stage(request, from_env_name,
                                                  from_stage_name)
    agent_configs = environs_helper.get_env_agent_config(
        request, from_env_name, from_stage_name)
    script_configs = environs_helper.get_env_script_config(
        request, from_env_name, from_stage_name)
    alarms_configs = environs_helper.get_env_alarms_config(
        request, from_env_name, from_stage_name)
    metrics_configs = environs_helper.get_env_metrics_config(
        request, from_env_name, from_stage_name)
    webhooks_configs = environs_helper.get_env_hooks_config(
        request, from_env_name, from_stage_name)
    promotes_configs = environs_helper.get_env_promotes_config(
        request, from_env_name, from_stage_name)

    new_data = {}
    new_data['envName'] = env_name
    new_data['stageName'] = stage_name
    new_data['description'] = description
    new_data['buildName'] = from_stage['buildName']
    new_data['branch'] = from_stage['branch']
    new_data['chatroom'] = from_stage['chatroom']
    new_data['maxParallel'] = from_stage['maxParallel']
    new_data['priority'] = from_stage['priority']
    new_data['stuckThreshold'] = from_stage['stuckThreshold']
    new_data['successThreshold'] = from_stage['successThreshold']
    new_data['acceptanceType'] = from_stage['acceptanceType']
    new_data['emailRecipients'] = from_stage['emailRecipients']
    new_data['notifyAuthors'] = from_stage['notifyAuthors']
    new_data['watchRecipients'] = from_stage['watchRecipients']
    new_data['maxDeployNum'] = from_stage['maxDeployNum']
    new_data['maxDeployDay'] = from_stage['maxDeployDay']
    new_data['overridePolicy'] = from_stage['overridePolicy']

    new_stage = environs_helper.create_env(request, new_data)

    # now clone all the extra configs
    if agent_configs:
        environs_helper.update_env_agent_config(request, env_name, stage_name,
                                                agent_configs)
    if script_configs:
        environs_helper.update_env_script_config(request, env_name, stage_name,
                                                 script_configs)
    if alarms_configs:
        environs_helper.update_env_alarms_config(request, env_name, stage_name,
                                                 alarms_configs)
    if metrics_configs:
        environs_helper.update_env_metrics_config(request, env_name,
                                                  stage_name, metrics_configs)
    if webhooks_configs:
        environs_helper.update_env_hooks_config(request, env_name, stage_name,
                                                webhooks_configs)
    if promotes_configs:
        environs_helper.update_env_promotes_config(request, env_name,
                                                   stage_name,
                                                   promotes_configs)

    return new_stage
예제 #2
0
def get_service_metrics(request, name, stage):
    metrics = environs_helper.get_env_metrics_config(request, name, stage)
    data = {}
    for metric in metrics:
        data[metric['title']] = _get_latest_metrics(metric['url'])
    return HttpResponse(json.dumps({'html': data}),
                        content_type="application/json")
예제 #3
0
    def get(self, request, name, stage=None):
        envs = environs_helper.get_all_env_stages(request, name)

        if len(envs) == 0:
            return redirect('/')

        stages, env = common.get_all_stages(envs, stage)
        env_promote = environs_helper.get_env_promotes_config(request, name, env['stageName'])
        stage = env['stageName']
        username = request.teletraan_user_id.name
        request_feedback = check_feedback_eligible(request, username)
        groups = environs_helper.get_env_capacity(request, name, stage, capacity_type="GROUP")
        metrics = environs_helper.get_env_metrics_config(request, name, stage)
        alarms = environs_helper.get_env_alarms_config(request, name, stage)

        if not env['deployId']:
            capacity_hosts = deploys_helper.get_missing_hosts(request, name, stage)
            response = render(request, 'environs/env_landing.html', {
                "env": env,
                "env_promote": env_promote,
                "stages": stages,
                "metrics": metrics,
                "alarms": alarms,
                "request_feedback": request_feedback,
                "groups": groups,
                "capacity_hosts": capacity_hosts,
                "pinterest": IS_PINTEREST,
            })
            showMode = 'complete'
            sortByStatus = 'true'
        else:
            # Get deploy progress
            progress = deploys_helper.update_progress(request, name, stage)
            showMode = _fetch_param_with_cookie(
                request, 'showMode', MODE_COOKIE_NAME, 'complete')
            sortByStatus = _fetch_param_with_cookie(
                request, 'sortByStatus', STATUS_COOKIE_NAME, 'true')
            report = agent_report.gen_report(request, env, progress, sortByStatus=sortByStatus)
            report.showMode = showMode
            report.sortByStatus = sortByStatus
            response = render(request, 'environs/env_landing.html', {
                "env": env,
                "env_promote": env_promote,
                "stages": stages,
                "report": report,
                "has_deploy": True,
                "metrics": metrics,
                "alarms": alarms,
                "request_feedback": request_feedback,
                "groups": groups,
                "pinterest": IS_PINTEREST,
            })

        # save preferences
        response.set_cookie(ENV_COOKIE_NAME, genEnvCookie(request, name))
        response.set_cookie(MODE_COOKIE_NAME, showMode)
        response.set_cookie(STATUS_COOKIE_NAME, sortByStatus)

        return response
예제 #4
0
    def get(self, request, name, stage):
        if request.is_ajax():
            env = environs_helper.get_env_by_stage(request, name, stage)
            metrics = environs_helper.get_env_metrics_config(request, name, stage)
            html = render_to_string('configs/metrics_config.tmpl', {
                "env": env,
                "metrics": metrics,
                "csrf_token": get_token(request),
            })
            return HttpResponse(json.dumps({'html': html}), content_type="application/json")

        envs = environs_helper.get_all_env_stages(request, name)
        stages, env = common.get_all_stages(envs, stage)
        metrics = environs_helper.get_env_metrics_config(request, name, stage)

        return render(request, 'configs/metrics_config.html', {
            "env": env,
            "stages": stages,
            "metrics": metrics,
        })
예제 #5
0
    def get(self, request, name, stage):
        if request.is_ajax():
            env = environs_helper.get_env_by_stage(request, name, stage)
            metrics = environs_helper.get_env_metrics_config(
                request, name, stage)
            html = render_to_string('configs/metrics_config.tmpl', {
                "env": env,
                "metrics": metrics,
                "csrf_token": get_token(request),
            })
            return HttpResponse(json.dumps({'html': html}),
                                content_type="application/json")

        envs = environs_helper.get_all_env_stages(request, name)
        stages, env = common.get_all_stages(envs, stage)
        metrics = environs_helper.get_env_metrics_config(request, name, stage)

        return render(request, 'configs/metrics_config.html', {
            "envs": envs,
            "env": env,
            "stages": stages,
            "metrics": metrics,
        })
예제 #6
0
파일: common.py 프로젝트: lilida/teletraan
def clone_from_stage_name(request, env_name, stage_name, from_env_name, from_stage_name,
                          description, external_id):
    from_stage = environs_helper.get_env_by_stage(request, from_env_name, from_stage_name)
    agent_configs = environs_helper.get_env_agent_config(request, from_env_name, from_stage_name)
    script_configs = environs_helper.get_env_script_config(request, from_env_name, from_stage_name)
    alarms_configs = environs_helper.get_env_alarms_config(request, from_env_name, from_stage_name)
    metrics_configs = environs_helper.get_env_metrics_config(request, from_env_name,
                                                             from_stage_name)
    webhooks_configs = environs_helper.get_env_hooks_config(request, from_env_name, from_stage_name)
    promotes_configs = environs_helper.get_env_promotes_config(request, from_env_name,
                                                               from_stage_name)

    new_data = {}
    new_data['envName'] = env_name
    new_data['stageName'] = stage_name
    new_data['description'] = description
    new_data['buildName'] = from_stage['buildName']
    new_data['branch'] = from_stage['branch']
    new_data['chatroom'] = from_stage['chatroom']
    new_data['maxParallel'] = from_stage['maxParallel']
    new_data['priority'] = from_stage['priority']
    new_data['stuckThreshold'] = from_stage['stuckThreshold']
    new_data['successThreshold'] = from_stage['successThreshold']
    new_data['acceptanceType'] = from_stage['acceptanceType']
    new_data['emailRecipients'] = from_stage['emailRecipients']
    new_data['notifyAuthors'] = from_stage['notifyAuthors']
    new_data['watchRecipients'] = from_stage['watchRecipients']
    new_data['maxDeployNum'] = from_stage['maxDeployNum']
    new_data['maxDeployDay'] = from_stage['maxDeployDay']
    new_data['overridePolicy'] = from_stage['overridePolicy']
    new_data['externalId'] = external_id

    new_stage = environs_helper.create_env(request, new_data)

    # now clone all the extra configs
    if agent_configs:
        environs_helper.update_env_agent_config(request, env_name, stage_name, agent_configs)
    if script_configs:
        environs_helper.update_env_script_config(request, env_name, stage_name, script_configs)
    if alarms_configs:
        environs_helper.update_env_alarms_config(request, env_name, stage_name, alarms_configs)
    if metrics_configs:
        environs_helper.update_env_metrics_config(request, env_name, stage_name, metrics_configs)
    if webhooks_configs:
        environs_helper.update_env_hooks_config(request, env_name, stage_name, webhooks_configs)
    if promotes_configs:
        environs_helper.update_env_promotes_config(request, env_name, stage_name, promotes_configs)

    return new_stage
예제 #7
0
    def get(self, request, name, stage=None):
        envs = environs_helper.get_all_env_stages(request, name)

        if len(envs) == 0:
            return redirect('/')

        stages, env = common.get_all_stages(envs, stage)
        env_promote = environs_helper.get_env_promotes_config(request, name, env['stageName'])
        stage = env['stageName']
        username = request.teletraan_user_id.name
        request_feedback = check_feedback_eligible(request, username)
        groups = environs_helper.get_env_capacity(request, name, stage, capacity_type="GROUP")
        metrics = environs_helper.get_env_metrics_config(request, name, stage)
        alarms = environs_helper.get_env_alarms_config(request, name, stage)
        basic_cluster_info = None
        if IS_PINTEREST:
            basic_cluster_info = clusters_helper.get_cluster(request, name, stage)

        if not env['deployId']:
            capacity_hosts = deploys_helper.get_missing_hosts(request, name, stage)
            provisioning_hosts = environ_hosts_helper.get_hosts(request, name, stage)
            if IS_PINTEREST:
                basic_cluster_info = clusters_helper.get_cluster(request, env['envName'], env['stageName'])
                if basic_cluster_info and basic_cluster_info.get('capacity'):
                    hosts_in_cluster = clusters_helper.get_host_names(request, env['envName'], env['stageName'])
                    num_to_fake = basic_cluster_info.get('capacity') - len(hosts_in_cluster)
                    for i in range(num_to_fake):
                        faked_host = {}
                        faked_host['hostName'] = 'UNKNOWN'
                        faked_host['hostId'] = 'UNKNOWN'
                        faked_host['state'] = 'PROVISIONED'
                        provisioning_hosts.append(faked_host)

            response = render(request, 'environs/env_landing.html', {
                "env": env,
                "env_promote": env_promote,
                "stages": stages,
                "metrics": metrics,
                "alarms": alarms,
                "request_feedback": request_feedback,
                "groups": groups,
                "capacity_hosts": capacity_hosts,
                "provisioning_hosts": provisioning_hosts,
                "basic_cluster_info": basic_cluster_info,
                "pinterest": IS_PINTEREST,
            })
            showMode = 'complete'
            sortByStatus = 'true'
        else:
            # Get deploy progress
            progress = deploys_helper.update_progress(request, name, stage)
            showMode = _fetch_param_with_cookie(
                request, 'showMode', MODE_COOKIE_NAME, 'complete')
            sortByStatus = _fetch_param_with_cookie(
                request, 'sortByStatus', STATUS_COOKIE_NAME, 'true')
            report = agent_report.gen_report(request, env, progress, sortByStatus=sortByStatus)
            report.showMode = showMode
            report.sortByStatus = sortByStatus
            response = render(request, 'environs/env_landing.html', {
                "env": env,
                "env_promote": env_promote,
                "stages": stages,
                "report": report,
                "has_deploy": True,
                "metrics": metrics,
                "alarms": alarms,
                "request_feedback": request_feedback,
                "groups": groups,
                "basic_cluster_info": basic_cluster_info,
                "pinterest": IS_PINTEREST,
            })

        # save preferences
        response.set_cookie(ENV_COOKIE_NAME, genEnvCookie(request, name))
        response.set_cookie(MODE_COOKIE_NAME, showMode)
        response.set_cookie(STATUS_COOKIE_NAME, sortByStatus)

        return response
def clone_cluster(request, src_name, src_stage):
    try:
        params = request.POST
        dest_name = params.get('new_environment', src_name)
        dest_stage = params.get('new_stage', src_stage + '_clone')

        src_cluster_name = '{}-{}'.format(src_name, src_stage)
        dest_cluster_name = '{}-{}'.format(dest_name, dest_stage)

        ##0. teletraan service get src env buildName
        src_env = environs_helper.get_env_by_stage(request, src_name,
                                                   src_stage)
        build_name = src_env.get('buildName', None)
        external_id = src_env.get('externalId', None)

        ##1. teletraan service create a new env
        dest_env = environs_helper.create_env(
            request, {
                'envName': dest_name,
                'stageName': dest_stage,
                'buildName': build_name,
                'externalId': external_id
            })
        log.info('clone_cluster, created a new env %s' % dest_env)

        ##2. rodimus service get src_cluster config
        src_cluster_info = clusters_helper.get_cluster(request,
                                                       src_cluster_name)
        log.info('clone_cluster, src cluster info %s' % src_cluster_info)
        configs = src_cluster_info.get('configs')
        if configs:
            cmp_group = configs.get('cmp_group')
            if cmp_group:
                cmp_groups_set = set(cmp_group.split(','))
                cmp_groups_set.remove(src_cluster_name)
                cmp_groups_set.remove('CMP')
                cmp_groups_set.add(dest_cluster_name)
                # CMP needs to be the first in the list
                configs['cmp_group'] = ','.join(['CMP'] + list(cmp_groups_set))
                src_cluster_info['configs'] = configs

        ##3. rodimus service post create cluster
        src_cluster_info['clusterName'] = dest_cluster_name
        src_cluster_info['capacity'] = 0
        log.info('clone_cluster, request clone cluster info %s' %
                 src_cluster_info)
        dest_cluster_info = clusters_helper.create_cluster_with_env(
            request, dest_cluster_name, dest_name, dest_stage,
            src_cluster_info)
        log.info('clone_cluster, cloned cluster info %s' % dest_cluster_info)

        ##4. teletraan service update_env_basic_config
        environs_helper.update_env_basic_config(
            request,
            dest_name,
            dest_stage,
            data={"clusterName": dest_cluster_name})
        ##5. teletraan service set up env and group relationship
        environs_helper.update_env_capacity(request,
                                            dest_name,
                                            dest_stage,
                                            capacity_type="GROUP",
                                            data=[dest_cluster_name])

        ##6. get src script_config
        src_script_configs = environs_helper.get_env_script_config(
            request, src_name, src_stage)
        src_agent_configs = environs_helper.get_env_agent_config(
            request, src_name, src_stage)
        src_alarms_configs = environs_helper.get_env_alarms_config(
            request, src_name, src_stage)
        src_metrics_configs = environs_helper.get_env_metrics_config(
            request, src_name, src_stage)
        src_webhooks_configs = environs_helper.get_env_hooks_config(
            request, src_name, src_stage)

        ##8. clone all the extra configs
        if src_agent_configs:
            environs_helper.update_env_agent_config(request, dest_name,
                                                    dest_stage,
                                                    src_agent_configs)
        if src_script_configs:
            environs_helper.update_env_script_config(request, dest_name,
                                                     dest_stage,
                                                     src_script_configs)
        if src_alarms_configs:
            environs_helper.update_env_alarms_config(request, dest_name,
                                                     dest_stage,
                                                     src_alarms_configs)
        if src_metrics_configs:
            environs_helper.update_env_metrics_config(request, dest_name,
                                                      dest_stage,
                                                      src_metrics_configs)
        if src_webhooks_configs:
            environs_helper.update_env_hooks_config(request, dest_name,
                                                    dest_stage,
                                                    src_webhooks_configs)

        return HttpResponse(json.dumps(src_cluster_info),
                            content_type="application/json")
    except NotAuthorizedException as e:
        log.error("Have an NotAuthorizedException error {}".format(e))
        return HttpResponse(e, status=403, content_type="application/json")
    except Exception as e:
        log.error("Failed to clone cluster env_name: %s, stage_name: %s" %
                  (src_name, src_stage))
        log.error(traceback.format_exc())
        return HttpResponse(e, status=500, content_type="application/json")
예제 #9
0
    def get(self, request, name, stage=None):
        envs = environs_helper.get_all_env_stages(request, name)

        if len(envs) == 0:
            return redirect('/')

        stages, env = common.get_all_stages(envs, stage)
        env_promote = environs_helper.get_env_promotes_config(
            request, name, env['stageName'])
        stage = env['stageName']
        username = request.teletraan_user_id.name
        request_feedback = check_feedback_eligible(request, username)
        groups = environs_helper.get_env_capacity(request,
                                                  name,
                                                  stage,
                                                  capacity_type="GROUP")
        metrics = environs_helper.get_env_metrics_config(request, name, stage)
        alarms = environs_helper.get_env_alarms_config(request, name, stage)
        basic_cluster_info = clusters_helper.get_cluster(request, name, stage)

        if not env['deployId']:
            capacity_hosts = deploys_helper.get_missing_hosts(
                request, name, stage)
            provisioning_hosts = environ_hosts_helper.get_hosts(
                request, name, stage)
            response = render(
                request, 'environs/env_landing.html', {
                    "env": env,
                    "env_promote": env_promote,
                    "stages": stages,
                    "metrics": metrics,
                    "alarms": alarms,
                    "request_feedback": request_feedback,
                    "groups": groups,
                    "capacity_hosts": capacity_hosts,
                    "provisioning_hosts": provisioning_hosts,
                    "basic_cluster_info": basic_cluster_info,
                    "pinterest": IS_PINTEREST,
                })
            showMode = 'complete'
            sortByStatus = 'true'
        else:
            # Get deploy progress
            progress = deploys_helper.update_progress(request, name, stage)
            showMode = _fetch_param_with_cookie(request, 'showMode',
                                                MODE_COOKIE_NAME, 'complete')
            sortByStatus = _fetch_param_with_cookie(request, 'sortByStatus',
                                                    STATUS_COOKIE_NAME, 'true')
            report = agent_report.gen_report(request,
                                             env,
                                             progress,
                                             sortByStatus=sortByStatus)
            report.showMode = showMode
            report.sortByStatus = sortByStatus
            response = render(
                request, 'environs/env_landing.html', {
                    "env": env,
                    "env_promote": env_promote,
                    "stages": stages,
                    "report": report,
                    "has_deploy": True,
                    "metrics": metrics,
                    "alarms": alarms,
                    "request_feedback": request_feedback,
                    "groups": groups,
                    "basic_cluster_info": basic_cluster_info,
                    "pinterest": IS_PINTEREST,
                })

        # save preferences
        response.set_cookie(ENV_COOKIE_NAME, genEnvCookie(request, name))
        response.set_cookie(MODE_COOKIE_NAME, showMode)
        response.set_cookie(STATUS_COOKIE_NAME, sortByStatus)

        return response
예제 #10
0
def get_service_metrics(request, name, stage):
    metrics = environs_helper.get_env_metrics_config(request, name, stage)
    data = {}
    for metric in metrics:
        data[metric['title']] = _get_latest_metrics(metric['url'])
    return HttpResponse(json.dumps({'html': data}), content_type="application/json")
예제 #11
0
def clone_cluster(request, src_name, src_stage):
    try:
        params = request.POST
        dest_name = params.get('new_environment', src_name)
        dest_stage = params.get('new_stage', src_stage + '_clone')

        src_cluster_name = '{}-{}'.format(src_name, src_stage)
        dest_cluster_name = '{}-{}'.format(dest_name, dest_stage)

        ##0. teletraan service get src env buildName
        src_env = environs_helper.get_env_by_stage(request, src_name, src_stage)
        build_name = src_env.get('buildName', None)

        ##1. teletraan service create a new env
        dest_env = environs_helper.create_env(request, {
            'envName': dest_name,
            'stageName': dest_stage,
            'buildName': build_name
        })
        log.info('clone_cluster, created a new env %s' % dest_env)

        ##2. rodimus service get src_cluster config
        src_cluster_info = clusters_helper.get_cluster(request, src_cluster_name)
        log.info('clone_cluster, src cluster info %s' % src_cluster_info)
        configs = src_cluster_info.get('configs')
        if configs:
            cmp_group = configs.get('cmp_group')
            if cmp_group:
                cmp_groups_set = set(cmp_group.split(','))
                cmp_groups_set.remove(src_cluster_name)
                cmp_groups_set.remove('CMP')
                cmp_groups_set.add(dest_cluster_name)
                # CMP needs to be the first in the list
                configs['cmp_group'] = ','.join(['CMP'] + list(cmp_groups_set))
                src_cluster_info['configs'] = configs

        ##3. rodimus service post create cluster
        src_cluster_info['clusterName'] = dest_cluster_name
        src_cluster_info['capacity'] = 0
        log.info('clone_cluster, request clone cluster info %s' % src_cluster_info)
        dest_cluster_info = clusters_helper.create_cluster(request, dest_cluster_name, src_cluster_info)
        log.info('clone_cluster, cloned cluster info %s' % dest_cluster_info)

        ##4. teletraan service update_env_basic_config
        environs_helper.update_env_basic_config(request, dest_name, dest_stage,
                                                data={"clusterName": dest_cluster_name}
                                                )
        ##5. teletraan service set up env and group relationship
        environs_helper.update_env_capacity(request, dest_name, dest_stage, capacity_type="GROUP",
                                            data=[dest_cluster_name])

        ##6. get src script_config
        src_script_configs = environs_helper.get_env_script_config(request, src_name, src_stage)
        src_agent_configs = environs_helper.get_env_agent_config(request, src_name, src_stage)
        src_alarms_configs = environs_helper.get_env_alarms_config(request, src_name, src_stage)
        src_metrics_configs = environs_helper.get_env_metrics_config(request, src_name, src_stage)
        src_webhooks_configs = environs_helper.get_env_hooks_config(request, src_name, src_stage)

        ##8. clone all the extra configs
        if src_agent_configs:
            environs_helper.update_env_agent_config(request, dest_name, dest_stage, src_agent_configs)
        if src_script_configs:
            environs_helper.update_env_script_config(request, dest_name, dest_stage, src_script_configs)
        if src_alarms_configs:
            environs_helper.update_env_alarms_config(request, dest_name, dest_stage, src_alarms_configs)
        if src_metrics_configs:
            environs_helper.update_env_metrics_config(request, dest_name, dest_stage, src_metrics_configs)
        if src_webhooks_configs:
            environs_helper.update_env_hooks_config(request, dest_name, dest_stage, src_webhooks_configs)

        return HttpResponse(json.dumps(src_cluster_info), content_type="application/json")
    except NotAuthorizedException as e:
        log.error("Have an NotAuthorizedException error {}".format(e))
        return HttpResponse(e, status=403, content_type="application/json")
    except Exception as e:
        log.error("Failed to clone cluster env_name: %s, stage_name: %s" % (src_name, src_stage))
        log.error(traceback.format_exc())
        return HttpResponse(e, status=500, content_type="application/json")