generate: container: Improve optional feature handling
Things missing should now cause less problems. Signed-off-by: Enno Tensing <tenno@suij.in>
This commit is contained in:
parent
6d9f63abb5
commit
260a0dcc52
1 changed files with 39 additions and 22 deletions
|
@ -384,12 +384,17 @@ class Environment:
|
||||||
|
|
||||||
def create(self) -> str:
|
def create(self) -> str:
|
||||||
"""Create env file."""
|
"""Create env file."""
|
||||||
cmd = f"# Create env-file {self.file}\n"
|
cmd = ""
|
||||||
|
header = f"# Create env-file {self.file}\n"
|
||||||
|
|
||||||
for var in self.variables:
|
for var in self.variables:
|
||||||
escaped_var = var.replace("'", "%b")
|
escaped_var = var.replace("'", "%b")
|
||||||
cmd += f"printf '{escaped_var}\\n' \"'\" \"'\" >> '{self.file}'\n"
|
cmd += f"printf '{escaped_var}\\n' \"'\" \"'\" >> '{self.file}'\n"
|
||||||
|
|
||||||
return cmd
|
if cmd == "":
|
||||||
|
return ""
|
||||||
|
|
||||||
|
return header + cmd
|
||||||
|
|
||||||
def remove(self) -> str:
|
def remove(self) -> str:
|
||||||
"""Remove env file."""
|
"""Remove env file."""
|
||||||
|
@ -418,27 +423,41 @@ class Ports:
|
||||||
return cls([], [])
|
return cls([], [])
|
||||||
tcp_ports = maybe(val, "tcp")
|
tcp_ports = maybe(val, "tcp")
|
||||||
udp_ports = maybe(val, "udp")
|
udp_ports = maybe(val, "udp")
|
||||||
|
if tcp_ports is None:
|
||||||
|
tcp_ports = []
|
||||||
|
if udp_ports is None:
|
||||||
|
udp_ports = []
|
||||||
|
if not isinstance(tcp_ports, list) and not isinstance(udp_ports, list):
|
||||||
|
logger.log_warning("Port configuration is malformed!")
|
||||||
|
return cls([], [])
|
||||||
if not isinstance(tcp_ports, list):
|
if not isinstance(tcp_ports, list):
|
||||||
logger.log_warning("Key tcp_ports is not an array!")
|
logger.log_warning("tcp_ports configuration is malformed!")
|
||||||
return cls([], [])
|
return cls([], udp_ports)
|
||||||
if not isinstance(udp_ports, list):
|
if not isinstance(udp_ports, list):
|
||||||
logger.log_warning("Key udp_ports is not an array!")
|
logger.log_warning("udp_ports configuration is malformed!")
|
||||||
return cls([], [])
|
return cls(tcp_ports, [])
|
||||||
return cls(tcp_ports, udp_ports)
|
return cls(tcp_ports, udp_ports)
|
||||||
|
|
||||||
def command(self) -> str:
|
def command(self) -> str:
|
||||||
"""Option for podman container create."""
|
"""Option for podman container create."""
|
||||||
ports = ""
|
|
||||||
seperator = " \\\n"
|
seperator = " \\\n"
|
||||||
ports += seperator.join(
|
tcp_ports = seperator.join(
|
||||||
[f"\t--publish {port}/tcp" for port in self.tcp_ports]
|
[f"\t--publish {port}/tcp" for port in self.tcp_ports]
|
||||||
)
|
)
|
||||||
ports += seperator
|
udp_ports = seperator.join(
|
||||||
ports += seperator.join(
|
|
||||||
[f"\t--publish {port}/udp" for port in self.udp_ports]
|
[f"\t--publish {port}/udp" for port in self.udp_ports]
|
||||||
)
|
)
|
||||||
ports += seperator
|
|
||||||
return ports
|
if tcp_ports == "" and udp_ports == "":
|
||||||
|
return ""
|
||||||
|
|
||||||
|
if tcp_ports == "":
|
||||||
|
return udp_ports + seperator
|
||||||
|
|
||||||
|
if udp_ports == "":
|
||||||
|
return tcp_ports + seperator
|
||||||
|
|
||||||
|
return tcp_ports + seperator + udp_ports + seperator
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
@ -469,11 +488,11 @@ class Network:
|
||||||
"""Option for podman container create."""
|
"""Option for podman container create."""
|
||||||
if self.mode == "":
|
if self.mode == "":
|
||||||
return ""
|
return ""
|
||||||
cmd = f"--network={self.mode}"
|
cmd = f"\t--network={self.mode}"
|
||||||
opts = ",".join(self.options)
|
opts = ",".join(self.options)
|
||||||
if opts != "":
|
if opts != "":
|
||||||
cmd += f":{opts}"
|
cmd += f":{opts}"
|
||||||
return cmd
|
return cmd + " \\\n"
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
@ -513,9 +532,7 @@ class Capability:
|
||||||
if val is None:
|
if val is None:
|
||||||
return []
|
return []
|
||||||
if not isinstance(val, dict):
|
if not isinstance(val, dict):
|
||||||
logger.log_warning(
|
logger.log_warning("Capabilities key is malformed!")
|
||||||
"Capabilities key is malformed!"
|
|
||||||
)
|
|
||||||
return []
|
return []
|
||||||
add = [cls(value, "add") for value in val["add"]]
|
add = [cls(value, "add") for value in val["add"]]
|
||||||
drop = [cls(value, "drop") for value in val["drop"]]
|
drop = [cls(value, "drop") for value in val["drop"]]
|
||||||
|
@ -554,11 +571,11 @@ class Dns:
|
||||||
if len(self.servers) == 0 and self.search == "":
|
if len(self.servers) == 0 and self.search == "":
|
||||||
return ""
|
return ""
|
||||||
if len(self.servers) == 0:
|
if len(self.servers) == 0:
|
||||||
return f"--dns-search={self.search}"
|
return f"\t--dns-search={self.search} \\\n"
|
||||||
if self.search == "":
|
if self.search == "":
|
||||||
return f"--dns={','.join(self.servers)}"
|
return f"\t--dns={','.join(self.servers)} \\\n"
|
||||||
|
|
||||||
cmd = f"--dns-search={self.search} \\\n\t--dns="
|
cmd = f"\t--dns-search={self.search} \\\n\t--dns="
|
||||||
cmd += ",".join(self.servers)
|
cmd += ",".join(self.servers)
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
|
@ -667,8 +684,8 @@ class Container:
|
||||||
cmd += f"\t--restart={self.restart} \\\n"
|
cmd += f"\t--restart={self.restart} \\\n"
|
||||||
cmd += f"\t--pull={self.pull_policy} \\\n"
|
cmd += f"\t--pull={self.pull_policy} \\\n"
|
||||||
cmd += f"\t--tz={self.timezone} \\\n"
|
cmd += f"\t--tz={self.timezone} \\\n"
|
||||||
cmd += f"\t{self.network.command()} \\\n"
|
cmd += f"{self.network.command()}"
|
||||||
cmd += f"\t{self.dns.command()} \\\n"
|
cmd += f"{self.dns.command()}"
|
||||||
cmd += f"{self.ports.command()}"
|
cmd += f"{self.ports.command()}"
|
||||||
if self.env is not None:
|
if self.env is not None:
|
||||||
cmd += f"\t{self.env.command()} \\\n"
|
cmd += f"\t{self.env.command()} \\\n"
|
||||||
|
|
Loading…
Add table
Reference in a new issue