예제 #1
0
 def get_deploy_instances(deploy_module_name, region):
     instances = ec2api.find_instances(region, ['*%s*' % deploy_module_name], is_running=True)
     private_ip_list = []
     key_name = None
     for instance in instances:
         key_name = instance.key_name
         private_ip_list.append(instance.private_ip_address)
     if not key_name:
         raise Exception('no instance found named: %s' % deploy_module_name)
     key_file_path = os.path.join(HOME_PATH, 'pem', '%s.pem' % key_name)
     return private_ip_list, key_file_path
예제 #2
0
def start_service(request):
    region = request.GET.get('region')
    layer_name = request.GET.get('layer_name')
    module_info = request.GET.get('module_info')
    if not module_info or not region or not layer_name:
        return HttpResponse('bad request!', status=400)
    if layer_name in ['topoLayer', 'basicService']:
        tag_pattern = '*-%s-*' % module_info.split('-')[0]
    else:
        tag_pattern = '*-%s-*' % module_info
    instances = ec2api.find_instances(region, [tag_pattern])
    ret = ec2api.start_instances(instances)
    return HttpResponse(json.dumps(ret))
예제 #3
0
 def start_topo_and_basic_instances(self):
     for order, regions in self.region_order_dict.items():
         for region in regions:
             instance_patterns = ['*-%s-*' % topo_service for topo_service in TOPO_MODULES]
             basic_service_list = BasicServiceDeployInfo.get_all_basic_service(region, exclude=['rabbitmq'])
             instance_patterns.extend(['*-%s-*' % basic_service for basic_service in basic_service_list])
             instances = ec2api.find_instances(region, instance_patterns)
             start_failed_instances = ec2api.start_instances(instances)['failed']
             if start_failed_instances:
                 logger.error('start topo service failed in region: %s, failed services: %s' % (
                     region,
                     start_failed_instances
                 ))
                 raise Exception('start topo instances failed: %s,\
                 cancel basic service start process' % start_failed_instances)
예제 #4
0
 def stop_old_module_instances(self):
     for _, regions in self.region_order_dict.items():
         for region_name in regions:
             region_obj = RegionInfo.objects.get(region=region_name)
             modules = region_obj.moduleinfo_set.filter(
                 order=self.round_num - 1)
             instance_patterns = [
                 '*-%s-%s-*' % (module.module_name, module.current_version)
                 for module in modules
             ]
             logger.debug(
                 'in %s, round %s ,old version instances patterns are %s' %
                 (region_name, self.round_num - 1, instance_patterns))
             instances = ec2api.find_instances(region_name,
                                               instance_patterns, True)
             for instance in instances:
                 instance.stop()
예제 #5
0
def scan_instances_and_save_module(region, username):
    region_obj = RegionInfo.objects.get(region=region)
    user_obj = User.objects.get(username=username)
    for layer, modules in biz_modules.items():
        order = layer_order_map[layer]
        for module in modules:
            instances = ec2api.find_instances(region, ['*-%s-*' % module])
            max_version = '1.0.0'
            count = 0
            instance_name = None
            module_name = None
            module_version = None
            for instance in instances:
                instance_name = ec2api.get_instance_tag_name(instance)
                module_name, module_version = ec2api.get_module_info(
                    instance_name)
                if version_cmp(module_version, max_version) == 1:
                    count = 0
                    max_version = module_version
                elif version_cmp(module_version, max_version) == 0:
                    count += 1
            elb_names = ELB_MODULES.get(module_name)
            if count:
                mi = ModuleInfo(module_name=module_name,
                                current_version=max_version,
                                instance_count=count + 1,
                                elb_names=elb_names,
                                user=user_obj,
                                order=-1)
                mi.save()
                mi.regions.add(region_obj)
        for service in module.split('_'):
            if service in STANDARD_MODULES:
                service_type = 'standard'
            elif service in TOMCAT_MODULES:
                service_type = 'tomcat'
            else:
                service_type = 'other'
            biz_module = BizServiceLayer(module=mi,
                                         service_name=service,
                                         layer_name=layer,
                                         start_order=order,
                                         service_type=service_type)
            biz_module.save()
예제 #6
0
def start_services(request):
    region = request.GET.get('region')
    layer = request.GET.get('layer_name')
    module_infos = request.GET.get('module_infos')
    if not region or not layer or not module_infos:
        return HttpResponse('bad request!', status=400)
    module_info_list = module_infos.split(',')
    if layer in ['topoLayer', 'basicService']:
        tag_pattern = [
            '*-%s-*' % module_info.split('-')[0]
            for module_info in module_info_list
        ]
    else:
        tag_pattern = [
            '*-%s-*' % module_info for module_info in module_info_list
        ]
    instances = ec2api.find_instances(region, tag_pattern)
    ret = ec2api.start_instances(instances)
    return HttpResponse(json.dumps(ret))
예제 #7
0
def get_update_instances(region, update_module_info, current_username):
    """
    choose instances to create ami base on update-module list
    generate a hosts file: ../hosts
    Args:
        region: specify region where to scan instances
        update_module_info : dict of updated module's name and version
        current_username: username
    return : dict, if success, {'ret': True, 'dest_instance_info': xxx, 'module_id_dict': xxx}
                   else: {'ret': False, 'msg': 'xxxx'}
    """
    logger.info('----------start----------')
    logger.info('get instances which will create business AMIs...')
    instances = ec2api.find_instances(region, ['*'], is_running=True)
    ret_dict = _get_dest_instance_info(instances, update_module_info)
    if not ret_dict['ret']:
        return ret_dict
    checked_list = []
    check_key_failed_list = []
    dest_instance_info = ret_dict['dest_instance_info']
    try:
        with open('hosts_%s_%s' % (current_username, region), 'w') as fp:
            for instance_id, ip, key, module in dest_instance_info:
                if key not in checked_list:
                    pass_check, key_info = _check_key_info(key)
                    if not pass_check:
                        check_key_failed_list.append(key + '.pem')
                    else:
                        fp.write('%s ansible_ssh_private_key_file=%s\n' %
                                 (ip, key_info))
    except:
        error_msg = 'occur error when write hosts file:\n%s' % traceback.format_exc(
        )
        logger.error(error_msg)
        return {'ret': False, 'msg': error_msg}
    if check_key_failed_list:
        error_msg = 'these private key file check not pass(non exist or chmod failed): %s' % check_key_failed_list
        logger.error(error_msg)
        return {'ret': False, 'msg': error_msg}
    logger.debug('instances will be chose to create AMI are:\n%s' %
                 dest_instance_info)
    return ret_dict
예제 #8
0
 def start_services(modules, region):
     instance_patterns = ['*-%s-*' % pattern for pattern in modules]
     instances = ec2api.find_instances(region, instance_patterns)
     start_results = ec2api.start_instances(instances)
     if start_results['failed']:
         raise Exception('some instance start failed: %s' %
                         start_results['failed'])
     current_time = time.time()
     process_pool = MyPool()
     process_results = []
     while modules:
         module = modules.pop()
         if BizInstanceStarter.ping_services(module, region):
             result = process_pool.apply_async(check_module_state,
                                               (module, region))
             process_results.append((module, result))
         else:
             modules.insert(0, module)
             if time.time() - current_time > MAX_WAIT_SECONDS_EVERY_LAYER:
                 error_msg = "some modules' instances cannot ping in %s seconds: %s" % (
                     MAX_WAIT_SECONDS_EVERY_LAYER, ','.join(modules))
                 logger.error(error_msg)
                 raise Exception(error_msg)
     process_pool.close()
     process_pool.join()
     ret = {'success': [], 'failed': {}}
     for module, process in process_results:
         process_return = process.get()
         if not process.successful():
             error_msg = 'execute check module: %s state failed, details: %s' % (
                 module, process_return)
             ret['failed'].update({module: error_msg})
         else:
             ret['success'] += process_return['success']
             ret['failed'].update(process_return['failed'])
     logger.debug('start services success: %s, failed: %s' %
                  (ret['success'], ret['failed'].keys()))
     return ret