Created
July 14, 2015 13:42
-
-
Save daviddyball/9cc8e44382ed68442573 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
# 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/>. | |
DOCUMENTATION = ''' | |
--- | |
module: ecs_service | |
short_description: Update service on ECS cluster | |
description: | |
- Usage Amazon ECS API to update a service definition. | |
version_added: "" | |
options: | |
cluster: | |
description: | |
- The name of the ECS cluster to operate on | |
required: true | |
default: null | |
aliases: [] | |
service: | |
description: | |
- The name of the ECS service to update | |
required: true | |
default: null | |
aliases: [] | |
region: | |
description: | |
- The AWS region to operate in | |
required: false | |
default: region specified in boto configuration | |
aliases: [] | |
desired_count: | |
description: | |
- The desired number of service instances to run | |
required: true | |
default: null | |
aliases: [] | |
wait: | |
description: | |
- Wait for the call to complete, or exit after call has been submitted | |
default: false | |
aliases: [] | |
''' | |
EXAMPLES = ''' | |
# Update desired count of `service1` on `primary-cluster` to 2 | |
- ecs_service: | |
cluster: primary-cluster | |
service: service1 | |
region: us-east-1 | |
desired_count: 2 | |
# Stop all running instances of the service and wait for completion | |
- ecs_service: | |
cluster: primary-cluster | |
service: service1 | |
desired_count: 0 | |
region: us-east-1 | |
wait: true | |
''' | |
import json | |
HAS_BOTO3 = True | |
try: | |
import boto3 | |
except ImportError: | |
HAS_BOTO3 = False | |
class ECSManager: | |
def __init__(self, module): | |
self.module = module | |
self.cluster = module.params.get('cluster') | |
self.service = module.params.get('service') | |
self.desired_count = module.params.get('desired_count') | |
self.region = module.params.get('region') | |
self.wait = module.params.get('wait') | |
def run(self): | |
ecs = None | |
if self.region: | |
ecs = boto3.client('ecs', region_name=self.region) | |
else: | |
ecs = boto3.client('ecs') | |
service = ecs.describe_services( | |
cluster=self.cluster, | |
services=[self.service] | |
)['services'][0] | |
if self.desired_count == service['runningCount']: | |
self.module.exit_json( | |
msg='desired_count == running_count (%s == %s)' % (service['desiredCount'], service['runningCount']), | |
changed=False | |
) | |
else: | |
changed = True | |
ecs.update_service( | |
cluster=self.cluster, | |
service=self.service, | |
desiredCount=self.desired_count | |
) | |
if self.wait: | |
waiter = ecs.get_waiter('services_stable') | |
waiter.wait(cluster=self.cluster, services=[self.service]) | |
self.module.exit_json( | |
msg="Successfully changed %s instance count on %s to %s" % ( | |
self.service, self.cluster, self.desired_count | |
), | |
changed=True, | |
success=True, | |
output="%s instance count on %s changed from %s to %s" % ( | |
self.service, | |
self.cluster, | |
service['runningCount'], | |
self.desired_count | |
) | |
) | |
# Check and Note any existing image to compare to | |
self.module.exit_json( | |
msg=msg, | |
changed=changed, | |
success=True, | |
output=output | |
) | |
def main(): | |
argument_spec = dict( | |
cluster=dict(type='str'), | |
service=dict(type='str'), | |
desired_count=dict(type='int'), | |
region=dict(default=None, type='str'), | |
wait=dict(default=False, type='bool') | |
) | |
module = AnsibleModule(argument_spec=argument_spec) | |
if not HAS_BOTO3: | |
module.fail_json( | |
msg='The Docker python client is required for this module' | |
) | |
manager = ECSManager(module) | |
manager.run() | |
from ansible.module_utils.basic import * | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More readable?