Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. class AWSPlugin(object):
  2. """ AWS Plugin class """
  3. def __init__(**kwargs):
  4. """ setting kwargs to class object variables"""
  5. pass
  6.  
  7. def get_stack_info(self, stack_name):
  8. """ returns the cloudformation stack details """
  9. client_response = None
  10. try:
  11. cfn_client = self.get_client(AWS_SERVICES.CLOUDFORMATION)
  12. response = cfn_client.describe_stacks(StackName=stack_name)
  13. client_response = response['Stacks'].pop()
  14. except ClientError as ce:
  15. self.log.error("Failed to retrieve cloudformation stack object : %s", ce)
  16. except Exception as e:
  17. self.log.exception("Exception occurred while retrieving cloudformation stack object : %s", e)
  18. return client_response
  19.  
  20. def create_cfn_stack(self, stack_name, cfn_template_file, parameters):
  21. """ creates a cloudformation stack """
  22. client_response = None
  23. try:
  24. with open(cfn_template_file, 'r') as jfp:
  25. template_body = jfp.read()
  26. cfn_client = self.get_client(AWS_SERVICES.CLOUDFORMATION)
  27. cfn_response = cfn_client.create_stack(
  28. StackName=stack_name,
  29. TemplateBody=template_body,
  30. Parameters=parameters,
  31. DisableRollback=False,
  32. Tags=[
  33. {
  34. 'Key': 'Name',
  35. 'Value': stack_name
  36. },
  37. ]
  38. )
  39. client_response = cfn_response['StackId']
  40. except ClientError as ce:
  41. self.log.error("Failed to create cloudformation stack : %s", ce)
  42. except Exception as e:
  43. self.log.exception("Exception occurred while creating cloudformation stack : %s", e)
  44. return client_response
  45.  
  46. def create_cfn_stack(self, stack_name, cfn_template_file, parameters):
  47. """ creates a cloudformation stack """
  48. client_response = None
  49. try:
  50. existing_stack = self.get_stack_info(stack_name)
  51. if existing_stack is not None:
  52. raise Exception("Stack %s is already exists and in %s state" %(stack_name, existing_stack['Status']))
  53. except ClientError as ce:
  54. print "Stack is not present and its good to create with name %s" % (stack_name)
  55. with open(cfn_template_file, 'r') as jfp:
  56. template_body = jfp.read()
  57. cfn_client = self.get_client(AWS_SERVICES.CLOUDFORMATION)
  58. cfn_response = cfn_client.create_stack(
  59. StackName=stack_name,
  60. TemplateBody=template_body,
  61. Parameters=parameters,
  62. DisableRollback=False,
  63. Tags=[
  64. {
  65. 'Key': 'Name',
  66. 'Value': stack_name
  67. },
  68. ]
  69. )
  70. client_response = response['StackId']
  71. except Exception as e:
  72. self.log.exception("Exception occurred while creating cloudformation stack : %s", e)
  73. return client_response
  74.  
  75. def create_cfn_stack(self, stack_name, cfn_template_file, parameters):
  76. """ creates a cloudformation stack """
  77. client_response = None
  78. try:
  79. try:
  80. existing_stack = self.get_stack_info(stack_name)
  81. if existing_stack is not None:
  82. raise Exception("Stack %s is already exists and in %s state" %(stack_name, existing_stack['Status']))
  83. except ClientError as ce:
  84. print "Stack is not present and its good to create with name %s" % (stack_name)
  85. with open(cfn_template_file, 'r') as jfp:
  86. template_body = jfp.read()
  87. cfn_client = self.get_client(AWS_SERVICES.CLOUDFORMATION)
  88. cfn_response = cfn_client.create_stack(
  89. StackName=stack_name,
  90. TemplateBody=template_body,
  91. Parameters=parameters,
  92. DisableRollback=False,
  93. Tags=[
  94. {
  95. 'Key': 'Name',
  96. 'Value': stack_name
  97. },
  98. ]
  99. )
  100. client_response = cfn_response['StackId']
  101. except ClientError as ce:
  102. self.log.error("Failed to create cloudformation stack : %s", ce)
  103. except Exception as e:
  104. self.log.exception("Exception occurred while creating cloudformation stack : %s", e)
  105. return client_response
  106.  
  107. def create_cfn_stack(self, stack_name, cfn_template_file, parameters):
  108. """ creates a cloudformation stack """
  109. client_response = None
  110. try:
  111. existing_stack = self.get_stack_info(stack_name)
  112. if existing_stack is not None:
  113. raise Exception("Stack %s is already exists and in %s state" %(stack_name, existing_stack['Status']))
  114. except ClientError as ce:
  115. print "Stack is not present and its good to create with name %s" % (stack_name)
  116.  
  117. try:
  118. with open(cfn_template_file, 'r') as jfp:
  119. template_body = jfp.read()
  120. cfn_client = self.get_client(AWS_SERVICES.CLOUDFORMATION)
  121. cfn_response = cfn_client.create_stack(
  122. StackName=stack_name,
  123. TemplateBody=template_body,
  124. Parameters=parameters,
  125. DisableRollback=False,
  126. Tags=[
  127. {
  128. 'Key': 'Name',
  129. 'Value': stack_name
  130. },
  131. ]
  132. )
  133. client_response = cfn_response['StackId']
  134. except ClientError as ce:
  135. self.log.error("Failed to create cloudformation stack : %s", ce)
  136. except Exception as e:
  137. self.log.exception("Exception occurred while creating cloudformation stack : %s", e)
  138. return client_response
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement