Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import boto3
  4.  
  5. ecs_cli = boto3.client('ecs')
  6. ec2_cli = boto3.client('ec2')
  7.  
  8. class Terminate():
  9. MIN_TASKS_RUNNING = 2
  10.  
  11. def __init__(self, cluster_name):
  12. self.cluster_name = cluster_name
  13.  
  14. def put_in_draining(self, container_instance_id):
  15. result = ecs_cli.describe_container_instances(
  16. cluster=self.cluster_name, containerInstances=[container_instance_id]
  17. )
  18. for container_instance in result['containerInstances']:
  19. ec2_instance_id = container_instance['ec2InstanceId']
  20. check = self.check_and_get_tag_terminate(ec2_instance_id, tag_key='Terminate')
  21. if 'yes' in check:
  22. return ('The instance id is already schedule to terminate, just pass')
  23. else:
  24. result = ecs_cli.update_container_instances_state(
  25. cluster=self.cluster_name, containerInstances=[container_instance_id], status='DRAINING'
  26. )
  27. if result['failures']:
  28. return ('Error: ' + str(result['failures'][0]))
  29. else:
  30. # increase_spot_fleet
  31. return (
  32. 'The ContainerInstanceId %s or EC2 - %s * is moving to Draining' %
  33. (container_instance_id, ec2_instance_id)
  34. )
  35.  
  36. def put_tag_terminate(self, ec2_instance_id):
  37. # tag name terminate with true value
  38. result = ec2_cli.create_tags(Resources=[ec2_instance_id], Tags=[{'Key': 'Terminate', 'Value': 'yes'}])
  39. if result['ResponseMetadata']['HTTPStatusCode'] == 200:
  40. return ('Tagged with Sucess - instanceId ' + ec2_instance_id)
  41. else:
  42. return ('Failure to tag - instanceId ' + ec2_instance_id)
  43.  
  44. def check_and_get_tag_terminate(self, ec2_instance_id, tag_key):
  45. result = ec2_cli.describe_instances(InstanceIds=[ec2_instance_id])
  46. for reservation in result['Reservations']:
  47. for inst in reservation['Instances']:
  48. for tag in inst['Tags']:
  49. if tag_key in tag['Key']:
  50. value = tag['Value']
  51. return (value)
  52. return ('There is no Tag: ' + tag_key)
  53.  
  54. def terminate(self, ec2_instance_id, tag_key='Terminate'):
  55. # check the tag terminate_time exists and your value if the value is equal o less than the time now, terminate.
  56. check = self.check_and_get_tag_terminate(ec2_instance_id, tag_key='Terminate')
  57. if 'yes' in check:
  58. result = ec2_cli.terminate_instances(InstanceIds=[ec2_instance_id])
  59. return ('Terminating ' + ec2_instance_id)
  60. else:
  61. return ('no tag Name: %s founded, just pass' % (tag_key))
  62.  
  63. def check_and_terminate(self):
  64. instances_in_draining = self.check_instance_in_draining()
  65. if instances_in_draining:
  66. result = ecs_cli.describe_container_instances(
  67. cluster=self.cluster_name, containerInstances=instances_in_draining
  68. )
  69. for container_instance in result['containerInstances']:
  70. # if the number os task is less than 2 so terminate then, if not pass
  71. running_count = container_instance['runningTasksCount']
  72. if running_count < Terminate.MIN_TASKS_RUNNING or running_count == Terminate.MIN_TASKS_RUNNING:
  73. ec2_instance_Id = container_instance['ec2InstanceId']
  74. self.terminate(ec2_instance_Id)
  75. return ('Killing ' + ec2_instance_Id)
  76. else:
  77. return (
  78. 'Waiting to reduce the number of Tasks Running, now the instance has ' +
  79. str(container_instance['runningTasksCount'])
  80. )
  81. else:
  82. return 'Nothing to do, just pass'
  83.  
  84. def check_instance_in_draining(self):
  85. instances = []
  86. result = ecs_cli.list_container_instances(cluster=self.cluster_name, status='DRAINING')
  87. for instance_arns in result['containerInstanceArns']:
  88. instances.append(instance_arns)
  89. return instances
  90.  
  91. # using the method put_in_draining.
  92.  
  93. t = Terminate(cluster_name='homolog')
  94. t.put_in_draining(container_instance_id='0b42e17e-7764-4b05-9159-b26cf8cf9fb3')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement