def waitForStack(self, stackn, timeout=10, sleeptime=30): """ wait for a stack to become "..._COMPLETE" waits for timeout * sleeptime seconds returns the stack status """ try: waiting = True status = None cn = 0 sleeptime = 30 while waiting: cn += 1 if cn > timeout: log.error( f"Timeout expired waiting for stack {stackn} to become ready" ) waiting = not waiting break status = self.stackStatus(stackn) if status is not None and "COMPLETE" in status: log.info(f"Stack {stackn} is {status}") waiting = not waiting break elif status is None: log.warning(f"stack {stackn} does not exist (anymore)") waiting = not waiting break time.sleep(sleeptime) return status except Exception as e: fname = sys._getframe().f_code.co_name errorRaise(fname, e)
def stackStatus(self, stackname): try: stack = self.stackDetails(stackname) return stack["StackStatus"] if stack is not None else None except Exception as e: fname = sys._getframe().f_code.co_name errorRaise(fname, e)
def updateStack(self, **kwargs): try: resp = self.client.update_stack(**kwargs) if "StackId" in resp: return resp["StackId"] return None except Exception as e: fname = sys._getframe().f_code.co_name errorRaise(fname, e)
def expandDictToList(xdict, keyname="Key", valuename="Value"): try: op = [] for key in xdict: tmp = {keyname: key, valuename: xdict[key]} op.append(tmp) return op except Exception as e: fname = sys._getframe().f_code.co_name errorRaise(fname, e)
def buildStackParams(options): try: pd = makeParamDict(options["params"]) lpd = expandDictToList(pd, keyname="ParameterKey", valuename="ParameterValue") return lpd except Exception as e: fname = sys._getframe().f_code.co_name errorRaise(fname, e)
def makeParamDict(strparams): try: pd = {} if "=" in strparams: ea = strparams.split(",") for p in ea: tmp = p.split("=") pd[tmp[0].strip()] = tmp[1].strip() return pd except Exception as e: fname = sys._getframe().f_code.co_name errorRaise(fname, e)
def stackDetails(self, stackname): try: resp = self.client.describe_stacks(StackName=stackname) if "Stacks" in resp: stack = resp["Stacks"][0] return stack except ClientError as ce: log.error(f"stack: {stackname} does not exist") return None except Exception as e: fname = sys._getframe().f_code.co_name errorRaise(fname, e)
def buildStackTags(options): try: gtags = { "owner": "sre", "environment": "prod", "product": options["product"], "role": "stack", "version": __version__, } return expandDictToList(gtags) except Exception as e: fname = sys._getframe().f_code.co_name errorRaise(fname, e)
def buildStackDict(options): try: template = getTemplate(options["templatefn"]) if template is None: raise Exception(f"""Failed to read {options["templatefn"]}""") xd = {"StackName": options["stackname"], "TemplateBody": template} xd["Parameters"] = buildStackParams(options) xd["Capabilities"] = ["CAPABILITY_NAMED_IAM"] xd["Tags"] = buildStackTags(options) return xd except Exception as e: fname = sys._getframe().f_code.co_name errorRaise(fname, e)
def getTemplate(fn): try: op = None with open(fn, "r") as ifn: lines = ifn.readlines() for line in lines: if op is None: op = line else: op += line return op except Exception as e: fname = sys._getframe().f_code.co_name errorRaise(fname, e)
def deleteStack(self, stackname): try: self.client.delete_stack(StackName=stackname) except Exception as e: fname = sys._getframe().f_code.co_name errorRaise(fname, e)