update create/set pct command
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Mathieu Broillet 2023-06-21 12:17:15 +02:00
parent e09f768736
commit bdc7d1bd03
No known key found for this signature in database
GPG Key ID: 7D4F25BC50A0AA32
2 changed files with 88 additions and 31 deletions

View File

@ -9,7 +9,7 @@ class LXC(LinuxMachine):
"""LXC object""" """LXC object"""
def __init__(self, lxc_id: int, lxc_hostname: str, os: dict, resources: dict, network: dict, options: dict, 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__() super().__init__()
self.lxc_id = lxc_id self.lxc_id = lxc_id
@ -44,6 +44,7 @@ class LXC(LinuxMachine):
self.creation = creation self.creation = creation
self.deploy = deploy self.deploy = deploy
self.features = features
self.pve = proxmox_host self.pve = proxmox_host

View File

@ -37,16 +37,17 @@ def load_lxc(lxc_id: int, content: str or bytes, pve: ProxmoxHost):
# Extract values from JSON # Extract values from JSON
lxc_id = lxc_id lxc_id = lxc_id
lxc_hostname = data["lxc_hostname"] lxc_hostname = data.get("lxc_hostname")
os = data["os"] os = data.get("os")
resources = data["resources"] resources = data.get("resources")
network = data["network"] network = data.get("network")
options = data["options"] options = data.get("options")
creation = data["creation"] creation = data.get("creation")
deploy = data["deploy"] deploy = data.get("deploy")
features = data.get("features")
# Create LXC object # 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) lxcs.append(lxc)
@ -89,32 +90,87 @@ def generate_pct_command_for_lxc(lxc: LXC, create: bool = True):
:return: pct command :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: 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 # Create command
pct_command = f"pct create {lxc.get_id()} {lxc.get_os_template()} " \ pct_command = f"pct create {lxc.get_id()} {lxc.get_os_template()} {' '.join(command_params)}"
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()}"
else: else:
# Update command # Update command
pct_command = f"pct set {lxc.get_id()} " \ # Combine common parameters only
f"--hostname {lxc.get_hostname()} " \ command_params = common_params
f"--cores {lxc.get_cpu()} " \ pct_command = f"pct set {lxc.get_id()} {' '.join(command_params)}"
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()} "
# TODO: add gateway4 # TODO: add gateway4
# f"ip6={self.ipv6},gw6={self.gateway6},trunks={self.vlan} " \ # TODO # f"ip6={self.ipv6},gw6={self.gateway6},trunks={self.vlan} " \ # TODO