-
Notifications
You must be signed in to change notification settings - Fork 17
/
getvariables.rb
142 lines (125 loc) · 4.06 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/ruby
require 'json'
require 'net/http'
require 'optparse'
options = {:iam_profile_name => nil, :aws_account => 'default' }
optparse = OptionParser.new do |opts|
opts.banner = "Usage: getvariables.rb [options]"
opts.on('-i','--iam-profile-name iam_profile_name', 'IAM Profile Name') do |iam_profile_name|
options[:iam_profile_name] = iam_profile_name
end
opts.on('-a','--aws-account aws_account', 'AWS Account Name') do |aws_account|
options[:aws_account] = aws_account
end
opts.on('-h', '--help', 'Displays Help') do
puts opts
exit
end
end
optparse.parse!
ENV.delete('AWS_ACCESS_KEY_ID')
ENV.delete('AWS_SECRET_ACCESS_KEY')
def is_iam_instance?(profile_name)
return nil if profile_name.nil?
begin
iam_info = Net::HTTP.get('169.254.169.254','/latest/meta-data/iam/info')
rescue Exception => e
return nil
end
return true if JSON.parse(iam_info)['InstanceProfileArn'].split(':')[5].split('/')[1] == profile_name
return nil
end
profiles = []
unless is_iam_instance?(options[:iam_profile_name])
File.open(File.expand_path('~/.aws/credentials'), 'r') do |f|
f.each_line do |l|
next unless l.gsub!(/^\[\s*(.*?)\s*\].*/, '\1')
l.chomp!
profiles.push(l)
end
end
else
profiles = options[:aws_account].split(',')
end
primary_azs = {}
secondary_azs = {}
tertiary_azs = {}
az_counts = {}
az_lists = {}
az_letters = {}
is_iam_instance = is_iam_instance?(options[:iam_profile_name])
data = profiles.map do |account|
regions_json = if is_iam_instance
`aws ec2 describe-regions --output json --region us-east-1`
else
`aws ec2 describe-regions --output json --profile #{account} --region us-east-1`
end
if $?.exitstatus != 0
print "Failed to run aws ec2 describe-regions --output json --profile #{account} --region us-east-1\n"
[]
else
regions = JSON.parse(regions_json)['Regions'].map { |d| d['RegionName'] }
regions.map do |region|
azs_json = if is_iam_instance
`aws ec2 describe-availability-zones --output json --region #{region}`
else
`aws ec2 describe-availability-zones --output json --profile #{account} --region #{region}`
end
if $?.exitstatus != 0
print "Failed to run aws ec2 describe-availability-zones --output json --profile #{account} --region #{region}\n"
{}
else
JSON.parse(azs_json)['AvailabilityZones'].map do |tuple|
tuple[:name] = "#{account}-#{tuple['RegionName']}"
tuple[:sortkey] = "#{account}-#{tuple['ZoneName']}"
tuple
end
end
end.flatten
end
end.flatten.reject { |tuple| tuple['State'] != 'available' }.sort do |a,b|
a[:sortkey] <=> b[:sortkey]
end
data.each do |tuple|
az_counts[tuple[:name]] ||= 0
az_counts[tuple[:name]] = az_counts[tuple[:name]]+1
az_lists[tuple[:name]] ||= []
az_lists[tuple[:name]].push tuple['ZoneName']
az_letters[tuple[:name]] ||= []
az_letters[tuple[:name]].push tuple['ZoneName'][-1,1]
if !primary_azs[tuple[:name]]
primary_azs[tuple[:name]] = tuple['ZoneName']
elsif !secondary_azs[tuple[:name]]
secondary_azs[tuple[:name]] = tuple['ZoneName']
elsif !tertiary_azs[tuple[:name]]
tertiary_azs[tuple[:name]] = tuple['ZoneName']
end
end
[az_lists, az_letters].each do |squash|
squash.each_key { |k| squash[k] = squash[k].join ',' }
end
az_counts.each_key { |k| az_counts[k] = "#{az_counts[k]}" }
output = {
"variable" => {
"primary_azs" => {
"default" => primary_azs
},
'secondary_azs' => {
"default" => secondary_azs
},
'tertiary_azs' => {
"default" => tertiary_azs
},
'list_all' => {
'default' => az_lists
},
'list_letters' => {
'default' => az_letters
},
'az_counts' => {
'default' => az_counts
}
}
}
File.open('variables.tf.json.new', 'w') { |f| f.puts JSON.pretty_generate(output) }
File.rename 'variables.tf.json.new', 'variables.tf.json'