-
Notifications
You must be signed in to change notification settings - Fork 2
/
machines.py
179 lines (158 loc) · 6.21 KB
/
machines.py
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import ipaddress
import json
import logging
import os.path
import re
import socket
import subprocess
import sys
import yaml
def get_dnsmasq_machines():
for machine in get_machines():
yield (machine['type'], machine['name'], machine['mac'], machine['ip'])
def save_dnsmasq_machines():
domain = socket.getfqdn().split('.', 1)[-1]
def __save(machines, type):
with open(f'/etc/dnsmasq.d/{type}-machines.conf', 'w') as f:
for (_, hostname, mac, ip) in (m for m in machines if m[0] == type):
f.write(f'dhcp-host={mac},{ip},{hostname}\n')
machines = list(get_dnsmasq_machines())
__save(machines, 'virtual')
__save(machines, 'physical')
# NB a server is identified by its SMBIOS UUID.
# NB until a server is accepted, it will be continuously pxe booting
# talos and rebooting.
# NB after a server is accepted, it will be wiped, and then sidero will
# stop pxe booting it.
# NB the Server resource will eventually have the properties:
# status.isClean: true
# status.ready: true
# NB the talos configuration file is server by sidero at:
# http://$control_plane_ip/configdata?uuid=$server_uuid
# NB we can only use that endpoint after the server was allocated.
# see https://www.sidero.dev/docs/v0.3/resource-configuration/metadata/
# see https://www.sidero.dev/docs/v0.3/resource-configuration/servers/
# see https://www.sidero.dev/docs/v0.3/resource-configuration/serverclasses/
# see https://www.sidero.dev/docs/v0.3/guides/patching/
# see https://www.sidero.dev/docs/v0.3/guides/first-cluster/
# see https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/#use-a-json-merge-patch-to-update-a-deployment
def save_sidero_machines():
# add Servers.
for m in get_machines():
if 'uuid' not in m:
continue
config = {
'apiVersion': 'metal.sidero.dev/v1alpha1',
'kind': 'Server',
'metadata': {
'name': m['uuid'],
'labels': {
'name': m['name'],
'role': m['role'],
'arch': m['arch'],
},
},
'spec': {
'accepted': True,
'configPatches': [
{
'op': 'replace',
'path': '/machine/install',
'value': {
'disk': m['installDisk'],
'extraKernelArgs': [
'ipv6.disable=1'
],
},
},
],
},
}
if m['bmcType'] == 'ipmi':
config['spec']['bmc'] = {
'endpoint': m['bmcIp'],
'port': m['bmcPort'],
'user': 'admin',
'pass': 'password',
}
logging.info(f'Adding the {m["name"]} Server...')
subprocess.run(['kubectl', 'apply', '-f', '-'], input=yaml.dump(config, encoding='utf-8'), check=True)
# add ServerClasses.
for role in ('controlplane', 'worker'):
config = {
'apiVersion': 'metal.sidero.dev/v1alpha1',
'kind': 'ServerClass',
'metadata': {
'name': role,
},
'spec': {
'selector': {
'matchLabels': {
'role': role,
},
},
},
}
logging.info(f'Adding the {role} ServerClass...')
subprocess.run(['kubectl', 'apply', '-f', '-'], input=yaml.dump(config, encoding='utf-8'), check=True)
def get_machines(prefix='/vagrant'):
with open(os.path.join(prefix, 'Vagrantfile'), 'r') as f:
for line in f:
m = re.match(r'^\s*CONFIG_PANDORA_DHCP_RANGE = \'(.+?),.+?\'', line)
if m and m.groups(1):
ip_address = ipaddress.ip_address(m.group(1))
m = re.match(r'^\s*CONFIG_PANDORA_HOST_IP = \'(.+?)\'', line)
if m and m.groups(1):
host_ip_address = ipaddress.ip_address(m.group(1))
with open(os.path.join(prefix, 'machines.yaml'), 'r') as f:
machines = yaml.safe_load(f)
# populate the missing mac address.
for machine in machines:
if machine['type'] != 'virtual':
continue
if 'mac' not in machine:
machine['mac'] = '08:00:27:00:00:%02x' % (machine['hostNumber'])
# populate the missing uuid.
for machine in machines:
if machine['type'] != 'virtual':
continue
if 'uuid' not in machine:
machine['uuid'] = '00000000-0000-4000-8000-%s' % (machine['mac'].replace(':', ''))
# populate the missing ip address.
for machine in machines:
if 'ip' not in machine:
machine['ip'] = str(ip_address + machine['hostNumber'])
# populate the virtual machines vbmc ip address and port.
for machine in machines:
if machine['type'] != 'virtual':
continue
machine['bmcType'] = 'ipmi'
machine['bmcIp'] = str(host_ip_address)
machine['bmcPort'] = 8000 + machine['hostNumber']
machine['bmcQmpPort'] = 9000 + machine['hostNumber']
# populate the machines amt bmc ip address and port.
for machine in machines:
if not 'bmcType' in machine:
continue
if machine['bmcType'] != 'amt':
continue
if 'bmcIp' not in machine:
machine['bmcIp'] = machine['ip']
if 'bmcPort' not in machine:
machine['bmcPort'] = 16992
# populate the missing installDisk.
for machine in machines:
if 'installDisk' not in machine:
if machine['type'] == 'virtual':
machine['installDisk'] = '/dev/vda'
elif machine['type'] == 'physical':
machine['installDisk'] = '/dev/sda'
return machines
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
if 'get-machines-json' in sys.argv:
print(json.dumps(get_machines('.'), indent=4))
if 'save-dnsmasq-machines' in sys.argv:
save_dnsmasq_machines()
if 'save-sidero-machines' in sys.argv:
save_sidero_machines()