Class: StackMaster::Stack

Inherits:
Object
  • Object
show all
Includes:
Utils::Initializable
Defined in:
lib/stack_master/stack.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Initializable

#attributes=, #initialize

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



3
4
5
# File 'lib/stack_master/stack.rb', line 3

def files
  @files
end

#notification_arnsObject (readonly)

Returns the value of attribute notification_arns.



3
4
5
# File 'lib/stack_master/stack.rb', line 3

def notification_arns
  @notification_arns
end

#outputsObject (readonly)

Returns the value of attribute outputs.



3
4
5
# File 'lib/stack_master/stack.rb', line 3

def outputs
  @outputs
end

#parametersObject (readonly)

Returns the value of attribute parameters.



3
4
5
# File 'lib/stack_master/stack.rb', line 3

def parameters
  @parameters
end

#regionObject (readonly)

Returns the value of attribute region.



3
4
5
# File 'lib/stack_master/stack.rb', line 3

def region
  @region
end

#role_arnObject (readonly)

Returns the value of attribute role_arn.



3
4
5
# File 'lib/stack_master/stack.rb', line 3

def role_arn
  @role_arn
end

#stack_idObject (readonly)

Returns the value of attribute stack_id.



3
4
5
# File 'lib/stack_master/stack.rb', line 3

def stack_id
  @stack_id
end

#stack_nameObject (readonly)

Returns the value of attribute stack_name.



3
4
5
# File 'lib/stack_master/stack.rb', line 3

def stack_name
  @stack_name
end

#stack_policy_bodyObject (readonly)

Returns the value of attribute stack_policy_body.



3
4
5
# File 'lib/stack_master/stack.rb', line 3

def stack_policy_body
  @stack_policy_body
end

#stack_statusObject (readonly)

Returns the value of attribute stack_status.



3
4
5
# File 'lib/stack_master/stack.rb', line 3

def stack_status
  @stack_status
end

#tagsObject (readonly)

Returns the value of attribute tags.



3
4
5
# File 'lib/stack_master/stack.rb', line 3

def tags
  @tags
end

#template_bodyObject (readonly)

Returns the value of attribute template_body.



3
4
5
# File 'lib/stack_master/stack.rb', line 3

def template_body
  @template_body
end

#template_formatObject (readonly)

Returns the value of attribute template_format.



3
4
5
# File 'lib/stack_master/stack.rb', line 3

def template_format
  @template_format
end

Class Method Details

.find(region, stack_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/stack_master/stack.rb', line 32

def self.find(region, stack_name)
  cf = StackMaster.cloud_formation_driver
  cf_stack = cf.describe_stacks({ stack_name: stack_name }).stacks.first
  return unless cf_stack

  parameters = cf_stack.parameters.each_with_object({}) do |param_struct, params_hash|
    params_hash[param_struct.parameter_key] = param_struct.parameter_value
  end
  template_body ||= cf.get_template({ stack_name: stack_name, template_stage: 'Original' }).template_body
  template_format = TemplateUtils.identify_template_format(template_body)
  stack_policy_body ||= cf.get_stack_policy({ stack_name: stack_name }).stack_policy_body
  outputs = cf_stack.outputs
  tags = cf_stack.tags&.each_with_object({}) do |tag_struct, tags_hash|
    tags_hash[tag_struct.key] = tag_struct.value
  end || {}

  new(region: region,
      stack_name: stack_name,
      stack_id: cf_stack.stack_id,
      parameters: parameters,
      tags: tags,
      template_body: template_body,
      template_format: template_format,
      outputs: outputs,
      role_arn: cf_stack.role_arn,
      notification_arns: cf_stack.notification_arns,
      stack_policy_body: stack_policy_body,
      stack_status: cf_stack.stack_status)
rescue Aws::CloudFormation::Errors::ValidationError
  nil
end

.generate(stack_definition, config) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/stack_master/stack.rb', line 64

def self.generate(stack_definition, config)
  parameter_hash = ParameterLoader.load(
    parameter_files: stack_definition.all_parameter_files,
    parameters: stack_definition.parameters
  )
  template_parameters = ParameterResolver.resolve(config, stack_definition, parameter_hash[:template_parameters])
  compile_time_parameters = ParameterResolver.resolve(
    config,
    stack_definition,
    parameter_hash[:compile_time_parameters]
  )
  template_body = TemplateCompiler.compile(
    config,
    stack_definition.compiler,
    stack_definition.template_dir,
    stack_definition.template,
    compile_time_parameters,
    stack_definition.compiler_options
  )
  template_format = TemplateUtils.identify_template_format(template_body)
  stack_policy_body =
    (File.read(stack_definition.stack_policy_file_path) if stack_definition.stack_policy_file_path)
  new(region: stack_definition.region,
      stack_name: stack_definition.stack_name,
      tags: stack_definition.tags,
      parameters: template_parameters,
      template_body: template_body,
      template_format: template_format,
      role_arn: stack_definition.role_arn,
      notification_arns: stack_definition.notification_arns,
      stack_policy_body: stack_policy_body)
end

.generate_without_parameters(stack_definition, config) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/stack_master/stack.rb', line 97

def self.generate_without_parameters(stack_definition, config)
  parameter_hash = ParameterLoader.load(
    parameter_files: stack_definition.all_parameter_files,
    parameters: stack_definition.parameters
  )
  compile_time_parameters = ParameterResolver.resolve(
    config,
    stack_definition,
    parameter_hash[:compile_time_parameters]
  )
  template_body = TemplateCompiler.compile(
    config,
    stack_definition.compiler,
    stack_definition.template_dir,
    stack_definition.template,
    compile_time_parameters,
    stack_definition.compiler_options
  )
  template_format = TemplateUtils.identify_template_format(template_body)
  stack_policy_body =
    (File.read(stack_definition.stack_policy_file_path) if stack_definition.stack_policy_file_path)
  new(region: stack_definition.region,
      stack_name: stack_definition.stack_name,
      tags: stack_definition.tags,
      parameters: {},
      template_body: template_body,
      template_format: template_format,
      role_arn: stack_definition.role_arn,
      notification_arns: stack_definition.notification_arns,
      stack_policy_body: stack_policy_body)
end

Instance Method Details

#aws_parametersObject



139
140
141
# File 'lib/stack_master/stack.rb', line 139

def aws_parameters
  Utils.hash_to_aws_parameters(parameters)
end

#aws_tagsObject



143
144
145
# File 'lib/stack_master/stack.rb', line 143

def aws_tags
  Utils.hash_to_aws_tags(tags)
end

#max_template_size(use_s3) ⇒ Object



129
130
131
132
133
# File 'lib/stack_master/stack.rb', line 129

def max_template_size(use_s3)
  return TemplateUtils::MAX_S3_TEMPLATE_SIZE if use_s3

  TemplateUtils::MAX_TEMPLATE_SIZE
end

#parameters_with_defaultsObject



28
29
30
# File 'lib/stack_master/stack.rb', line 28

def parameters_with_defaults
  template_default_parameters.merge(parameters)
end

#templateObject



147
148
149
# File 'lib/stack_master/stack.rb', line 147

def template
  @template ||= TemplateUtils.maybe_compressed_template_body(template_body)
end

#template_default_parametersObject



19
20
21
22
23
24
25
26
# File 'lib/stack_master/stack.rb', line 19

def template_default_parameters
  TemplateUtils
    .template_hash(template)
    .fetch('Parameters', {})
    .each_with_object({}) do |(parameter_name, description), result|
      result[parameter_name] = description['Default']&.to_s
    end
end

#too_big?(use_s3 = false) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/stack_master/stack.rb', line 135

def too_big?(use_s3 = false)
  template.size > max_template_size(use_s3)
end