Permalink
Browse files

support for deregistering instance from loadbalancer when terminating

 * new "terminate" method in Instance class
 * deregister from declared load balancer (don't touch the undeclared ones)
 * update README file
  • Loading branch information...
sebastien requiem
sebastien requiem committed Sep 9, 2010
1 parent 9ea14d4 commit 463761dd56cf46e6c5c7d4c7e9a3d43f5a5bc8ae
Showing with 28 additions and 13 deletions.
  1. +0 −4 README.txt
  2. +28 −9 mr/awsome/__init__.py
View
@@ -63,10 +63,6 @@ Some other available options are:
loadbalancers = loadbalancer1, loadbalancer2
-Please note that for now, when terminating an instance, it is not removed
-from the loadbalancer. The attached volume will be detached automatically.
-
-
**Startup scripts**
The startup_script option above allows you to write a script which is run
View
@@ -220,6 +220,23 @@ def startup_script(self, overrides):
sys.exit(1)
return result
+
+
+ def terminate(self, overrides={}):
+ instance = self.instance
+ # Check if we are in any Load Balancer
+ loadbalancers = loadbalancers = dict((x.name, x) for x in self.conn_elb.get_all_load_balancers())
+ for loadbalancer_id in self.config.get('loadbalancers', []):
+ if loadbalancer_id not in loadbalancers:
+ log.error("Unknow loadbalancer %s ... just ignoring" % loadbalancer_id)
+ continue
+ log.info("Deregistering instance from loadbalancer %s" % loadbalancer_id)
+ loadbalancer = loadbalancers[loadbalancer_id]
+ loadbalancer.deregister_instances(instance.id)
+ instance.terminate()
+ return
+
+
def start(self, overrides={}):
instance = self.instance
if instance is not None:
@@ -262,8 +279,9 @@ def start(self, overrides={}):
if loadbalancer_id not in loadbalancers:
log.error("Unknow loadbalancer %s" % loadbalancer_id)
return
+ log.info("Adding instance to loadbalancer %s" % loadbalancer_id)
loadbalancer = loadbalancers[loadbalancer_id]
- loadbalancer.register_instances(self.instance.id)
+ loadbalancer.register_instances(instance.id)
volumes = dict((x.id, x) for x in self.conn.get_all_volumes())
for volume_id, device in self.config.get('volumes', []):
@@ -470,14 +488,15 @@ def cmd_terminate(self, argv, help):
args = parser.parse_args(argv)
server = self.ec2.instances[args.server[0]]
instance = server.instance
- if instance is None:
- return
- if instance.state != 'running':
- log.info("Instance state: %s", instance.state)
- log.info("Instance not terminated")
- return
- rc = server.conn.terminate_instances([instance.id])
- instance._update(rc[0])
+ #if instance is None:
+ # return
+ #if instance.state != 'running':
+ # log.info("Instance state: %s", instance.state)
+ # log.info("Instance not terminated")
+ # return
+# rc = server.conn.terminate_instances([instance.id])
+# instance._update(rc[0])
+ server.terminate()
log.info("Instance terminated")
def _parse_overrides(self, options):

0 comments on commit 463761d

Please sign in to comment.