| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: ecs_service_info |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: List or describe services in ECS |
| 12 |
description: |
| 13 |
- Lists or describes services in ECS. |
| 14 |
author: |
| 15 |
- "Mark Chance (@Java1Guy)" |
| 16 |
- "Darek Kaczynski (@kaczynskid)" |
| 17 |
options: |
| 18 |
details: |
| 19 |
description: |
| 20 |
- Set this to true if you want detailed information about the services. |
| 21 |
required: false |
| 22 |
default: false |
| 23 |
type: bool |
| 24 |
events: |
| 25 |
description: |
| 26 |
- Whether to return ECS service events. Only has an effect if I(details=true). |
| 27 |
required: false |
| 28 |
default: true |
| 29 |
type: bool |
| 30 |
cluster: |
| 31 |
description: |
| 32 |
- The cluster ARNS in which to list the services. |
| 33 |
required: false |
| 34 |
type: str |
| 35 |
service: |
| 36 |
description: |
| 37 |
- One or more services to get details for |
| 38 |
required: false |
| 39 |
type: list |
| 40 |
elements: str |
| 41 |
aliases: ['name'] |
| 42 |
extends_documentation_fragment: |
| 43 |
- amazon.aws.common.modules |
| 44 |
- amazon.aws.region.modules |
| 45 |
- amazon.aws.boto3 |
| 46 |
""" |
| 47 |
|
| 48 |
EXAMPLES = r""" |
| 49 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 50 |
|
| 51 |
# Basic listing example |
| 52 |
- community.aws.ecs_service_info: |
| 53 |
cluster: test-cluster |
| 54 |
service: console-test-service |
| 55 |
details: true |
| 56 |
register: output |
| 57 |
|
| 58 |
# Basic listing example |
| 59 |
- community.aws.ecs_service_info: |
| 60 |
cluster: test-cluster |
| 61 |
register: output |
| 62 |
""" |
| 63 |
|
| 64 |
RETURN = r""" |
| 65 |
services: |
| 66 |
description: When details is false, returns an array of service ARNs, otherwise an array of complex objects as described below. |
| 67 |
returned: success |
| 68 |
type: complex |
| 69 |
contains: |
| 70 |
clusterArn: |
| 71 |
description: The Amazon Resource Name (ARN) of the of the cluster that hosts the service. |
| 72 |
returned: always |
| 73 |
type: str |
| 74 |
desiredCount: |
| 75 |
description: The desired number of instantiations of the task definition to keep running on the service. |
| 76 |
returned: always |
| 77 |
type: int |
| 78 |
loadBalancers: |
| 79 |
description: A list of load balancer objects |
| 80 |
returned: always |
| 81 |
type: complex |
| 82 |
contains: |
| 83 |
loadBalancerName: |
| 84 |
description: the name |
| 85 |
returned: always |
| 86 |
type: str |
| 87 |
containerName: |
| 88 |
description: The name of the container to associate with the load balancer. |
| 89 |
returned: always |
| 90 |
type: str |
| 91 |
containerPort: |
| 92 |
description: The port on the container to associate with the load balancer. |
| 93 |
returned: always |
| 94 |
type: int |
| 95 |
pendingCount: |
| 96 |
description: The number of tasks in the cluster that are in the PENDING state. |
| 97 |
returned: always |
| 98 |
type: int |
| 99 |
runningCount: |
| 100 |
description: The number of tasks in the cluster that are in the RUNNING state. |
| 101 |
returned: always |
| 102 |
type: int |
| 103 |
serviceArn: |
| 104 |
description: |
| 105 |
- The Amazon Resource Name (ARN) that identifies the service. The ARN contains the arn:aws:ecs namespace, followed by the region of the |
| 106 |
service, the AWS account ID of the service owner, the service namespace, and then the service name. |
| 107 |
sample: 'arn:aws:ecs:us-east-1:123456789012:service/my-service' |
| 108 |
returned: always |
| 109 |
type: str |
| 110 |
serviceName: |
| 111 |
description: A user-generated string used to identify the service |
| 112 |
returned: always |
| 113 |
type: str |
| 114 |
status: |
| 115 |
description: The valid values are ACTIVE, DRAINING, or INACTIVE. |
| 116 |
returned: always |
| 117 |
type: str |
| 118 |
taskDefinition: |
| 119 |
description: The ARN of a task definition to use for tasks in the service. |
| 120 |
returned: always |
| 121 |
type: str |
| 122 |
deployments: |
| 123 |
description: list of service deployments |
| 124 |
returned: always |
| 125 |
type: list |
| 126 |
elements: dict |
| 127 |
events: |
| 128 |
description: list of service events |
| 129 |
returned: when events is true |
| 130 |
type: list |
| 131 |
elements: dict |
| 132 |
""" |
| 133 |
|
| 134 |
try: |
| 135 |
import botocore |
| 136 |
except ImportError: |
| 137 |
pass # caught by AnsibleAWSModule |
| 138 |
|
| 139 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 140 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 141 |
|
| 142 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 143 |
|
| 144 |
|
| 145 |
class EcsServiceManager: |
| 146 |
"""Handles ECS Services""" |
| 147 |
|
| 148 |
def __init__(self, module): |
| 149 |
self.module = module |
| 150 |
self.ecs = module.client("ecs") |
| 151 |
|
| 152 |
@AWSRetry.jittered_backoff(retries=5, delay=5, backoff=2.0) |
| 153 |
def list_services_with_backoff(self, **kwargs): |
| 154 |
paginator = self.ecs.get_paginator("list_services") |
| 155 |
try: |
| 156 |
return paginator.paginate(**kwargs).build_full_result() |
| 157 |
except is_boto3_error_code("ClusterNotFoundException") as e: |
| 158 |
self.module.fail_json_aws(e, "Could not find cluster to list services") |
| 159 |
|
| 160 |
@AWSRetry.jittered_backoff(retries=5, delay=5, backoff=2.0) |
| 161 |
def describe_services_with_backoff(self, **kwargs): |
| 162 |
return self.ecs.describe_services(**kwargs) |
| 163 |
|
| 164 |
def list_services(self, cluster): |
| 165 |
fn_args = dict() |
| 166 |
if cluster and cluster is not None: |
| 167 |
fn_args["cluster"] = cluster |
| 168 |
try: |
| 169 |
response = self.list_services_with_backoff(**fn_args) |
| 170 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 171 |
self.module.fail_json_aws(e, msg="Couldn't list ECS services") |
| 172 |
relevant_response = dict(services=response["serviceArns"]) |
| 173 |
return relevant_response |
| 174 |
|
| 175 |
def describe_services(self, cluster, services): |
| 176 |
fn_args = dict() |
| 177 |
if cluster and cluster is not None: |
| 178 |
fn_args["cluster"] = cluster |
| 179 |
fn_args["services"] = services |
| 180 |
try: |
| 181 |
response = self.describe_services_with_backoff(**fn_args) |
| 182 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 183 |
self.module.fail_json_aws(e, msg="Couldn't describe ECS services") |
| 184 |
running_services = [self.extract_service_from(service) for service in response.get("services", [])] |
| 185 |
services_not_running = response.get("failures", []) |
| 186 |
return running_services, services_not_running |
| 187 |
|
| 188 |
def extract_service_from(self, service): |
| 189 |
# some fields are datetime which is not JSON serializable |
| 190 |
# make them strings |
| 191 |
if "deployments" in service: |
| 192 |
for d in service["deployments"]: |
| 193 |
if "createdAt" in d: |
| 194 |
d["createdAt"] = str(d["createdAt"]) |
| 195 |
if "updatedAt" in d: |
| 196 |
d["updatedAt"] = str(d["updatedAt"]) |
| 197 |
if "events" in service: |
| 198 |
if not self.module.params["events"]: |
| 199 |
del service["events"] |
| 200 |
else: |
| 201 |
for e in service["events"]: |
| 202 |
if "createdAt" in e: |
| 203 |
e["createdAt"] = str(e["createdAt"]) |
| 204 |
return service |
| 205 |
|
| 206 |
|
| 207 |
def chunks(services, chunk_size): |
| 208 |
"""Yield successive chunk_size-sized chunks from services. |
| 209 |
https://stackoverflow.com/a/312464""" |
| 210 |
for i in range(0, len(services), chunk_size): |
| 211 |
yield services[i:i + chunk_size] # fmt: skip |
| 212 |
|
| 213 |
|
| 214 |
def main(): |
| 215 |
argument_spec = dict( |
| 216 |
details=dict(type="bool", default=False), |
| 217 |
events=dict(type="bool", default=True), |
| 218 |
cluster=dict(), |
| 219 |
service=dict(type="list", elements="str", aliases=["name"]), |
| 220 |
) |
| 221 |
|
| 222 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 223 |
|
| 224 |
show_details = module.params.get("details") |
| 225 |
|
| 226 |
task_mgr = EcsServiceManager(module) |
| 227 |
if show_details: |
| 228 |
if module.params["service"]: |
| 229 |
services = module.params["service"] |
| 230 |
else: |
| 231 |
services = task_mgr.list_services(module.params["cluster"])["services"] |
| 232 |
ecs_info = dict(services=[], services_not_running=[]) |
| 233 |
for chunk in chunks(services, 10): |
| 234 |
running_services, services_not_running = task_mgr.describe_services(module.params["cluster"], chunk) |
| 235 |
ecs_info["services"].extend(running_services) |
| 236 |
ecs_info["services_not_running"].extend(services_not_running) |
| 237 |
else: |
| 238 |
ecs_info = task_mgr.list_services(module.params["cluster"]) |
| 239 |
|
| 240 |
module.exit_json(changed=False, **ecs_info) |
| 241 |
|
| 242 |
|
| 243 |
if __name__ == "__main__": |
| 244 |
main() |