import sys
import tempfile
import os
import os.path
# For some weird reason cloud-init won't work with files dumped by pyyaml, and I'm fucking tired of debugging this
cloudinit = """#cloud-config
hostname: {hostname}
fqdn: {hostname}.local
manage_etc_hosts: true
users:
- name: ubuntu
sudo: ALL=(ALL) NOPASSWD:ALL
groups: users, admin, sudo
home: /home/ubuntu
shell: /bin/bash
lock_passwd: false
ssh-authorized-keys:
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDvNCyaO5P/vIuzxuiBY9XJ9YrZXByLUOUGpEjIdw8G1s2f2Iu8ung7S6x6XnCckaclTk3zJgg4cJlSyWuEUeMrz4npKvd3W8P4n8lj8xwhqDQ9ywta9TXriBih5Z/PPNvAYncx28F9BIDA71Q0Bybmv4/XTtPPgxE6hpjmVnjVE5pgwKceVR3Cj4Yl6+at3bC3Z15K61sNmciCV7yGUOpDkf3ZS/VBDOQIZ1GVEvkMBhNiYw2V/X2EwY3EBHVFjiryeMiwCcutTfsDvCXxsMr/1KkOu+fAbcS77+T8n7igaTQ1P59hH6VsUhwemIpnwt5NzPa9fl/+Nedw7WXx0GB1OZDUorH3L5cdKQ+nDEjUAuL+F5jjHHJMYk6V4HqNzuBxZ6lbUzdin0jyUoW8NDxDBFp+J18Mid4QiQmPhLJPkRCjY/Mf0uRnnjIB0HtysBRkHgnwVdYUu52eY/QFHqiAra2kvBspp4MiryKGpZbKTF81DnibDOrWWDB12x6+HXnY6Gpbyk0JeeILgh2VD1L0CRnyg4VC9sSUEzOnHi+4X99D89DUuvthlZWcFTny83IhX44dyI82YTNzlET6oJss76mHNLBG+6k80bVHvPw4qLNAIlpX905EaJ3SQhMonw7geOasLzT4IgSLbA22jYzKcYU8zslCBykr50jDJd3u7Q==
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCvzoxDG/eJQPNOUWkdzRzSk9oq8kxrB5x9UdlGQiARlBIMNiTZhvR7KxvYzaGxIQlgk+/4rikIcoQyPMz0yiQA8spvZv7QyG+ikhrpHxly2iH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WPYCxHaQPIRG8ttTY3B0PM5ftJlvjLtuPyeGeXU1L6f9s8pmWg855371HnyMmFzO2iA1G2FxIIZmWOICh9fZ/LeUwpmuRNuCovi7lZ3v3QCgFm1uYMbbCBOd7aF9H6nigFf5PuwjA+JkrD1t2MGscHHt9GVs2KMaNIfqdZ7jiLstcyKe0E/ADD+fXGBSkhLFUHUL88oO4rr13fzGGNmZmiF+ESjAXWuqR4VjC9rCErAXIiBI07QGFzEpVMvSoZVIeqrbKBSo9T8pptsYYQ6DzZ5F4UanepnrEPiabyi4GtZrqc3dDBinGQ25UBiFFDtJ7cogrDppPopE/ExtjNZG/Lbyshqnltfj2dKgxSyMqVQFyfIR3UIKNscS1uo+2yKwpOE0LSWfAaa5U36UdYjx9LCL0ODdA1kwKWoqUEbGRjX7slfrKzrn5zc1faOYonZyI3dmNPcYXz6dqY3DyHhykszdO939z5+GtjiTpen7CuAl8QPm6Ad0wvONFzNcYFtN40UHmiUPkGxMNWsDJWuPWGpR1IDDT8oYT0WQXJyTTQiHP+0wVc8CQI8Xjw7ivEYLJaQeP+UNiYQ==
# only cert auth via ssh (console access can still login)
ssh_pwauth: false
disable_root: true
chpasswd:
list: |
ubuntu:ansibleme
expire: False
packages:
- qemu-guest-agent
- python3
# written to /var/log/cloud-init-output.log
final_message: "The system is finally up, after $UPTIME seconds"
"""
netplan = """version: 2
ethernets:
enp1s0:
dhcp4: false
dhcp6: false
addresses: [ {addr}/24 ]
gateway4: {gateway}
nameservers:
addresses: [ 8.8.8.8, 8.8.4.4 ]
search: [ local ]
"""
libvirt = """
{hostname}
{ram}
{ram}
2
hvm
destroy
restart
destroy
/usr/bin/qemu-system-x86_64
{interfacecfg}
/dev/urandom
"""
def run_iso(hostname:str, ipaddr:str):
isoout = "/home/marcsello/ultrabirbcluster/vm_generator_luna/iso"
os.makedirs(isoout, exist_ok=True)
cloudinit_fhandle, cloudinit_fname = tempfile.mkstemp(suffix='.yaml')
with open(cloudinit_fhandle, "wt") as f:
f.write(
cloudinit.format(hostname=hostname)
)
netplan_fname = None
netplan_generated = False
if ipaddr:
if not ipaddr.startswith('@'):
gateway = '192.168.' + ipaddr.split('.')[2] + '.1' # Ezen Kristóf jót nevetett
netplan_fhandle, netplan_fname = tempfile.mkstemp(suffix='.yaml')
with open(netplan_fhandle, "wt") as f:
f.write(
netplan.format(
addr=ipaddr,
gateway=gateway,
)
)
netplan_generated = True
else:
fname = ipaddr[1:]
netplan_fname = fname
if netplan_fname:
os.system(f'cloud-localds -v {isoout}/cloudinit-{hostname}.iso --network-config={netplan_fname} {cloudinit_fname}')
else:
os.system(f'cloud-localds -v {isoout}/cloudinit-{hostname}.iso {cloudinit_fname}')
os.unlink(cloudinit_fname)
if netplan_generated:
os.unlink(netplan_fname)
def run_img(hostname:str, img_src:str):
lv_name = hostname.replace('-', '_')
hostdrive = f"/dev/vmdatastore/{lv_name}"
os.system(f'dd if={img_src} of={hostdrive} bs=4M')
def run_xml(outdir:str, hostname:str, ram:int, net:str):
xmlout = os.path.join(outdir, 'xml')
isoout = "/var/lib/libvirt/images/cloudiso"
cloudinitiso = os.path.join(isoout, f"cloudinit-{hostname}.iso")
lv_name = hostname.replace('-', '_')
hostdrive = f"/dev/vmdatastore/{lv_name}"
os.makedirs(xmlout, exist_ok=True)
interfacecfg = ""
interfaces=list(net.split(','))
for i, n in enumerate(interfaces):
multi = ""
if i == 0 and len(interfaces) > 1:
multi = 'multifunction="on"'
# Defining address like this fixes the interface to be enp1s0
# And consequitive interfaces will be enp1s0f{n}
interfacecfg += f"""
"""
with open(os.path.join(xmlout, f"{hostname}.xml"), "wt") as of:
of.write(
libvirt.format(
hostname=hostname,
ram=ram,
interfacecfg=interfacecfg,
cloudinitiso=cloudinitiso,
hostdrive=hostdrive
)
)
def run_ssh(outdir:str, hostname:str, ipaddr:str):
if not ipaddr:
return
sshout_file = os.path.join(outdir, 'ssh_config')
with open(sshout_file, 'at') as f:
f.write("\n".join([
f"Host {hostname}",
f"\tHostName {ipaddr}",
f"\tProxyJump luna",
"",""
]))
def main():
if len(sys.argv) < 3:
print("Usage: generate.py [stage] [hostsfile] [image source] [output]")
return
stage = sys.argv[1]
hostsfile = sys.argv[2]
img_src = sys.argv[3]
outdir = sys.argv[4]
if stage not in ['all', 'img', 'iso', 'xml', 'ssh']:
print("Stage should be: all, img, iso or xml")
return
with open(hostsfile, 'rt') as f:
for line in f:
line = line.replace('\n','').strip()
if not line or line.startswith('#'):
# skip empty and comment lines
continue
hostname, ram, net, ipaddr = line.split(' ')
ram=int(ram)
ipaddr = None if ipaddr == '-' else ipaddr
print(f"Working on {hostname}...")
if stage in ['iso', 'all']:
run_iso(hostname, ipaddr)
if stage in ['img', 'all']:
run_img(hostname, img_src)
if stage in ['xml', 'all']:
run_xml(outdir,hostname, ram, net)
if stage in ['ssh', 'all']:
run_ssh(outdir, hostname, ipaddr)
if __name__ == "__main__":
main()