87 lines
2.5 KiB
Python
87 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 pulumiverse_time as time
|
|
import additional_configs as add
|
|
from jinja2 import Template
|
|
from passlib.hash import sha512_crypt
|
|
|
|
config = pulumi.Config()
|
|
|
|
env = {
|
|
'username': config.get('username'),
|
|
'password': config.get('password'),
|
|
'sshkeys': add.SSH_KEYS,
|
|
'name': config.get('hostname'),
|
|
}
|
|
|
|
pool = lv.Pool("pool",
|
|
type = "dir",
|
|
path = config.get('poolpath')
|
|
)
|
|
|
|
baseimg = lv.Volume("base-image",
|
|
pool = pool.name,
|
|
source = add.CLOUD_IMAGES[config.get('image')]
|
|
)
|
|
|
|
volume = lv.Volume("disk",
|
|
base_volume_id = baseimg.id,
|
|
pool = pool.name,
|
|
size = int(config.get('disksize')) * 1024 ** 3
|
|
)
|
|
|
|
# Stream configuration files
|
|
cloudinit = lv.CloudInitDisk("cloud-init",
|
|
meta_data = Template(open("./templates/meta-data.jinja", "r").read()).render(env),
|
|
user_data = Template(open("./templates/user-data.jinja", "r").read()).render(env),
|
|
network_config = open("./templates/network-config", "r").read()
|
|
)
|
|
|
|
vm = lv.Domain(config.get('hostname'),
|
|
boot_devices = [ lv.DomainBootDeviceArgs(
|
|
devs = [ "hd", "cdrom" ]
|
|
) ],
|
|
cloudinit = cloudinit.id,
|
|
vcpu = config.get('vcpu'),
|
|
disks = [ lv.DomainDiskArgs(volume_id = volume.id, scsi=True) ],
|
|
memory = int(config.get('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")
|
|
)
|
|
)
|
|
|
|
wait_time = time.Sleep("wait_time", create_duration="30s",
|
|
opts = pulumi.ResourceOptions(depends_on = [ vm ])
|
|
)
|
|
|
|
# Creating the inventory file
|
|
inventory = command.local.Command("a-inventory",
|
|
create = vm.network_interfaces[0]['addresses'][0].apply(
|
|
lambda ipaddr: f"echo '{env['name']} ansible_host={ipaddr} ansible_user={env['username']}' >./inventory"
|
|
),
|
|
delete = "rm -f ./inventory",
|
|
opts = pulumi.ResourceOptions(depends_on = [ wait_time ])
|
|
)
|
|
|
|
# Applying the command
|
|
for playbook in config.get('playbooks').split(','):
|
|
execute_ansible = command.local.Command(f"a-{playbook.split('.')[0]}",
|
|
create = f"ansible-playbook {playbook}",
|
|
delete = "rm -f ./ansible.log",
|
|
opts = pulumi.ResourceOptions(depends_on = [ inventory ])
|
|
)
|
|
|
|
pulumi.export("ip", vm.network_interfaces[0]['addresses'][0])
|