module CloudFormationTool

Constants

VERSION

Public Instance Methods

aws_config() click to toggle source
# File lib/cloud_formation_tool.rb, line 69
def aws_config
  {
    credentials: awscreds,
    region: region,
    http_read_timeout: 5
  }
end
awsas() click to toggle source
# File lib/cloud_formation_tool.rb, line 93
def awsas
  require 'aws-sdk-autoscaling'
  $__aws_as ||= Aws::AutoScaling::Client.new aws_config
end
awscdn() click to toggle source
# File lib/cloud_formation_tool.rb, line 98
def awscdn
  require 'aws-sdk-cloudfront'
  $__aws_cdn ||= Aws::CloudFront::Client.new aws_config
end
awscf() click to toggle source
# File lib/cloud_formation_tool.rb, line 88
def awscf
  require 'aws-sdk-cloudformation'
  $__aws_cf ||= Aws::CloudFormation::Client.new aws_config
end
awscreds() click to toggle source
# File lib/cloud_formation_tool.rb, line 64
def awscreds
  require 'aws-sdk-core'
  $__aws_creds ||= Aws::SharedCredentials.new(profile_name: profile)
end
awsec2() click to toggle source
# File lib/cloud_formation_tool.rb, line 77
def awsec2
  require 'aws-sdk-ec2'
  $__aws_ec2 ||= Aws::EC2::Client.new aws_config
end
awss3(s3reg = nil) click to toggle source
# File lib/cloud_formation_tool.rb, line 82
def awss3(s3reg = nil)
  require 'aws-sdk-s3'
  s3reg ||= region
  ($__aws_s3 ||= {})[region] ||= Aws::S3::Client.new aws_config.merge(region: s3reg)
end
cf_bucket_name(region, key = nil) click to toggle source
# File lib/cloud_formation_tool.rb, line 124
def cf_bucket_name(region, key = nil)
  # generate random key if one wasn't given
  key ||= ((0...12).map { [*'a'..'z',*'0'..'9'][rand(36)] }.join)
  "cf-templates-#{key}-#{region}"
end
find_profile(dir = nil, default = nil) click to toggle source
# File lib/cloud_formation_tool.rb, line 46
def find_profile(dir = nil, default = nil)
  dir ||= Dir.pwd
  return default if (dir == "/")
  begin
    return File.read("#{dir}/.awsprofile").chomp
  rescue Errno::ENOENT
    return find_profile(File.dirname(dir))
  end
end
profile() click to toggle source
# File lib/cloud_formation_tool.rb, line 60
def profile
  $__profile ||= find_profile(nil, ENV['AWS_PROFILE'] || ENV['AWS_DEFAULT_PROFILE'] || 'default')
end
region() click to toggle source
# File lib/cloud_formation_tool.rb, line 56
def region
  $__region ||= (ENV['AWS_DEFAULT_REGION'] || 'us-west-1')
end
s3_bucket_name(region) click to toggle source
# File lib/cloud_formation_tool.rb, line 103
def s3_bucket_name(region)
  name = nil
  # see if we already have a cf-templates bucket for this region
  bucket = awss3.list_buckets.buckets.select do |b|
      b.name =~ /cf-templates-(\w+)-#{region}/
  end.first
  
  # otherwise try to create one
  if bucket.nil?
    name = cf_bucket_name(region)
    log "Creating CF template bucket #{name}"
    awss3.create_bucket({
      acl: "private",
      bucket: name
    }.merge(if region == 'us-east-1' then {} else { create_bucket_configuration: { location_constraint: region } } end))
    name
  else
    bucket[:name]
  end
end