Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import boto3
- ecs_cli = boto3.client('ecs')
- ec2_cli = boto3.client('ec2')
- class Terminate():
- MIN_TASKS_RUNNING = 2
- def __init__(self, cluster_name):
- self.cluster_name = cluster_name
- def put_in_draining(self, container_instance_id):
- result = ecs_cli.describe_container_instances(
- cluster=self.cluster_name, containerInstances=[container_instance_id]
- )
- for container_instance in result['containerInstances']:
- ec2_instance_id = container_instance['ec2InstanceId']
- check = self.check_and_get_tag_terminate(ec2_instance_id, tag_key='Terminate')
- if 'yes' in check:
- return ('The instance id is already schedule to terminate, just pass')
- else:
- result = ecs_cli.update_container_instances_state(
- cluster=self.cluster_name, containerInstances=[container_instance_id], status='DRAINING'
- )
- if result['failures']:
- return ('Error: ' + str(result['failures'][0]))
- else:
- # increase_spot_fleet
- return (
- 'The ContainerInstanceId %s or EC2 - %s * is moving to Draining' %
- (container_instance_id, ec2_instance_id)
- )
- def put_tag_terminate(self, ec2_instance_id):
- # tag name terminate with true value
- result = ec2_cli.create_tags(Resources=[ec2_instance_id], Tags=[{'Key': 'Terminate', 'Value': 'yes'}])
- if result['ResponseMetadata']['HTTPStatusCode'] == 200:
- return ('Tagged with Sucess - instanceId ' + ec2_instance_id)
- else:
- return ('Failure to tag - instanceId ' + ec2_instance_id)
- def check_and_get_tag_terminate(self, ec2_instance_id, tag_key):
- result = ec2_cli.describe_instances(InstanceIds=[ec2_instance_id])
- for reservation in result['Reservations']:
- for inst in reservation['Instances']:
- for tag in inst['Tags']:
- if tag_key in tag['Key']:
- value = tag['Value']
- return (value)
- return ('There is no Tag: ' + tag_key)
- def terminate(self, ec2_instance_id, tag_key='Terminate'):
- # check the tag terminate_time exists and your value if the value is equal o less than the time now, terminate.
- check = self.check_and_get_tag_terminate(ec2_instance_id, tag_key='Terminate')
- if 'yes' in check:
- result = ec2_cli.terminate_instances(InstanceIds=[ec2_instance_id])
- return ('Terminating ' + ec2_instance_id)
- else:
- return ('no tag Name: %s founded, just pass' % (tag_key))
- def check_and_terminate(self):
- instances_in_draining = self.check_instance_in_draining()
- if instances_in_draining:
- result = ecs_cli.describe_container_instances(
- cluster=self.cluster_name, containerInstances=instances_in_draining
- )
- for container_instance in result['containerInstances']:
- # if the number os task is less than 2 so terminate then, if not pass
- running_count = container_instance['runningTasksCount']
- if running_count < Terminate.MIN_TASKS_RUNNING or running_count == Terminate.MIN_TASKS_RUNNING:
- ec2_instance_Id = container_instance['ec2InstanceId']
- self.terminate(ec2_instance_Id)
- return ('Killing ' + ec2_instance_Id)
- else:
- return (
- 'Waiting to reduce the number of Tasks Running, now the instance has ' +
- str(container_instance['runningTasksCount'])
- )
- else:
- return 'Nothing to do, just pass'
- def check_instance_in_draining(self):
- instances = []
- result = ecs_cli.list_container_instances(cluster=self.cluster_name, status='DRAINING')
- for instance_arns in result['containerInstanceArns']:
- instances.append(instance_arns)
- return instances
- # using the method put_in_draining.
- t = Terminate(cluster_name='homolog')
- t.put_in_draining(container_instance_id='0b42e17e-7764-4b05-9159-b26cf8cf9fb3')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement