-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlib.rb
146 lines (136 loc) · 5.07 KB
/
lib.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
143
144
145
146
require 'open3'
def get_docker_hub_auth
config_path = File.expand_path '~/.docker/config.json'
return nil unless File.exists? config_path
config = JSON.load File.read(config_path)
return nil unless config.has_key?('auths') && config['auths'].has_key?('https://index.docker.io/v1/')
config['auths']['https://index.docker.io/v1/']['auth']
end
def virtual_machines
save_config
save_virtual_machines_config
machines = JSON.load(File.read('shared/machines.json')).select{|m| m['type'] == 'virtual'}
machines.each_with_index.map do |m, i|
[m['name'], m['arch'], m['firmware'], m['ip'], m['mac'], m['bmcType'], m['bmcIp'], m['bmcPort'], m['bmcQmpPort']]
end
end
def save_config
FileUtils.mkdir_p 'shared'
File.write('shared/config.json', JSON.pretty_generate({
'CONFIG_PANDORA_IP': CONFIG_PANDORA_IP,
'CONFIG_CONTROL_PLANE_VIP': CONFIG_CONTROL_PLANE_VIP,
'CONFIG_PANDORA_LOAD_BALANCER_RANGE': CONFIG_PANDORA_LOAD_BALANCER_RANGE,
'CONFIG_VECTOR_CHART_VERSION': CONFIG_VECTOR_CHART_VERSION,
'CONFIG_METALLB_CHART_VERSION': CONFIG_METALLB_CHART_VERSION,
'CONFIG_EXTERNAL_DNS_CHART_VERSION': CONFIG_EXTERNAL_DNS_CHART_VERSION,
'CONFIG_CERT_MANAGER_CHART_VERSION': CONFIG_CERT_MANAGER_CHART_VERSION,
'CONFIG_TRAEFIK_CHART_VERSION': CONFIG_TRAEFIK_CHART_VERSION,
'CONFIG_KUBERNETES_DASHBOARD_CHART_VERSION': CONFIG_KUBERNETES_DASHBOARD_CHART_VERSION,
}))
end
def save_virtual_machines_config
stdout, stderr, status = Open3.capture3('python3', 'machines.py', 'get-machines-json')
if status.exitstatus != 0
raise "failed to run python3 machines.py get-machines-json. status=#{status.exitstatus} stdout=#{stdout} stderr=#{stderr}"
end
FileUtils.mkdir_p 'shared'
File.write('shared/machines.json', stdout)
end
def vbmc_domain_name(machine)
"#{File.basename(File.dirname(__FILE__))}_#{machine.name}"
end
def vbmc_container_name(machine, bmc_type)
"vbmc-emulator-#{bmc_type}-#{vbmc_domain_name(machine)}"
end
def vbmc_up(machine, bmc_type, bmc_ip, bmc_port)
case bmc_type
when 'redfish'
vbmc_up_redfish(machine, bmc_type, bmc_ip, bmc_port)
when 'ipmi'
vbmc_up_ipmi(machine, bmc_type, bmc_ip, bmc_port)
end
end
def vbmc_up_redfish(machine, bmc_type, bmc_ip, bmc_port)
vbmc_destroy(machine, bmc_type)
container_name = vbmc_container_name(machine, bmc_type)
machine.ui.info("Creating the #{container_name} docker container...")
stdout, stderr, status = Open3.capture3(
'docker',
'run',
'--rm',
'--name',
container_name,
'--detach',
'-v',
'/var/run/libvirt/libvirt-sock:/var/run/libvirt/libvirt-sock',
'-v',
'/var/run/libvirt/libvirt-sock-ro:/var/run/libvirt/libvirt-sock-ro',
'-e',
"SUSHY_EMULATOR_ALLOWED_INSTANCES=#{machine.id}",
'-p',
"#{bmc_ip}:#{bmc_port}:8000/tcp",
'ruilopes/sushy-vbmc-emulator')
if status.exitstatus != 0
if stderr.include? 'No such container'
return
end
raise "failed to run the #{container_name} docker container. status=#{status.exitstatus} stdout=#{stdout} stderr=#{stderr}"
end
end
def vbmc_up_ipmi(machine, bmc_type, bmc_ip, bmc_port)
vbmc_destroy(machine, bmc_type)
container_name = vbmc_container_name(machine, bmc_type)
machine.ui.info("Creating the #{container_name} docker container...")
stdout, stderr, status = Open3.capture3(
'docker',
'run',
'--rm',
'--name',
container_name,
'--detach',
'-v',
'/var/run/libvirt/libvirt-sock:/var/run/libvirt/libvirt-sock',
'-v',
'/var/run/libvirt/libvirt-sock-ro:/var/run/libvirt/libvirt-sock-ro',
'-e',
"VBMC_EMULATOR_DOMAIN_NAME=#{vbmc_domain_name(machine)}",
'-e',
"VBMC_EMULATOR_USERNAME=admin",
'-e',
"VBMC_EMULATOR_PASSWORD=password",
'-p',
"#{bmc_ip}:#{bmc_port}:6230/udp",
'ruilopes/vbmc-emulator')
if status.exitstatus != 0
if stderr.include? 'No such container'
return
end
raise "failed to run the #{container_name} docker container. status=#{status.exitstatus} stdout=#{stdout} stderr=#{stderr}"
end
end
def vbmc_destroy(machine, bmc_type)
container_name = vbmc_container_name(machine, bmc_type)
stdout, stderr, status = Open3.capture3('docker', 'inspect', container_name)
if status.exitstatus != 0
if stderr.include? 'No such object'
return
end
raise "failed to inspect the #{container_name} docker container. status=#{status.exitstatus} stdout=#{stdout} stderr=#{stderr}"
end
machine.ui.info("Destroying the #{container_name} docker container...")
stdout, stderr, status = Open3.capture3('docker', 'kill', '--signal', 'INT', container_name)
if status.exitstatus != 0
if stderr.include? 'No such container'
return
end
raise "failed to kill the #{container_name} docker container. status=#{status.exitstatus} stdout=#{stdout} stderr=#{stderr}"
end
stdout, stderr, status = Open3.capture3('docker', 'wait', container_name)
if status.exitstatus != 0
if stderr.include? 'No such container'
return
end
raise "failed to wait for the #{container_name} docker container. status=#{status.exitstatus} stdout=#{stdout} stderr=#{stderr}"
end
return
end