Sha256: 01ba710238df09e837e1fa71a9d398c26b2d76501adcc593b135925171397020

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

module Humidifier
  module Reservoir
    # Represents a CloudFormation stack. This contains all of the logic for
    # interfacing with humidifier to deploy stacks, validate them, and display
    # them.
    class Stack
      attr_reader :name, :pattern

      def initialize(name, pattern = nil)
        @name    = name
        @pattern = pattern
      end

      def deploy(wait = false)
        if humidifier_stack.resources.empty?
          puts "Refusing to deploy stack #{humidifier_stack.name} with no resources"
          return
        end
        valid?

        opts = { capabilities: %w[CAPABILITY_IAM CAPABILITY_NAMED_IAM] }
        wait ? humidifier_stack.deploy_and_wait(opts) : humidifier_stack.deploy(opts)
      end

      def resources
        Reservoir.files_for(name).each_with_object({}) do |filepath, resources|
          resources.merge!(parse(filepath, File.basename(filepath, '.yml')))
        end
      end

      def stack_name
        @stack_name ||= "#{Reservoir.stack_prefix}#{name}"
      end

      def to_cf
        humidifier_stack.to_cf
      end

      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

      def humidifier_stack
        Humidifier::Stack.new(
          name: stack_name,
          description: "Resources for #{stack_name}",
          resources: resources
        )
      end

      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
          resources[name] = mapping.resource_for(name, attributes)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
humidifier-reservoir-0.1.0 lib/humidifier/reservoir/stack.rb
humidifier-reservoir-0.0.1 lib/humidifier/reservoir/stack.rb