2022-08-20 00:45:47 +02:00
|
|
|
"""A Pulumi-based infrastructure with libvirt and Python"""
|
|
|
|
|
|
|
|
import pulumi
|
|
|
|
import pulumi_libvirt as lv
|
|
|
|
import pulumi_command as command
|
2022-08-20 01:29:44 +02:00
|
|
|
import yaml
|
|
|
|
from dotmap import DotMap
|
|
|
|
from jinja2 import Template
|
2022-08-20 01:49:13 +02:00
|
|
|
from passlib.hash import sha512_crypt
|
2022-08-20 01:29:44 +02:00
|
|
|
|
|
|
|
conf = DotMap(yaml.safe_load(open("./input.yaml", "r").read()))
|
2023-07-03 21:05:54 +02:00
|
|
|
cloudimages = DotMap(yaml.safe_load(open('cloud-images.yaml').read()))
|
2022-08-20 01:29:44 +02:00
|
|
|
|
|
|
|
# Replacing readed password with its own counterpart.
|
|
|
|
conf.password = sha512_crypt.hash(conf.password, rounds=4096)
|
2022-08-20 00:45:47 +02:00
|
|
|
|
|
|
|
config = pulumi.Config()
|
|
|
|
|
2022-08-20 01:29:44 +02:00
|
|
|
pool = lv.Pool("pool",
|
2022-08-20 00:45:47 +02:00
|
|
|
type = "dir",
|
2022-08-20 01:29:44 +02:00
|
|
|
path = conf.poolpath
|
2022-08-20 00:45:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
baseimg = lv.Volume("base-image",
|
|
|
|
pool = pool.name,
|
2023-07-03 21:05:54 +02:00
|
|
|
source = cloudimages[conf.image]
|
2022-08-20 00:45:47 +02:00
|
|
|
#source = "https://cdimage.debian.org/images/cloud/bullseye-backports/20220711-1073/debian-11-backports-generic-amd64-20220711-1073.qcow2"
|
|
|
|
)
|
|
|
|
|
|
|
|
volume = lv.Volume("disk",
|
|
|
|
base_volume_id = baseimg.id,
|
|
|
|
pool = pool.name,
|
2022-08-20 01:29:44 +02:00
|
|
|
size = int(conf.disksize) * 1024 ** 3
|
2022-08-20 00:45:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# Stream configuration files
|
2022-08-20 01:29:44 +02:00
|
|
|
userdata = Template(open("./cloud-init/user-data.jinja", "r").read()).render(conf)
|
|
|
|
metadata = Template(open("./cloud-init/meta-data.jinja", "r").read()).render(conf)
|
2022-08-20 00:45:47 +02:00
|
|
|
networkconfig = open("./cloud-init/network-config", "r").read()
|
|
|
|
|
|
|
|
cloudinit = lv.CloudInitDisk("cloud-init",
|
|
|
|
meta_data = metadata,
|
|
|
|
user_data = userdata,
|
|
|
|
network_config = networkconfig
|
|
|
|
)
|
|
|
|
|
2022-08-20 02:06:34 +02:00
|
|
|
vm = lv.Domain(conf.name,
|
2022-08-20 00:45:47 +02:00
|
|
|
boot_devices = [ lv.DomainBootDeviceArgs(
|
|
|
|
devs = [ "hd", "cdrom" ]
|
|
|
|
) ],
|
|
|
|
cloudinit = cloudinit.id,
|
2022-08-24 13:49:12 +02:00
|
|
|
vcpu = conf.vcpu,
|
2022-08-20 00:45:47 +02:00
|
|
|
disks = [ lv.DomainDiskArgs(volume_id = volume.id) ],
|
2022-08-20 01:29:44 +02:00
|
|
|
memory = int(conf.ram),
|
2022-08-20 00:45:47 +02:00
|
|
|
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")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Creating the inventory file
|
|
|
|
inventory = command.local.Command("a-inventory",
|
|
|
|
create = vm.network_interfaces[0]['addresses'][0].apply(
|
2022-08-20 01:29:44 +02:00
|
|
|
lambda ipaddr: f"echo '{conf.name} ansible_host={ipaddr} ansible_user={conf.username}' >./inventory"
|
2022-08-20 00:45:47 +02:00
|
|
|
),
|
|
|
|
delete = "rm -f ./inventory",
|
|
|
|
opts = pulumi.ResourceOptions(depends_on = [ vm ])
|
|
|
|
)
|
|
|
|
|
|
|
|
# Applying the command
|
|
|
|
execute_ansible = command.local.Command("a-deploy",
|
2022-08-20 01:29:44 +02:00
|
|
|
create = f"ansible-playbook {conf.ansible_playbook}",
|
2022-08-20 00:45:47 +02:00
|
|
|
delete = "rm -f ./ansible.log",
|
|
|
|
opts = pulumi.ResourceOptions(depends_on = [ inventory ])
|
|
|
|
)
|
|
|
|
|
|
|
|
pulumi.export("ip", vm.network_interfaces[0]['addresses'][0])
|