def __init__(self, name, environment): self.name = name self.environment = environment self.stack_name = get_service_stack_name(environment, name) self.client = get_client_for('cloudformation', self.environment) self.s3client = get_client_for('s3', self.environment) self.bucket_name = 'cloudlift-service-template' self.environment_stack = self._get_environment_stack() self.existing_events = get_stack_events(self.client, self.stack_name) self.service_configuration = ServiceConfiguration( self.name, self.environment)
def _get_existing_subnet_info(self, prefix): existing_subnet_ids = {} c = 1 while True: try: subnet_id = list( filter( lambda x: x['OutputKey'] == f"{prefix}{c}", self.environment_stack['Outputs'] ) )[0]['OutputValue'] existing_subnet_ids[f"{prefix}{c}"] = subnet_id c += 1 except: break subnet_client = get_client_for('ec2', self.env) subnet_info = subnet_client.describe_subnets(SubnetIds=list(existing_subnet_ids.values()))['Subnets'] existing_subnet_azs = {} az_balance = {} for az in self.availability_zones: az_balance[az] = 0 for info in subnet_info: existing_subnet_azs[info['SubnetId']] = info['AvailabilityZone'] if info['AvailabilityZone'] in az_balance: az_balance[info['AvailabilityZone']] = az_balance[info['AvailabilityZone']] + 1 else: az_balance[info['AvailabilityZone']] = 1 return existing_subnet_ids, existing_subnet_azs, az_balance
def _get_listener_rule_priority_for_service(self, listener_arn, service_name, information_fetcher): elb_client = get_client_for('elbv2', self.environment) response = elb_client.describe_rules(ListenerArn=listener_arn, ) listener_rules = list(response.get('Rules', [])) while 'NextMarker' in response: response = elb_client.describe_rules( ListenerArn=listener_arn, Marker=response['NextMarker'], ) listener_rules.extend(response.get('Rules', [])) if information_fetcher: service_listener_rule = information_fetcher.get_existing_listener_rule_summary( service_name) if service_listener_rule: matching_priority = next( (rule['Priority'] for rule in listener_rules if rule['RuleArn'] == service_listener_rule['PhysicalResourceId']), None) if matching_priority: return int(matching_priority) return self._get_random_available_listener_rule_priority( listener_rules, listener_arn)
def _fetch_current_image_uri(self): ecs_client = get_client_for('ecs', self.environment) if len(self.service_info) < 1: raise UnrecoverableException( "cannot get running image_uri: no ECS services found") ecs_service_name = next( service_info['ecs_service_name'] for service, service_info in self.service_info.items() if service_info.get('ecs_service_name')) ecs_services = ecs_client.describe_services( cluster=self.cluster_name, services=[ecs_service_name], )['services'] if len(ecs_services) < 1: raise UnrecoverableException( "cannot get running image_uri: no service found.") task_definition_arn = ecs_services[0]['taskDefinition'] task_definition = ecs_client.describe_task_definition( taskDefinition=task_definition_arn) return task_definition['taskDefinition']['containerDefinitions'][0][ 'image']
def _get_ami_id(self): if self.ami_id: return self.ami_id # Pick from https://docs.aws.amazon.com/AmazonECS/latest/developerguide/al2ami.html ssm_client = get_client_for('ssm', self.env) ami_response = ssm_client.get_parameter( Name='/aws/service/ecs/optimized-ami/amazon-linux-2/recommended') return json.loads(ami_response['Parameter']['Value'])['image_id']
def _get_environment_stack(self): try: cfn_client = get_client_for('cloudformation', self.env) environment_stack = cfn_client.describe_stacks( StackName=get_cluster_name(self.env) )['Stacks'][0] except ClientError: return None return environment_stack
def __init__(self, service_name, environment): self.service_name = service_name self.environment = environment self.path_prefix = "/%s/%s/" % (self.environment, self.service_name) # TODO: Use the below two lines when all parameter store actions # require MFA # # mfa_region = get_region_for_environment(environment) # mfa_session = mfa.get_mfa_session(mfa_region) # ssm_client = mfa_session.client('ssm') self.client = get_client_for('ssm', environment)
def init_stack_info(self): try: self.stack_name = get_service_stack_name(self.environment, self.name) stack = get_client_for('cloudformation', self.environment).describe_stacks( StackName=self.stack_name)['Stacks'][0] self.ecs_service_names = [ service_name['OutputValue'] for service_name in list( filter(lambda x: x['OutputKey'].endswith('EcsServiceName'), stack['Outputs'])) ] except ClientError as client_error: err = str(client_error) if "Stack with id %s does not exist" % self.stack_name in err: log_err( "%s cluster not found. Create the environment cluster using `create_environment` command." % self.environment) else: raise UnrecoverableException(str(client_error))