~tieong/pulumi-kimsufi

6d18c849ade515c55228e14bf798eb23e1e969cd — Thomas Ieong 1 year, 1 month ago 1482c28
Added a higher level component resource.
2 files changed, 159 insertions(+), 83 deletions(-)

M __main__.py
A group.py
M __main__.py => __main__.py +23 -83
@@ 1,98 1,38 @@
"""A Libvirt Python Pulumi program"""

from pathlib import Path
import vm
import group

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

import pulumi_command as command
import pulumi_libvirt as libvirt

import pulumi
def domains_use_1GB_memory(args: ResourceTransformationArgs):
    # only modify resources that are a Domain type
    if args.type_ == "libvirt:index/domain:Domain":
        modified_args = args.props
        modified_args.memory = 4096

# A pool for all cluster volumes
cluster = libvirt.Pool(
    "cluster",
    type="dir",
    path="/home/user/cluster_storage",
    opts=pulumi.ResourceOptions(
        delete_before_replace=True,
    ),
)
        log.info(str(modified_args))

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,
    ),
        return ResourceTransformationResult(props=modified_args, opts=args.opts)

)

ubuntu = libvirt.Volume(
vmGroup = group.new_vm_group(
    "ubuntu",
    pool=cluster.name,
    source=(
    "/home/user/cluster_storage",
    (
        "https://cloud-images.ubuntu.com"
        "/releases/focal/release"
        "/ubuntu-20.04-server-cloudimg-amd64.img"
    ),
    "192.168.10.0/24",
    1,
    ResourceOptions(transformations=[domains_use_1GB_memory]),
)

cloud_init_network_config = """\
version: 2
ethernets:
  ens3:
    dhcp4: true
"""

HOSTNAME = "test"
FQDN = HOSTNAME + "." + "example.com"

cloud_init_user_data = f"""\
#cloud-config
disable_root: false
ssh_pwauth: True
chpasswd:
  list: |
        ubuntu:ubuntu
  expire: False
packages:
    - qemu-guest-agent
    - python3

hostname: "{HOSTNAME}"
fqdn: "{FQDN}"
final_message: "The system is finall up, after $UPTIME seconds"

power_state:
  delay: "+30"
  mode: reboot
  message: Bye Bye
  timeout: 30
  condition: True
"""


cloud_init = libvirt.CloudInitDisk(
    "cloud-init",
    libvirt.CloudInitDiskArgs(
        network_config=cloud_init_network_config,
        pool=cluster.name,
        user_data=cloud_init_user_data,
    ),
)

vm_outputs = vm.new_vm(
    "ubuntu",
    cluster.name,
    ubuntu.id,
    cloud_init.id,
    network.id
)
pulumi.export("IP Address", vm_outputs.ip)
pulumi.export("VM name", vm_outputs.name)
export("VMs", vmGroup.vms)

A group.py => group.py +136 -0
@@ 0,0 1,136 @@
from typing import Optional

from vm import new_vm

import pulumi_command as command
import pulumi_libvirt as libvirt
from pulumi import (
    ResourceOptions,
    ResourceTransformationArgs,
    ResourceTransformationResult,
    ComponentResource,
    export,
)


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


def new_vm_group(
    groupName: str,
    hostStoragePoolPath: str,
    vmImageSource: str,
    ipCIDR: str,
    numberOfVMs: int,
    opts: Optional[ResourceOptions] = None,
):
    resource = VMGroup(groupName)

    # A pool for all cluster volumes
    cluster = libvirt.Pool(
        "cluster",
        type="dir",
        path=hostStoragePoolPath,
        opts=ResourceOptions(
            parent=resource,
            delete_before_replace=True,
        ),
    )

    network = libvirt.Network(
        "network",
        libvirt.NetworkArgs(
            addresses=[ipCIDR],
            mode="nat",
            autostart=True,
            bridge="virbr0",
            dhcp=libvirt.NetworkDhcpArgs(enabled=True),
        ),
        opts=ResourceOptions(
            parent=resource,
            delete_before_replace=True,
        ),
    )

    ubuntu = libvirt.Volume(
        "ubuntu",
        pool=cluster.name,
        source=vmImageSource,
        opts=ResourceOptions(
            parent=resource,
        ),
    )

    cloud_init_network_config = """\
version: 2
ethernets:
  ens3:
    dhcp4: true
"""

    HOSTNAME = "test"
    FQDN = HOSTNAME + "." + "example.com"

    cloud_init_user_data = f"""\
#cloud-config
disable_root: false
ssh_pwauth: True
chpasswd:
  list: |
        ubuntu:ubuntu
  expire: False
packages:
    - qemu-guest-agent
    - python3

hostname: "{HOSTNAME}"
fqdn: "{FQDN}"
final_message: "The system is finall up, after $UPTIME seconds"

power_state:
  delay: "+30"
  mode: reboot
  message: Bye Bye
  timeout: 30
  condition: True
"""

    cloud_init = libvirt.CloudInitDisk(
        "cloud-init",
        libvirt.CloudInitDiskArgs(
            network_config=cloud_init_network_config,
            pool=cluster.name,
            user_data=cloud_init_user_data,
        ),
        opts=ResourceOptions(
            parent=resource,
        ),
    )

    vmOutputs = []

    for i in range(numberOfVMs):
        vm_name = f"{groupName}-{i}"

        vm = new_vm(
            vm_name,
            cluster.name,
            ubuntu.id,
            cloud_init.id,
            network.id,
            ResourceOptions(
                parent=resource,
            ),
        )

        vmOutputs.append({"ip": vm.ip, "name": vm.name})

    resource.name = groupName
    resource.vms = vmOutputs

    resource.register_outputs({"name": groupName, "vms": vmOutputs})

    return resource