-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_inventory.rb
executable file
·118 lines (100 loc) · 3.15 KB
/
generate_inventory.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
#!/usr/bin/env ruby
require 'json'
require 'yaml'
require 'fileutils'
class InventoryManager
def initialize(vmfloaty_dir = File.expand_path('~/.vmfloaty'))
# ensure the $vmfloaty_dir exists
FileUtils.mkdir_p(vmfloaty_dir)
@vmfloaty_dir = vmfloaty_dir
@inventory_json = fetch_inventory_json
end
def process_inventory
parsed_json = parse_inventory_json
save_inventory_yaml(parsed_json)
generate_ssh_config(parsed_json)
end
private
def fetch_inventory_json
# get json list of active vmfloaty VMs
output = `floaty list --active --service vmpooler --json`.strip
# abort if empty string returned, i.e., no vmfloaty VMs
raise 'Error fetching inventory json: Do you have any vmfloaty VMs?' if output.empty?
output
end
def parse_inventory_json
return [] if @inventory_json.empty?
begin
JSON.parse(@inventory_json)
rescue JSON::ParserError => e
puts "Error parsing JSON: #{e.message}"
{}
end
end
def save_inventory_yaml(parsed_json)
output = {
'config' => {
'transport' => 'ssh',
'ssh' => {
'native-ssh' => true,
'load-config' => true,
'login-shell' => 'bash',
'tty' => false,
'host-key-check' => false,
'run-as' => 'root',
'user' => 'root'
}
},
'targets' => []
}
# loop over each vmfloaty VM and create a bolt ssh target
parsed_json.each do |_key, value|
target = {
'name' => value['fqdn'].split('.').first,
'uri' => value['fqdn'],
'alias' => [],
'config' => {
'transport' => 'ssh'
}
}
output['targets'] << target
end
output_file_path = File.join(@vmfloaty_dir, 'inventory.yaml')
begin
File.open(output_file_path, 'w') { |file| file.write(output.to_yaml) }
puts "Inventory.yaml generated successfully at #{output_file_path}"
rescue StandardError => e
puts "Error writing YAML file: #{e.message}"
exit(1)
end
end
def generate_ssh_config(parsed_json)
ssh_config = "Host >>>>>vmfloaty_VMs<<<<<\n"
# loop over each vmfloaty VM and create an ssh config entry
parsed_json.each do |_key, value|
fqdn = value['fqdn']
ssh_config += "Host #{fqdn}\n"
# if the VM domain name matches then use perforce smallstep ssh config
if fqdn.include?('vmpooler-prod.puppet.net')
ssh_config += " Include '/Users/gavin.didrichsen/.step/ssh/includes'\n"
else
ssh_config += " User root\n"
ssh_config += " IdentityFile ~/.ssh/id_rsa-acceptance\n"
ssh_config += " StrictHostKeyChecking no\n"
ssh_config += " UserKnownHostsFile /dev/null\n"
end
end
output_file_path = File.join(@vmfloaty_dir, '.ssh_config')
begin
File.open(output_file_path, 'w') { |file| file.write(ssh_config) }
puts ".ssh_config generated successfully at #{output_file_path}"
rescue StandardError => e
puts "Error writing SSH config file: #{e.message}"
exit(1)
end
end
end
if __FILE__ == $PROGRAM_NAME
inventory_manager = InventoryManager.new
inventory_manager.process_inventory
end