Module: CloudFormationTool

Included in:
CLI::Groups, CLI::Invalidate, CLI::ListStacks, CLI::Main::CFToolHelper, CLI::Recycle, CLI::Scale, CLI::Servers, CloudFormation::CloudFrontDistribution, CloudFormation::CloudFrontInvalidation, 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/groups.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/invalidate.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,
lib/cloud_formation_tool/cloud_formation/nested_stack.rb,
lib/cloud_formation_tool/cloud_formation/cloud_front_distribution.rb

Defined Under Namespace

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

Constant Summary collapse

VERSION =
'1.3.4'

Instance Method Summary collapse

Instance Method Details

#aws_configObject



69
70
71
72
73
74
75
# File 'lib/cloud_formation_tool.rb', line 69

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

#awsasObject



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

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

#awscdnObject



98
99
100
101
# File 'lib/cloud_formation_tool.rb', line 98

def awscdn
  require 'aws-sdk-cloudfront'
  $__aws_cdn ||= Aws::CloudFront::Client.new aws_config
end

#awscfObject



88
89
90
91
# File 'lib/cloud_formation_tool.rb', line 88

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

#awscredsObject



64
65
66
67
# File 'lib/cloud_formation_tool.rb', line 64

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

#awsec2Object



77
78
79
80
# 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) ⇒ Object



82
83
84
85
86
# 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) ⇒ Object



124
125
126
127
128
# 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) ⇒ Object



46
47
48
49
50
51
52
53
54
# 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

#profileObject



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

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

#regionObject



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

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

#s3_bucket_name(region) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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