示例#1
0
    def create(self,
               config_body=None,
               version=None,
               build_arg=None,
               dockerfile=None,
               ssh=None,
               cache_from=None):
        '''
            Create and execute CloudFormation template for ECS service
            and related dependencies
        '''
        log_bold("Initiating service creation")
        if config_body is None:
            self.service_configuration.edit_config()
        else:
            self.service_configuration.set_config(config_body)

        self.service_configuration.validate()
        ecr_repo_config = self.service_configuration.get_config().get(
            'ecr_repo')
        ecr = ECR(
            region=get_region_for_environment(self.environment),
            repo_name=ecr_repo_config.get('name'),
            account_id=ecr_repo_config.get('account_id', None),
            assume_role_arn=ecr_repo_config.get('assume_role_arn', None),
            version=version,
            build_args=build_arg,
            dockerfile=dockerfile,
            ssh=ssh,
            cache_from=cache_from,
        )
        ecr.upload_artefacts()

        template_generator = ServiceTemplateGenerator(
            self.service_configuration, self.environment_stack,
            self.env_sample_file, ecr.image_uri)
        service_template_body = template_generator.generate_service()

        try:
            options = prepare_stack_options_for_template(
                service_template_body, self.environment, self.stack_name)
            self.client.create_stack(
                StackName=self.stack_name,
                Parameters=[{
                    'ParameterKey': 'Environment',
                    'ParameterValue': self.environment,
                }],
                OnFailure='DO_NOTHING',
                Capabilities=['CAPABILITY_NAMED_IAM'],
                **options,
            )
            log_bold("Submitted to cloudformation. Checking progress...")
            self._print_progress()
        except ClientError as boto_client_error:
            error_code = boto_client_error.response['Error']['Code']
            if error_code == 'AlreadyExistsException':
                raise UnrecoverableException("Stack " + self.stack_name +
                                             " already exists.")
            else:
                raise boto_client_error
    def _add_mappings(self):

        region = get_region_for_environment(self.env)
        self.template.add_mapping('AWSRegionToAMI',
                                  {region: {
                                      "AMI": self._get_ami_id()
                                  }})
示例#3
0
 def _add_mappings(self):
     # 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')
     ami_id = json.loads(ami_response['Parameter']['Value'])['image_id']
     region = get_region_for_environment(self.env)
     self.template.add_mapping('AWSRegionToAMI', {region: {"AMI": ami_id}})
示例#4
0
 def start_session(self, mfa_code):
     user_id = (self.sts_client.get_caller_identity()['Arn'].split("/")[0]
                ).split(":")[-1]
     if user_id == "user":
         if mfa_code == None:
             mfa_code = prompt("MFA code")
         mfa.do_mfa_login(mfa_code,
                          get_region_for_environment(self.environment))
         target_instance = self._get_target_instance()
     elif user_id == "assumed-role":
         target_instance = self._get_target_instance()
     self._initiate_session(target_instance)
 def fetch_current_desired_count(self):
     desired_counts = {}
     try:
         deployment_ecs_client = EcsClient(
             None, None, get_region_for_environment(self.environment))
         for logical_service_name, service_config in self.service_info.items(
         ):
             deployment = DeployAction(deployment_ecs_client,
                                       self.cluster_name,
                                       service_config["ecs_service_name"])
             desired_counts[
                 logical_service_name] = deployment.service.desired_count
         log("Existing service counts: " + str(desired_counts))
     except Exception:
         pass
     return desired_counts
 def fetch_current_desired_count(self):
     desired_counts = {}
     try:
         deployment_ecs_client = EcsClient(
             None, None, get_region_for_environment(self.environment))
         for logical_service_name, service_config in self.service_info.items(
         ):
             deployment = DeployAction(deployment_ecs_client,
                                       self.cluster_name,
                                       service_config["ecs_service_name"])
             if deployment.service:
                 desired_counts[
                     logical_service_name] = deployment.service.desired_count
         log("Existing service counts: " + str(desired_counts))
     except Exception as e:
         raise UnrecoverableException(
             "Could not find existing services. {}".format(e))
     return desired_counts
示例#7
0
 def start_session(self, mfa_code):
     mfa.do_mfa_login(mfa_code,
                      get_region_for_environment(self.environment))
     target_instance = self._get_target_instance()
     self._initiate_session(target_instance)
示例#8
0
 def region(self):
     return get_region_for_environment(self.environment)