2022-08-21 01:35:03 +02:00
|
|
|
"""A Pulumi deployment with libvirt and Python"""
|
2023-12-29 20:38:11 +01:00
|
|
|
# vim:sts=4:sw=4
|
2022-08-20 00:14:02 +02:00
|
|
|
|
2022-08-21 01:35:03 +02:00
|
|
|
import yaml
|
|
|
|
from dotmap import DotMap
|
2022-08-20 00:14:02 +02:00
|
|
|
import pulumi
|
|
|
|
import pulumi_libvirt as lv
|
2022-08-21 01:35:03 +02:00
|
|
|
from passlib.hash import sha512_crypt
|
|
|
|
from jinja2 import Template
|
2022-08-20 00:14:02 +02:00
|
|
|
|
2022-08-21 01:35:03 +02:00
|
|
|
conf = DotMap(yaml.safe_load(open("input.yaml").read()))
|
2023-07-03 21:05:54 +02:00
|
|
|
cloudimages = DotMap(yaml.safe_load(open("cloud-images.yaml").read()))
|
2022-08-21 01:35:03 +02:00
|
|
|
conf.password = sha512_crypt.hash(conf.password, rounds=4096)
|
2022-08-20 00:14:02 +02:00
|
|
|
config = pulumi.Config()
|
|
|
|
|
2023-12-29 20:38:11 +01:00
|
|
|
pool = lv.Pool("mypool", type="dir", path=conf.poolpath)
|
|
|
|
baseimg = lv.Volume("myimage", pool=pool.name, source=cloudimages[conf.image])
|
2022-08-20 00:14:02 +02:00
|
|
|
|
2022-08-21 01:35:03 +02:00
|
|
|
volume = lv.Volume("mydisk",
|
2023-12-29 20:38:11 +01:00
|
|
|
base_volume_id=baseimg.id,
|
|
|
|
pool=pool.name,
|
|
|
|
size=int(conf.disksize) * 1024 ** 3
|
|
|
|
)
|
2022-08-20 00:14:02 +02:00
|
|
|
|
|
|
|
# Stream configuration files
|
2022-08-21 01:35:03 +02:00
|
|
|
userdata = Template(open("./cloud-init/user-data.jinja").read()).render(conf)
|
|
|
|
metadata = Template(open("./cloud-init/meta-data.jinja").read()).render(conf)
|
|
|
|
networkconfig = open("./cloud-init/network-config").read()
|
2022-08-20 00:14:02 +02:00
|
|
|
|
|
|
|
cloudinit = lv.CloudInitDisk("cloud-init",
|
2023-12-29 20:38:11 +01:00
|
|
|
meta_data=metadata,
|
|
|
|
user_data=userdata,
|
|
|
|
network_config=networkconfig
|
|
|
|
)
|
2022-08-20 00:14:02 +02:00
|
|
|
|
2022-08-21 01:35:03 +02:00
|
|
|
vm = lv.Domain(conf.name,
|
2023-12-29 20:38:11 +01:00
|
|
|
boot_devices=[lv.DomainBootDeviceArgs(devs=["hd", "cdrom"])],
|
|
|
|
cloudinit=cloudinit.id,
|
|
|
|
vcpu=conf.vcpu,
|
|
|
|
disks=[lv.DomainDiskArgs(volume_id=volume.id)],
|
|
|
|
memory=int(conf.ram),
|
|
|
|
network_interfaces=[lv.DomainNetworkInterfaceArgs(
|
|
|
|
network_name="default",
|
|
|
|
wait_for_lease=True
|
|
|
|
)
|
|
|
|
],
|
|
|
|
consoles=[lv.DomainConsoleArgs(
|
|
|
|
type="pty",
|
|
|
|
target_port='0',
|
|
|
|
target_type="serial"
|
|
|
|
)],
|
|
|
|
opts=pulumi.ResourceOptions(
|
|
|
|
custom_timeouts=pulumi.CustomTimeouts(create="3m")
|
|
|
|
)
|
|
|
|
)
|
2022-08-20 00:14:02 +02:00
|
|
|
|
|
|
|
pulumi.export("ip", vm.network_interfaces[0]['addresses'][0])
|