khuedoan-homelab/metal/reset.py

109 lines
3.0 KiB
Python
Raw Normal View History

2020-11-12 22:50:27 +07:00
#!/bin/python
import os
2020-11-12 23:28:45 +07:00
import time
2020-11-14 23:16:01 +07:00
import string
2020-11-12 22:50:27 +07:00
user = "root"
nodes = [
2020-11-12 23:28:45 +07:00
{
2020-11-12 23:39:38 +07:00
'name': 'Node 0',
'mac': '00:23:24:d1:f3:f0',
2020-11-13 00:43:43 +07:00
'ip': '192.168.1.110',
2020-11-14 22:23:50 +07:00
'hostname': 'node0.homelab.khuedoan.com'
2020-11-12 23:28:45 +07:00
},
{
2020-11-12 23:39:38 +07:00
'name': 'Node 1',
'mac': '00:23:24:d1:f4:d6',
2020-11-13 00:43:43 +07:00
'ip': '192.168.1.111',
2020-11-14 22:23:50 +07:00
'hostname': 'node1.homelab.khuedoan.com'
2020-11-12 23:28:45 +07:00
},
{
2020-11-12 23:39:38 +07:00
'name': 'Node 2',
'mac': '00:23:24:e7:04:60',
2020-11-13 00:43:43 +07:00
'ip': '192.168.1.112',
2020-11-14 22:23:50 +07:00
'hostname': 'node2.homelab.khuedoan.com'
2020-11-12 23:28:45 +07:00
},
{
2020-11-12 23:39:38 +07:00
'name': 'Node 3',
'mac': '00:23:24:d1:f5:69',
2020-11-13 00:43:43 +07:00
'ip': '192.168.1.113',
2020-11-14 22:23:50 +07:00
'hostname': 'node3.homelab.khuedoan.com'
2020-11-12 23:28:45 +07:00
}
2020-11-12 22:50:27 +07:00
]
2020-11-12 23:37:48 +07:00
def is_alive(node):
2020-11-13 00:29:25 +07:00
return os.system(f"ping -q -c 1 {node['ip']}") == 0
2020-11-12 23:37:48 +07:00
def is_ready(node):
2020-11-13 00:31:42 +07:00
return os.system(f"ssh -q -o StrictHostKeyChecking=no {user}@{node['ip']} exit") == 0
2020-11-14 23:16:01 +07:00
def generate_network_config(node):
with open('./kickstart/config/00-00-00-00-00-00.ks.template', 'r') as template_file:
template = string.Template(template_file.read())
config = template.substitute({
'NETWORK_DEVICE': 'eno1',
'IP': node['ip'],
2020-11-15 01:59:17 +07:00
'GATEWAY': '192.168.1.1',
2020-11-14 23:16:01 +07:00
'NETMASK': '255.255.255.0',
'HOSTNAME': node['hostname'],
2020-11-15 01:59:17 +07:00
'DNS1': '8.8.8.8',
'DNS2': '8.8.8.8'
2020-11-14 23:16:01 +07:00
})
2020-11-14 23:25:09 +07:00
with open(f"./kickstart/config/{node['mac'].replace(':', '-')}.ks", 'w') as config_file:
2020-11-14 23:16:01 +07:00
config_file.write(config)
2020-11-12 23:37:48 +07:00
def poweroff(node):
if is_alive(node):
2020-11-12 23:39:38 +07:00
print(f"Poweroff {node['name']}")
2020-11-13 00:31:42 +07:00
os.system(f"ssh -q -o StrictHostKeyChecking=no {user}@{node['ip']} poweroff")
2020-11-12 23:37:48 +07:00
else:
2020-11-12 23:39:38 +07:00
print(f"Node {node['name']} is already dead!")
2020-11-12 22:50:27 +07:00
2020-11-12 23:40:02 +07:00
def wake(node):
2020-11-12 23:37:48 +07:00
os.system(f"wol {node['mac']}")
2020-11-12 23:28:45 +07:00
2020-11-13 00:25:17 +07:00
def forget(node):
os.system(f"ssh-keygen -R {node['ip']}")
os.system(f"ssh-keygen -R {node['hostname']}")
2020-11-21 17:40:24 +07:00
# TODO provision nodes with Ansible
def provision(node):
2020-11-15 03:26:28 +07:00
os.system(f"ssh -q -o StrictHostKeyChecking=no {user}@{node['ip']} '\
yum install -y yum-utils && \
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo && \
yum install -y docker-ce docker-ce-cli containerd.io && \
2020-11-21 17:40:24 +07:00
yum install -y iscsi-initiator-utils && \
2020-11-15 03:26:28 +07:00
systemctl enable --now docker && \
2020-11-21 19:39:01 +07:00
systemctl enable --now iscsid && \
2020-11-15 03:26:28 +07:00
systemctl disable --now firewalld \
'")
2020-11-12 23:28:45 +07:00
if __name__ == "__main__":
os.chdir(f"./infra/pxe-server")
2020-11-14 23:16:01 +07:00
for node in nodes:
generate_network_config(node)
2020-11-12 23:28:45 +07:00
os.system(f"docker-compose up -d --build")
2020-11-12 23:37:48 +07:00
for node in nodes:
poweroff(node)
2020-11-13 00:25:17 +07:00
forget(node)
2020-11-12 23:37:48 +07:00
2020-11-12 23:28:45 +07:00
time.sleep(10)
2020-11-12 23:37:48 +07:00
for node in nodes:
wake(node)
while not all(is_ready(node) for node in nodes):
print("Waiting for all servers to start up...")
time.sleep(10)
2020-11-15 03:26:28 +07:00
for node in nodes:
2020-11-21 17:40:24 +07:00
provision(node)
2020-11-15 03:26:28 +07:00
os.system(f"docker-compose down")