Module: CloudFormationTool

Included in:
CLI::ListStacks, CLI::Main::CFToolHelper, CLI::Recycle, CLI::Scale, CLI::Servers, CloudFormation::Stack, Storable
Defined in:
lib/cloud_formation_tool.rb,
lib/cloud_formation_tool/cli.rb,
lib/cloud_formation_tool/errors.rb,
lib/cloud_formation_tool/version.rb,
lib/cloud_formation_tool/cli/main.rb,
lib/cloud_formation_tool/storable.rb,
lib/cloud_formation_tool/cli/scale.rb,
lib/cloud_formation_tool/cli/create.rb,
lib/cloud_formation_tool/cli/delete.rb,
lib/cloud_formation_tool/cli/output.rb,
lib/cloud_formation_tool/cli/status.rb,
lib/cloud_formation_tool/cloud_init.rb,
lib/cloud_formation_tool/cli/compile.rb,
lib/cloud_formation_tool/cli/monitor.rb,
lib/cloud_formation_tool/cli/recycle.rb,
lib/cloud_formation_tool/cli/servers.rb,
lib/cloud_formation_tool/cli/parameters.rb,
lib/cloud_formation_tool/cli/list_stacks.rb,
lib/cloud_formation_tool/cloud_formation.rb,
lib/cloud_formation_tool/cli/param_support.rb,
lib/cloud_formation_tool/cloud_formation/stack.rb,
lib/cloud_formation_tool/cloud_formation/lambda_code.rb

Defined Under Namespace

Modules: CLI, Errors, Storable Classes: CloudFormation, CloudInit

Constant Summary collapse

VERSION =
'1.0.4'

Instance Method Summary collapse

Instance Method Details

#aws_configObject



42
43
44
45
46
47
48
# File 'lib/cloud_formation_tool.rb', line 42

def aws_config
  {
    credentials: awscreds,
    region: region,
    http_read_timeout: 5
  }
end

#awsasObject



66
67
68
69
# File 'lib/cloud_formation_tool.rb', line 66

def awsas
  require 'aws-sdk-autoscaling'
  $__aws_as ||= Aws::AutoScaling::Client.new aws_config
end

#awscfObject



61
62
63
64
# File 'lib/cloud_formation_tool.rb', line 61

def awscf
  require 'aws-sdk-cloudformation'
  $__aws_cf ||= Aws::CloudFormation::Client.new aws_config
end

#awscredsObject



37
38
39
40
# File 'lib/cloud_formation_tool.rb', line 37

def awscreds
  require 'aws-sdk-core'
  $__aws_creds ||= Aws::SharedCredentials.new(profile_name: profile)
end

#awsec2Object



50
51
52
53
# File 'lib/cloud_formation_tool.rb', line 50

def awsec2
  require 'aws-sdk-ec2'
  $__aws_ec2 ||= Aws::EC2::Client.new aws_config
end

#awss3(s3reg = nil) ⇒ Object



55
56
57
58
59
# File 'lib/cloud_formation_tool.rb', line 55

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) ⇒ Object



92
93
94
95
96
# File 'lib/cloud_formation_tool.rb', line 92

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) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/cloud_formation_tool.rb', line 19

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

#profileObject



33
34
35
# File 'lib/cloud_formation_tool.rb', line 33

def profile
  $__profile ||= find_profile(nil, ENV['AWS_PROFILE'] || ENV['AWS_DEFAULT_PROFILE'] || 'default')
end

#regionObject



29
30
31
# File 'lib/cloud_formation_tool.rb', line 29

def region
  $__region ||= (ENV['AWS_DEFAULT_REGION'] || 'us-west-1')
end

#s3_bucket_name(region) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cloud_formation_tool.rb', line 71

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