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
|
2024-09-19 19:14:13 +02:00
|
|
|
import pulumiverse_time as time
|
|
|
|
import additional_configs as add
|
2022-08-20 01:29:44 +02:00
|
|
|
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
|
|
|
|
2022-08-20 00:45:47 +02:00
|
|
|
config = pulumi.Config()
|
|
|
|
|
2024-09-19 19:14:13 +02:00
|
|
|
env = {
|
|
|
|
'username': config.get('username'),
|
|
|
|
'password': config.get('password'),
|
|
|
|
'sshkeys': add.SSH_KEYS,
|
|
|
|
'name': config.get('hostname'),
|
|
|
|
}
|
|
|
|
|
2022-08-20 01:29:44 +02:00
|
|
|
pool = lv.Pool("pool",
|
2022-08-20 00:45:47 +02:00
|
|
|
type = "dir",
|
2024-09-19 19:14:13 +02:00
|
|
|
path = config.get('poolpath')
|
2022-08-20 00:45:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
baseimg = lv.Volume("base-image",
|
|
|
|
pool = pool.name,
|
2024-09-19 19:14:13 +02:00
|
|
|
source = add.CLOUD_IMAGES[config.get('image')]
|
2022-08-20 00:45:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
volume = lv.Volume("disk",
|
|
|
|
base_volume_id = baseimg.id,
|
|
|
|
pool = pool.name,
|
2024-09-19 19:14:13 +02:00
|
|
|
size = int(config.get('disksize')) * 1024 ** 3
|
2022-08-20 00:45:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# Stream configuration files
|
|
|
|
cloudinit = lv.CloudInitDisk("cloud-init",
|
2024-09-21 18:22:48 +02:00
|
|
|
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()
|
2022-08-20 00:45:47 +02:00
|
|
|
)
|
|
|
|
|
2024-09-19 19:14:13 +02:00
|
|
|
vm = lv.Domain(config.get('hostname'),
|
2022-08-20 00:45:47 +02:00
|
|
|
boot_devices = [ lv.DomainBootDeviceArgs(
|
|
|
|
devs = [ "hd", "cdrom" ]
|
|
|
|
) ],
|
|
|
|
cloudinit = cloudinit.id,
|
2024-09-19 19:14:13 +02:00
|
|
|
vcpu = config.get('vcpu'),
|
2024-03-15 00:47:30 +01:00
|
|
|
disks = [ lv.DomainDiskArgs(volume_id = volume.id, scsi=True) ],
|
2024-09-19 19:14:13 +02:00
|
|
|
memory = int(config.get('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")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2024-09-19 19:14:13 +02:00
|
|
|
wait_time = time.Sleep("wait_time", create_duration="30s",
|
|
|
|
opts = pulumi.ResourceOptions(depends_on = [ vm ])
|
|
|
|
)
|
|
|
|
|
2022-08-20 00:45:47 +02:00
|
|
|
# Creating the inventory file
|
|
|
|
inventory = command.local.Command("a-inventory",
|
|
|
|
create = vm.network_interfaces[0]['addresses'][0].apply(
|
2024-09-19 19:14:13 +02:00
|
|
|
lambda ipaddr: f"echo '{env['name']} ansible_host={ipaddr} ansible_user={env['username']}' >./inventory"
|
2022-08-20 00:45:47 +02:00
|
|
|
),
|
|
|
|
delete = "rm -f ./inventory",
|
2024-09-19 19:14:13 +02:00
|
|
|
opts = pulumi.ResourceOptions(depends_on = [ wait_time ])
|
2022-08-20 00:45:47 +02:00
|
|
|
)
|
|
|
|
|
2024-09-21 18:24:47 +02:00
|
|
|
deploy = command.local.Command('ap deploy',
|
|
|
|
create = 'ansible-playbook deploy.yml',
|
|
|
|
delete = 'rm -f ./ansible.log',
|
|
|
|
opts = pulumi.ResourceOptions(depends_on = [ inventory ])
|
|
|
|
)
|
|
|
|
|
|
|
|
# Running additional playbooks
|
|
|
|
for playbook in sorted(config.get('playbooks').split(',')):
|
|
|
|
execute_ansible = command.local.Command(f"ap {playbook.split('.')[0]}",
|
2024-09-19 19:14:13 +02:00
|
|
|
create = f"ansible-playbook {playbook}",
|
|
|
|
delete = "rm -f ./ansible.log",
|
2024-09-21 18:24:47 +02:00
|
|
|
opts = pulumi.ResourceOptions(depends_on = [ deploy ])
|
2024-09-19 19:14:13 +02:00
|
|
|
)
|
2022-08-20 00:45:47 +02:00
|
|
|
|
|
|
|
pulumi.export("ip", vm.network_interfaces[0]['addresses'][0])
|