def test_notify_success(post_mock, url, service_match, task_definition): post_mock.return_value = NotifyResponseSuccessfulMock() slack = SlackNotification(url, service_match) slack.notify_failure('my-cluster', 'my-error', 'my-service', 'my-rule') payload = { 'username': '******', 'attachments': [{ 'pretext': 'Deployment failed', 'color': 'danger', 'fields': [ { 'title': 'Cluster', 'value': 'my-cluster', 'short': True }, { 'title': 'Service', 'value': 'my-service', 'short': True }, { 'title': 'Scheduled Task', 'value': 'my-rule', 'short': True }, { 'title': 'Duration', 'value': '0:00:00', 'short': True }, { 'title': 'Error', 'value': 'my-error', 'short': True }, ] }] } post_mock.assert_called_with(url, json=payload)
def deploy(cluster, service, tag, image, command, env, secret, role, execution_role, task, region, access_key_id, secret_access_key, profile, timeout, newrelic_apikey, newrelic_appid, newrelic_region, comment, user, ignore_warnings, diff, deregister, rollback, exclusive_env, exclusive_secrets, sleep_time, slack_url, slack_service_match='.*'): """ Redeploy or modify a service. \b CLUSTER is the name of your cluster (e.g. 'my-custer') within ECS. SERVICE is the name of your service (e.g. 'my-app') within ECS. When not giving any other options, the task definition will not be changed. It will just be duplicated, so that all container images will be pulled and redeployed. """ try: client = get_client(access_key_id, secret_access_key, region, profile) deployment = DeployAction(client, cluster, service) td = get_task_definition(deployment, task) 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, exclusive_env) td.set_secrets(secret, exclusive_secrets) td.set_role_arn(role) td.set_execution_role_arn(execution_role) slack = SlackNotification( getenv('SLACK_URL', slack_url), getenv('SLACK_SERVICE_MATCH', slack_service_match)) slack.notify_start(cluster, tag, td, comment, user, service=service) click.secho('Deploying based on task definition: %s\n' % td.family_revision) if diff: print_diff(td) new_td = create_task_definition(deployment, td) try: deploy_task_definition(deployment=deployment, task_definition=new_td, title='Deploying new task definition', success_message='Deployment successful', failure_message='Deployment failed', timeout=timeout, deregister=deregister, previous_task_definition=td, ignore_warnings=ignore_warnings, sleep_time=sleep_time) except TaskPlacementError as e: slack.notify_failure(cluster, str(e), service=service) if rollback: click.secho('%s\n' % str(e), fg='red', err=True) rollback_task_definition(deployment, td, new_td, sleep_time=sleep_time) exit(1) else: raise record_deployment(tag, newrelic_apikey, newrelic_appid, newrelic_region, comment, user) slack.notify_success(cluster, td.revision, service=service) except (EcsError, NewRelicException) as e: click.secho('%s\n' % str(e), fg='red', err=True) exit(1)
def test_notify_failure_failed(post, url, service_match, task_definition): with raises(SlackException): post.return_value = NotifyResponseUnsuccessfulMock() slack = SlackNotification(url, service_match) slack.notify_failure('my-cluster', 'my-error', 'my-service', 'my-rule')
def test_notify_failure_without_url(post_mock, url, service_match, task_definition): slack = SlackNotification(None, None) slack.notify_failure('my-cluster', 'my-error', 'my-service', 'my-rule') post_mock.assert_not_called()
def deploy(cluster, service, tag, image, command, health_check, cpu, memory, memoryreservation, privileged, essential, env, secret, ulimit, system_control, port, mount, log, role, execution_role, task, region, access_key_id, secret_access_key, profile, timeout, newrelic_apikey, newrelic_appid, newrelic_region, comment, user, ignore_warnings, diff, deregister, rollback, exclusive_env, exclusive_secrets, sleep_time, exclusive_ulimits, exclusive_system_controls, exclusive_ports, exclusive_mounts, volume, add_container, remove_container, slack_url, slack_service_match='.*'): """ Redeploy or modify a service. \b CLUSTER is the name of your cluster (e.g. 'my-custer') within ECS. SERVICE is the name of your service (e.g. 'my-app') within ECS. When not giving any other options, the task definition will not be changed. It will just be duplicated, so that all container images will be pulled and redeployed. """ try: client = get_client(access_key_id, secret_access_key, region, profile) deployment = DeployAction(client, cluster, service) td = get_task_definition(deployment, task) # If there is a new container, add it at frist. td.add_containers(add_container) td.remove_containers(remove_container) td.set_images(tag, **{key: value for (key, value) in image}) td.set_commands(**{key: value for (key, value) in command}) td.set_health_checks(**{key: vals for (key, *vals) in health_check}) 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_essential(**{key: value for (key, value) in essential}) 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, service=service) click.secho('Deploying based on task definition: %s\n' % td.family_revision) if diff: print_diff(td) new_td = create_task_definition(deployment, td) try: deploy_task_definition(deployment=deployment, task_definition=new_td, title='Deploying new task definition', success_message='Deployment successful', failure_message='Deployment failed', timeout=timeout, deregister=deregister, previous_task_definition=td, ignore_warnings=ignore_warnings, sleep_time=sleep_time) except TaskPlacementError as e: slack.notify_failure(cluster, str(e), service=service) if rollback: click.secho('%s\n' % str(e), fg='red', err=True) rollback_task_definition(deployment, td, new_td, sleep_time=sleep_time) exit(1) else: raise record_deployment(tag, newrelic_apikey, newrelic_appid, newrelic_region, comment, user) slack.notify_success(cluster, td.revision, service=service) except (EcsError, NewRelicException) as e: click.secho('%s\n' % str(e), fg='red', err=True) exit(1)