-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetvariables.rb
62 lines (56 loc) · 1.99 KB
/
getvariables.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/ruby
require 'json'
profiles = []
File.open(File.expand_path('~/.aws/credentials'), 'r') do |f|
f.each_line do |l|
next unless l.gsub!(/^\[\s*(\w+)\s*\].*/, '\1')
l.chomp!
profiles.push(l)
end
end
base_amis = Hash.new
pipeline_amis = Hash.new
data = profiles.map do |account|
regions_json = `aws ec2 describe-regions --profile #{account} --region us-east-1`
if $?.exitstatus != 0
print "Failed to run aws ec2 describe-regions --profile #{account}"
exit 1
end
regions = JSON.parse(regions_json)['Regions'].map { |d| d['RegionName'] }
regions.map do |region|
images_json = `aws ec2 describe-images --profile #{account} --region #{region} --filters "Name=tag-key,Values=Name" "Name=tag-value,Values=base-image"`
if $?.exitstatus != 0
print "Failed to run aws ec2 describe-images --profile #{account} --region #{region}"
exit 1
end
images = JSON.parse(images_json)['Images'].sort_by { |hash| hash['Name'] }
unless images.empty?
latest_image = images.last
base_amis.merge!(Hash["#{region}" => latest_image["ImageId"]])
end
images_json = `aws ec2 describe-images --profile #{account} --region #{region} --filters "Name=tag-key,Values=Name" "Name=tag-value,Values=pipeline-image"`
if $?.exitstatus != 0
print "Failed to run aws ec2 describe-images --profile #{account} --region #{region}"
exit 1
end
images = JSON.parse(images_json)['Images'].sort_by { |hash| hash['Name'] }
unless images.empty?
latest_image = images.last
pipeline_amis.merge!(Hash["#{region}" => latest_image["ImageId"]])
end
end
end
output = {
"variable" => {
"digit_base_ami_id" => {
"description" => "The DIGIT base ami",
"default" => base_amis
},
"visii_pipeline_ami_id" => {
"description" => "The Visii pipeline ami",
"default" => pipeline_amis
}
}
}
File.open('variables.tf.json.new', 'w') { |f| f.puts JSON.pretty_generate(output) }
File.rename 'variables.tf.json.new', 'variables.tf.json'