Exemple #1
0
def test_find_missing_services(cluster, connection):
    missing = utils.find_missing_services(
        ecs=connection.ecs,
        cluster=cluster["cluster"]["clusterName"],
        services={"service-1", "service-2"},
    )
    assert missing == {"service-1", "service-2"}
Exemple #2
0
def test_find_missing_services_existing(cluster, connection, definition):
    retval = connection.ecs.register_task_definition(**definition.as_dict())
    task_definition_arn = retval["taskDefinition"]["taskDefinitionArn"]

    connection.ecs.create_service(
        cluster=cluster["cluster"]["clusterName"],
        serviceName="service-2",
        taskDefinition=task_definition_arn,
    )

    missing = utils.find_missing_services(
        ecs=connection.ecs,
        cluster=cluster["cluster"]["clusterName"],
        services={"service-1", "service-2"},
    )
    assert missing == {"service-1"}
Exemple #3
0
def test_find_missing_services_paginate(cluster, connection, definition):
    retval = connection.ecs.register_task_definition(**definition.as_dict())
    task_definition_arn = retval["taskDefinition"]["taskDefinitionArn"]

    connection.ecs.create_service(
        cluster=cluster["cluster"]["clusterName"],
        serviceName="service-2",
        taskDefinition=task_definition_arn,
    )
    connection.ecs.create_service(
        cluster=cluster["cluster"]["clusterName"],
        serviceName="service-40",
        taskDefinition=task_definition_arn,
    )

    all_services = {"service-%d" % i for i in range(39)}
    missing = utils.find_missing_services(
        ecs=connection.ecs,
        cluster=cluster["cluster"]["clusterName"],
        services=all_services)
    all_services.remove("service-2")
    assert missing == all_services
Exemple #4
0
def start_deployment(
    config: typing.Dict[str, typing.Any],
    connection: Connection,
    task_definitions: typing.Dict[str, TaskDefinition],
    create_missing_services: bool = False,
) -> None:
    """Start the deployment.

    The following steps are executed:

    1. Check if all services defined in the task definitions exist
    2. The task definitions are registered with AWS
    3. The before_deploy tasks are started
    4. The services are updated to reference the last task definitions
    5. The client poll's AWS until all deployments are finished.
    6. The after_deploy tasks are started.

    """
    cluster_name = config["cluster_name"]
    services = config["services"]

    # Before doing anything, lets check if we need to create new services. By
    # default we don't do that anymore (terraform should be used)
    new_services = utils.find_missing_services(connection.ecs,
                                               cluster=cluster_name,
                                               services=set(services.keys()))
    if not create_missing_services and new_services:
        names = ", ".join(new_services)
        raise DeploymentFailed("The following services are missing: %s" %
                               names)

    # Register the task definitions in ECS
    register_task_definitions(connection, task_definitions)

    # Run tasks before deploying services
    tasks_before_deploy = config.get("before_deploy", [])
    run_tasks(connection, cluster_name, task_definitions, tasks_before_deploy)

    # Update services
    for service_name, service in services.items():
        task_definition = task_definitions[service["task_definition"]]
        if service_name in new_services:
            logger.info(
                "Creating new service %s with task definition %s",
                service_name,
                task_definition.name,
            )
            connection.ecs.create_service(
                cluster=cluster_name,
                serviceName=service_name,
                desiredCount=1,
                taskDefinition=task_definition.arn,
            )
        else:
            logger.info(
                "Updating service %s with task definition %s",
                service_name,
                task_definition.name,
            )
            connection.ecs.update_service(
                cluster=cluster_name,
                service=service_name,
                taskDefinition=task_definition.arn,
            )

    is_finished = wait_for_deployments(connection, cluster_name,
                                       services.keys())

    if not is_finished:
        raise DeploymentFailed("Timeout")

    # Run tasks after deploying services
    tasks_after_deploy = config.get("after_deploy", [])
    run_tasks(connection, cluster_name, task_definitions, tasks_after_deploy)

    # Deregister old task definitions
    deregister_task_definitions(connection, task_definitions)