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_config ⇒ Object
91
92
93
94
95
96
97
98
|
# File 'lib/cloud_formation_tool.rb', line 91
def aws_config
{
profile: profile,
region: region,
http_read_timeout: 5
}
end
|
#awsas ⇒ Object
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
|
#awscdn ⇒ Object
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
|
#awscf ⇒ Object
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
|
#awscreds ⇒ Object
85
86
87
88
89
|
# File 'lib/cloud_formation_tool.rb', line 85
def awscreds
config = Aws::SharedConfig.new(profile_name: profile, config_enabled: true)
$__aws_creds ||= config.credentials
end
|
#awsec2 ⇒ Object
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
|
#awsecs ⇒ Object
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)
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
|
#region ⇒ Object
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
bucket = awss3(region).list_buckets.buckets.select do |b|
b.name =~ /cf-templates-(\w+)-#{region}/
end.select do |b|
begin
Aws::S3::Bucket.new(b.name, client: awss3(region)).objects.first
true
rescue Aws::S3::Errors::NoSuchBucket => e
false
end
end.first
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
|