Exemple #1
0
def run(cluster, task, count, command, env, region, access_key_id, secret_access_key, profile):
    """
    Run a one-off task.

    \b
    CLUSTER is the name of your cluster (e.g. 'my-custer') within ECS.
    TASK is the name of your task definintion (e.g. 'mytask') within ECS.
    COMMAND is the number of tasks your service should run.
    """
    try:
        client = get_client(access_key_id, secret_access_key, region, profile)
        action = RunAction(client, cluster)

        task_definition = action.get_task_definition(task)
        task_definition.set_commands(**{key: value for (key, value) in command})
        task_definition.set_environment(env)
        print_diff(task_definition, 'Using task definition: %s' % task)

        action.run(task_definition, count, 'ECS Deploy')

        click.secho('Successfully started %d instances of task: %s' % (len(action.started_tasks), task_definition.family_revision), fg='green')
        for started_task in action.started_tasks:
            click.secho('- %s' % started_task['taskArn'], fg='green')
        click.secho(' ')

    except Exception as e:
        click.secho('%s\n' % str(e), fg='red', err=True)
        exit(1)
Exemple #2
0
def run(cluster, task, count, command, env, region, access_key_id,
        secret_access_key, profile, diff):
    """
    Run a one-off task.

    \b
    CLUSTER is the name of your cluster (e.g. 'my-custer') within ECS.
    TASK is the name of your task definition (e.g. 'my-task') within ECS.
    COMMAND is the number of tasks your service should run.
    """
    try:
        client = get_client(access_key_id, secret_access_key, region, profile)
        action = RunAction(client, cluster)

        td = action.get_task_definition(task)
        td.set_commands(**{key: value for (key, value) in command})
        td.set_environment(env)

        if diff:
            print_diff(td, 'Using task definition: %s' % task)

        action.run(td, count, 'ECS Deploy')

        click.secho('Successfully started %d instances of task: %s' %
                    (len(action.started_tasks), td.family_revision),
                    fg='green')

        for started_task in action.started_tasks:
            click.secho('- %s' % started_task['taskArn'], fg='green')
        click.secho(' ')

    except Exception as e:
        click.secho('%s\n' % str(e), fg='red', err=True)
        exit(1)
Exemple #3
0
def cron(cluster, task, rule, image, tag, command, env, role, region,
         access_key_id, secret_access_key, newrelic_apikey, newrelic_appid,
         newrelic_region, comment, user, profile, diff, deregister, rollback,
         slack_url, slack_service_match):
    """
    Update a scheduled task.

    \b
    CLUSTER is the name of your cluster (e.g. 'my-custer') within ECS.
    TASK is the name of your task definition (e.g. 'my-task') within ECS.
    RULE is the name of the rule to use the new task definition.
    """
    try:
        client = get_client(access_key_id, secret_access_key, region, profile)
        action = RunAction(client, cluster)

        td = action.get_task_definition(task)
        click.secho('Update task definition based on: %s\n' %
                    td.family_revision)

        td.set_images(tag, **{key: value for (key, value) in image})
        td.set_commands(**{key: value for (key, value) in command})
        td.set_environment(env)
        td.set_role_arn(role)

        slack = SlackNotification(
            getenv('SLACK_URL', slack_url),
            getenv('SLACK_SERVICE_MATCH', slack_service_match))
        slack.notify_start(cluster, tag, td, comment, user, rule=rule)

        if diff:
            print_diff(td)

        new_td = create_task_definition(action, td)

        client.update_rule(cluster=cluster, rule=rule, task_definition=new_td)
        click.secho('Updating scheduled task')
        click.secho('Successfully updated scheduled task %s\n' % rule,
                    fg='green')

        slack.notify_success(cluster, td.revision, rule=rule)

        record_deployment(tag, newrelic_apikey, newrelic_appid,
                          newrelic_region, comment, user)

        if deregister:
            deregister_task_definition(action, td)

    except EcsError as e:
        click.secho('%s\n' % str(e), fg='red', err=True)
        exit(1)
Exemple #4
0
def test_run_action_run(client, task_definition):
    action = RunAction(client, CLUSTER_NAME)
    client.run_task.return_value = dict(tasks=[dict(taskArn='A'), dict(taskArn='B')])
    action.run(task_definition, 2, 'test')

    client.run_task.assert_called_once_with(
        cluster=CLUSTER_NAME,
        task_definition=task_definition.family_revision,
        count=2,
        started_by='test',
        overrides=dict(containerOverrides=task_definition.get_overrides())
    )

    assert len(action.started_tasks) == 2
Exemple #5
0
def test_run_action_run(client, task_definition):
    action = RunAction(client, CLUSTER_NAME)
    client.run_task.return_value = dict(
        tasks=[dict(taskArn='A'), dict(taskArn='B')])
    action.run(task_definition, 2, 'test')

    client.run_task.assert_called_once_with(
        cluster=CLUSTER_NAME,
        task_definition=task_definition.family_revision,
        count=2,
        started_by='test',
        overrides=dict(containerOverrides=task_definition.get_overrides()))

    assert len(action.started_tasks) == 2
Exemple #6
0
def test_run_action_run(client, task_definition):
    action = RunAction(client, CLUSTER_NAME)
    client.run_task.return_value = dict(
        tasks=[dict(taskArn='A'), dict(taskArn='B')])
    action.run(task_definition, 2, 'test', LAUNCH_TYPE_EC2, (), (), False)

    client.run_task.assert_called_once_with(
        cluster=CLUSTER_NAME,
        task_definition=task_definition.family_revision,
        count=2,
        started_by='test',
        overrides=dict(containerOverrides=task_definition.get_overrides()),
        launchtype=LAUNCH_TYPE_EC2,
        subnets=(),
        security_groups=(),
        public_ip=False)

    assert len(action.started_tasks) == 2
Exemple #7
0
def cron(cluster, task, rule, image, tag, command, env, role, tag_container,
         region, access_key_id, secret_access_key, timeout, ignore_warnings,
         newrelic_apikey, newrelic_appid, comment, user, profile, diff,
         deregister, rollback):
    """
    Update a task definition, and update the `rule` to use the new task definition.

    \b
    CLUSTER is the name of your cluster (e.g. 'my-cluster') within ECS.
    TASK is the name of your task definition (e.g. 'my-task') within ECS.
    RULE is the name of the rule to use the new task definition.
    """
    try:
        if tag_container and not tag:
            raise ValueError(
                "--tag must be specified if --tag-container is present")

        client = get_client(access_key_id, secret_access_key, region, profile)
        action = RunAction(client, cluster)

        td = action.get_task_definition(task)
        td.set_images(tag, tag_container,
                      **{key: value
                         for (key, value) in image})
        td.set_commands(**{key: value for (key, value) in command})
        td.set_environment(env)
        td.set_role_arn(role)

        if diff:
            print_diff(td)

        new_td = create_task_definition(action, td)
        target = client.events.list_targets_by_rule(Rule=rule)['Targets'][0]
        client.update_rule(rule=rule,
                           target=target,
                           arn=new_td.arn.partition('task-definition')[0] +
                           'cluster/' + cluster,
                           task_definition_arn=new_td.arn)
        click.secho('Updated scheduled task %s' % new_td.arn)

    except EcsError as e:
        click.secho('%s\n' % str(e), fg='red', err=True)
        exit(1)
Exemple #8
0
def test_run_action(client):
    action = RunAction(client, CLUSTER_NAME)
    assert len(action.started_tasks) == 0
Exemple #9
0
def cron(cluster, task, rule, image, tag, command, cpu, memory,
         memoryreservation, privileged, env, secret, ulimit, system_control,
         port, mount, log, role, execution_role, region, access_key_id,
         secret_access_key, newrelic_apikey, newrelic_appid, newrelic_region,
         comment, user, profile, diff, deregister, rollback, exclusive_env,
         exclusive_secrets, slack_url, slack_service_match, exclusive_ulimits,
         exclusive_system_controls, exclusive_ports, exclusive_mounts, volume):
    """
    Update a scheduled task.

    \b
    CLUSTER is the name of your cluster (e.g. 'my-custer') within ECS.
    TASK is the name of your task definition (e.g. 'my-task') within ECS.
    RULE is the name of the rule to use the new task definition.
    """
    try:
        client = get_client(access_key_id, secret_access_key, region, profile)
        action = RunAction(client, cluster)

        td = action.get_task_definition(task)
        click.secho('Update task definition based on: %s\n' %
                    td.family_revision)

        td.set_images(tag, **{key: value for (key, value) in image})
        td.set_commands(**{key: value for (key, value) in command})
        td.set_cpu(**{key: value for (key, value) in cpu})
        td.set_memory(**{key: value for (key, value) in memory})
        td.set_memoryreservation(
            **{key: value
               for (key, value) in memoryreservation})
        td.set_privileged(**{key: value for (key, value) in privileged})
        td.set_environment(env, exclusive_env)
        td.set_secrets(secret, exclusive_secrets)
        td.set_ulimits(ulimit, exclusive_ulimits)
        td.set_system_controls(system_control, exclusive_system_controls)
        td.set_port_mappings(port, exclusive_ports)
        td.set_mount_points(mount, exclusive_mounts)
        td.set_log_configurations(log)
        td.set_role_arn(role)
        td.set_execution_role_arn(execution_role)
        td.set_volumes(volume)

        slack = SlackNotification(
            getenv('SLACK_URL', slack_url),
            getenv('SLACK_SERVICE_MATCH', slack_service_match))
        slack.notify_start(cluster, tag, td, comment, user, rule=rule)

        if diff:
            print_diff(td)

        new_td = create_task_definition(action, td)

        client.update_rule(cluster=cluster, rule=rule, task_definition=new_td)
        click.secho('Updating scheduled task')
        click.secho('Successfully updated scheduled task %s\n' % rule,
                    fg='green')

        slack.notify_success(cluster, td.revision, rule=rule)

        record_deployment(tag, newrelic_apikey, newrelic_appid,
                          newrelic_region, comment, user)

        if deregister:
            deregister_task_definition(action, td)

    except EcsError as e:
        click.secho('%s\n' % str(e), fg='red', err=True)
        exit(1)