Exemplo n.º 1
0
def create_new_task_definition(color, ecr_image_uri, ecs_service_name,
                               env_name, sample_env_file_path, secrets_name,
                               service_name, client, cluster_name,
                               deployment_identifier, ecs_service_logical_name,
                               service_configuration, region):
    deployment = DeployAction(client, cluster_name, ecs_service_name)
    task_definition = deployment.get_current_task_definition(
        deployment.service)
    essential_container = find_essential_container(
        task_definition[u'containerDefinitions'])
    container_configurations = build_config(env_name, service_name,
                                            ecs_service_logical_name,
                                            sample_env_file_path,
                                            essential_container, secrets_name)
    task_definition.compute_diffs(essential_container, ecr_image_uri)
    print_task_diff(ecs_service_name, task_definition.diff, color)

    builder = TaskDefinitionBuilder(environment=env_name,
                                    service_name=ecs_service_logical_name,
                                    configuration=service_configuration,
                                    region=region,
                                    application_name=service_name)
    updated_task_definition = EcsTaskDefinition(
        builder.build_task_definition(
            container_configurations=container_configurations,
            ecr_image_uri=ecr_image_uri,
            fallback_task_role=task_definition.role_arn,
            fallback_task_execution_role=task_definition.execution_role_arn,
            deployment_identifier=deployment_identifier))
    return deployment.update_task_definition(updated_task_definition)
Exemplo n.º 2
0
def deploy_new_version(client, cluster_name, ecs_service_name,
                       deploy_version_tag, service_name, sample_env_file_path,
                       env_name, color='white', complete_image_uri=None):
    env_config = build_config(env_name, service_name, sample_env_file_path)
    deployment = DeployAction(client, cluster_name, ecs_service_name)
    if deployment.service.desired_count == 0:
        desired_count = 1
    else:
        desired_count = deployment.service.desired_count
    deployment.service.set_desired_count(desired_count)
    task_definition = deployment.get_current_task_definition(
        deployment.service
    )
    if complete_image_uri is not None:
        container_name = task_definition['containerDefinitions'][0]['name']
        task_definition.set_images(
            deploy_version_tag,
            **{container_name: complete_image_uri}
        )
    else:
        task_definition.set_images(deploy_version_tag)
    for container in task_definition.containers:
        task_definition.apply_container_environment(container, env_config)
    print_task_diff(ecs_service_name, task_definition.diff, color)
    new_task_definition = deployment.update_task_definition(task_definition)
    response = deploy_and_wait(deployment, new_task_definition, color)
    if response:
        log_bold(ecs_service_name + " Deployed successfully.")
    else:
        log_err(ecs_service_name + " Deployment failed.")
    return response
Exemplo n.º 3
0
def revert_deployment(client, cluster_name, ecs_service_name, color,
                      timeout_seconds, deployment_identifier, **kwargs):
    deployment = DeployAction(client, cluster_name, ecs_service_name)
    previous_task_defn = deployment.get_task_definition_by_deployment_identifier(
        deployment.service, deployment_identifier)
    deploy_task_definition(client, previous_task_defn, cluster_name,
                           ecs_service_name, color, timeout_seconds, 'Revert')
 def _fetch_current_desired_count(self):
     stack_name = get_service_stack_name(self.env, self.application_name)
     self.desired_counts = {}
     try:
         stack = region_service.get_client_for(
             'cloudformation',
             self.env
         ).describe_stacks(StackName=stack_name)['Stacks'][0]
         ecs_service_outputs = filter(
             lambda x: x['OutputKey'].endswith('EcsServiceName'),
             stack['Outputs']
         )
         ecs_service_names = []
         for service_name in ecs_service_outputs:
             ecs_service_names.append({
                 "key": service_name['OutputKey'],
                 "value": service_name['OutputValue']
             })
         ecs_client = EcsClient(None, None, self.region)
         for service_name in ecs_service_names:
             deployment = DeployAction(
                 ecs_client,
                 self.cluster_name,
                 service_name["value"]
             )
             actual_service_name = service_name["key"]. \
                 replace("EcsServiceName", "")
             self.desired_counts[actual_service_name] = deployment. \
                 service.desired_count
         log("Existing service counts: " + str(self.desired_counts))
     except Exception:
         log_bold("Could not find existing services.")
Exemplo n.º 5
0
def create_new_task_definition(color, ecr_image_uri, ecs_service_name,
                               env_name, sample_env_file_path, secrets_name,
                               service_name, client, cluster_name,
                               deployment_identifier, ecs_service_logical_name,
                               service_configuration, region):
    deployment = DeployAction(client, cluster_name, ecs_service_name)
    task_definition = deployment.get_current_task_definition(
        deployment.service)
    essential_container = find_essential_container(
        task_definition[u'containerDefinitions'])
    container_configurations = build_config(env_name, service_name,
                                            ecs_service_logical_name,
                                            sample_env_file_path,
                                            essential_container, secrets_name)
    task_definition.compute_diffs(essential_container, ecr_image_uri)
    print_task_diff(ecs_service_name, task_definition.diff, color)

    builder = TaskDefinitionBuilder(
        environment=env_name,
        service_name=ecs_service_logical_name,
        configuration=service_configuration,
        region=region,
    )
    updated_task_definition = EcsTaskDefinition(
        builder.build_task_definition(
            container_configurations=container_configurations,
            ecr_image_uri=ecr_image_uri,
            fallback_task_role=task_definition.role_arn,
            fallback_task_execution_role=task_definition.execution_role_arn,
        ))
    diff = DeepDiff(task_definition, updated_task_definition)
    diff.pop('dictionary_item_removed', 'no dictionary_item_removed')

    if diff:
        log_with_color(
            f"{ecs_service_name} task definition diffs: {pformat(diff)}",
            color)
    return deployment.update_task_definition(updated_task_definition,
                                             deployment_identifier)
 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
Exemplo n.º 7
0
def deploy_task_definition(client, task_definition, cluster_name,
                           ecs_service_name, color, timeout_secs, action_name):
    deployment = DeployAction(client, cluster_name, ecs_service_name)
    log_with_color(f"Starting {action_name} for {ecs_service_name}", color)
    if deployment.service.desired_count == 0:
        desired_count = 1
    else:
        desired_count = deployment.service.desired_count
    deployment.service.set_desired_count(desired_count)
    deployment_succeeded = deploy_and_wait(deployment, task_definition, color,
                                           timeout_secs)
    if not deployment_succeeded:
        record_deployment_failure_metric(deployment.cluster_name,
                                         deployment.service_name)
        raise UnrecoverableException(ecs_service_name +
                                     f" {action_name} failed.")
    log_with_color(
        f"{ecs_service_name} {action_name}: Completed successfully.", color)
 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