~tieong/pulumi-kimsufi

b763de7bea9341f50fd53b2abe3ee6c8796e5f44 — Thomas Ieong 1 year, 1 month ago c7160d6
Changed config to suit my infra
3 files changed, 54 insertions(+), 55 deletions(-)

M __main__.py
M group.py
M vm.py
M __main__.py => __main__.py +12 -23
@@ 1,35 1,24 @@
"""A Libvirt Python Pulumi program"""

import group
from vm import Vm

from pulumi import (
    ResourceOptions,
    ResourceTransformationArgs,
    ResourceTransformationResult,
    export,
    log,
    runtime,
)

# When I have the time I will add CI/CD, pastebin ...

def domains_use_1GB_memory(
    args: ResourceTransformationArgs,
) -> ResourceTransformationResult:
    # only modify resources that are a Domain type
    if args.type_ == "libvirt:index/domain:Domain":
        args.props["memory"] = 1024
        return ResourceTransformationResult(props=args.props, opts=args.opts)


vmGroup = group.new_vm_group(
    "guix",
vm_group = group.new_vm_group(
    "pool1",
    "/home/user/cluster_storage",
    (
        "/home/user/projects/kimsufi-infra/guix-images/router.qcow2"
    ),
    ["192.168.15.0/24"],
    1,
    ResourceOptions(transformations=[domains_use_1GB_memory]),
    ["192.168.1.0/24"],
    [
        Vm(
            "web",
            "/home/user/projects/kimsufi-infra/guix-images/web.qcow2",
            "192.168.1.10",
        ),
    ]
)

export("VMs", vmGroup.vms)

M group.py => group.py +19 -19
@@ 1,6 1,6 @@
from typing import Optional, List

from vm import new_vm
from vm import new_vm, Vm

import pulumi_command as command
import pulumi_libvirt as libvirt


@@ 22,15 22,14 @@ class VMGroup(ComponentResource):
def new_vm_group(
    group_name: str,
    pool_path: str,
    image: str,
    addresses: List[str],
    vm_num: int,
    vms: List[Vm],
    opts: Optional[ResourceOptions] = None,
):
    resource = VMGroup(group_name, opts=opts)

    cluster = libvirt.Pool(
        "guix-pool",
        "default-pool",
        type="dir",
        path=pool_path,
        opts=ResourceOptions(


@@ 43,7 42,8 @@ def new_vm_group(
        "default",
        libvirt.NetworkArgs(
            addresses=addresses,
            mode="nat",
            #mode="nat",
            mode="open",
            autostart=True,
            bridge="virbr0",
            dhcp=libvirt.NetworkDhcpArgs(enabled=True),


@@ 54,26 54,26 @@ def new_vm_group(
        ),
    )

    guix = libvirt.Volume(
        "guix-qcow2-base",
        pool=cluster.name,
        source=image,
        format="qcow2",
        opts=ResourceOptions(
            parent=resource,
        ),
    )

    vm_outputs = []

    for i in range(vm_num):
        vm_name = f"{group_name}-{i}"
    for v in vms:
        vm_base = libvirt.Volume(
            f"{v.name}-qcow2-base",
            pool=cluster.name,
            source=v.source,
            format="qcow2",
            opts=ResourceOptions(
                parent=resource,
            ),
        )

        vm_name = f"{group_name}-{v.name}"

        vm = new_vm(
            vm_name,
            cluster.name,
            guix.id,
            vm_base.id,
            network.id,
            v,
            opts=ResourceOptions(
                parent=resource,
            ),

M vm.py => vm.py +23 -13
@@ 5,37 5,45 @@ from pulumi import Output, ResourceOptions, ComponentResource
import pulumi_libvirt as libvirt


class VM(ComponentResource):
def gigabytes_to_bytes(gigabytes: int):
    return gigabytes * 1_000_000_000


@dataclass
class Vm:
    """jkl"""
    name: str
    source: str
    address: str
    size: int = gigabytes_to_bytes(11)


class VmComponent(ComponentResource):
    def __init__(self, name: str, opts: ResourceOptions = None):
        # Change pkg something something to my state
        super().__init__("kimsufi-infra:pkg/vm:vm", name, None, opts)


def gigabytes_to_bytes(gigabytes: int):
    return gigabytes * 1_000_000_000


def new_vm(
    name: str,
    pool_name: Output[str],
    base_volume_id: Output[str],
    network_id: Output[str],
    size: int = gigabytes_to_bytes(11),
    vm: Vm,
    opts: Optional[ResourceOptions] = None,
) -> VM:
    resource = VM(name, opts=opts)
) -> VmComponent:
    resource = VmComponent(vm.name, opts=opts)

    filesystem = libvirt.Volume(
        f"{name}-filesystem",
        f"{vm.name}-filesystem",
        base_volume_id=base_volume_id,
        pool=pool_name,
        size=size,
        size=vm.size,
        format="qcow2",
        opts=ResourceOptions(parent=resource),
    )

    domain = libvirt.Domain(
        f"{name}-domain",
        f"{vm.name}-domain",
        autostart=True,
        # Don't really know what's happening when set to True
        # but the addresses field is empty.


@@ 52,7 60,9 @@ def new_vm(
        disks=[libvirt.DomainDiskArgs(volume_id=filesystem.id)],
        network_interfaces=[
            libvirt.DomainNetworkInterfaceArgs(
                network_id=network_id, wait_for_lease=True
                network_id=network_id,
                wait_for_lease=False,
                addresses=[vm.address],
            )
        ],
        opts=ResourceOptions(