85 lines
2.5 KiB
Python
85 lines
2.5 KiB
Python
"""A Pulumi-based infrastructure with libvirt and Python"""
|
|
|
|
import pulumi
|
|
import pulumi_libvirt as lv
|
|
import pulumi_command as command
|
|
import yaml
|
|
from dotmap import DotMap
|
|
from jinja2 import Template
|
|
from passlib.hash import sha512_crypt
|
|
|
|
conf = DotMap(yaml.safe_load(open("./input.yaml", "r").read()))
|
|
|
|
# Replacing readed password with its own counterpart.
|
|
conf.password = sha512_crypt.hash(conf.password, rounds=4096)
|
|
|
|
config = pulumi.Config()
|
|
|
|
pool = lv.Pool("pool",
|
|
type = "dir",
|
|
path = conf.poolpath
|
|
)
|
|
|
|
baseimg = lv.Volume("base-image",
|
|
pool = pool.name,
|
|
source = conf.image_url
|
|
#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,
|
|
size = int(conf.disksize) * 1024 ** 3
|
|
)
|
|
|
|
# Stream configuration files
|
|
userdata = Template(open("./cloud-init/user-data.jinja", "r").read()).render(conf)
|
|
metadata = Template(open("./cloud-init/meta-data.jinja", "r").read()).render(conf)
|
|
networkconfig = open("./cloud-init/network-config", "r").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")
|
|
)
|
|
)
|
|
|
|
# Creating the inventory file
|
|
inventory = command.local.Command("a-inventory",
|
|
create = vm.network_interfaces[0]['addresses'][0].apply(
|
|
lambda ipaddr: f"echo '{conf.name} ansible_host={ipaddr} ansible_user={conf.username}' >./inventory"
|
|
),
|
|
delete = "rm -f ./inventory",
|
|
opts = pulumi.ResourceOptions(depends_on = [ vm ])
|
|
)
|
|
|
|
# Applying the command
|
|
execute_ansible = command.local.Command("a-deploy",
|
|
create = f"ansible-playbook {conf.ansible_playbook}",
|
|
delete = "rm -f ./ansible.log",
|
|
opts = pulumi.ResourceOptions(depends_on = [ inventory ])
|
|
)
|
|
|
|
pulumi.export("ip", vm.network_interfaces[0]['addresses'][0])
|