Commit 2fa7cf63 by e0d

make the missing server case better

parent 763cb84c
...@@ -25,6 +25,8 @@ import logging ...@@ -25,6 +25,8 @@ import logging
import os import os
from distutils import spawn from distutils import spawn
class MissingHostError(Exception):
pass
class LifecycleHandler: class LifecycleHandler:
...@@ -72,36 +74,47 @@ class LifecycleHandler: ...@@ -72,36 +74,47 @@ class LifecycleHandler:
asg = as_message['AutoScalingGroupName'] asg = as_message['AutoScalingGroupName']
token = as_message['LifecycleActionToken'] token = as_message['LifecycleActionToken']
if self.verify_ok_to_retire(as_message['EC2InstanceId']): try:
logging.info("Host is marked as OK to retire, retiring {instance}".format( if self.verify_ok_to_retire(as_message['EC2InstanceId']):
instance=instance_id))
self.continue_lifecycle(asg, token, self.hook) logging.info("Host is marked as OK to retire, retiring {instance}".format(
instance=instance_id))
self.continue_lifecycle(asg, token, self.hook)
if not self.dry_run:
logging.info("Deleting message with body {message}".format(message=as_message))
self.sqs_con.delete_message(queue, sqs_message)
else:
logging.info("Would have deleted message with body {message}".format(message=as_message))
if not self.dry_run:
logging.info("Deleting message with body {message}".format(message=as_message))
self.sqs_con.delete_message(queue, sqs_message)
else: else:
logging.info("Would have deleted message with body {message}".format(message=as_message)) logging.info("Recording lifecycle heartbeat for instance {instance}".format(
instance=instance_id))
else: self.record_lifecycle_action_heartbeat(asg, token, self.hook)
logging.info("Recording lifecycle heartbeat for instance {instance}".format( except MissingHostError as mhe:
instance=instance_id)) logging.exception(mhe)
# There is nothing we can do to recover from this, so we
# still delete the message
self.delete_sqs_message(self,queue,sqs_message,as_message)
self.record_lifecycle_action_heartbeat(asg, token, self.hook) # These notifications are sent when configuring a new lifecycle hook, they can be
# These notifications are send when configuring a new lifecycle hook, they can be
# deleted safely # deleted safely
elif as_message['Event'] == LifecycleHandler.TEST_NOTIFICATION: elif as_message['Event'] == LifecycleHandler.TEST_NOTIFICATION:
if not self.dry_run: self.delete_sqs_message(self,queue,sqs_message,as_message)
logging.info("Deleting message with body {message}".format(message=as_message))
self.sqs_con.delete_message(queue, sqs_message)
else:
logging.info("Would have deleted message with body {message}".format(message=as_message))
else: else:
raise NotImplemented("Encountered message, {message_id}, of unexpected type.".format( raise NotImplemented("Encountered message, {message_id}, of unexpected type.".format(
message_id=as_message['MessageId'])) message_id=as_message['MessageId']))
def delete_sqs_message(self,queue, sqs_message, as_message, dry_run):
if not self.dry_run:
logging.info("Deleting message with body {message}".format(message=as_message))
self.sqs_con.delete_message(queue, sqs_message)
else:
logging.info("Would have deleted message with body {message}".format(message=as_message))
def record_lifecycle_action_heartbeat(self, asg, token, hook): def record_lifecycle_action_heartbeat(self, asg, token, hook):
command = self.base_cli_command + "autoscaling record-lifecycle-action-heartbeat " \ command = self.base_cli_command + "autoscaling record-lifecycle-action-heartbeat " \
...@@ -160,8 +173,9 @@ class LifecycleHandler: ...@@ -160,8 +173,9 @@ class LifecycleHandler:
else: else:
# No instance for id in SQS message this can happen if something else # No instance for id in SQS message this can happen if something else
# has terminated the instances outside of this workflow # has terminated the instances outside of this workflow
logging.warn("Instance with id {id} is referenced in an SQS message, but does not exist.") message = "Instance with id {id} is referenced in an SQS message, but does not exist.".\
return True format(id=instance_id)
raise MissingHostError(message)
if __name__=="__main__": if __name__=="__main__":
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment