11#!/usr/bin/env python2.6
22
3- # Copyright 2013-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+ # Copyright 2013-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
44#
55# Licensed under the Amazon Software License (the "License"). You may not use this file except in compliance with the
66# License. A copy of the License is located at
@@ -122,15 +122,25 @@ def lockHost(s,hostname,unlock=False):
122122 return _r
123123
124124def selfTerminate (region , asg , instance_id ):
125+ _as_conn = boto .ec2 .autoscale .connect_to_region (region ,proxy = boto .config .get ('Boto' , 'proxy' ),
126+ proxy_port = boto .config .get ('Boto' , 'proxy_port' ))
127+ if not maintainSize (region , asg ):
128+ log .info ("terminating %s" % instance_id )
129+ _as_conn .terminate_instance (instance_id , decrement_capacity = True )
130+
131+ def maintainSize (region , asg ):
125132 _as_conn = boto .ec2 .autoscale .connect_to_region (region ,proxy = boto .config .get ('Boto' , 'proxy' ),
126133 proxy_port = boto .config .get ('Boto' , 'proxy_port' ))
127134 _asg = _as_conn .get_all_groups (names = [asg ])[0 ]
128135 _capacity = _asg .desired_capacity
129136 _min_size = _asg .min_size
130137 log .debug ("capacity=%d min_size=%d" % (_capacity , _min_size ))
131138 if _capacity > _min_size :
132- log .info ("terminating %s" % instance_id )
133- _as_conn .terminate_instance (instance_id , decrement_capacity = True )
139+ log .debug ('capacity greater then min size.' )
140+ return False
141+ else :
142+ log .debug ('capacity less then or equal to min size.' )
143+ return True
134144
135145def main ():
136146 logging .basicConfig (
@@ -157,6 +167,8 @@ def main():
157167 if jobs == True :
158168 log .info ('Instance has active jobs.' )
159169 else :
170+ if maintainSize (region , asg ):
171+ continue
160172 # avoid race condition by locking and verifying
161173 lockHost (s , hostname )
162174 jobs = getJobs (s , hostname )
0 commit comments