def _get_a_list(self, d, list_name, names=None, max=-1, startToken=None): if names == None: sorted_keys = sorted(d.keys()) else: for n in names: if n not in d: empty_list = AWSListType(list_name) return (empty_list, None) sorted_keys = sorted(names) next_name = None if max is None or max < 0: max = len(sorted_keys) elif max != len(sorted_keys): next_name = d[sorted_keys[max]].LaunchConfigurationName activated = False if startToken is None: activated = True lc_list_type = AWSListType(list_name) for i in range(0, max): k = sorted_keys[i] if startToken and d[k] == startToken: activated = True if not activated: continue lc_list_type.add_item(d[k]) if lc_list_type.get_length() == max: return (lc_list_type, next_name) return (lc_list_type, next_name)
def get_autoscale_instances(self, user_obj, instance_id_list=None, max=-1, startToken=None): lc_list_type = AWSListType("AutoScalingInstances") instance_list = self._find_all_instances(user_obj, instance_id_list) for inst in instance_list: out_t = convert_instance_type(inst['domain_name'], inst) lc_list_type.add_item(out_t) return (lc_list_type, None)
def get_autoscale_groups(self, user_obj, names=None, max=-1, startToken=None): epu_list = self._epum_client.list_domains(caller=user_obj.access_id) log(logging.DEBUG, "Incoming epu list is %s" % (str(epu_list))) next_token = None epu_list.sort() asg_list_type = AWSListType('AutoScalingGroups') for asg_name in epu_list: if asg_list_type.get_length() >= max and max > -1: break if asg_name == startToken: startToken = None if startToken is None and (names is None or asg_name in names): asg_description = self._epum_client.describe_domain( asg_name, caller=user_obj.access_id) asg = convert_epu_description_to_asg_out( asg_description, asg_name) asg_list_type.add_item(asg) # XXX need to set next_token return (asg_list_type, next_token)
def get_launch_configs(self, user_obj, names=None, max=-1, startToken=None): next_token = None dts = self._dtrs_client.list_dts(user_obj.access_id) dts.sort() # now that we have the final list, look up each description lc_list_type = AWSListType('LaunchConfigurations') for lc_name in dts: if lc_list_type.get_length() >= max and max > -1: break dt_descr = self._get_dt_details(lc_name, user_obj.access_id) for site in sorted(dt_descr['mappings'].keys()): mapped_def = dt_descr['mappings'][site] out_name = '%s@%s' % (lc_name, site) if lc_list_type.get_length() >= max and max > -1: break if out_name == startToken: startToken = None if startToken is None and (names is None or out_name in names): ot_lc = LaunchConfigurationType('LaunchConfiguration') ot_lc.BlockDeviceMappings = AWSListType('BlockDeviceMappings') if 'CreatedTime' not in mapped_def.keys(): # This is an LC that was created with the new Phantom API, ignore it continue tm = _get_time(mapped_def['CreatedTime']) ot_lc.CreatedTime = DateTimeType('CreatedTime', tm) ot_lc.ImageId = mapped_def['iaas_image'] ot_lc.InstanceMonitoring = InstanceMonitoringType('InstanceMonitoring') ot_lc.InstanceMonitoring.Enabled = False ot_lc.InstanceType = mapped_def['iaas_allocation'] ot_lc.KernelId = None ot_lc.KeyName = phantom_get_default_key_name() ot_lc.LaunchConfigurationARN = mapped_def['LaunchConfigurationARN'] ot_lc.LaunchConfigurationName = out_name ot_lc.RamdiskId = None ot_lc.SecurityGroups = AWSListType('SecurityGroups') contextualization = dt_descr.get('contextualization') if contextualization is not None and contextualization.get('method') == 'userdata': # UserData should be base64-encoded to be properly decoded by boto ot_lc.UserData = base64.b64encode(contextualization.get('userdata')) else: ot_lc.UserData = None lc_list_type.add_item(ot_lc) # XXX need to set next_token return (lc_list_type, next_token)
def convert_epu_description_to_asg_out(desc, asg): inst_list = desc['instances'] name = desc['name'] config = desc['config'] log(logging.DEBUG, "Changing the config: %s" %(str(config))) #asg.DesiredCapacity = int(config['engine_conf']['preserve_n']) asg.Instances = AWSListType('Instances') for inst in inst_list: log(logging.DEBUG, "Converting instance %s" %(str(inst))) out_t = InstanceType('Instance') out_t.AutoScalingGroupName = name out_t.HealthStatus = _is_healthy(inst['state']) if 'state_desc' in inst and inst['state_desc'] is not None: out_t.HealthStatus = out_t.HealthStatus + " " + str(inst['state_desc']) out_t.LifecycleState = inst['state'] out_t.AvailabilityZone = inst['site'] out_t.LaunchConfigurationName = asg.LaunchConfigurationName if 'iaas_id' in inst: out_t.InstanceId = inst['iaas_id'] else: out_t.InstanceId = "" asg.Instances.type_list.append(out_t) return asg
def get_autoscale_groups(self, user_obj, names=None, max=-1, startToken=None): epu_list = self._epum_client.list_domains(caller=user_obj.access_id) log(logging.DEBUG, "Incoming epu list is %s" %(str(epu_list))) next_token = None epu_list.sort() asg_list_type = AWSListType('AutoScalingGroups') for asg_name in epu_list: if asg_list_type.get_length() >= max and max > -1: break if asg_name == startToken: startToken = None if startToken is None and (names is None or asg_name in names): asg_description = self._epum_client.describe_domain(asg_name, caller=user_obj.access_id) asg = convert_epu_description_to_asg_out(asg_description, asg_name) asg_list_type.add_item(asg) # XXX need to set next_token return (asg_list_type, next_token)
def _set_instances(self, asg): asg.Instances = AWSListType('Instances') for i in range(asg.DesiredCapacity): out_t = InstanceType('Instance') out_t.AutoScalingGroupName = asg.AutoScalingGroupName out_t.HealthStatus = 'Healthy' out_t.LifecycleState = '400-Running' out_t.AvailabilityZone = 'alamo' out_t.LaunchConfigurationName = 'stuff' out_t.InstanceId = 'i-' + str(uuid.uuid4()).split('-')[0] asg.Instances.type_list.append(out_t)
def get_launch_configs(self, user_obj, names=None, max=-1, startToken=None): next_token = None use_max = max if use_max > -1: use_max = max + 1 db_lco = self._db.get_lcs(user_obj, names, use_max, startToken, log=self._log) if max > -1 and db_lco: nxt = db_lco.pop(-1) next_token = nxt.LaunchConfigurationName # if we ever expect people to have more than a few launch configs we should change this to itertools lc_list_type = AWSListType('LaunchConfigurations') for lcdb in db_lco: lc = db_launch_config_to_outtype(lcdb) lc_list_type.type_list.append(lc) return (lc_list_type, next_token)
def get_autoscale_groups(self, user_obj, names=None, max=-1, startToken=None): next_token = None use_max = max if use_max > -1: use_max = max + 1 db_asgs = self._db.get_asgs(user_obj, names, use_max, startToken, log=self._log) if max > -1 and db_asg: nxt = db_asg.pop(-1) next_token = nxt.AutoScalingGroupName asg_list_type = AWSListType('AutoScalingGroups') for asgdb in db_asgs: a = db_asg_to_outtype(asgdb) asg_list_type.type_list.append(a) self._set_instances(a) return (asg_list_type, next_token)
def convert_epu_description_to_asg_out(desc, name): log(logging.DEBUG, "conversion description: %s" % (str(desc))) config = desc['config']['engine_conf'] asg = AutoScalingGroupType('AutoScalingGroup') asg.AutoScalingGroupName = desc['name'] asg.DesiredCapacity = config['minimum_vms'] tm = _get_key_or_none(config, 'CreatedTime') if tm: tm = _get_time(config['CreatedTime']) asg.CreatedTime = DateTimeType('CreatedTime', tm) asg.AutoScalingGroupARN = _get_key_or_none(config, 'AutoScalingGroupARN') asg.AvailabilityZones = AWSListType('AvailabilityZones') dt_name = config.get('dtname') if dt_name is None: dt_name = config.get('deployable_type') asg.HealthCheckType = _get_key_or_none(config, 'HealthCheckType') asg.LaunchConfigurationName = "%s" % (dt_name) asg.MaxSize = config.get('maximum_vms') if asg.MaxSize is None: asg.MaxSize = config.get('maximum_vms') asg.MinSize = config.get('minimum_vms') if asg.MinSize is None: asg.MinSize = config.get('minimum_vms') asg.PlacementGroup = _get_key_or_none(config, 'PlacementGroup') #asg.Status asg.VPCZoneIdentifier = _get_key_or_none(config, 'VPCZoneIdentifier') asg.EnabledMetrics = AWSListType('EnabledMetrics') asg.HealthCheckGracePeriod = 0 asg.LoadBalancerNames = AWSListType('LoadBalancerNames') asg.SuspendedProcesses = AWSListType('SuspendedProcesses') asg.Tags = AWSListType('Tags') asg.Cooldown = 0 inst_list = desc['instances'] asg.Instances = AWSListType('Instances') for inst in inst_list: out_t = convert_instance_type(name, inst) asg.Instances.type_list.append(out_t) return asg
def db_asg_to_outtype(asg_db): asg = AutoScalingGroupType('AutoScalingGroup') asg.AutoScalingGroupName = asg_db.AutoScalingGroupName asg.AutoScalingGroupARN = asg_db.AutoScalingGroupARN asg.LaunchConfigurationName = asg_db.LaunchConfigurationName asg.VPCZoneIdentifier = asg_db.VPCZoneIdentifier asg.HealthCheckType = asg_db.HealthCheckType asg.PlacementGroup = asg_db.PlacementGroup asg.Status = asg_db.Status asg.DesiredCapacity = asg_db.DesiredCapacity asg.MaxSize = asg_db.MaxSize asg.MinSize = asg_db.MinSize asg.Cooldown = asg_db.DefaultCooldown asg.HealthCheckGracePeriod = asg_db.HealthCheckGracePeriod asg.AvailabilityZones = AWSListType('AvailabilityZones') asg.AvailabilityZones.type_list.append(asg_db.AvailabilityZones) asg.CreatedTime = DateTimeType('CreatedTime', asg_db.CreatedTime) return asg
def get_launch_configs(self, user_obj, names=None, max=-1, startToken=None): next_token = None dts = self._dtrs_client.list_dts(user_obj.access_id) dts.sort() # now that we have the final list, look up each description lc_list_type = AWSListType('LaunchConfigurations') for lc_name in dts: if lc_list_type.get_length() >= max and max > -1: break dt_descr = self._get_dt_details(lc_name, user_obj.access_id) for site in sorted(dt_descr['mappings'].keys()): mapped_def = dt_descr['mappings'][site] out_name = '%s@%s' % (lc_name, site) if lc_list_type.get_length() >= max and max > -1: break if out_name == startToken: startToken = None if startToken is None and (names is None or out_name in names): ot_lc = LaunchConfigurationType('LaunchConfiguration') ot_lc.BlockDeviceMappings = AWSListType( 'BlockDeviceMappings') if 'CreatedTime' not in mapped_def.keys(): # This is an LC that was created with the new Phantom API, ignore it continue tm = _get_time(mapped_def['CreatedTime']) ot_lc.CreatedTime = DateTimeType('CreatedTime', tm) ot_lc.ImageId = mapped_def['iaas_image'] ot_lc.InstanceMonitoring = InstanceMonitoringType( 'InstanceMonitoring') ot_lc.InstanceMonitoring.Enabled = False ot_lc.InstanceType = mapped_def['iaas_allocation'] ot_lc.KernelId = None ot_lc.KeyName = phantom_get_default_key_name() ot_lc.LaunchConfigurationARN = mapped_def[ 'LaunchConfigurationARN'] ot_lc.LaunchConfigurationName = out_name ot_lc.RamdiskId = None ot_lc.SecurityGroups = AWSListType('SecurityGroups') contextualization = dt_descr.get('contextualization') if contextualization is not None and contextualization.get( 'method') == 'userdata': # UserData should be base64-encoded to be properly decoded by boto ot_lc.UserData = base64.b64encode( contextualization.get('userdata')) else: ot_lc.UserData = None lc_list_type.add_item(ot_lc) # XXX need to set next_token return (lc_list_type, next_token)