Sha256: d76b0e72c408c6e4c9caecce9e5694d60a18241b399c04cc00d1af525c41b2b9

Contents?: true

Size: 1.68 KB

Versions: 3

Compression:

Stored size: 1.68 KB

Contents

require 'json'

module SimpleDeploy
  class StackUpdater

    def initialize(args)
      @config = SimpleDeploy.config
      @logger = SimpleDeploy.logger
      @entry = args[:entry]
      @name = args[:name]
      @template_body = args[:template_body]
    end

    def update_stack(attributes)
      if parameter_updated?(attributes) || @template_body
        @logger.debug 'Updated parameters or new template found.'
        update
      else
        @logger.debug 'No parameters require updating and no new template found.'
        false
      end
    end

    private

    def update
      if status.wait_for_stable
        @logger.info "Updating Cloud Formation stack #{@name}."
        cloud_formation.update :name       => @name,
                               :parameters => read_parameters_from_entry_attributes,
                               :template   => @template_body
      else
        raise "#{@name} did not reach a stable state."
      end
    end

    def parameter_updated?(attributes)
      (template_parameters - updated_parameters(attributes)) != template_parameters
    end

    def template_parameters
      json = JSON.parse @template_body
      json['Parameters'].nil? ? [] : json['Parameters'].keys
    end

    def updated_parameters attributes
      (attributes.map { |s| s.keys }).flatten
    end

    def read_parameters_from_entry_attributes
      h = {}
      entry_attributes = @entry.attributes
      template_parameters.each do |p|
        h[p] = entry_attributes[p] if entry_attributes[p]
      end
      h
    end

    def cloud_formation
      @cloud_formation ||= AWS::CloudFormation.new
    end

    def status
      @status ||= Status.new :name => @name
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
simple_deploy-0.7.6.beta.1 lib/simple_deploy/stack/stack_updater.rb
simple_deploy-0.7.5 lib/simple_deploy/stack/stack_updater.rb
simple_deploy-0.7.4 lib/simple_deploy/stack/stack_updater.rb