class Humidifier::Reservoir::Stack

Represents a CloudFormation stack. This contains all of the logic for interfacing with humidifier to deploy stacks, validate them, and display them.

Constants

Export

Represents an exported resource in a stack for use in cross-stack references.

Attributes

exports[R]
name[R]
pattern[R]
prefix[R]

Public Class Methods

new(name, pattern: nil, prefix: nil) click to toggle source
# File lib/humidifier/reservoir/stack.rb, line 22
def initialize(name, pattern: nil, prefix: nil)
  @name    = name
  @pattern = pattern
  @prefix  = prefix
  @exports = []
end

Public Instance Methods

create_change_set() click to toggle source
# File lib/humidifier/reservoir/stack.rb, line 29
def create_change_set
  return if !ensure_resources('change') || !valid?

  opts = { capabilities: %w[CAPABILITY_IAM CAPABILITY_NAMED_IAM] }
  humidifier_stack.create_change_set(opts)
end
deploy(wait = false, parameter_values = {}) click to toggle source
# File lib/humidifier/reservoir/stack.rb, line 36
def deploy(wait = false, parameter_values = {})
  return if !ensure_resources('deploy') || !valid?

  opts = {
    capabilities: %w[CAPABILITY_IAM CAPABILITY_NAMED_IAM],
    parameters: parameter_values
  }
  humidifier_stack.public_send(wait ? :deploy_and_wait : :deploy, opts)
end
parameters() click to toggle source
# File lib/humidifier/reservoir/stack.rb, line 46
def parameters
  @parameters ||=
    begin
      parameter_filepath =
        Reservoir.files_for(name).detect do |filepath|
          File.basename(filepath, '.yml') == 'parameters'
        end

      parameter_filepath ? ParameterList.from(parameter_filepath) : {}
    end
end
resources() click to toggle source
# File lib/humidifier/reservoir/stack.rb, line 58
def resources
  Reservoir.files_for(name).each_with_object({}) do |filepath, resources|
    basename = File.basename(filepath, '.yml')

    # Explicitly skip past parameters so we can pull them out later
    next if basename == 'parameters'

    resources.merge!(parse(filepath, basename))
  end
end
stack_name() click to toggle source
# File lib/humidifier/reservoir/stack.rb, line 69
def stack_name
  @stack_name ||= "#{prefix || Reservoir.stack_prefix}#{name}"
end
to_cf() click to toggle source
# File lib/humidifier/reservoir/stack.rb, line 73
def to_cf
  humidifier_stack.to_cf
end
upload() click to toggle source
# File lib/humidifier/reservoir/stack.rb, line 77
def upload
  return if !ensure_resources('upload') || !valid?
  humidifier_stack.upload
end
valid?() click to toggle source
# File lib/humidifier/reservoir/stack.rb, line 82
      def valid?
        humidifier_stack.valid?
      rescue Aws::CloudFormation::Errors::AccessDenied
        raise Error, <<~MSG
          The authenticated AWS profile does not have the requisite permissions
          to run this command. Ensure the profile has
          cloudformation:ValidateTemplate.
        MSG
      end

Private Instance Methods

ensure_resources(action = 'deploy') click to toggle source
# File lib/humidifier/reservoir/stack.rb, line 94
def ensure_resources(action = 'deploy')
  return true if humidifier_stack.resources.any?
  puts "Refusing to #{action} stack #{humidifier_stack.name} with no " \
       'resources'
  false
end
humidifier_stack() click to toggle source
# File lib/humidifier/reservoir/stack.rb, line 101
def humidifier_stack
  Humidifier::Stack.new(
    name: stack_name,
    description: "Resources for #{stack_name}",
    resources: resources,
    outputs: outputs,
    parameters: parameters
  )
end
outputs() click to toggle source
# File lib/humidifier/reservoir/stack.rb, line 111
def outputs
  exports.each_with_object({}) do |export, exported|
    exported[export.name] =
      Humidifier::Output.new(
        value: export.value,
        export_name: export.name
      )
  end
end
parse(filepath, type) click to toggle source
# File lib/humidifier/reservoir/stack.rb, line 121
def parse(filepath, type)
  mapping = Reservoir.mapping_for(type)
  return {} if mapping.nil?

  loaded = YAML.load_file(filepath)
  return {} unless loaded

  loaded.each_with_object({}) do |(name, attributes), resources|
    next if pattern && name !~ pattern

    attribute = attributes.delete('export')
    exports << Export.new(name, attribute) if attribute

    resources[name] = mapping.resource_for(name, attributes)
  end
end