From bdc7d1bd036c862a1310952bc5cb79bde909e187 Mon Sep 17 00:00:00 2001 From: Mathieu Broillet Date: Wed, 21 Jun 2023 12:17:15 +0200 Subject: [PATCH] update create/set pct command --- src/lxc/lxc.py | 3 +- src/lxc/lxc_utils.py | 116 ++++++++++++++++++++++++++++++++----------- 2 files changed, 88 insertions(+), 31 deletions(-) diff --git a/src/lxc/lxc.py b/src/lxc/lxc.py index 12487f6..68f0318 100644 --- a/src/lxc/lxc.py +++ b/src/lxc/lxc.py @@ -9,7 +9,7 @@ class LXC(LinuxMachine): """LXC object""" def __init__(self, lxc_id: int, lxc_hostname: str, os: dict, resources: dict, network: dict, options: dict, - creation: dict, deploy: dict, proxmox_host: ProxmoxHost): + creation: dict, deploy: dict, features: dict, proxmox_host: ProxmoxHost): super().__init__() self.lxc_id = lxc_id @@ -44,6 +44,7 @@ class LXC(LinuxMachine): self.creation = creation self.deploy = deploy + self.features = features self.pve = proxmox_host diff --git a/src/lxc/lxc_utils.py b/src/lxc/lxc_utils.py index 8a3238f..d4463b5 100644 --- a/src/lxc/lxc_utils.py +++ b/src/lxc/lxc_utils.py @@ -37,16 +37,17 @@ def load_lxc(lxc_id: int, content: str or bytes, pve: ProxmoxHost): # Extract values from JSON lxc_id = lxc_id - lxc_hostname = data["lxc_hostname"] - os = data["os"] - resources = data["resources"] - network = data["network"] - options = data["options"] - creation = data["creation"] - deploy = data["deploy"] + lxc_hostname = data.get("lxc_hostname") + os = data.get("os") + resources = data.get("resources") + network = data.get("network") + options = data.get("options") + creation = data.get("creation") + deploy = data.get("deploy") + features = data.get("features") # Create LXC object - lxc = LXC(lxc_id, lxc_hostname, os, resources, network, options, creation, deploy, pve) + lxc = LXC(lxc_id, lxc_hostname, os, resources, network, options, creation, deploy, features, pve) lxcs.append(lxc) @@ -89,32 +90,87 @@ def generate_pct_command_for_lxc(lxc: LXC, create: bool = True): :return: pct command """ + # Common parameters for both create and update commands + common_params = [ + f"--hostname {lxc.get_hostname()}", + f"--cores {lxc.get_cpu()}", + f"--memory {lxc.get_memory()}", + f"--swap {lxc.get_memory()}", + f"--onboot {int(lxc.is_start_on_boot())}", + f"--ostype {lxc.get_os_name()}", + ] + + # Check and include specific net0 parameters + net0_params = [] + if lxc.network.get("name") and lxc.network.get("name") != "": + net0_params.append(f"name={lxc.network['name']}") + if lxc.network.get("bridge") and lxc.network.get("bridge") != "": + net0_params.append(f"bridge={lxc.network['bridge']}") + if lxc.network.get("firewall" and lxc.network.get("firewall") != ""): + net0_params.append(f"firewall={lxc.network['firewall']}") + if lxc.network.get("gw") and lxc.network.get("gw") != "": + net0_params.append(f"gw={lxc.network['gw']}") + if lxc.network.get("gw6") and lxc.network.get("gw6") != "": + net0_params.append(f"gw6={lxc.network['gw6']}") + if lxc.network.get("hwaddr") and lxc.network.get("hwaddr") != "": + net0_params.append(f"hwaddr={lxc.network['hwaddr']}") + if lxc.network.get("ip") and lxc.network.get("ip") != "": + net0_params.append(f"ip={lxc.get_ipv4(netmask=True)}") + if lxc.network.get("ip6") and lxc.network.get("ip6") != "": + net0_params.append(f"ip6={lxc.get_ipv6()}") + if lxc.network.get("link_down") and lxc.network.get("link_down") != "": + net0_params.append(f"link_down={lxc.network['link_down']}") + if lxc.network.get("mtu") and lxc.network.get("mtu") != "": + net0_params.append(f"mtu={lxc.network['mtu']}") + if lxc.network.get("rate") and lxc.network.get("rate") != "": + net0_params.append(f"rate={lxc.network['rate']}") + if lxc.network.get("tag") and lxc.network.get("tag") != "": + net0_params.append(f"tag={lxc.network['tag']}") + if lxc.network.get("trunks") and lxc.network.get("trunks") != "": + net0_params.append(f"trunks={lxc.network['trunks']}") + if lxc.network.get("type") and lxc.network.get("type") != "": + net0_params.append(f"type={lxc.network['type']}") + + if net0_params: + common_params.append(f"--net0 {','.join(net0_params)}") + if create: + # Additional parameters for create command + create_params = [ + f"--password {lxc.get_password()}", + f"--storage {lxc.get_storage()}", + f"--unprivileged {not lxc.is_privileged()}", + f"--rootfs volume={lxc.get_storage()}:{lxc.get_disk()},size={lxc.get_disk()}", + "--ssh-public-keys /root/.ssh/id_rsa.pub", + ] + + # Check and include specific features based on their values + features = [] + if lxc.features.get("force_rw_sys") and lxc.features.get("force_rw_sys") != "": + features.append(f"force_rw_sys={lxc.features['force_rw_sys']}") + if lxc.features.get("fuse") and lxc.features.get("fuse") != "": + features.append(f"fuse={lxc.features['fuse']}") + if lxc.features.get("keyctl") and lxc.features.get("keyctl") != "": + features.append(f"keyctl={lxc.features['keyctl']}") + if lxc.features.get("mknod") and lxc.features.get("mknod") != "": + features.append(f"mknod={lxc.features['mknod']}") + if lxc.features.get("mount") and lxc.features.get("mount") != "": + features.append(f"mount={';'.join(lxc.features['mount'])}") + if lxc.features.get("nesting") and lxc.features.get("nesting") != "": + features.append(f"nesting={lxc.features['nesting']}") + + if features: + create_params.append(f"--features {','.join(features)}") + + # Combine common and create-specific parameters + command_params = common_params + create_params # Create command - pct_command = f"pct create {lxc.get_id()} {lxc.get_os_template()} " \ - f"--hostname {lxc.get_hostname()} " \ - f"--cores {lxc.get_cpu()} " \ - f"--memory {lxc.get_memory()} " \ - f"--swap {lxc.get_swap()} " \ - f"--net0 name=eth0,bridge={lxc.get_bridge()},ip={lxc.get_ipv4(netmask=True)},hwaddr={lxc.get_mac()},type=veth " \ - f"--onboot {int(lxc.is_start_on_boot())} " \ - f"--ostype {lxc.get_os_name()} " \ - f"--password {lxc.get_password()} " \ - f"--storage {lxc.get_storage()} " \ - f"--unprivileged {not lxc.is_privileged()} " \ - f"--rootfs volume={lxc.get_storage()}:{lxc.get_disk()},size={lxc.get_disk()} " \ - f"--ssh-public-keys /root/.ssh/id_rsa.pub " \ - f"--unprivileged {not lxc.is_privileged()}" + pct_command = f"pct create {lxc.get_id()} {lxc.get_os_template()} {' '.join(command_params)}" else: # Update command - pct_command = f"pct set {lxc.get_id()} " \ - f"--hostname {lxc.get_hostname()} " \ - f"--cores {lxc.get_cpu()} " \ - f"--memory {lxc.get_memory()} " \ - f"--swap {lxc.get_memory()} " \ - f"--net0 name=eth0,bridge={lxc.get_bridge()},ip={lxc.get_ipv4(netmask=True)},hwaddr={lxc.get_mac()},type=veth " \ - f"--onboot {int(lxc.is_start_on_boot())} " \ - f"--ostype {lxc.get_os_name()} " + # Combine common parameters only + command_params = common_params + pct_command = f"pct set {lxc.get_id()} {' '.join(command_params)}" # TODO: add gateway4 # f"ip6={self.ipv6},gw6={self.gateway6},trunks={self.vlan} " \ # TODO