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



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
# File 'lib/stack_master/stack.rb', line 33

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.inject({}) do |params_hash, param_struct|
    params_hash[param_struct.parameter_key] = param_struct.parameter_value
    params_hash
  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

  new(region: region,
      stack_name: stack_name,
      stack_id: cf_stack.stack_id,
      parameters: parameters,
      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



62
63
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
# File 'lib/stack_master/stack.rb', line 62

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



95
96
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
# File 'lib/stack_master/stack.rb', line 95

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



137
138
139
# File 'lib/stack_master/stack.rb', line 137

def aws_parameters
  Utils.hash_to_aws_parameters(parameters)
end

#aws_tagsObject



141
142
143
# File 'lib/stack_master/stack.rb', line 141

def aws_tags
  Utils.hash_to_aws_tags(tags)
end

#max_template_size(use_s3) ⇒ Object



127
128
129
130
131
# File 'lib/stack_master/stack.rb', line 127

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

  TemplateUtils::MAX_TEMPLATE_SIZE
end

#parameters_with_defaultsObject



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

def parameters_with_defaults
  template_default_parameters.merge(parameters)
end

#templateObject



145
146
147
# File 'lib/stack_master/stack.rb', line 145

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

#template_default_parametersObject



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

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

#too_big?(use_s3 = false) ⇒ Boolean



133
134
135
# File 'lib/stack_master/stack.rb', line 133

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