added generator script
This commit is contained in:
		
							
								
								
									
										245
									
								
								vm_generator/generate.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										245
									
								
								vm_generator/generate.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,245 @@ | ||||
| import sys | ||||
| import tempfile | ||||
| import shutil | ||||
| 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+4X99D89DUuv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j5Lt5WViYTAy9SjrQwQ0jdeOtUYyzkeIwpJSmbRLHILL5zLAJFtIKND+XgRclzIM7uD2UqtzEGtmuLE8qx05bukwa7nqjG6UwDpN3nVO3aXcjnzLyuHWnz2NJc0ye2oVwdqPzZQ== | ||||
|       - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCfNP0DqG5ULSZVC6YUsS5NPfkSVwIOwuIoPJPAaQOfj6/+ZXXgb59x/X9gzfQ5peMhpa1m7z0qowCb+2gvDa07/2RwDEjTZzHWPYCxHaQPIRG8ttTY3B0PM5ftJlvjLtuPyeGeXU1L6f9s8pmWg855371HnyMmFzO2iA1G2FxIIZmWOICh9fZ/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 | ||||
| # written to /var/log/cloud-init-output.log | ||||
| final_message: "The system is finally up, after $UPTIME seconds" | ||||
| """ | ||||
|  | ||||
| libvirt = """<domain type="kvm"> | ||||
|   <name>{hostname}</name> | ||||
|   <metadata> | ||||
|     <libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0"> | ||||
|       <libosinfo:os id="http://ubuntu.com/ubuntu/20.04"/> | ||||
|     </libosinfo:libosinfo> | ||||
|   </metadata> | ||||
|   <memory unit="MiB">{ram}</memory> | ||||
|   <currentMemory unit="MiB">{ram}</currentMemory> | ||||
|   <vcpu placement="static">2</vcpu> | ||||
|   <os> | ||||
|     <type arch="x86_64" machine="pc-q35-6.1">hvm</type> | ||||
|     <boot dev="hd"/> | ||||
|   </os> | ||||
|   <features> | ||||
|     <acpi/> | ||||
|     <apic/> | ||||
|     <vmport state="off"/> | ||||
|   </features> | ||||
|   <cpu mode="host-model" check="partial"/> | ||||
|   <clock offset="utc"> | ||||
|     <timer name="rtc" tickpolicy="catchup"/> | ||||
|     <timer name="pit" tickpolicy="delay"/> | ||||
|     <timer name="hpet" present="no"/> | ||||
|   </clock> | ||||
|   <on_poweroff>destroy</on_poweroff> | ||||
|   <on_reboot>restart</on_reboot> | ||||
|   <on_crash>destroy</on_crash> | ||||
|   <pm> | ||||
|     <suspend-to-mem enabled="no"/> | ||||
|     <suspend-to-disk enabled="no"/> | ||||
|   </pm> | ||||
|   <devices> | ||||
|     <emulator>/usr/bin/qemu-system-x86_64</emulator> | ||||
|     <disk type="file" device="cdrom"> | ||||
|       <driver name="qemu" type="raw"/> | ||||
|       <source file="{cloudinitiso}"/> | ||||
|       <target dev="sda" bus="sata"/> | ||||
|       <readonly/> | ||||
|     </disk> | ||||
|     <disk type="file" device="disk"> | ||||
|       <driver name="qemu" type="qcow2"/> | ||||
|       <source file="{hostdrive}"/> | ||||
|       <target dev="vda" bus="virtio"/> | ||||
|     </disk> | ||||
|     <controller type="usb" index="0" model="qemu-xhci" ports="15"> | ||||
|       <address type="pci" domain="0x0000" bus="0x02" slot="0x00" function="0x0"/> | ||||
|     </controller> | ||||
|     <controller type="scsi" index="0" model="virtio-scsi"> | ||||
|       <address type="pci" domain="0x0000" bus="0x03" slot="0x00" function="0x0"/> | ||||
|     </controller> | ||||
|     <controller type="sata" index="0"> | ||||
|       <address type="pci" domain="0x0000" bus="0x00" slot="0x1f" function="0x2"/> | ||||
|     </controller> | ||||
|     <controller type="pci" index="0" model="pcie-root"/> | ||||
|     <controller type="pci" index="1" model="pcie-root-port"> | ||||
|       <model name="pcie-root-port"/> | ||||
|       <target chassis="1" port="0x10"/> | ||||
|       <address type="pci" domain="0x0000" bus="0x00" slot="0x02" function="0x0" multifunction="on"/> | ||||
|     </controller> | ||||
|     <controller type="pci" index="2" model="pcie-root-port"> | ||||
|       <model name="pcie-root-port"/> | ||||
|       <target chassis="2" port="0x11"/> | ||||
|       <address type="pci" domain="0x0000" bus="0x00" slot="0x02" function="0x1"/> | ||||
|     </controller> | ||||
|     <controller type="pci" index="3" model="pcie-root-port"> | ||||
|       <model name="pcie-root-port"/> | ||||
|       <target chassis="3" port="0x12"/> | ||||
|       <address type="pci" domain="0x0000" bus="0x00" slot="0x02" function="0x2"/> | ||||
|     </controller> | ||||
|     <controller type="pci" index="4" model="pcie-root-port"> | ||||
|       <model name="pcie-root-port"/> | ||||
|       <target chassis="4" port="0x13"/> | ||||
|       <address type="pci" domain="0x0000" bus="0x00" slot="0x02" function="0x3"/> | ||||
|     </controller> | ||||
|     <controller type="pci" index="5" model="pcie-root-port"> | ||||
|       <model name="pcie-root-port"/> | ||||
|       <target chassis="5" port="0x14"/> | ||||
|       <address type="pci" domain="0x0000" bus="0x00" slot="0x02" function="0x4"/> | ||||
|     </controller> | ||||
|     <controller type="pci" index="6" model="pcie-root-port"> | ||||
|       <model name="pcie-root-port"/> | ||||
|       <target chassis="6" port="0x15"/> | ||||
|       <address type="pci" domain="0x0000" bus="0x00" slot="0x02" function="0x5"/> | ||||
|     </controller> | ||||
|     <controller type="pci" index="7" model="pcie-root-port"> | ||||
|       <model name="pcie-root-port"/> | ||||
|       <target chassis="7" port="0x16"/> | ||||
|       <address type="pci" domain="0x0000" bus="0x00" slot="0x02" function="0x6"/> | ||||
|     </controller> | ||||
|     <controller type="pci" index="8" model="pcie-root-port"> | ||||
|       <model name="pcie-root-port"/> | ||||
|       <target chassis="8" port="0x17"/> | ||||
|       <address type="pci" domain="0x0000" bus="0x00" slot="0x02" function="0x7"/> | ||||
|     </controller> | ||||
|     <controller type="virtio-serial" index="0"> | ||||
|       <address type="pci" domain="0x0000" bus="0x04" slot="0x00" function="0x0"/> | ||||
|     </controller> | ||||
|     <interface type="network"> | ||||
|       <source network="{virbr}"/> | ||||
|       <model type="virtio"/> | ||||
|     </interface> | ||||
|     <serial type="pty"> | ||||
|       <target type="isa-serial" port="0"> | ||||
|         <model name="isa-serial"/> | ||||
|       </target> | ||||
|     </serial> | ||||
|     <console type="pty"> | ||||
|       <target type="serial" port="0"/> | ||||
|     </console> | ||||
|     <channel type="unix"> | ||||
|       <target type="virtio" name="org.qemu.guest_agent.0"/> | ||||
|       <address type="virtio-serial" controller="0" bus="0" port="1"/> | ||||
|     </channel> | ||||
|     <channel type="spicevmc"> | ||||
|       <target type="virtio" name="com.redhat.spice.0"/> | ||||
|       <address type="virtio-serial" controller="0" bus="0" port="2"/> | ||||
|     </channel> | ||||
|     <input type="tablet" bus="usb"> | ||||
|       <address type="usb" bus="0" port="1"/> | ||||
|     </input> | ||||
|     <input type="mouse" bus="ps2"/> | ||||
|     <input type="keyboard" bus="ps2"/> | ||||
|     <graphics type="spice" autoport="yes"> | ||||
|       <listen type="address"/> | ||||
|     </graphics> | ||||
|     <sound model="ich9"> | ||||
|       <address type="pci" domain="0x0000" bus="0x00" slot="0x1b" function="0x0"/> | ||||
|     </sound> | ||||
|     <audio id="1" type="spice"/> | ||||
|     <video> | ||||
|       <model type="qxl" ram="65536" vram="65536" vgamem="16384" heads="1" primary="yes"/> | ||||
|       <address type="pci" domain="0x0000" bus="0x00" slot="0x01" function="0x0"/> | ||||
|     </video> | ||||
|     <redirdev bus="usb" type="spicevmc"> | ||||
|       <address type="usb" bus="0" port="2"/> | ||||
|     </redirdev> | ||||
|     <redirdev bus="usb" type="spicevmc"> | ||||
|       <address type="usb" bus="0" port="3"/> | ||||
|     </redirdev> | ||||
|     <memballoon model="virtio"> | ||||
|       <address type="pci" domain="0x0000" bus="0x06" slot="0x00" function="0x0"/> | ||||
|     </memballoon> | ||||
|     <rng model="virtio"> | ||||
|       <backend model="random">/dev/urandom</backend> | ||||
|       <address type="pci" domain="0x0000" bus="0x07" slot="0x00" function="0x0"/> | ||||
|     </rng> | ||||
|   </devices> | ||||
| </domain> | ||||
| """ | ||||
|  | ||||
| def main(): | ||||
|  | ||||
| 	if len(sys.argv) < 2: | ||||
| 		print("Usage: generate.py [workdir] [image source]") | ||||
| 		return | ||||
|  | ||||
| 	workdir = sys.argv[1] | ||||
| 	img_src = sys.argv[2] | ||||
|  | ||||
| 	os.makedirs(os.path.join(workdir, 'xml'), exist_ok=True) | ||||
| 	os.makedirs(os.path.join(workdir, 'img'), exist_ok=True) | ||||
| 	os.makedirs(os.path.join(workdir, 'iso'), exist_ok=True) | ||||
|  | ||||
| 	with open(os.path.join(workdir, 'hosts'), 'rt') as f: | ||||
|  | ||||
| 		for line in f: | ||||
| 			line = line.replace('\n','') | ||||
| 			if not line: | ||||
| 				# skip empty lines | ||||
| 				continue | ||||
|  | ||||
| 			hostname, virbr, ram = line.split(' ') | ||||
| 			ram=int(ram) | ||||
|  | ||||
| 			print("Working on",hostname) | ||||
|  | ||||
|  | ||||
| 			fhandle, fname = tempfile.mkstemp(suffix='.yaml') | ||||
| 			with open(fhandle, "wt") as of: | ||||
| 				of.write( | ||||
| 					cloudinit.format(hostname=hostname) | ||||
| 				) | ||||
|  | ||||
| 			os.system(f'cloud-localds -v {workdir}/iso/cloudinit-{hostname}.iso {fname}') | ||||
|  | ||||
| 			os.unlink(fname) | ||||
|  | ||||
| 			hostdrive = os.path.join(workdir, f"img/{hostname}.qcow2") | ||||
|  | ||||
| 			with open(os.path.join(workdir, f"xml/{hostname}.xml"), "wt") as of: | ||||
| 				of.write( | ||||
| 					libvirt.format( | ||||
| 						hostname=hostname, | ||||
| 						ram=ram, | ||||
| 						virbr=virbr, | ||||
| 						cloudinitiso=os.path.join(workdir, f"iso/cloudinit-{hostname}.iso"), | ||||
| 						hostdrive=hostdrive | ||||
| 					) | ||||
| 				) | ||||
|  | ||||
| 			shutil.copy(img_src, hostdrive) | ||||
| 			os.system(f"qemu-img resize {hostdrive} +100G") | ||||
|  | ||||
|  | ||||
| if __name__ == "__main__": | ||||
| 	main() | ||||
							
								
								
									
										13
									
								
								vm_generator/hosts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								vm_generator/hosts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | ||||
| edge-cluster-1-master-1 edge-1 2048 | ||||
| edge-cluster-1-worker-1 edge-1 4096 | ||||
| edge-cluster-1-worker-2 edge-1 4096 | ||||
|  | ||||
| edge-cluster-2-master-1 edge-2 2048 | ||||
| edge-cluster-2-worker-1 edge-2 4096 | ||||
| edge-cluster-2-worker-2 edge-2 4096 | ||||
|  | ||||
| cloud-cluster-1-master-1 cloud 2048 | ||||
| cloud-cluster-1-worker-1 cloud 4096 | ||||
| cloud-cluster-1-worker-2 cloud 4096 | ||||
|  | ||||
| site-emulator-1 site 2048 | ||||
		Reference in New Issue
	
	Block a user