module Admiral::Util::CloudFormation

Constants

DEFAULT_RECIPES
FAILED_STATS
SUCCESS_STATS

Public Instance Methods

client() click to toggle source
# File lib/admiral-cloudformation/util.rb, line 11
def client
  AWS::CloudFormation.new
end
create_stack(stack_name, template, params) click to toggle source
# File lib/admiral-cloudformation/util.rb, line 15
def create_stack(stack_name, template, params)
  stack = client.stacks[stack_name]

  if stack.exists?
    raise '[admiral] Stack already exists. Use update command instead.'
  end

  puts "[admiral] Creating CloudFormation stack #{stack_name}."
  begin
    stack = client.stacks.create(stack_name, template, :parameters => params)
    wait_for_stack_op_to_finish stack
  rescue => e
    puts "[admiral] Error creating stack #{stack_name}: #{e}"
  end
end
destroy(stack_name) click to toggle source
# File lib/admiral-cloudformation/util.rb, line 49
def destroy(stack_name)
  stack = client.stacks[stack_name]

  if stack.exists?
    puts "Deleting stack #{stack_name}"
    stack.delete
  else
    puts "Environment does not exist"
  end
end
name() click to toggle source
# File lib/admiral-cloudformation/util.rb, line 64
def name
  ENV["ADMIRAL_NAME"] || "test"
end
params(env, options = {}) click to toggle source
# File lib/admiral-cloudformation/util.rb, line 68
def params(env, options = {})
  JSON.parse File.read(options[:params] || "#{env}.json")
end
stack_name(env) click to toggle source
# File lib/admiral-cloudformation/util.rb, line 60
def stack_name(env)
  "#{env}-#{name}"
end
update_stack(stack_name, template, params) click to toggle source
# File lib/admiral-cloudformation/util.rb, line 31
def update_stack(stack_name, template, params)
  stack = client.stacks[stack_name]

  unless stack.exists?
    raise "[admiral] CloudFormation stack #{stack_name} doesn't exist. Use create instead."
  end

  begin
    puts "[admiral] Updating CloudFormation stack #{stack_name}"
    stack.update(:template => template, :parameters => params)
    wait_for_stack_op_to_finish stack
  rescue => e
    puts "[admiral] Error updating stack #{stack_name}: #{e}"
    # raise unless e.message =~ /No updates are to be performed/
    # puts "Your CloudFormation stack is already up to date"
  end
end

Private Instance Methods

all_availability_zones() click to toggle source
# File lib/admiral-cloudformation/util.rb, line 93
def all_availability_zones
  ec2 = AWS::EC2.new
  ec2.availability_zones.map(&:name)
end
cf_query_output(stack, key) click to toggle source
# File lib/admiral-cloudformation/util.rb, line 86
def cf_query_output(stack, key)
  output = stack.outputs.find do |o|
    /#{key}/i =~ o.key
  end
  output && output.value
end
wait_for_stack_op_to_finish(stack) click to toggle source
# File lib/admiral-cloudformation/util.rb, line 74
def wait_for_stack_op_to_finish(stack)
  stats = stack.status.downcase.to_sym
  puts "[admiral] Stack #{stack.name} current status: #{stats}"

  while !SUCCESS_STATS.include?(stats)
    sleep 15
    stats = stack.status.downcase.to_sym
    raise "[admiral] Resource stack update failed." if FAILED_STATS.include?(stats)
    puts "[admiral] Stack #{stack.name} current status: #{stats}"
  end
end