Module: CloudFormationTool

Included in:
CLI::Groups, CLI::Invalidate, CLI::ListStacks, CLI::Main::CFToolHelper, CLI::Recycle, CLI::Scale, CLI::Servers, CLI::Services, 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/services.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.5.16'

Instance Method Summary collapse

Instance Method Details

#aws_configObject



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

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

#awsasObject



120
121
122
123
# File 'lib/cloud_formation_tool.rb', line 120

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

#awscdnObject



130
131
132
133
# File 'lib/cloud_formation_tool.rb', line 130

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

#awscfObject



115
116
117
118
# File 'lib/cloud_formation_tool.rb', line 115

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

#awscredsObject



85
86
87
88
89
# File 'lib/cloud_formation_tool.rb', line 85

def awscreds
  #$__aws_creds ||= Aws::SharedCredentials.new(profile_name: profile)
  config = Aws::SharedConfig.new(profile_name: profile, config_enabled: true)
  $__aws_creds ||= config.credentials
end

#awsec2Object



100
101
102
103
# File 'lib/cloud_formation_tool.rb', line 100

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

#awsecsObject



125
126
127
128
# File 'lib/cloud_formation_tool.rb', line 125

def awsecs
  require 'aws-sdk-ecs'
  $__aws_ecs ||= Aws::ECS::Client.new aws_config
end

#awss3(s3reg = nil) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/cloud_formation_tool.rb', line 105

def awss3(s3reg = nil)
  require 'aws-sdk-s3'
  s3reg ||= region
  begin
    ($__aws_s3 ||= {})[region] ||= Aws::S3::Client.new aws_config.merge(region: s3reg)
  rescue Aws::Errors::InvalidSSOToken => e
    raise CloudFormationTool::Errors::AuthError, "SSO login failed: #{e.message}"
  end
end

#cf_bucket_name(region, key = nil) ⇒ Object



166
167
168
169
170
# File 'lib/cloud_formation_tool.rb', line 166

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



64
65
66
67
68
69
70
71
72
# File 'lib/cloud_formation_tool.rb', line 64

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(name = nil) ⇒ Object



81
82
83
# File 'lib/cloud_formation_tool.rb', line 81

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

#regionObject



74
75
76
77
78
79
# File 'lib/cloud_formation_tool.rb', line 74

def region
  $__region ||= ENV['AWS_REGION'] ||
      Aws::SharedConfig.new(profile_name: profile, config_enabled: true).profile_region ||
      ENV['AWS_DEFAULT_REGION'] ||
      'us-east-1'
end

#s3_bucket_name(region) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/cloud_formation_tool.rb', line 135

def s3_bucket_name(region)
  name = nil
  # see if we already have a cf-templates bucket for this region
  bucket = awss3(region).list_buckets.buckets.select do |b|
    b.name =~ /cf-templates-(\w+)-#{region}/
  end.select do |b|
    # S3 started showing us buckets that were deleted, and can't actually be accessible, so filter those
    begin
      Aws::S3::Bucket.new(b.name, client: awss3(region)).objects.first
      true
    rescue Aws::S3::Errors::NoSuchBucket => e
      false
    end
  end.first
  
  # otherwise try to create one
  if bucket.nil?
    name = cf_bucket_name(region)
    log "Creating CF template bucket #{name}"
    awss3(region).create_bucket({
      acl: "private",
      bucket: name,
      object_ownership: 'BucketOwnerPreferred'
    }.merge(if region == 'us-east-1' then {} else { create_bucket_configuration: { location_constraint: region } } end))
    awss3(region).delete_public_access_block({bucket: name})
    name
  else
    bucket[:name]
  end
end