class Cumulus::Common::Manager

Public: Base class for AWS resource manager classes.

Classes that extend this class should provide the following methods:

resource_name - return the resource name type (ie "Autoscaling Group", "Security Group", etc)
local_resources - return a Hash of local resource name to local resource config object
aws_resources - return a Hash of aws resource name to aws resource object
diff_resource - a function that will produce an array of differences between the local resource
  passed in and the aws resource passed in
unmanaged_diff - return the correct type of diff from an AWS resource
added_diff - return the correct type of diff from a local configuration object
create - given a local configuration, create the AWS resource
update - given a local configuration and an array of diffs, update the AWS resource

Additionally, the following instance variables can be set to change the behavior of the manager:

create_asset - if true, the asset will be created, if false, a warning will be printed about
  the asset not being created

Public Class Methods

new() click to toggle source
# File lib/common/manager/Manager.rb, line 26
def initialize
  @migration_root = "generated"
  @create_asset = true
end

Public Instance Methods

diff() click to toggle source

Public: Print a diff between local configuration and configuration in AWS

# File lib/common/manager/Manager.rb, line 32
def diff
  each_difference(local_resources, true) { |key, diffs| print_difference(key, diffs) }
end
diff_one(name) click to toggle source

Public: Print the diff between local configuration and AWS for a single resource

name - the name of the resource to diff

# File lib/common/manager/Manager.rb, line 39
def diff_one(name)
  each_difference(filter_local(name), false) { |key, diffs| print_difference(key, diffs) }
end
filter_local(name) click to toggle source

Public: Select local resources based on name

# File lib/common/manager/Manager.rb, line 61
def filter_local(name)
  local_resources.reject { |key, l| l.name != name }
end
list() click to toggle source

Public: Print out the names of all resources managed by Cumulus

# File lib/common/manager/Manager.rb, line 44
def list
  puts local_resources.map { |key, l| l.name }.join(" ")
end
sync() click to toggle source

Public: Sync local configuration to AWS

# File lib/common/manager/Manager.rb, line 49
def sync
  each_difference(local_resources, true) { |key, diffs| sync_difference(key, diffs) }
end
sync_one(name) click to toggle source

Public: Sync local configuration to AWS for a single resource

name - the name of the resource to sync

# File lib/common/manager/Manager.rb, line 56
def sync_one(name)
  each_difference(filter_local(name), false) { |key, diffs| sync_difference(key, diffs) }
end

Private Instance Methods

each_difference(locals, include_unmanaged, &f) click to toggle source

Internal: Loop through the differences between local configuration and AWS

locals - the local configurations to compare against include_unmanaged - whether to include unmanaged resources in the list of changes f - a function that will be passed the name of the resource and an array of

diffs
# File lib/common/manager/Manager.rb, line 73
def each_difference(locals, include_unmanaged, &f)

  unmanaged = if include_unmanaged
    Hash[aws_resources.map do |key, resource|
      [key, [unmanaged_diff(resource)]] if !locals.include?(key)
    end.compact]
  else
    {}
  end

  managed = Hash[locals.map do |key, resource|
    if !aws_resources.include?(key)
      [key, [added_diff(resource)]]
    else
      [key, diff_resource(resource, aws_resources[key])]
    end
  end]

  combined = unmanaged.merge(managed)
  sorted_keys = combined.keys.sort
  sorted_keys.each do |key|
    f.call(key, combined[key])
  end
end
print_difference(key, diffs) click to toggle source

Internal: Print differences.

key - the name of the resource to print diffs - the differences between local configuration and AWS

sync_difference(key, diffs) click to toggle source

Internal: Sync differences.

key - the name of the resource to sync diffs - the differences between local configuration and AWS

# File lib/common/manager/Manager.rb, line 126
def sync_difference(key, diffs)
  if diffs.size > 0

    StatusCodes::set_status(StatusCodes::SYNC_DIFFS)

    if diffs[0].type == DiffChange::UNMANAGED
      puts diffs[0]
    elsif diffs[0].type == DiffChange::ADD
      if @create_asset
        puts Colors.added("creating #{local_resources[key].name}...")
        create(local_resources[key])
      else
        puts "not creating #{local_resources[key].name}..."
      end
    else
      puts Colors.blue("updating #{local_resources[key].name}...")
      update(local_resources[key], diffs)
    end
  end
end