Class: StackMaster::Stack

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

Constant Summary collapse

MAX_TEMPLATE_SIZE =
51200
MAX_S3_TEMPLATE_SIZE =
460800

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.



6
7
8
# File 'lib/stack_master/stack.rb', line 6

def files
  @files
end

#notification_arnsObject (readonly)

Returns the value of attribute notification_arns.



6
7
8
# File 'lib/stack_master/stack.rb', line 6

def notification_arns
  @notification_arns
end

#outputsObject (readonly)

Returns the value of attribute outputs.



6
7
8
# File 'lib/stack_master/stack.rb', line 6

def outputs
  @outputs
end

#parametersObject (readonly)

Returns the value of attribute parameters.



6
7
8
# File 'lib/stack_master/stack.rb', line 6

def parameters
  @parameters
end

#regionObject (readonly)

Returns the value of attribute region.



6
7
8
# File 'lib/stack_master/stack.rb', line 6

def region
  @region
end

#stack_idObject (readonly)

Returns the value of attribute stack_id.



6
7
8
# File 'lib/stack_master/stack.rb', line 6

def stack_id
  @stack_id
end

#stack_nameObject (readonly)

Returns the value of attribute stack_name.



6
7
8
# File 'lib/stack_master/stack.rb', line 6

def stack_name
  @stack_name
end

#stack_policy_bodyObject (readonly)

Returns the value of attribute stack_policy_body.



6
7
8
# File 'lib/stack_master/stack.rb', line 6

def stack_policy_body
  @stack_policy_body
end

#stack_statusObject (readonly)

Returns the value of attribute stack_status.



6
7
8
# File 'lib/stack_master/stack.rb', line 6

def stack_status
  @stack_status
end

#tagsObject (readonly)

Returns the value of attribute tags.



6
7
8
# File 'lib/stack_master/stack.rb', line 6

def tags
  @tags
end

#template_bodyObject (readonly)

Returns the value of attribute template_body.



6
7
8
# File 'lib/stack_master/stack.rb', line 6

def template_body
  @template_body
end

#template_formatObject (readonly)

Returns the value of attribute template_format.



6
7
8
# File 'lib/stack_master/stack.rb', line 6

def template_format
  @template_format
end

Class Method Details

.find(region, stack_name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/stack_master/stack.rb', line 55

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_body
  template_format = 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,
      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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/stack_master/stack.rb', line 82

def self.generate(stack_definition, config)
  parameter_hash = ParameterLoader.load(stack_definition.parameter_files)
  template_body = TemplateCompiler.compile(config, stack_definition.template_file_path)
  template_format = identify_template_format(template_body)
  parameters = ParameterResolver.resolve(config, stack_definition, parameter_hash)
  stack_policy_body = if stack_definition.stack_policy_file_path
                        File.read(stack_definition.stack_policy_file_path)
                      end
  new(region: stack_definition.region,
      stack_name: stack_definition.stack_name,
      tags: stack_definition.tags,
      parameters: parameters,
      template_body: template_body,
      template_format: template_format,
      notification_arns: stack_definition.notification_arns,
      stack_policy_body: stack_policy_body)
end

Instance Method Details

#aws_parametersObject



115
116
117
# File 'lib/stack_master/stack.rb', line 115

def aws_parameters
  Utils.hash_to_aws_parameters(parameters)
end

#aws_tagsObject



119
120
121
# File 'lib/stack_master/stack.rb', line 119

def aws_tags
  Utils.hash_to_aws_tags(tags)
end

#max_template_size(use_s3) ⇒ Object



106
107
108
109
# File 'lib/stack_master/stack.rb', line 106

def max_template_size(use_s3)
  return MAX_S3_TEMPLATE_SIZE if use_s3
  MAX_TEMPLATE_SIZE
end

#maybe_compressed_template_bodyObject



31
32
33
34
35
36
# File 'lib/stack_master/stack.rb', line 31

def maybe_compressed_template_body
  # Do not compress the template if it's not JSON because parsing YAML as a hash ignores
  # CloudFormation-specific tags such as !Ref
  return template_body if template_body.size <= MAX_TEMPLATE_SIZE || template_format != :json
  @compressed_template_body ||= JSON.dump(template_hash)
end

#missing_parameters?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/stack_master/stack.rb', line 49

def missing_parameters?
  parameters_with_defaults.any? do |key, value|
    value == nil
  end
end

#parameters_with_defaultsObject



45
46
47
# File 'lib/stack_master/stack.rb', line 45

def parameters_with_defaults
  template_default_parameters.merge(parameters)
end

#template_default_parametersObject



38
39
40
41
42
43
# File 'lib/stack_master/stack.rb', line 38

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

#template_hashObject



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

def template_hash
  return unless template_body
  @template_hash ||= case template_format
                     when :json
                       JSON.parse(template_body)
                     when :yaml
                       YAML.load(template_body)
                     end
end

#too_big?(use_s3 = false) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/stack_master/stack.rb', line 111

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