Skip to content

Commit 6ae6a40

Browse files
author
Peter Benjamin
committed
Refactor RestackerConfig class.
1 parent 2b4a744 commit 6ae6a40

1 file changed

Lines changed: 69 additions & 11 deletions

File tree

source/lib/restacker_config.rb

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ class RestackerConfig
22
def self.load_config(plane)
33
plane = get_plane if plane.nil?
44
config = find_config
5-
# puts "Inside 'self.load_config'"
6-
# puts "Config: #{config}"
7-
# puts "Plane: #{plane}"
85
if config[plane].nil?
96
puts "Plane not found (#{plane}). Please see #{CONFIG_FILE}."
107
exit
@@ -21,6 +18,54 @@ def self.get_plane(options)
2118
plane.to_sym
2219
end
2320

21+
def self.configure(location)
22+
config = find_config()
23+
puts Rainbow("Configuration file location:").white.bright + " #{CONFIG_FILE}"
24+
25+
target = config.fetch(location, {}).fetch(:target)
26+
new_account_number, new_role_name, new_role_prefix = ""
27+
old_account_number = target[:account_number].to_s
28+
29+
print Rainbow("Label [\"#{target[:label]}\"]:").white.bright
30+
new_label = gets.chomp
31+
32+
loop do
33+
print Rainbow("Account Number [#{old_account_number}]:").white.bright
34+
35+
new_account_number = gets.chomp
36+
break if (new_account_number =~ /\d{12,}/ || new_account_number.empty?)
37+
end
38+
39+
loop do
40+
print Rainbow("Role Name [#{target[:role_name]}]:").white.bright
41+
new_role_name = gets.chomp
42+
break if (new_role_name =~ /[\w&&\S\-]/ || new_role_name.empty?)
43+
end
44+
45+
loop do
46+
print Rainbow("Role Prefix [#{target[:role_prefix]}]:").white.bright
47+
new_role_prefix = gets.chomp
48+
break if (new_role_prefix =~ /[\w&&\S\-\/]/ || new_role_prefix.empty?)
49+
end
50+
51+
target[:label] = new_label.empty? ? target[:label] : new_label
52+
target[:account_number] = new_account_number.empty? ? target[:account_number] : new_account_number
53+
target[:role_name] = new_role_name.empty? ? target[:role_name] : new_role_name
54+
target[:role_prefix] = new_role_prefix.empty? ? target[:role_prefix] : new_role_prefix
55+
56+
File.open(CONFIG_FILE, 'w') do |f|
57+
f.write config.to_yaml
58+
end
59+
end
60+
61+
def self.latest_amis(rhel=nil)
62+
latest_amis = YAML.load(get_object(find_config[:ctrl][:bucket][:ami_key]))
63+
if rhel
64+
return latest_amis[rhel]
65+
end
66+
latest_amis
67+
end
68+
2469
private
2570
def self.find_config
2671
Dir.mkdir(CONFIG_DIR) unless Dir.exist?(CONFIG_DIR)
@@ -40,13 +85,26 @@ def self.find_config
4085
end
4186

4287
def self.find_default_plane
43-
# puts "Inside 'self.find_default_plane'"
44-
config = find_config()
45-
# puts "config: #{config}"
46-
if config[:default][:label]
47-
config[:default][:label].to_sym
48-
else
49-
raise "Location was not provided and no default location was found in #{CONFIG_FILE}."
50-
end
88+
find_config[:default][:label] || raise(Rainbow("Location was not provided and no default location was found in #{CONFIG_FILE}.").red)
89+
end
90+
91+
def self.bucket
92+
find_config[:ctrl][:bucket][:name]
93+
end
94+
95+
def self.prefix
96+
find_config[:ctrl][:bucket][:prefix] if find_config[:ctrl][:prefix]
97+
end
98+
99+
def self.list_objects
100+
s3 = Aws::S3::Client.new
101+
keys = s3.list_objects(bucket: bucket, prefix: prefix).contents.map(&:key)
102+
end
103+
104+
def self.get_object(key_input)
105+
s3 = Aws::S3::Client.new
106+
keys = s3.list_objects(bucket: bucket, prefix: prefix).contents.map(&:key)
107+
key = keys.select { |key| key.match(key_input) }.first.to_s
108+
s3.get_object(bucket: bucket, key: key).body.read
51109
end
52110
end

0 commit comments

Comments
 (0)