Пример #1
0
def get_resources_num(request):
    """
    get num of EC2, ELB, AMI in selected region
    Args:
        request (django.http.request.HttpRequest)
    """
    region = RegionInfo.get_region(request)
    awssession = AwsAccount.get_awssession(region)
    elb_client = awssession.client('elb')
    elbs = elb_client.describe_load_balancers()
    elbnum = len(elbs['LoadBalancerDescriptions'])
    ec2conn = awssession.resource('ec2')
    instances = ec2conn.instances.filter(Filters=[{
        'Name': 'tag:Name',
        'Values': ['preprd-*', 'prd-*', 'PRD-*']
    }])
    ec2num = 0
    for _ in instances:
        ec2num += 1
    images = ec2conn.images.filter(Filters=[{
        'Name': 'is-public',
        'Values': ['false']
    }])
    aminum = 0
    for _ in images:
        aminum += 1
    logger.debug('thers are %s ec2, %s elb, %s ami' % (ec2num, elbnum, aminum))
    return HttpResponse(json.dumps({'aminum': aminum,
                                    'ec2num': ec2num,
                                    'elbnum': elbnum}))
Пример #2
0
def add_elb_instances(request):
    """
    add instances to each elb.
    Args:
        request (django.http.request.HttpRequest)
    """
    region = request.GET.get('region')
    if not region:
        return HttpResponse('region not present!', status=400)
    elbconn = AwsAccount.get_awssession(region).client('elb')
    loadbalancers = elbconn.describe_load_balancers().get(
        'LoadBalancerDescriptions')
    elbs = []
    if loadbalancers:
        for lb in loadbalancers:
            if lb['VPCId'] == DEFAULT_PREPRD_VPC[region][1]:
                elbs.append(lb.get('LoadBalancerName'))
    response = {'ret': True, 'msg': {}}
    for elbname in elbs:
        register_result = LoadBalancerCreater.register_instances(
            region, elbname)
        if not register_result['ret']:
            response.update({'ret': False})
            response['msg'].update({elbname: register_result['msg']})
    return HttpResponse(json.dumps(response))
Пример #3
0
def create_business_amis(region, module_version_dict, module_id_dict):
    """
    create amis
    Args:
        region (string): region to create ami
        module_version_dict (dict): module name to version dict
        module_id_dict (dict): module name and instance id
    Returns:
        image list each items contains module name and image object
    """
    image_list = []
    boto_session = AwsAccount.get_awssession(region)
    ec2res = boto_session.resource('ec2')
    for module in module_version_dict:
        module_version = module_version_dict.get(module)
        image_name = _get_image_name(region, module, module_version)
        instance_id = module_id_dict.get(module)
        image = create_ami(ec2res, instance_id, image_name)
        # make sure the ami has already created.
        while True:
            try:
                image.load()
                break
            except:
                pass
        logger.info(
            "finish creating %s'ami, its name is %s, id is %s, wait its state change to available."
            % (module, image.name, image.image_id))
        image_list.append((module, image))
    logger.info('all amis have created, waiting to be available')
    avail_ami_list, failed_ami_list = wait_ami_available(image_list)
    if failed_ami_list:
        logger.error('some ami create failed: %s' % failed_ami_list)
    return avail_ami_list, failed_ami_list
Пример #4
0
 def get_statck_status(region, stack_name):
     aws_session = AwsAccount.get_awssession(region)
     cfnconn = aws_session.resource('cloudformation')
     stack = cfnconn.Stack(stack_name)
     try:
         stack_status = stack.stack_status
     except ClientError, ce:
         if 'does not exist' in ce.message:
             stack_status = 'DELETE_COMPLETE'
         else:
             raise
Пример #5
0
def find_instances(region, tag_name_patterns, is_running=False):
    vpc_id = DEFAULT_PREPRD_VPC[region][1]
    ec2conn = AwsAccount.get_awssession(region).resource('ec2')
    filters = [{
        'Name': 'vpc-id',
        'Values': [vpc_id]
    }, {
        'Name': 'tag:Name',
        'Values': tag_name_patterns
    }]
    if is_running:
        filters.append({'Name': 'instance-state-name', 'Values': ['running']})
    return ec2conn.instances.filter(Filters=filters)
Пример #6
0
def to_dict(loadbalancer, region, can_operate):
    """
    generate elb infomation dict for datatables.
    Args:
        loadbalancer (dict): a loadbalancer infos return by describe-loadbalancers
        region (string): region name
        can_operate (bool): if has operate permission with elb page
    """
    ret = {}
    loadbalancer_name = loadbalancer.get('LoadBalancerName')
    ret.update({'loadbalancer_name': loadbalancer_name})
    ret.update({'dns_name': loadbalancer.get('DNSName')})
    instances = loadbalancer.get('Instances', '')
    in_services = []
    out_services = []
    if instances:
        awssession = AwsAccount.get_awssession(region)
        elbclient = awssession.client('elb')
        instance_states = elbclient.describe_instance_health(
            LoadBalancerName=loadbalancer_name,
            Instances=instances,
        ).get('InstanceStates')
        if instance_states:
            for state in instance_states:
                if state['State'] == 'InService':
                    in_services.append(state['InstanceId'])
                else:
                    out_services.append(state['InstanceId'])
            in_services, out_services = __format_instance_info(
                region, in_services, out_services)
    ret.update({'in_service_list': in_services})
    ret.update({'out_service_list': out_services})
    if can_operate:
        ret.update({
            'operations':
            '''<button class="btn btn-primary btn-sm" type="button" onclick="editInstance('%s')">
                                        <i class="fa fa-pencil"></i>
                                            编辑实例
                                     </button>''' % loadbalancer_name
        })
    else:
        ret.update({
            'operations':
            '''<button class="btn btn-primary btn-sm" type="button"
                                 onclick="alertMessage('无法编辑实例,请联系管理员进行授权', 'small', 'Message', 'Primary')">
                                <i class="fa fa-pencil"></i>
                                    编辑实例(未授权)
                             </button>'''
        })
    logger.debug('elb row data: %s' % ret)
    return ret
Пример #7
0
 def __init__(self, account_name, region):
     self.account_name = account_name
     self.region = region
     self.session = AwsAccount.get_awssession(region, account_name)
     self.resources = {
         "images": [],
         "keypairs": [],
         "instance_types": [],
         "vpcs": [],
         "subnets": {},
         "security_groups": {},
         "instance_profiles": [],
         "elbs": {}
     }
Пример #8
0
 def get_stack_resources_status(region, stack_name):
     aws_session = AwsAccount.get_awssession(region)
     cfnconn = aws_session.resource('cloudformation')
     stack = cfnconn.Stack(stack_name)
     events = stack.events.all()
     resource_dict = {}
     for e in events:
         resource_name = e.logical_resource_id
         resource_status = e.resource_status
         resource_status_reason = e.resource_status_reason
         if resource_name in resource_dict:
             continue
         resource_dict.update({resource_name: [resource_status, resource_status_reason]})
     return resource_dict
Пример #9
0
def check_instances_conf(request):
    try:
        module_id_dict = json.loads(request.POST['module_id_dict'])
        region = request.POST['region']
    except (KeyError, ValueError):
        return HttpResponse('bad request: check instance conf', status=400)
    boto_session = AwsAccount.get_awssession(region)
    ec2res = boto_session.resource('ec2')
    check_instances = []
    for instance_id in module_id_dict.values():
        instance = ec2res.Instance(instance_id)
        check_instances.append(instance)
    try:
        ec2_checker = Ec2Checker(check_instances, region)
        check_result = ec2_checker.check()
        logger.debug('ec2 config check result:\n%s' % json.dumps(check_result))

        check_report = Report(check_instances, check_result)
        check_result_report = check_report.report()
        if_check_pass = check_report.pass_check(check_result)
        if if_check_pass:
            return HttpResponse(
                json.dumps({
                    'ret': True,
                    'info': {
                        'percentage': 0.4,
                        'detail': u'实例检查结果:%s' % check_result_report,
                        'todo': u'删除日志文件...'
                    }
                }))
        else:
            return HttpResponse(
                json.dumps({
                    'ret': False,
                    'info': {
                        'percentage': 0.4,
                        'detail': u'实例检查结果:%s' % check_result_report,
                        'todo': u'实例检查未通过,停止创建AMI'
                    }
                }))
    except:
        return HttpResponse(
            json.dumps({
                'ret': False,
                'info': {
                    'percentage': 0.4,
                    'detail': u'执行实例检查时出错:\n%s' % traceback.format_exc(),
                    'todo': u'实例检查出错,停止创建AMI'
                }
            }))
Пример #10
0
 def find_all_instances(region, account_name):
     region_obj = RegionInfo.objects.get(region=region)
     basicservice_objs = region_obj.basicservicedeployinfo_set.all()
     basic_service_names = [
         service.service_name for service in basicservice_objs
     ]
     session = AwsAccount.get_awssession(region, account_name)
     ec2conn = session.resource('ec2')
     instances = ec2conn.instances.filter(Filters=[{
         'Name':
         'tag:Name',
         'Values':
         ['*-%s-*' % service_name for service_name in basic_service_names]
     }])
     return instances
Пример #11
0
def deal_instance_for_elb(elbname, region, instance_ids):
    """
    register or unregister instance for elb
    Args:
        elbname (string): loadbalancer name
        region (string): region name
        instance_ids (list): instance id list that finally registed in elb.
    """
    registered_instance_ids = get_instances_in_elb(region, elbname)
    logger.debug('registered_ids: %s' % registered_instance_ids)
    logger.debug('choosed instance ids: %s' % instance_ids)
    regist_instance_ids = [
        instance_id for instance_id in instance_ids
        if instance_id not in registered_instance_ids
    ]
    logger.debug(regist_instance_ids)
    deregist_instance_ids = [
        instance_id for instance_id in registered_instance_ids
        if instance_id not in instance_ids
    ]
    logger.debug(deregist_instance_ids)
    elbconn = AwsAccount.get_awssession(region).client('elb')
    if deregist_instance_ids:
        deregister_instances_params = []
        for instance_id in deregist_instance_ids:
            deregister_instances_params.append({'InstanceId': instance_id})
        try:
            elbconn.deregister_instances_from_load_balancer(
                LoadBalancerName=elbname,
                Instances=deregister_instances_params)
        except:
            error_msg = 'deregister instance failed for %s, error message: \n%s' % (
                elbname, traceback.format_exc())
            logger.error(error_msg)
            return {'ret': False, 'msg': error_msg}
    if regist_instance_ids:
        register_instances_params = []
        for instance_id in regist_instance_ids:
            register_instances_params.append({'InstanceId': instance_id})
        try:
            elbconn.register_instances_with_load_balancer(
                LoadBalancerName=elbname, Instances=register_instances_params)
        except:
            error_msg = 'register instance failed for %s, error message: \n%s' % (
                elbname, traceback.format_exc())
            logger.error(error_msg)
            return {'ret': False, 'msg': error_msg}
    return {'ret': True}
Пример #12
0
 def create_elb_stack(region, elbs):
     """
     create elb's cloudformation stack in specified region.
     Args:
         region (string): region name
         elbs (list): list of elb names want to create
     """
     aws_session = AwsAccount.get_awssession(region)
     cfnconn = aws_session.resource('cloudformation')
     elb_cfn_template = ElbCfnTemplate(region)
     template_content = elb_cfn_template.get_content()
     stack_name = elb_cfn_template.stack_name
     template_params = elb_cfn_template.get_template_params(elbs)
     stack = cfnconn.create_stack(StackName=stack_name,
                                  TemplateBody=template_content,
                                  Parameters=template_params,
                                  OnFailure='DELETE'  # when create elb stack failed, delete it
                                  )
     return stack.stack_name
Пример #13
0
def add_instance_tags(request):
    region = request.POST.get('region')
    account = request.POST.get('account')
    instance_ids = request.POST.getlist('instanceIds[]')
    opset_name = request.POST.get('opsetName')
    if not region or not account or not instance_ids or not opset_name:
        return HttpResponse('bad request!', status=400)
    try:
        region_obj = RegionInfo.objects.get(region=region)
        account_name = 'cn-%s' % account if region == 'cn-north-1' else 'en-%s' % account
        account_obj = AwsAccount.objects.get(name=account_name)
        ec2optionset = Ec2OptionSet.objects.get(name=opset_name, region=region_obj, account=account_obj)
    except (RegionInfo.DoesNotExist, AwsAccount.DoesNotExist, Ec2OptionSet.DoesNotExist) as ne:
        return HttpResponse('model not found in db: %s' % ne.message, status=500)
    session = AwsAccount.get_awssession(region, account)
    ec2_resource = session.resource('ec2')
    logger.info('add instance and ebs tags: %s, %s, %s, %s' % (
        account_name,
        region,
        opset_name,
        instance_ids
    ))

    try:
        tags = get_tags_by_module(ec2optionset.module, region)
        result = add_ec2_tags(ec2_resource, tags, instance_ids)
    except:
        error_msg = 'add instance tags failed, error msg:\n%s' % traceback.format_exc()
        logger.error(error_msg)
        return HttpResponse(error_msg, status=500)
    if result['failed']:
        error_msg = 'some instances add instance tags failed %s' % ', '.join(result['failed'])
        logger.error(error_msg)
        return HttpResponse(error_msg, status=500)
    logger.info('all instance tags add success')

    try:
        result = add_volume_tags(ec2_resource, instance_ids)
    except:
        error_msg = 'add ebs tags failed, error msg:\n%s' % traceback.format_exc()
        logger.error(error_msg)
        return HttpResponse(error_msg, status=500)
    return HttpResponse(json.dumps({'ret': True}))
Пример #14
0
 def launch_instances(self, region_obj, module_obj):
     region_name = region_obj.region
     module_name = module_obj.module_name
     logger.info('launch instances for module: %s in region: %s' %
                 (module_name, region_name))
     ec2opset_objs = module_obj.ec2optionset_set.filter(region=region_obj)
     opset_count = ec2opset_objs.count()
     if opset_count == 0:
         self.failed_modules[region_name].update({
             module_name:
             'no launch parameters found in %s' % region_name
         })
         return
     if opset_count > 1:
         self.failed_modules[region_name].update({
             module_name:
             'too many launch parameters (%s) found for module in %s.' %
             (opset_count, region_name)
         })
         return
     ec2opset_obj = ec2opset_objs[0]
     if ec2opset_obj.image is None:
         self.failed_modules[region_name].update({
             module_name:
             'image in %s has been registered.' % region_name
         })
         return
     try:
         tags = get_tags_by_module(module_obj, region_name)
         ec2opset_obj.tags = tags
         session = AwsAccount.get_awssession(region_name)
         ec2res = session.resource('ec2')
         elbclient = session.client('elb')
         instance_ids = run_instances(ec2res, elbclient, ec2opset_obj,
                                      module_obj.instance_count)
     except:
         self.failed_modules[region_name].update({
             module_name:
             'launch instance faled: \n%s' % traceback.format_exc()
         })
         return
     self.add_tags(ec2res, module_obj, region_name, instance_ids)
Пример #15
0
 def get_template_params(self, elbs):
     params_list = []
     vpc_id = DEFAULT_PREPRD_VPC[self.region][1]
     param_dict = {"ParameterKey": "vpcId", "ParameterValue": vpc_id}
     params_list.append(param_dict)
     ec2conn = AwsAccount.get_awssession(self.region).resource('ec2')
     vpc = ec2conn.Vpc(vpc_id)
     # todo: get args from db table AwsResource
     subnets = vpc.subnets.all()
     private_subnets = []
     public_subnets = []
     for subnet in subnets:
         for tag in subnet.tags:
             if tag['Key'] == 'Name':
                 subnet_name = tag['Value']
                 if 'prv' in subnet_name:
                     private_subnets.append(subnet.subnet_id)
                 elif 'pub' in subnet_name:
                     public_subnets.append(subnet.subnet_id)
     params_list.append({
         "ParameterKey": "privateSubnet1",
         "ParameterValue": private_subnets[0]
     })
     params_list.append({
         "ParameterKey": "privateSubnet2",
         "ParameterValue": private_subnets[1]
     })
     params_list.append({
         "ParameterKey": "publicSubnet1",
         "ParameterValue": public_subnets[0]
     })
     params_list.append({
         "ParameterKey": "publicSubnet2",
         "ParameterValue": public_subnets[1]
     })
     for elb_name in elbs:
         params_list.append({
             "ParameterKey": ''.join(elb_name.split('-')),
             "ParameterValue": 'yes'
         })
     return params_list
Пример #16
0
def register_instance(request):
    region = request.GET.get('region')
    if not region:
        return HttpResponse('region not present!', status=400)
    elbname = request.GET.get('loadbalancerName')
    if not elbname:
        return HttpResponse('loadbalancer name not present!', status=400)
    instance_ids = request.GET['choosedInstanceIds']
    if not instance_ids.startswith('i-'):
        id_list = []
    else:
        id_list = instance_ids.split(',')
    deal_result = elb_api.deal_instance_for_elb(elbname, region, id_list)
    if not deal_result['ret']:
        return HttpResponse(deal_result['msg'], status=500)
    time.sleep(1)
    elbconn = AwsAccount.get_awssession(region).client('elb')
    lb = elbconn.describe_load_balancers(
        LoadBalancerNames=[elbname])['LoadBalancerDescriptions'][0]
    rowdata = elb_api.to_dict(lb, region, True)
    return HttpResponse(json.dumps(rowdata))
Пример #17
0
def download_conf_template(region, module_name, module_version):
    """
    download conf template file from s3 and save template info to db.
    Args:
        region (string): region name
        module_name (string): module name
        module_version (string): module version
    """
    session = AwsAccount.get_awssession(region)
    s3res = session.resource('s3')
    s3dir = '%s/%s' % (module_name, 'config')
    conf_templates = []
    for config in ['sys', 'log4j']:
        conf_name = '%s-%s-%s' % (module_name, config, module_version)
        config_s3_path = '%s/%s' % (s3dir, conf_name)
        dest_dir = os.path.join(MODULE_DEPLOY_CONF_DIR, module_name, 'conf-template')
        if not os.path.isdir(dest_dir):
            os.makedirs(dest_dir)
        logger.info('download %s from s3, bucket: %s, path: %s' % (
            conf_name,
            DEPLOY_BUCKET_NAME,
            config_s3_path
        ))
        conf_s3_obj = s3res.Object(DEPLOY_BUCKET_NAME, config_s3_path)
        file_path = os.path.join(dest_dir, conf_name)
        with open(file_path, 'wb') as data:
            try:
                conf_s3_obj.download_fileobj(data)
            except:
                error_msg = 'download %s from s3 failed, error msg:\n%s' % (conf_name, traceback.format_exc())
                logger.error(error_msg)
                # os.remove(file_path)
                return conf_templates
        with open(file_path, 'r') as file_reader:
            template_content = file_reader.read()
        conftemplate = ConfTemplate(module_name, config, region,
                                    template_content, module_version)
        conftemplate.save_or_update()
        conf_templates.append(conftemplate.conf_template)
    return conf_templates
Пример #18
0
 def _create_elbs_in_region(region, elb_list=None):
     """
     create an elb stack in specify region and wait it create complete.
     after create success, add instances to each elb.
     Args:
         region (string): region name
         elb_list (list): elb name list, if set None or empty, will create all elbs in elb cfn template.
     """
     try:
         if not elb_list:
             elb_list = LoadBalancerCreater.get_can_create_elbs(region)
         stack_name = LoadBalancerCreater.create_elb_stack(region, elb_list)
         while True:
             stack_status = LoadBalancerCreater.get_statck_status(region, stack_name)
             if stack_status == 'CREATE_COMPLETE':
                 aws_session = AwsAccount.get_awssession(region)
                 elbclient = aws_session.client('elb')
                 loadbalancers = elbclient.describe_load_balancers().get('LoadBalancerDescriptions')
                 if loadbalancers:
                     LoadbalancerInfo.objects.filter(region=RegionInfo.objects.get(region=region)).delete()
                     for lb in loadbalancers:
                         if lb['VPCId'] == DEFAULT_PREPRD_VPC[region][1]:
                             LoadbalancerInfo.save_elb_info(lb, RegionInfo.objects.get(region=region))
                 LoadBalancerCreater._add_instances_to_elbs(region, elb_list)
                 return True
             elif stack_status == 'DELETE_COMPLETE':
                 error_msg = 'create elb stack failed in region: %s' % region
                 logger.error(error_msg)
                 # todo: send mail
                 # mailSender = MailSender()
                 # mailSender.send_mail_when_exception_in_deamon(error_msg)
                 return False
             time.sleep(1)
     except:
         error_msg = 'occur error at progress of creating elbs in region: %s\n' % region
         error_msg += traceback.format_exc()
         logger.error(error_msg)
         # todo: send mail
         # MailSender().send_mail_when_exception_in_deamon(error_msg)
         raise Exception(error_msg)
Пример #19
0
def update_elb_stack(request):
    region = request.GET.get('region')
    if not region:
        return HttpResponse('region not present!', status=400)
    choosed_elbs = request.GET.get('choosedElbs')
    method = request.GET.get('method')
    if not method:
        return HttpResponse('method not present!', status=400)
    elb_cfn_template = ElbCfnTemplate(region)
    stackname = elb_cfn_template.stack_name
    cfnconn = AwsAccount.get_awssession(region).resource('cloudformation')
    stack = cfnconn.Stack(stackname)
    if method == 'delete':
        try:
            stack.delete()
        except Exception, e:
            return HttpResponse(
                json.dumps({
                    'ret': False,
                    'msg': 'delete stack failed: %s' % str(e)
                }))
        return HttpResponse(json.dumps({'ret': True}))
Пример #20
0
def add_auth(region, ami_dict):
    success_list = []
    failed_list = []
    prd_account = _get_prd_account_id(region)
    boto_session = AwsAccount.get_awssession(region)
    ec2resource = boto_session.resource('ec2')
    for module_name in ami_dict:
        image_id = ami_dict.get(module_name)
        image = ec2resource.Image(image_id)
        image_name = image.name
        response = image.modify_attribute(
            LaunchPermission={'Add': [{
                'UserId': prd_account
            }]})
        if response.get('ResponseMetadata').get('HTTPStatusCode') != 200:
            logger.error(
                "error when add %s's launch permission to prd account: %s" %
                (image_name, prd_account))
            failed_list.append(module_name, image_name, image_id)
        else:
            logger.info("ami: %s's creating work done." % image_name)
            success_list.append((module_name, image_name, image_id))
    return success_list, failed_list
Пример #21
0
 def register_instances(region, elb_name):
     logger.info('start to add instances to elb: %s in region: %s' % (elb_name, region))
     try:
         instances = LoadBalancerCreater.get_instances_of_elb(region, elb_name)
         register_instance_ids = []
         for instance in instances:
             instance_id = instance.instance_id
             register_instance_ids.append({'InstanceId': instance_id})
         if register_instance_ids:
             elbconn = AwsAccount.get_awssession(region).client('elb')
             elbconn.register_instances_with_load_balancer(LoadBalancerName=elb_name,
                                                           Instances=register_instance_ids
                                                           )
     except:
         error_msg = 'failed when adding instances to elb: %s in %s\n' % (elb_name, region)
         error_msg += traceback.format_exc()
         logger.error(error_msg)
         return {'ret': False, 'msg': error_msg}
         # todo: send mail
         # MailSender().send_mail_when_exception_in_deamon(errorMsg)
     else:
         logger.info('success adding instances to elb: %s in %s' % (elb_name, region))
         return {'ret': True}
Пример #22
0
def get_loadbalancers(request):
    if 'region' in request.GET:
        region = request.GET.get('region')
        try:
            vpc_id = DEFAULT_PREPRD_VPC[region][1]
        except KeyError:
            return HttpResponse(
                json.dumps({
                    'data': [],
                    'debugMessage': 'region selected not deploy PrePRD'
                }))
        start_time = time.time()
        awssession = AwsAccount.get_awssession(region)
        elbclient = awssession.client('elb')
        loadbalancers = elbclient.describe_load_balancers().get(
            'LoadBalancerDescriptions')
        logger.debug('get all elbs cost %s seconds' %
                     (time.time() - start_time))
        ret = []
        start_time = time.time()
        region_obj = RegionInfo.objects.get(region=region)
        LoadbalancerInfo.objects.filter(region=region_obj).delete()
        logger.debug('delete elb info cost %s seconds' %
                     (time.time() - start_time))
        can_operate = UserPerm(request.user).judge_perm('operate', 'elb')
        start_time = time.time()
        if loadbalancers:
            for lb in loadbalancers:
                if lb['VPCId'] == vpc_id:
                    # todo: reduce to_dict method cost time(three times awsapi call)
                    ret.append(elb_api.to_dict(lb, region, can_operate))
                    LoadbalancerInfo.save_elb_info(lb, region_obj)
        logger.debug('generate elb table info cost %s seconds' %
                     (time.time() - start_time))
        return HttpResponse(json.dumps({'data': ret}))
    else:
        return HttpResponse('region not present!', status=400)
Пример #23
0
def run_instances_batch(request):
    module_name = request.GET.get('module_name')
    region = request.GET.get('region')
    account = request.GET.get('account')
    if not module_name or not region or not account:
        return HttpResponse('bad request!', status=400)
    region_obj = RegionInfo.objects.get(region=region)
    module = region_obj.moduleinfo_set.get(module_name=module_name)
    ec2opset_objs = module.ec2optionset_set.filter(region=region_obj)
    if ec2opset_objs.count() == 0:
        return HttpResponse(json.dumps({'success': False,
                                        'module': module_name,
                                        'info': 'module has no launch parameters'}))
    if ec2opset_objs.count() > 1:
        return HttpResponse(json.dumps({'success': False,
                                        'module': module_name,
                                        'info': 'too many launch parameters found.'}))
    ec2opset_obj = ec2opset_objs[0]
    if ec2opset_obj.image is None:
        return HttpResponse(json.dumps({'success': False,
                                        'module': module_name,
                                        'info': 'image has been registered.'}))
    tags = get_tags_by_module(module, region)
    ec2opset_obj.tags = tags
    session = AwsAccount.get_awssession(region, account)
    ec2res = session.resource('ec2')
    elbclient = session.client('elb')
    try:
        instance_ids = run_instances(ec2res, elbclient, ec2opset_obj, module.instance_count)
    except:
        return HttpResponse(json.dumps({'success': False,
                                        'module': module_name,
                                        'info': 'launch instance faled: \n%s' % traceback.format_exc()}))
    return HttpResponse(json.dumps({'success': True,
                                    'module': module_name,
                                    'info': ', '.join(instance_ids)
                                    }))
Пример #24
0
def run_ec2optionset(request):
    account = request.GET.get('account')
    region = request.GET.get('region')
    opset_name = request.GET.get('ec2optionset')
    num = request.GET.get('num')
    if not account or not region or not opset_name:
        return HttpResponse('bad request!', status=400)
    if not num:
        return HttpResponse('please enter the number of instances to launch.', status=400)
    try:
        region_obj = RegionInfo.objects.get(region=region)
        account_name = 'cn-%s' % account if region == 'cn-north-1' else 'en-%s' % account
        account_obj = AwsAccount.objects.get(name=account_name)
        ec2optionset = Ec2OptionSet.objects.get(name=opset_name, region=region_obj, account=account_obj)
    except (RegionInfo.DoesNotExist, AwsAccount.DoesNotExist, Ec2OptionSet.DoesNotExist) as ne:
        return HttpResponse('model not found in db: %s' % ne.message, status=500)
    session = AwsAccount.get_awssession(region, account)
    ec2_resource = session.resource('ec2')
    elb_client = session.client('elb')
    logger.info('launch instances: %s, %s, %s, %s' % (
        account_name,
        region,
        opset_name,
        num
    ))

    try:
        instance_ids = run_instances(ec2_resource, elb_client, ec2optionset, int(num))
    except:
        error_msg = "launch instances with opset: %s failed: \n%s" % (
            opset_name,
            traceback.format_exc()
        )
        logger.error(error_msg)
        return HttpResponse(error_msg, status=500)
    logger.info('launch instances done: %s' % opset_name)
    return HttpResponse(json.dumps({'ret': True, 'msg': instance_ids}))
Пример #25
0
def get_instances_in_elb(region, elbname):
    elbclient = AwsAccount.get_awssession(region).client('elb')
    instances = elbclient.describe_instance_health(
        LoadBalancerName=elbname)['InstanceStates']
    return [instance['InstanceId'] for instance in instances]
Пример #26
0
def __to_str(region, instance_id):
    ec2conn = AwsAccount.get_awssession(region).resource('ec2')
    instance = ec2conn.Instance(instance_id)
    instance_name = ec2api.get_instance_tag_name(instance)
    return '%s(%s)' % (instance_name, instance_id)
Пример #27
0
    def create_ami_in_region(self, region_name):
        region_obj = RegionInfo.objects.get(region=region_name)
        modules = region_obj.moduleinfo_set.filter(~Q(update_version=u'')
                                                   & ~Q(update_version=None))
        module_version_dict = {}
        for module in modules:
            module_version_dict.update(
                {module.module_name: module.update_version})
        ret_dict = get_update_instances(region_name,
                                        module_version_dict.copy(),
                                        self.username)
        if not ret_dict['ret']:
            self.results.update({'region': {'failed': ret_dict['msg']}})
            return
        logger.info('find instances to create ami in region: %s' % region_name)
        boto_session = AwsAccount.get_awssession(region_name)
        ec2res = boto_session.resource('ec2')
        check_instances = []
        for instance_id in ret_dict['module_id_dict'].values():
            instance = ec2res.Instance(instance_id)
            check_instances.append(instance)
        try:
            ec2_checker = Ec2Checker(check_instances, region_name)
            check_result = ec2_checker.check()
            logger.debug('ec2 config check result:\n%s' %
                         json.dumps(check_result))

            check_report = Report(check_instances, check_result)
            check_result_report = check_report.report()
            result_file_name = self.write_result_file(check_result_report,
                                                      region_name)
            pass_check = check_report.pass_check(check_result)
        except:
            error_msg = 'occur error when checking instances conf in region:%s\n%s ' % (
                region_name, traceback.format_exc())
            logger.error(error_msg)
            self.results.update({'region': {'failed': error_msg}})
            return
        if not pass_check:
            error_msg = 'instance conf check not pass in region %s, please see %s to get check result' % (
                region_name, result_file_name)
            logger.error(error_msg)
            self.results.update({'region': {'failed': error_msg}})
            return
        logger.info('instance check passed in region: %s' % region_name)
        try:
            retcode, ret_msg = delete_logs(self.username, region_name)
        except:
            error_msg = 'delete logs error: %s' % traceback.format_exc()
            logger.error(error_msg)
            self.results.update({'region': {'failed': error_msg}})
            return
        if not retcode:
            logger.error(ret_msg)
            self.results.update({'region': {'failed': ret_msg}})
            return
        logger.info('delete logs done.')
        try:
            avail_ami_list, failed_ami_list = create_business_amis(
                region_name, module_version_dict, ret_dict['module_id_dict'])
        except:
            error_msg = 'occur error when creating ami and waiting it available:\n%s' % traceback.format_exc(
            )
            logger.error('in region: %s, %s' % (region_name, error_msg))
            self.results.update({'region': {'failed': ret_msg}})
            return
        if failed_ami_list:
            error_msg = 'some ami create failed: %s' % failed_ami_list
            logger.error('in region: %s, %s' % (region_name, error_msg))
            self.results.update({'region': {'failed': error_msg}})
            return
        logger.info('ami create success, start to auth to prd')
        ami_dict = {}
        for module_name, _, ami_id in avail_ami_list:
            ami_dict.update({module_name: ami_id})
        try:
            auth_success_list, auth_failed_list = add_auth(
                region_name, ami_dict)
        except:
            error_msg = 'occur error when add ami auth to prd:\n%s' % traceback.format_exc(
            )
            logger.error('in region: %s, %s' % (region_name, error_msg))
            self.results.update({'region': {'failed': error_msg}})
            return
        if auth_failed_list:
            error_msg = 'some ami auth failed: %s' % auth_failed_list
            logger.error('in region: %s, %s' % (region_name, error_msg))
            self.results.update({'region': {'failed': error_msg}})
            return
        logger.info('ami create work done in %s' % region_name)
        self.results.update({'region': {'success': auth_success_list}})
Пример #28
0
 def delete_elb_stack(region):
     stack_name = 'elb-cn-beta' if region == 'cn-north-1' else 'elb-en-beta'
     session = AwsAccount.get_awssession(region)
     cfnconn = session.resource('cloudformation')
     stack = cfnconn.Stack(stack_name)
     stack.delete()