2015-09-09 23:05:43 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2016-12-06 10:35:25 +00:00
|
|
|
|
2015-09-09 23:05:43 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: ecs_service
|
|
|
|
short_description: create, terminate, start or stop a service in ecs
|
|
|
|
description:
|
2015-09-28 16:53:26 +00:00
|
|
|
- Creates or terminates ecs services.
|
2015-09-09 23:05:43 +00:00
|
|
|
notes:
|
2015-09-28 16:53:26 +00:00
|
|
|
- the service role specified must be assumable (i.e. have a trust relationship for the ecs service, ecs.amazonaws.com)
|
2018-06-18 12:22:50 +00:00
|
|
|
- for details of the parameters and returns see U(https://boto3.readthedocs.io/en/latest/reference/services/ecs.html)
|
2017-03-09 16:20:25 +00:00
|
|
|
- An IAM role must have been previously created
|
2016-01-11 21:23:04 +00:00
|
|
|
version_added: "2.1"
|
2016-02-24 10:55:36 +00:00
|
|
|
author:
|
2018-11-19 11:47:14 +00:00
|
|
|
- "Mark Chance (@Java1Guy)"
|
2016-02-24 10:55:36 +00:00
|
|
|
- "Darek Kaczynski (@kaczynskid)"
|
2017-01-05 01:48:40 +00:00
|
|
|
- "Stephane Maarek (@simplesteph)"
|
|
|
|
- "Zac Blazic (@zacblazic)"
|
|
|
|
|
2017-12-14 21:16:59 +00:00
|
|
|
requirements: [ json, botocore, boto3 ]
|
2015-09-09 23:05:43 +00:00
|
|
|
options:
|
2015-09-25 17:59:28 +00:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- The desired state of the service
|
|
|
|
required: true
|
|
|
|
choices: ["present", "absent", "deleting"]
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The name of the service
|
|
|
|
required: true
|
|
|
|
cluster:
|
|
|
|
description:
|
|
|
|
- The name of the cluster in which the service exists
|
|
|
|
required: false
|
|
|
|
task_definition:
|
|
|
|
description:
|
2016-12-30 15:50:09 +00:00
|
|
|
- The task definition the service will run. This parameter is required when state=present
|
2015-09-25 17:59:28 +00:00
|
|
|
required: false
|
|
|
|
load_balancers:
|
|
|
|
description:
|
|
|
|
- The list of ELBs defined for this service
|
|
|
|
required: false
|
|
|
|
desired_count:
|
|
|
|
description:
|
2016-12-30 15:50:09 +00:00
|
|
|
- The count of how many instances of the service. This parameter is required when state=present
|
2015-09-25 17:59:28 +00:00
|
|
|
required: false
|
|
|
|
client_token:
|
|
|
|
description:
|
2015-09-28 16:53:26 +00:00
|
|
|
- Unique, case-sensitive identifier you provide to ensure the idempotency of the request. Up to 32 ASCII characters are allowed.
|
2015-09-25 17:59:28 +00:00
|
|
|
required: false
|
|
|
|
role:
|
|
|
|
description:
|
2017-03-23 01:50:28 +00:00
|
|
|
- The name or full Amazon Resource Name (ARN) of the IAM role that allows your Amazon ECS container agent to make calls to your load balancer
|
2018-04-25 19:41:04 +00:00
|
|
|
on your behalf. This parameter is only required if you are using a load balancer with your service, in a network mode other than `awsvpc`.
|
2015-09-25 17:59:28 +00:00
|
|
|
required: false
|
|
|
|
delay:
|
|
|
|
description:
|
|
|
|
- The time to wait before checking that the service is available
|
|
|
|
required: false
|
|
|
|
default: 10
|
|
|
|
repeat:
|
|
|
|
description:
|
|
|
|
- The number of times to check that the service is available
|
|
|
|
required: false
|
|
|
|
default: 10
|
2018-11-24 01:26:44 +00:00
|
|
|
force_new_deployment:
|
|
|
|
description:
|
|
|
|
- Force deployment of service even if there are no changes
|
|
|
|
required: false
|
|
|
|
version_added: 2.8
|
|
|
|
type: bool
|
2017-01-05 01:48:40 +00:00
|
|
|
deployment_configuration:
|
|
|
|
description:
|
|
|
|
- Optional parameters that control the deployment_configuration; format is '{"maximum_percent":<integer>, "minimum_healthy_percent":<integer>}
|
|
|
|
required: false
|
|
|
|
version_added: 2.3
|
2017-04-27 07:33:23 +00:00
|
|
|
placement_constraints:
|
|
|
|
description:
|
|
|
|
- The placement constraints for the tasks in the service
|
|
|
|
required: false
|
|
|
|
version_added: 2.4
|
|
|
|
placement_strategy:
|
|
|
|
description:
|
|
|
|
- The placement strategy objects to use for tasks in your service. You can specify a maximum of 5 strategy rules per service
|
|
|
|
required: false
|
|
|
|
version_added: 2.4
|
2018-04-25 19:41:04 +00:00
|
|
|
network_configuration:
|
|
|
|
description:
|
|
|
|
- network configuration of the service. Only applicable for task definitions created with C(awsvpc) I(network_mode).
|
2018-06-19 12:46:40 +00:00
|
|
|
- assign_public_ip requires botocore >= 1.8.4
|
2018-06-19 18:35:53 +00:00
|
|
|
suboptions:
|
|
|
|
subnets:
|
|
|
|
description:
|
|
|
|
- A list of subnet IDs to associate with the task
|
|
|
|
version_added: 2.6
|
|
|
|
security_groups:
|
|
|
|
description:
|
|
|
|
- A list of security group names or group IDs to associate with the task
|
|
|
|
version_added: 2.6
|
|
|
|
assign_public_ip:
|
|
|
|
description:
|
|
|
|
- Whether the task's elastic network interface receives a public IP address. This option requires botocore >= 1.8.4.
|
2018-06-21 02:17:01 +00:00
|
|
|
type: bool
|
2018-06-19 18:35:53 +00:00
|
|
|
version_added: 2.7
|
2018-03-02 16:19:41 +00:00
|
|
|
launch_type:
|
|
|
|
description:
|
|
|
|
- The launch type on which to run your service
|
|
|
|
required: false
|
2018-05-24 18:29:20 +00:00
|
|
|
version_added: 2.7
|
2018-03-02 16:19:41 +00:00
|
|
|
choices: ["EC2", "FARGATE"]
|
2018-11-23 12:35:25 +00:00
|
|
|
health_check_grace_period_seconds:
|
|
|
|
description:
|
|
|
|
- Seconds to wait before health checking the freshly added/updated services. This option requires botocore >= 1.8.20.
|
|
|
|
required: false
|
|
|
|
version_added: 2.8
|
2016-01-24 00:11:49 +00:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- aws
|
|
|
|
- ec2
|
2015-09-09 23:05:43 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Note: These examples do not set authentication details, see the AWS Guide for details.
|
2015-09-28 16:53:26 +00:00
|
|
|
- ecs_service:
|
2015-09-25 17:59:28 +00:00
|
|
|
state: present
|
|
|
|
name: console-test-service
|
2015-09-28 16:53:26 +00:00
|
|
|
cluster: new_cluster
|
2017-04-27 07:33:23 +00:00
|
|
|
task_definition: 'new_cluster-task:1'
|
2015-09-25 17:59:28 +00:00
|
|
|
desired_count: 0
|
2015-09-09 23:05:43 +00:00
|
|
|
|
|
|
|
# Basic provisioning example
|
|
|
|
- ecs_service:
|
|
|
|
name: default
|
|
|
|
state: present
|
2015-09-28 16:53:26 +00:00
|
|
|
cluster: new_cluster
|
|
|
|
|
2018-04-25 19:41:04 +00:00
|
|
|
- name: create ECS service on VPC network
|
|
|
|
ecs_service:
|
|
|
|
state: present
|
|
|
|
name: console-test-service
|
|
|
|
cluster: new_cluster
|
|
|
|
task_definition: 'new_cluster-task:1'
|
|
|
|
desired_count: 0
|
|
|
|
network_configuration:
|
|
|
|
subnets:
|
|
|
|
- subnet-abcd1234
|
|
|
|
security_groups:
|
|
|
|
- sg-aaaa1111
|
|
|
|
- my_security_group
|
|
|
|
|
2015-09-09 23:05:43 +00:00
|
|
|
# Simple example to delete
|
2015-09-28 16:53:26 +00:00
|
|
|
- ecs_service:
|
2015-09-09 23:05:43 +00:00
|
|
|
name: default
|
|
|
|
state: absent
|
2015-09-28 16:53:26 +00:00
|
|
|
cluster: new_cluster
|
2017-01-05 01:48:40 +00:00
|
|
|
|
2017-04-27 07:33:23 +00:00
|
|
|
# With custom deployment configuration (added in version 2.3), placement constraints and strategy (added in version 2.4)
|
2017-01-05 01:48:40 +00:00
|
|
|
- ecs_service:
|
2017-04-27 07:33:23 +00:00
|
|
|
state: present
|
2017-01-05 01:48:40 +00:00
|
|
|
name: test-service
|
|
|
|
cluster: test-cluster
|
|
|
|
task_definition: test-task-definition
|
|
|
|
desired_count: 3
|
|
|
|
deployment_configuration:
|
|
|
|
minimum_healthy_percent: 75
|
|
|
|
maximum_percent: 150
|
2017-04-27 07:33:23 +00:00
|
|
|
placement_constraints:
|
|
|
|
- type: memberOf
|
|
|
|
expression: 'attribute:flavor==test'
|
|
|
|
placement_strategy:
|
|
|
|
- type: binpack
|
|
|
|
field: memory
|
2015-09-09 23:05:43 +00:00
|
|
|
'''
|
2016-01-11 21:23:04 +00:00
|
|
|
|
2016-02-24 10:55:36 +00:00
|
|
|
RETURN = '''
|
|
|
|
service:
|
|
|
|
description: Details of created service.
|
|
|
|
returned: when creating a service
|
|
|
|
type: complex
|
|
|
|
contains:
|
|
|
|
clusterArn:
|
|
|
|
description: The Amazon Resource Name (ARN) of the of the cluster that hosts the service.
|
|
|
|
returned: always
|
|
|
|
type: string
|
|
|
|
desiredCount:
|
|
|
|
description: The desired number of instantiations of the task definition to keep running on the service.
|
|
|
|
returned: always
|
|
|
|
type: int
|
|
|
|
loadBalancers:
|
|
|
|
description: A list of load balancer objects
|
|
|
|
returned: always
|
|
|
|
type: complex
|
|
|
|
contains:
|
|
|
|
loadBalancerName:
|
|
|
|
description: the name
|
|
|
|
returned: always
|
|
|
|
type: string
|
|
|
|
containerName:
|
|
|
|
description: The name of the container to associate with the load balancer.
|
|
|
|
returned: always
|
|
|
|
type: string
|
|
|
|
containerPort:
|
|
|
|
description: The port on the container to associate with the load balancer.
|
|
|
|
returned: always
|
|
|
|
type: int
|
|
|
|
pendingCount:
|
|
|
|
description: The number of tasks in the cluster that are in the PENDING state.
|
|
|
|
returned: always
|
|
|
|
type: int
|
|
|
|
runningCount:
|
|
|
|
description: The number of tasks in the cluster that are in the RUNNING state.
|
|
|
|
returned: always
|
|
|
|
type: int
|
|
|
|
serviceArn:
|
2017-03-23 01:50:28 +00:00
|
|
|
description: The Amazon Resource Name (ARN) that identifies the service. The ARN contains the arn:aws:ecs namespace, followed by the region
|
|
|
|
of the service, the AWS account ID of the service owner, the service namespace, and then the service name. For example,
|
|
|
|
arn:aws:ecs:region :012345678910 :service/my-service .
|
2016-02-24 10:55:36 +00:00
|
|
|
returned: always
|
|
|
|
type: string
|
|
|
|
serviceName:
|
|
|
|
description: A user-generated string used to identify the service
|
|
|
|
returned: always
|
|
|
|
type: string
|
|
|
|
status:
|
|
|
|
description: The valid values are ACTIVE, DRAINING, or INACTIVE.
|
|
|
|
returned: always
|
|
|
|
type: string
|
|
|
|
taskDefinition:
|
|
|
|
description: The ARN of a task definition to use for tasks in the service.
|
|
|
|
returned: always
|
|
|
|
type: string
|
|
|
|
deployments:
|
|
|
|
description: list of service deployments
|
|
|
|
returned: always
|
|
|
|
type: list of complex
|
2017-01-05 01:48:40 +00:00
|
|
|
deploymentConfiguration:
|
|
|
|
description: dictionary of deploymentConfiguration
|
|
|
|
returned: always
|
|
|
|
type: complex
|
|
|
|
contains:
|
|
|
|
maximumPercent:
|
|
|
|
description: maximumPercent param
|
|
|
|
returned: always
|
|
|
|
type: int
|
|
|
|
minimumHealthyPercent:
|
|
|
|
description: minimumHealthyPercent param
|
|
|
|
returned: always
|
|
|
|
type: int
|
2016-02-24 10:55:36 +00:00
|
|
|
events:
|
2017-04-27 07:33:23 +00:00
|
|
|
description: list of service events
|
|
|
|
returned: always
|
|
|
|
type: list of complex
|
|
|
|
placementConstraints:
|
|
|
|
description: List of placement constraints objects
|
|
|
|
returned: always
|
|
|
|
type: list of complex
|
|
|
|
contains:
|
|
|
|
type:
|
|
|
|
description: The type of constraint. Valid values are distinctInstance and memberOf.
|
|
|
|
returned: always
|
|
|
|
type: string
|
|
|
|
expression:
|
|
|
|
description: A cluster query language expression to apply to the constraint. Note you cannot specify an expression if the constraint type is
|
|
|
|
distinctInstance.
|
|
|
|
returned: always
|
|
|
|
type: string
|
|
|
|
placementStrategy:
|
|
|
|
description: List of placement strategy objects
|
2016-02-24 10:55:36 +00:00
|
|
|
returned: always
|
|
|
|
type: list of complex
|
2017-04-27 07:33:23 +00:00
|
|
|
contains:
|
|
|
|
type:
|
|
|
|
description: The type of placement strategy. Valid values are random, spread and binpack.
|
|
|
|
returned: always
|
|
|
|
type: string
|
|
|
|
field:
|
|
|
|
description: The field to apply the placement strategy against. For the spread placement strategy, valid values are instanceId
|
|
|
|
(or host, which has the same effect), or any platform or custom attribute that is applied to a container instance,
|
|
|
|
such as attribute:ecs.availability-zone. For the binpack placement strategy, valid values are CPU and MEMORY.
|
|
|
|
returned: always
|
|
|
|
type: string
|
2016-02-24 10:55:36 +00:00
|
|
|
ansible_facts:
|
|
|
|
description: Facts about deleted service.
|
|
|
|
returned: when deleting a service
|
|
|
|
type: complex
|
|
|
|
contains:
|
|
|
|
service:
|
|
|
|
description: Details of deleted service in the same structure described above for service creation.
|
|
|
|
returned: when service existed and was deleted
|
|
|
|
type: complex
|
2015-09-09 23:05:43 +00:00
|
|
|
'''
|
2016-10-23 20:41:03 +00:00
|
|
|
import time
|
|
|
|
|
2017-01-05 01:48:40 +00:00
|
|
|
DEPLOYMENT_CONFIGURATION_TYPE_MAP = {
|
|
|
|
'maximum_percent': 'int',
|
|
|
|
'minimum_healthy_percent': 'int'
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:41:04 +00:00
|
|
|
from ansible.module_utils.aws.core import AnsibleAWSModule
|
|
|
|
from ansible.module_utils.ec2 import ec2_argument_spec
|
|
|
|
from ansible.module_utils.ec2 import snake_dict_to_camel_dict, map_complex_type, get_ec2_security_group_ids_from_names
|
2017-01-05 01:48:40 +00:00
|
|
|
|
2015-09-09 23:05:43 +00:00
|
|
|
try:
|
|
|
|
import botocore
|
|
|
|
except ImportError:
|
2018-04-25 19:41:04 +00:00
|
|
|
pass # handled by AnsibleAWSModule
|
2016-10-23 20:41:03 +00:00
|
|
|
|
|
|
|
|
2015-09-09 23:05:43 +00:00
|
|
|
class EcsServiceManager:
|
|
|
|
"""Handles ECS Services"""
|
|
|
|
|
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
2018-04-25 19:41:04 +00:00
|
|
|
self.ecs = module.client('ecs')
|
|
|
|
self.ec2 = module.client('ec2')
|
2015-09-09 23:05:43 +00:00
|
|
|
|
2018-04-25 19:41:04 +00:00
|
|
|
def format_network_configuration(self, network_config):
|
|
|
|
result = dict()
|
2018-06-19 18:35:53 +00:00
|
|
|
if network_config['subnets'] is not None:
|
2018-04-25 19:41:04 +00:00
|
|
|
result['subnets'] = network_config['subnets']
|
|
|
|
else:
|
|
|
|
self.module.fail_json(msg="Network configuration must include subnets")
|
2018-06-19 18:35:53 +00:00
|
|
|
if network_config['security_groups'] is not None:
|
2018-04-25 19:41:04 +00:00
|
|
|
groups = network_config['security_groups']
|
|
|
|
if any(not sg.startswith('sg-') for sg in groups):
|
|
|
|
try:
|
|
|
|
vpc_id = self.ec2.describe_subnets(SubnetIds=[result['subnets'][0]])['Subnets'][0]['VpcId']
|
|
|
|
groups = get_ec2_security_group_ids_from_names(groups, self.ec2, vpc_id)
|
|
|
|
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
|
|
|
|
self.module.fail_json_aws(e, msg="Couldn't look up security groups")
|
|
|
|
result['securityGroups'] = groups
|
2018-06-19 18:35:53 +00:00
|
|
|
if network_config['assign_public_ip'] is not None:
|
|
|
|
if self.module.botocore_at_least('1.8.4'):
|
2018-06-21 02:17:01 +00:00
|
|
|
if network_config['assign_public_ip'] is True:
|
|
|
|
result['assignPublicIp'] = "ENABLED"
|
|
|
|
else:
|
|
|
|
result['assignPublicIp'] = "DISABLED"
|
2018-06-19 18:35:53 +00:00
|
|
|
else:
|
|
|
|
self.module.fail_json(msg='botocore needs to be version 1.8.4 or higher to use assign_public_ip in network_configuration')
|
2018-04-25 19:41:04 +00:00
|
|
|
return dict(awsvpcConfiguration=result)
|
2015-09-09 23:05:43 +00:00
|
|
|
|
|
|
|
def find_in_array(self, array_of_services, service_name, field_name='serviceArn'):
|
|
|
|
for c in array_of_services:
|
|
|
|
if c[field_name].endswith(service_name):
|
|
|
|
return c
|
|
|
|
return None
|
|
|
|
|
|
|
|
def describe_service(self, cluster_name, service_name):
|
|
|
|
response = self.ecs.describe_services(
|
|
|
|
cluster=cluster_name,
|
2017-04-27 07:33:23 +00:00
|
|
|
services=[service_name])
|
2015-09-09 23:05:43 +00:00
|
|
|
msg = ''
|
2017-12-07 16:27:06 +00:00
|
|
|
if len(response['failures']) > 0:
|
2015-09-09 23:05:43 +00:00
|
|
|
c = self.find_in_array(response['failures'], service_name, 'arn')
|
2017-04-27 07:33:23 +00:00
|
|
|
msg += ", failure reason is " + c['reason']
|
2017-12-07 16:27:06 +00:00
|
|
|
if c and c['reason'] == 'MISSING':
|
2015-09-09 23:05:43 +00:00
|
|
|
return None
|
|
|
|
# fall thru and look through found ones
|
2017-12-07 16:27:06 +00:00
|
|
|
if len(response['services']) > 0:
|
2015-09-09 23:05:43 +00:00
|
|
|
c = self.find_in_array(response['services'], service_name)
|
|
|
|
if c:
|
|
|
|
return c
|
2017-07-24 20:36:54 +00:00
|
|
|
raise Exception("Unknown problem describing service %s." % service_name)
|
2015-09-09 23:05:43 +00:00
|
|
|
|
2016-02-24 10:55:36 +00:00
|
|
|
def is_matching_service(self, expected, existing):
|
|
|
|
if expected['task_definition'] != existing['taskDefinition']:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if (expected['load_balancers'] or []) != existing['loadBalancers']:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if (expected['desired_count'] or 0) != existing['desiredCount']:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2017-04-27 07:33:23 +00:00
|
|
|
def create_service(self, service_name, cluster_name, task_definition, load_balancers,
|
|
|
|
desired_count, client_token, role, deployment_configuration,
|
2018-03-02 16:19:41 +00:00
|
|
|
placement_constraints, placement_strategy, network_configuration,
|
2018-11-23 12:35:25 +00:00
|
|
|
launch_type, health_check_grace_period_seconds):
|
2018-04-25 19:41:04 +00:00
|
|
|
params = dict(
|
2015-09-09 23:05:43 +00:00
|
|
|
cluster=cluster_name,
|
|
|
|
serviceName=service_name,
|
|
|
|
taskDefinition=task_definition,
|
|
|
|
loadBalancers=load_balancers,
|
|
|
|
desiredCount=desired_count,
|
|
|
|
clientToken=client_token,
|
2017-01-05 01:48:40 +00:00
|
|
|
role=role,
|
2017-04-27 07:33:23 +00:00
|
|
|
deploymentConfiguration=deployment_configuration,
|
|
|
|
placementConstraints=placement_constraints,
|
2018-05-24 18:29:20 +00:00
|
|
|
placementStrategy=placement_strategy
|
|
|
|
)
|
2018-04-25 19:41:04 +00:00
|
|
|
if network_configuration:
|
|
|
|
params['networkConfiguration'] = network_configuration
|
2018-05-24 18:29:20 +00:00
|
|
|
if launch_type:
|
|
|
|
params['launchType'] = launch_type
|
2018-11-23 12:35:25 +00:00
|
|
|
if self.health_check_setable(params) and health_check_grace_period_seconds is not None:
|
|
|
|
params['healthCheckGracePeriodSeconds'] = health_check_grace_period_seconds
|
2018-04-25 19:41:04 +00:00
|
|
|
response = self.ecs.create_service(**params)
|
|
|
|
return self.jsonize(response['service'])
|
2016-02-24 10:55:36 +00:00
|
|
|
|
|
|
|
def update_service(self, service_name, cluster_name, task_definition,
|
2018-11-24 01:26:44 +00:00
|
|
|
desired_count, deployment_configuration, network_configuration,
|
|
|
|
health_check_grace_period_seconds, force_new_deployment):
|
2018-04-25 19:41:04 +00:00
|
|
|
params = dict(
|
2016-02-24 10:55:36 +00:00
|
|
|
cluster=cluster_name,
|
|
|
|
service=service_name,
|
|
|
|
taskDefinition=task_definition,
|
2017-01-05 01:48:40 +00:00
|
|
|
desiredCount=desired_count,
|
2018-11-24 01:26:44 +00:00
|
|
|
deploymentConfiguration=deployment_configuration)
|
2018-04-25 19:41:04 +00:00
|
|
|
if network_configuration:
|
|
|
|
params['networkConfiguration'] = network_configuration
|
2018-11-23 12:35:25 +00:00
|
|
|
if self.health_check_setable(params):
|
|
|
|
params['healthCheckGracePeriodSeconds'] = health_check_grace_period_seconds
|
2018-11-24 01:26:44 +00:00
|
|
|
if force_new_deployment:
|
|
|
|
params['forceNewDeployment'] = force_new_deployment
|
2018-04-25 19:41:04 +00:00
|
|
|
response = self.ecs.update_service(**params)
|
|
|
|
return self.jsonize(response['service'])
|
|
|
|
|
|
|
|
def jsonize(self, service):
|
|
|
|
# some fields are datetime which is not JSON serializable
|
|
|
|
# make them strings
|
|
|
|
if 'createdAt' in service:
|
|
|
|
service['createdAt'] = str(service['createdAt'])
|
|
|
|
if 'deployments' in service:
|
|
|
|
for d in service['deployments']:
|
|
|
|
if 'createdAt' in d:
|
|
|
|
d['createdAt'] = str(d['createdAt'])
|
|
|
|
if 'updatedAt' in d:
|
|
|
|
d['updatedAt'] = str(d['updatedAt'])
|
|
|
|
if 'events' in service:
|
|
|
|
for e in service['events']:
|
|
|
|
if 'createdAt' in e:
|
|
|
|
e['createdAt'] = str(e['createdAt'])
|
|
|
|
return service
|
2015-09-09 23:05:43 +00:00
|
|
|
|
|
|
|
def delete_service(self, service, cluster=None):
|
|
|
|
return self.ecs.delete_service(cluster=cluster, service=service)
|
|
|
|
|
2018-04-25 19:41:04 +00:00
|
|
|
def ecs_api_handles_network_configuration(self):
|
|
|
|
# There doesn't seem to be a nice way to inspect botocore to look
|
|
|
|
# for attributes (and networkConfiguration is not an explicit argument
|
|
|
|
# to e.g. ecs.run_task, it's just passed as a keyword argument)
|
2018-11-23 12:35:25 +00:00
|
|
|
return self.module.botocore_at_least('1.7.44')
|
|
|
|
|
|
|
|
def health_check_setable(self, params):
|
|
|
|
load_balancers = params.get('loadBalancers', [])
|
|
|
|
# check if botocore (and thus boto3) is new enough for using the healthCheckGracePeriodSeconds parameter
|
|
|
|
return len(load_balancers) > 0 and self.module.botocore_at_least('1.8.20')
|
2018-04-25 19:41:04 +00:00
|
|
|
|
2015-09-09 23:05:43 +00:00
|
|
|
|
2017-04-27 07:33:23 +00:00
|
|
|
def main():
|
2015-09-09 23:05:43 +00:00
|
|
|
argument_spec = ec2_argument_spec()
|
|
|
|
argument_spec.update(dict(
|
2016-12-30 15:50:09 +00:00
|
|
|
state=dict(required=True, choices=['present', 'absent', 'deleting']),
|
|
|
|
name=dict(required=True, type='str'),
|
|
|
|
cluster=dict(required=False, type='str'),
|
|
|
|
task_definition=dict(required=False, type='str'),
|
|
|
|
load_balancers=dict(required=False, default=[], type='list'),
|
|
|
|
desired_count=dict(required=False, type='int'),
|
|
|
|
client_token=dict(required=False, default='', type='str'),
|
|
|
|
role=dict(required=False, default='', type='str'),
|
2015-09-09 23:05:43 +00:00
|
|
|
delay=dict(required=False, type='int', default=10),
|
2017-01-05 01:48:40 +00:00
|
|
|
repeat=dict(required=False, type='int', default=10),
|
2018-11-24 01:26:44 +00:00
|
|
|
force_new_deployment=dict(required=False, default=False, type='bool'),
|
2017-04-27 07:33:23 +00:00
|
|
|
deployment_configuration=dict(required=False, default={}, type='dict'),
|
|
|
|
placement_constraints=dict(required=False, default=[], type='list'),
|
2018-04-25 19:41:04 +00:00
|
|
|
placement_strategy=dict(required=False, default=[], type='list'),
|
2018-06-19 18:35:53 +00:00
|
|
|
network_configuration=dict(required=False, type='dict', options=dict(
|
|
|
|
subnets=dict(type='list'),
|
|
|
|
security_groups=dict(type='list'),
|
2018-06-21 02:17:01 +00:00
|
|
|
assign_public_ip=dict(type='bool'),
|
2018-06-19 18:35:53 +00:00
|
|
|
)),
|
2018-11-23 12:35:25 +00:00
|
|
|
launch_type=dict(required=False, choices=['EC2', 'FARGATE']),
|
|
|
|
health_check_grace_period_seconds=dict(required=False, type='int')
|
2015-09-09 23:05:43 +00:00
|
|
|
))
|
|
|
|
|
2018-04-25 19:41:04 +00:00
|
|
|
module = AnsibleAWSModule(argument_spec=argument_spec,
|
|
|
|
supports_check_mode=True,
|
2018-05-24 18:29:20 +00:00
|
|
|
required_if=[('state', 'present', ['task_definition', 'desired_count']),
|
|
|
|
('launch_type', 'FARGATE', ['network_configuration'])],
|
2018-04-25 19:41:04 +00:00
|
|
|
required_together=[['load_balancers', 'role']])
|
2015-09-09 23:05:43 +00:00
|
|
|
|
|
|
|
service_mgr = EcsServiceManager(module)
|
2018-04-25 19:41:04 +00:00
|
|
|
if module.params['network_configuration']:
|
|
|
|
if not service_mgr.ecs_api_handles_network_configuration():
|
|
|
|
module.fail_json(msg='botocore needs to be version 1.7.44 or higher to use network configuration')
|
|
|
|
network_configuration = service_mgr.format_network_configuration(module.params['network_configuration'])
|
|
|
|
else:
|
|
|
|
network_configuration = None
|
2017-01-05 01:48:40 +00:00
|
|
|
|
2017-01-08 20:45:06 +00:00
|
|
|
deployment_configuration = map_complex_type(module.params['deployment_configuration'],
|
2017-04-27 07:33:23 +00:00
|
|
|
DEPLOYMENT_CONFIGURATION_TYPE_MAP)
|
2017-01-08 20:45:06 +00:00
|
|
|
|
|
|
|
deploymentConfiguration = snake_dict_to_camel_dict(deployment_configuration)
|
2017-01-05 01:48:40 +00:00
|
|
|
|
2015-09-09 23:05:43 +00:00
|
|
|
try:
|
|
|
|
existing = service_mgr.describe_service(module.params['cluster'], module.params['name'])
|
2016-10-23 20:41:03 +00:00
|
|
|
except Exception as e:
|
2017-04-27 07:33:23 +00:00
|
|
|
module.fail_json(msg="Exception describing service '" + module.params['name'] + "' in cluster '" + module.params['cluster'] + "': " + str(e))
|
2015-09-09 23:05:43 +00:00
|
|
|
|
2016-12-30 15:50:09 +00:00
|
|
|
results = dict(changed=False)
|
2018-05-24 18:29:20 +00:00
|
|
|
|
|
|
|
if module.params['launch_type']:
|
|
|
|
if not module.botocore_at_least('1.8.4'):
|
|
|
|
module.fail_json(msg='botocore needs to be version 1.8.4 or higher to use launch_type')
|
2018-11-24 01:26:44 +00:00
|
|
|
if module.params['force_new_deployment']:
|
|
|
|
if not module.botocore_at_least('1.8.4'):
|
|
|
|
module.fail_json(msg='botocore needs to be version 1.8.4 or higher to use force_new_deployment')
|
2018-05-24 18:29:20 +00:00
|
|
|
|
2015-09-09 23:05:43 +00:00
|
|
|
if module.params['state'] == 'present':
|
2016-02-24 10:55:36 +00:00
|
|
|
|
|
|
|
matching = False
|
|
|
|
update = False
|
2018-05-24 18:29:20 +00:00
|
|
|
|
2017-12-07 16:27:06 +00:00
|
|
|
if existing and 'status' in existing and existing['status'] == "ACTIVE":
|
2018-11-24 01:26:44 +00:00
|
|
|
if module.params['force_new_deployment']:
|
|
|
|
update = True
|
|
|
|
elif service_mgr.is_matching_service(module.params, existing):
|
2016-02-24 10:55:36 +00:00
|
|
|
matching = True
|
2018-01-22 18:37:09 +00:00
|
|
|
results['service'] = existing
|
2016-02-24 10:55:36 +00:00
|
|
|
else:
|
|
|
|
update = True
|
|
|
|
|
|
|
|
if not matching:
|
2015-09-09 23:05:43 +00:00
|
|
|
if not module.check_mode:
|
2017-04-27 07:33:23 +00:00
|
|
|
|
2016-12-30 15:50:09 +00:00
|
|
|
role = module.params['role']
|
|
|
|
clientToken = module.params['client_token']
|
2018-10-17 16:29:23 +00:00
|
|
|
|
|
|
|
loadBalancers = []
|
|
|
|
for loadBalancer in module.params['load_balancers']:
|
|
|
|
if 'containerPort' in loadBalancer:
|
|
|
|
loadBalancer['containerPort'] = int(loadBalancer['containerPort'])
|
|
|
|
loadBalancers.append(loadBalancer)
|
2016-02-24 10:55:36 +00:00
|
|
|
|
2018-10-23 13:38:52 +00:00
|
|
|
for loadBalancer in loadBalancers:
|
|
|
|
if 'containerPort' in loadBalancer:
|
|
|
|
loadBalancer['containerPort'] = int(loadBalancer['containerPort'])
|
|
|
|
|
2016-02-24 10:55:36 +00:00
|
|
|
if update:
|
2018-01-22 18:37:09 +00:00
|
|
|
if (existing['loadBalancers'] or []) != loadBalancers:
|
|
|
|
module.fail_json(msg="It is not possible to update the load balancers of an existing service")
|
2016-02-24 10:55:36 +00:00
|
|
|
# update required
|
|
|
|
response = service_mgr.update_service(module.params['name'],
|
2017-12-07 16:27:06 +00:00
|
|
|
module.params['cluster'],
|
|
|
|
module.params['task_definition'],
|
|
|
|
module.params['desired_count'],
|
2018-04-25 19:41:04 +00:00
|
|
|
deploymentConfiguration,
|
2018-11-23 12:35:25 +00:00
|
|
|
network_configuration,
|
2018-11-24 01:26:44 +00:00
|
|
|
module.params['health_check_grace_period_seconds'],
|
|
|
|
module.params['force_new_deployment'])
|
2016-02-24 10:55:36 +00:00
|
|
|
else:
|
2018-05-24 18:29:20 +00:00
|
|
|
try:
|
|
|
|
response = service_mgr.create_service(module.params['name'],
|
|
|
|
module.params['cluster'],
|
|
|
|
module.params['task_definition'],
|
|
|
|
loadBalancers,
|
|
|
|
module.params['desired_count'],
|
|
|
|
clientToken,
|
|
|
|
role,
|
|
|
|
deploymentConfiguration,
|
|
|
|
module.params['placement_constraints'],
|
|
|
|
module.params['placement_strategy'],
|
|
|
|
network_configuration,
|
2018-11-23 12:35:25 +00:00
|
|
|
module.params['launch_type'],
|
|
|
|
module.params['health_check_grace_period_seconds']
|
|
|
|
)
|
2018-05-24 18:29:20 +00:00
|
|
|
except botocore.exceptions.ClientError as e:
|
2018-06-06 09:48:00 +00:00
|
|
|
module.fail_json_aws(e, msg="Couldn't create service")
|
2016-02-24 10:55:36 +00:00
|
|
|
|
2015-09-09 23:05:43 +00:00
|
|
|
results['service'] = response
|
|
|
|
|
|
|
|
results['changed'] = True
|
|
|
|
|
|
|
|
elif module.params['state'] == 'absent':
|
|
|
|
if not existing:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# it exists, so we should delete it and mark changed.
|
|
|
|
# return info about the cluster deleted
|
|
|
|
del existing['deployments']
|
|
|
|
del existing['events']
|
|
|
|
results['ansible_facts'] = existing
|
2017-12-07 16:27:06 +00:00
|
|
|
if 'status' in existing and existing['status'] == "INACTIVE":
|
2015-09-09 23:05:43 +00:00
|
|
|
results['changed'] = False
|
|
|
|
else:
|
|
|
|
if not module.check_mode:
|
|
|
|
try:
|
|
|
|
service_mgr.delete_service(
|
|
|
|
module.params['name'],
|
|
|
|
module.params['cluster']
|
|
|
|
)
|
2016-10-23 20:41:03 +00:00
|
|
|
except botocore.exceptions.ClientError as e:
|
2018-06-06 09:48:00 +00:00
|
|
|
module.fail_json_aws(e, msg="Couldn't delete service")
|
2015-09-09 23:05:43 +00:00
|
|
|
results['changed'] = True
|
|
|
|
|
|
|
|
elif module.params['state'] == 'deleting':
|
|
|
|
if not existing:
|
2017-04-27 07:33:23 +00:00
|
|
|
module.fail_json(msg="Service '" + module.params['name'] + " not found.")
|
2015-09-09 23:05:43 +00:00
|
|
|
return
|
|
|
|
# it exists, so we should delete it and mark changed.
|
|
|
|
# return info about the cluster deleted
|
|
|
|
delay = module.params['delay']
|
|
|
|
repeat = module.params['repeat']
|
|
|
|
time.sleep(delay)
|
|
|
|
for i in range(repeat):
|
|
|
|
existing = service_mgr.describe_service(module.params['cluster'], module.params['name'])
|
|
|
|
status = existing['status']
|
|
|
|
if status == "INACTIVE":
|
|
|
|
results['changed'] = True
|
|
|
|
break
|
|
|
|
time.sleep(delay)
|
2017-04-27 07:33:23 +00:00
|
|
|
if i is repeat - 1:
|
|
|
|
module.fail_json(msg="Service still not deleted after " + str(repeat) + " tries of " + str(delay) + " seconds each.")
|
2015-09-09 23:05:43 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
module.exit_json(**results)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|