pulumi-templates/libvirt-python/__main__.py

57 lines
1.6 KiB
Python

"""A Pulumi deployment with libvirt and Python"""
import yaml
from dotmap import DotMap
import pulumi
import pulumi_libvirt as lv
from passlib.hash import sha512_crypt
from jinja2 import Template
conf = DotMap(yaml.safe_load(open("input.yaml").read()))
conf.password = sha512_crypt.hash(conf.password, rounds=4096)
config = pulumi.Config()
pool = lv.Pool("mypool", type = "dir", path = conf.poolpath)
baseimg = lv.Volume("myimage", pool = pool.name, source = conf.image_url)
volume = lv.Volume("mydisk",
base_volume_id = baseimg.id,
pool = pool.name,
size = int(conf.disksize) * 1024 ** 3
)
# Stream configuration files
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()
cloudinit = lv.CloudInitDisk("cloud-init",
meta_data = metadata,
user_data = userdata,
network_config = networkconfig
)
vm = lv.Domain(conf.name,
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")
)
)
pulumi.export("ip", vm.network_interfaces[0]['addresses'][0])