Execute post hooks only on success, cleanups

This commit is contained in:
Stefan Reimer 2020-06-26 15:54:37 +01:00
parent 145a07b373
commit a92b56083b
2 changed files with 17 additions and 30 deletions

View File

@ -23,7 +23,11 @@ def exec_hooks(func):
def decorated(self, *args, **kwargs): def decorated(self, *args, **kwargs):
execute_hooks(self.hooks.get("pre_" + func.__name__, []), self) execute_hooks(self.hooks.get("pre_" + func.__name__, []), self)
response = func(self, *args, **kwargs) response = func(self, *args, **kwargs)
# Only execute post hook for successful actions
if response == "COMPLETE":
execute_hooks(self.hooks.get("post_" + func.__name__, []), self) execute_hooks(self.hooks.get("post_" + func.__name__, []), self)
return response return response
return decorated return decorated

View File

@ -30,16 +30,6 @@ import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class StackStatus(object):
"""
StackStatus stores simplified stack statuses.
"""
COMPLETE = "complete"
FAILED = "failed"
IN_PROGRESS = "in progress"
PENDING = "pending"
class Stack(object): class Stack(object):
def __init__(self, name, template, path, rel_path, ctx): def __init__(self, name, template, path, rel_path, ctx):
self.stackname = name self.stackname = name
@ -513,7 +503,7 @@ class Stack(object):
except ClientError as e: except ClientError as e:
if 'No updates are to be performed' in e.response['Error']['Message']: if 'No updates are to be performed' in e.response['Error']['Message']:
logger.info('No updates for {0}'.format(self.stackname)) logger.info('No updates for {0}'.format(self.stackname))
return StackStatus.COMPLETE return "COMPLETE"
else: else:
raise e raise e
@ -553,24 +543,17 @@ class Stack(object):
profile=self.profile, region=self.region) profile=self.profile, region=self.region)
return self._wait_for_completion() return self._wait_for_completion()
def describe(self):
"""
Returns the a description of the stack.
:returns: A stack description.
"""
return self.connection_manager.call(
"cloudformation",
"describe_stacks",
{"StackName": self.stackname},
profile=self.profile, region=self.region)
def get_status(self): def get_status(self):
""" """
Returns the stack's status. Returns the stack's status.
:returns: The stack's status. :returns: The stack's status.
""" """
try: try:
status = self.describe()["Stacks"][0]["StackStatus"] status = self.connection_manager.call(
"cloudformation",
"describe_stacks",
{"StackName": self.stackname},
profile=self.profile, region=self.region)["Stacks"][0]["StackStatus"]
except ClientError as e: except ClientError as e:
if e.response["Error"]["Message"].endswith("does not exist"): if e.response["Error"]["Message"].endswith("does not exist"):
return None return None
@ -607,13 +590,13 @@ class Stack(object):
def timed_out(elapsed): def timed_out(elapsed):
return elapsed >= timeout if timeout else False return elapsed >= timeout if timeout else False
status = StackStatus.IN_PROGRESS status = "IN_PROGRESS"
self.most_recent_event_datetime = ( self.most_recent_event_datetime = (
datetime.now(tzutc()) - timedelta(seconds=3) datetime.now(tzutc()) - timedelta(seconds=3)
) )
elapsed = 0 elapsed = 0
while status == StackStatus.IN_PROGRESS and not timed_out(elapsed): while status == "IN_PROGRESS" and not timed_out(elapsed):
status = self._get_simplified_status(self.get_status()) status = self._get_simplified_status(self.get_status())
if not status: if not status:
return None return None
@ -629,13 +612,13 @@ class Stack(object):
""" Returns the simplified Stack Status. """ """ Returns the simplified Stack Status. """
if status: if status:
if status.endswith("ROLLBACK_COMPLETE"): if status.endswith("ROLLBACK_COMPLETE"):
return StackStatus.FAILED return "FAILED"
elif status.endswith("_COMPLETE"): elif status.endswith("_COMPLETE"):
return StackStatus.COMPLETE return "COMPLETE"
elif status.endswith("_IN_PROGRESS"): elif status.endswith("_IN_PROGRESS"):
return StackStatus.IN_PROGRESS return "IN_PROGRESS"
elif status.endswith("_FAILED"): elif status.endswith("_FAILED"):
return StackStatus.FAILED return "FAILED"
else: else:
return 'Unknown' return 'Unknown'