~tieong/pulumi-kimsufi

706dde89e9a5f47943eed6651321f0951190d08d — Thomas Ieong 1 year, 1 month ago 7a7e83c
Created a vm module
2 files changed, 80 insertions(+), 46 deletions(-)

M __main__.py
A vm.py
M __main__.py => __main__.py +24 -46
@@ 1,6 1,7 @@
"""A Libvirt Python Pulumi program"""

from pathlib import Path
import vm

import pulumi_command as command
import pulumi_libvirt as libvirt


@@ 17,6 18,21 @@ cluster = libvirt.Pool(
    ),
)

network = libvirt.Network(
    "network",
    libvirt.NetworkArgs(
        addresses=["192.168.10.0/24"],
        mode="nat",
        autostart=True,
        bridge="virbr0",
        dhcp=libvirt.NetworkDhcpArgs(enabled=True),
    ),
    opts=pulumi.ResourceOptions(
        delete_before_replace=True,
    ),

)

ubuntu = libvirt.Volume(
    "ubuntu",
    pool=cluster.name,


@@ 27,13 43,6 @@ ubuntu = libvirt.Volume(
    ),
)

filesystem = libvirt.Volume(
    "filesystem",
    base_volume_id=ubuntu.id,
    pool=cluster.name,
    size=10000000000,
)

cloud_init_network_config = """\
version: 2
ethernets:


@@ 68,20 77,6 @@ power_state:
  condition: True
"""

network = libvirt.Network(
    "network",
    libvirt.NetworkArgs(
        addresses=["192.168.10.0/24"],
        mode="nat",
        autostart=True,
        bridge="virbr0",
        dhcp=libvirt.NetworkDhcpArgs(enabled=True),
    ),
    opts=pulumi.ResourceOptions(
        delete_before_replace=True,
    ),

)

cloud_init = libvirt.CloudInitDisk(
    "cloud-init",


@@ 92,29 87,12 @@ cloud_init = libvirt.CloudInitDisk(
    ),
)

domain = libvirt.Domain(
    "default",
    cloudinit=cloud_init.id,
    autostart=True,
    consoles=[
        libvirt.DomainConsoleArgs(
            type="pty",
            target_port="0",
            target_type="serial"
        )
    ],
    disks=[libvirt.DomainDiskArgs(volume_id=filesystem.id)],
    network_interfaces=[
        libvirt.DomainNetworkInterfaceArgs(
            network_id=network.id,
            wait_for_lease=True
        )
    ],
    opts=pulumi.ResourceOptions(
        delete_before_replace=True,
        replace_on_changes=["*"],
    ),
vm_outputs = vm.new_vm(
    "ubuntu",
    cluster.name,
    ubuntu.id,
    cloud_init.id,
    network.id
)

pulumi.export("IP Address", domain.network_interfaces[0].addresses[0])
pulumi.export("VM name", domain.name)
pulumi.export("IP Address", vm_outputs["IP Address"])
pulumi.export("VM name", vm_outputs["VM name"])

A vm.py => vm.py +56 -0
@@ 0,0 1,56 @@
from typing import Optional

from pulumi import Output
import pulumi
import pulumi_libvirt as libvirt

def new_vm(
        name: str,
        pool_name: Output[str],
        base_volume_id: Output[str],
        cloud_init_disk_id: Output[str],
        network_id: Output[str],
        opts: Optional[pulumi.ResourceOptions] = None,
):
    # return info about the newly created VM
    outputs = {}

    # create a filesystem volume for our VM
    # This filesystem will be based on the `ubuntu` volume above
    # we'll use a size of 10GB

    filesystem = libvirt.Volume(
        f"{name}-filesystem",
        base_volume_id=base_volume_id,
        pool=pool_name,
        size=10000000000,
    )
    # create a VM that has a name starting with ubuntu
    domain = libvirt.Domain(
        f"{name}-domain",
        cloudinit=cloud_init_disk_id,
        autostart=True,
        consoles=[
            libvirt.DomainConsoleArgs(
                type="pty",
                target_port="0",
                target_type="serial"
            )
        ],
        disks=[libvirt.DomainDiskArgs(volume_id=filesystem.id)],
        network_interfaces=[
            libvirt.DomainNetworkInterfaceArgs(
                network_id=network_id,
                wait_for_lease=True
            )
        ],
        opts=pulumi.ResourceOptions(
            delete_before_replace=True,
            replace_on_changes=["*"],
        ),
    )

    outputs["IP Address"] = domain.network_interfaces[0].addresses[0]
    outputs["VM name"] = domain.name

    return outputs