"""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 import ansible 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 ]) ) adeploy = ansible.AnsibleDeployment(vm, env, config.get('playbooks')) adeploy.create_inventory([ wait_time ]) adeploy.run_playbooks() pulumi.export("ip", vm.network_interfaces[0]['addresses'][0])