Class: PuppetX::Puppetlabs::Aws
- Inherits:
-
Puppet::Provider
- Object
- Puppet::Provider
- PuppetX::Puppetlabs::Aws
show all
- Defined in:
- lib/puppet_x/puppetlabs/aws.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.autoscaling_client(region = default_region) ⇒ Object
165
166
167
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 165
def self.autoscaling_client(region = default_region)
::Aws::AutoScaling::Client.new(client_config(region))
end
|
.client_config(region) ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 106
def self.client_config(region)
config = {logger: logger}
config[:http_proxy] = proxy_configuration if proxy_configuration
config[:credentials] = global_credentials if global_credentials
if global_configuration
config[:region] = region_from_global_configuration || region
else
config[:region] = region
end
config
end
|
.cloudwatch_client(region = default_region) ⇒ Object
173
174
175
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 173
def self.cloudwatch_client(region = default_region)
::Aws::CloudWatch::Client.new(client_config(region))
end
|
.customer_gateway_name_from_id(region, gateway_id) ⇒ Object
262
263
264
265
266
267
268
269
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 262
def self.customer_gateway_name_from_id(region, gateway_id)
@customer_gateways ||= name_cache_hash do |ec2, key|
response = ec2.describe_customer_gateways(customer_gateway_ids: [key])
name_from_tag(response.data.customer_gateways.first)
end
@customer_gateways[[region, gateway_id]]
end
|
.default_region ⇒ Object
40
41
42
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 40
def self.default_region
ENV['AWS_REGION'] || region_from_global_configuration || 'eu-west-1'
end
|
.ec2_client(region = default_region) ⇒ Object
140
141
142
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 140
def self.ec2_client(region = default_region)
::Aws::EC2::Client.new(client_config(region))
end
|
.elb_client(region = default_region) ⇒ Object
157
158
159
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 157
def self.elb_client(region = default_region)
::Aws::ElasticLoadBalancing::Client.new(client_config(region))
end
|
.gateway_name_from_id(region, gateway_id) ⇒ Object
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 305
def self.gateway_name_from_id(region, gateway_id)
ec2 = ec2_client(region)
@gateways ||= Hash.new do |h, key|
h[key] = if key == 'local'
'local'
elsif key
begin
igw_response = ec2.describe_internet_gateways(internet_gateway_ids: [key])
name_from_tag(igw_response.data.internet_gateways.first)
rescue ::Aws::EC2::Errors::InvalidInternetGatewayIDNotFound
begin
vgw_response = ec2.describe_vpn_gateways(vpn_gateway_ids: [key])
name_from_tag(vgw_response.data.vpn_gateways.first)
rescue ::Aws::EC2::Errors::InvalidVpnGatewayIDNotFound
nil
end
end
else
nil
end
end
@gateways[gateway_id]
end
|
.global_configuration ⇒ Object
86
87
88
89
90
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 86
def self.global_configuration
Puppet.initialize_settings unless Puppet[:confdir]
path = File.join(Puppet[:confdir], 'puppetlabs_aws_configuration.ini')
File.exists?(path) ? ini_parse(File.new(path)) : nil
end
|
.global_credentials ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 71
def self.global_credentials
begin
Puppet.initialize_settings unless Puppet[:confdir]
path = File.join(Puppet[:confdir], 'puppetlabs_aws_credentials.ini')
credentials = ::Aws::SharedCredentials.new(path: path)
credentials.loadable? ? credentials : nil
rescue ::Aws::Errors::NoSuchProfileError
nil
end
end
|
.has_name?(hash) ⇒ Boolean
238
239
240
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 238
def self.has_name?(hash)
!hash[:name].nil? && !hash[:name].empty?
end
|
.ini_parse(file) ⇒ Object
This method is vendored from the AWS SDK, rather than including an extra library just to parse an ini file
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 120
def self.ini_parse(file)
current_section = {}
map = {}
file.rewind
file.each_line do |line|
line = line.split(/^|\s;/).first section = line.match(/^\s*\[([^\[\]]+)\]\s*$/) unless line.nil?
if section
current_section = section[1]
elsif current_section
item = line.match(/^\s*(.+?)\s*=\s*(.+?)\s*$/) unless line.nil?
if item
map[current_section] = map[current_section] || {}
map[current_section][item[1]] = item[2]
end
end
end
map
end
|
.logger ⇒ Object
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 60
def self.logger
log_name = 'puppet-aws-debug.log'
if global_configuration and global_configuration['default'] and global_configuration['default']['logger']
Logger.new(log_name) if global_configuration['default']['logger'] == 'true'
elsif ENV['PUPPET_AWS_DEBUG_LOG'] and not ENV['PUPPET_AWS_DEBUG_LOG'].empty?
Logger.new(log_name)
else
nil
end
end
|
.name_cache_hash(&block) ⇒ Object
288
289
290
291
292
293
294
295
296
297
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 288
def self.name_cache_hash(&block)
Hash.new do |h, rk|
region, key = rk
h[key] = unless key.nil? || key.empty?
block.call(ec2_client(region), key)
else
nil
end
end
end
|
.name_from_tag(item) ⇒ Object
211
212
213
214
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 211
def self.name_from_tag(item)
name_tag = item.tags.detect { |tag| tag.key == 'Name' }
name_tag ? name_tag.value : nil
end
|
.options_name_from_id(region, options_id) ⇒ Object
279
280
281
282
283
284
285
286
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 279
def self.options_name_from_id(region, options_id)
@dhcp_options ||= name_cache_hash do |ec2, key|
response = ec2.describe_dhcp_options(dhcp_options_ids: [key])
name_from_tag(response.dhcp_options.first)
end
@dhcp_options[[region, options_id]]
end
|
.proxy_configuration ⇒ Object
96
97
98
99
100
101
102
103
104
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 96
def self.proxy_configuration
if global_configuration and global_configuration['default'] and global_configuration['default']['http_proxy']
global_configuration['default']['http_proxy']
elsif ENV['PUPPET_AWS_PROXY'] and not ENV['PUPPET_AWS_PROXY'].empty?
ENV['PUPPET_AWS_PROXY']
else
nil
end
end
|
.rds_client(region = default_region) ⇒ Object
193
194
195
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 193
def self.rds_client(region = default_region)
::Aws::RDS::Client.new({region: region})
end
|
.read_only(*methods) ⇒ Object
52
53
54
55
56
57
58
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 52
def self.read_only(*methods)
methods.each do |method|
define_method("#{method}=") do |v|
fail "#{method} property is read-only once #{resource.type} created."
end
end
end
|
.region_from_global_configuration ⇒ Object
92
93
94
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 92
def self.region_from_global_configuration
global_configuration['default']['region'] if global_configuration
end
|
.regions ⇒ Object
26
27
28
29
30
31
32
33
34
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 26
def self.regions
if ENV['AWS_REGION'] and not ENV['AWS_REGION'].empty?
[ENV['AWS_REGION']]
elsif global_configuration and global_configuration['default'] and global_configuration['default']['region']
[global_configuration['default']['region']]
else
ec2_client(default_region).describe_regions.data.regions.map(&:region_name)
end
end
|
.route53_client(region = default_region) ⇒ Object
181
182
183
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 181
def self.route53_client(region = default_region)
::Aws::Route53::Client.new(client_config(region))
end
|
.security_group_name_from_id(region, group_id) ⇒ Object
254
255
256
257
258
259
260
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 254
def self.security_group_name_from_id(region, group_id)
@groups ||= name_cache_hash do |ec2, key|
response = ec2.describe_security_groups(group_ids: [key])
response.data.security_groups.first.group_name
end
@groups[[region, group_id]]
end
|
.sqs_client(region = default_region) ⇒ Object
201
202
203
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 201
def self.sqs_client(region = default_region)
::Aws::SQS::Client.new({region: region})
end
|
216
217
218
219
220
221
222
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 216
def self.tags_for(item)
tags = {}
item.tags.each do |tag|
tags[tag.key] = tag.value unless tag.key == 'Name'
end
tags
end
|
.vpc_name_from_id(region, vpc_id) ⇒ Object
242
243
244
245
246
247
248
249
250
251
252
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 242
def self.vpc_name_from_id(region, vpc_id)
@vpcs ||= name_cache_hash do |ec2, key|
response = ec2.describe_vpcs(vpc_ids: [key])
if response.data.vpcs.first.to_hash.keys.include?(:group_name)
response.data.vpcs.first.group_name
elsif response.data.vpcs.first.to_hash.keys.include?(:tags)
name_from_tag(response.data.vpcs.first)
end
end
@vpcs[[region, vpc_id]]
end
|
.vpn_gateway_name_from_id(region, gateway_id) ⇒ Object
271
272
273
274
275
276
277
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 271
def self.vpn_gateway_name_from_id(region, gateway_id)
@vpn_gateways ||= name_cache_hash do |ec2, key|
response = ec2.describe_vpn_gateways(vpn_gateway_ids: [key])
name_from_tag(response.data.vpn_gateways.first)
end
@vpn_gateways[[region, gateway_id]]
end
|
Instance Method Details
#autoscaling_client(region = default_region) ⇒ Object
169
170
171
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 169
def autoscaling_client(region = default_region)
self.class.autoscaling_client(region)
end
|
#cloudwatch_client(region = default_region) ⇒ Object
177
178
179
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 177
def cloudwatch_client(region = default_region)
self.class.cloudwatch_client(region)
end
|
#default_region ⇒ Object
44
45
46
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 44
def default_region
self.class.default_region
end
|
#ec2_client(region = default_region) ⇒ Object
144
145
146
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 144
def ec2_client(region = default_region)
self.class.ec2_client(region)
end
|
#elb_client(region = default_region) ⇒ Object
161
162
163
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 161
def elb_client(region = default_region)
self.class.elb_client(region)
end
|
#queue_url_from_name(queue_name) ⇒ Object
299
300
301
302
303
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 299
def queue_url_from_name (queue_name )
sqs = sqs_client(target_region)
response = sqs.get_queue_url ({queue_name: name})
response.data.queue_url
end
|
#rds_client(region = default_region) ⇒ Object
189
190
191
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 189
def rds_client(region = default_region)
self.class.rds_client(region)
end
|
#regions ⇒ Object
36
37
38
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 36
def regions
self.class.regions
end
|
#route53_client(region = default_region) ⇒ Object
185
186
187
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 185
def route53_client(region = default_region)
self.class.route53_client(region)
end
|
#sqs_client(region = default_region) ⇒ Object
197
198
199
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 197
def sqs_client(region = default_region)
self.class.sqs_client(region)
end
|
224
225
226
227
228
229
230
231
232
233
234
235
236
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 224
def tags=(value)
Puppet.info("Updating tags for #{name} in region #{target_region}")
ec2 = ec2_client(target_region)
ec2.create_tags(
resources: [@property_hash[:id]],
tags: value.collect { |k,v| { :key => k, :value => v } }
) unless value.empty?
missing_tags = tags.keys - value.keys
ec2.delete_tags(
resources: [@property_hash[:id]],
tags: missing_tags.collect { |k| { :key => k } }
) unless missing_tags.empty?
end
|
206
207
208
209
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 206
def tags_for_resource
tags = resource[:tags] ? resource[:tags].map { |k,v| {key: k, value: v} } : []
tags << {key: 'Name', value: name}
end
|
#target_region ⇒ Object
48
49
50
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 48
def target_region
resource ? resource[:region] || region : region
end
|
#vpc_only_account? ⇒ Boolean
148
149
150
151
152
153
154
155
|
# File 'lib/puppet_x/puppetlabs/aws.rb', line 148
def vpc_only_account?
response = ec2_client.describe_account_attributes(
attribute_names: ['supported-platforms']
)
account_types = response.account_attributes.map(&:attribute_values).flatten.map(&:attribute_value)
account_types == ['VPC']
end
|