Esempio n. 1
0
    def post(self, request, name, stage):
        log.info("Post to capacity with data {0}".format(request.body))
        try:
            cluster_name = '{}-{}'.format(name, stage)
            cluster_info = json.loads(request.body)

            log.info("Create Capacity in the provider")
            if 'configs' in cluster_info:
                if 'spiffe_id' in cluster_info['configs']:
                    log.error("Teletraan does not support user to change spiffe_id %s"  % cluster_info['spiffe_id'])
                    raise TeletraanException("Teletraan does not support user to create spiffe_id")

            clusters_helper.create_cluster_with_env(request, cluster_name, name, stage, cluster_info)

            log.info("Associate cluster_name to environment")
            # Update cluster info
            environs_helper.update_env_basic_config(
                request, name, stage, data={"clusterName": cluster_name})

            log.info("Update capacity to the environment")
            # set up env and group relationship
            environs_helper.add_env_capacity(
                request, name, stage, capacity_type="GROUP", data=cluster_name)
            return HttpResponse("{}", 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("Have an error {}".format(e))
            return HttpResponse(e, status=500, content_type="application/json")
Esempio n. 2
0
    def post(self, request, name, stage):
        log.info("Post to capacity with data {0}".format(request.body))
        try:
            cluster_name = '{}-{}'.format(name, stage)
            cluster_info = json.loads(request.body)

            log.info("Create Capacity in the provider")
            clusters_helper.create_cluster_with_env(request, cluster_name, name, stage, cluster_info)

            log.info("Associate cluster_name to environment")
            # Update cluster info
            environs_helper.update_env_basic_config(
                request, name, stage, data={"clusterName": cluster_name})

            log.info("Update capacity to the environment")
            # set up env and group relationship
            environs_helper.add_env_capacity(
                request, name, stage, capacity_type="GROUP", data=cluster_name)
            return HttpResponse("{}", 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("Have an error {}".format(e))
            return HttpResponse(e, status=500, content_type="application/json")
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")