From 147e5630aa35b92eab7483d3061349d3ba762b0a Mon Sep 17 00:00:00 2001 From: Enno Tensing Date: Fri, 8 Aug 2025 16:20:12 +0200 Subject: [PATCH] generate: container: Create new ContainerOptions class for read_only and Co Signed-off-by: Enno Tensing --- generate/container.py | 90 ++++++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 27 deletions(-) diff --git a/generate/container.py b/generate/container.py index 9e5d367..de0e908 100644 --- a/generate/container.py +++ b/generate/container.py @@ -608,17 +608,69 @@ class Dns: return cmd +@dataclass +class ContainerOptions: + """Container-Meta settings.""" + + privileged: bool = False + read_only: bool = False + replace: bool = False + restart: str = "no" + pull_policy: str = "always" + timezone: str = "local" + is_valid: bool = False + + @classmethod + def from_json(cls, val: ConfigValue, logger: Log) -> Self: + """Create from JSON.""" + if val is None: + # Should not happen! + return cls() + if not isinstance(val, dict): + logger.log_error("Container config is invalid!") + return cls() + + privileged = maybe_or(val, "privileged", _or=False) + read_only = maybe_or(val, "read_only", _or=False) + replace = maybe_or(val, "replace", _or=False) + restart = maybe_or(val, "restart", "no") + pull_policy = maybe_or(val, "pull_policy", "always") + timezone = maybe_or(val, "timezone", "local") + + return cls( + privileged, + read_only, + replace, + restart, + pull_policy, + timezone, + is_valid=True, + ) + + def command(self) -> str: + """Option for podman conainter create.""" + cmd = "" + if self.privileged: + cmd += "\t--privileged \\\n" + if self.read_only: + cmd += "\t--read-only \\\n" + if self.replace: + cmd += "\t--replace \\\n" + if self.restart != "": + cmd += f"\t--restart={self.restart} \\\n" + if self.pull_policy != "": + cmd += f"\t--pull-policy={self.pull_policy} \\\n" + if self.timezone != "": + cmd += f"\t--tz={self.timezone} \\\n" + return "" + + class Container: """Container.""" name: str image: Image - privileged: bool - read_only: bool - replace: bool - restart: str - pull_policy: str - timezone: str + ct_opts: ContainerOptions network: Network dns: Dns ports: Ports @@ -640,12 +692,9 @@ class Container: if image is None: logger.log_error("No image set, aborting!") return - privileged = maybe(json, "privileged") - read_only = maybe(json, "read_only") - replace = maybe(json, "replace") - pull_policy = maybe_or(json, "pull_policy", "always") - restart = maybe_or(json, "restart", "no") - timezone = maybe_or(json, "timezone", "local") + ct_opts = ContainerOptions.from_json(json, logger) + if not ct_opts.is_valid: + return network = maybe(json, "network") dns = maybe(json, "dns") ports = maybe(json, "ports") @@ -656,12 +705,7 @@ class Container: accounting = maybe(json, "accounting") self.name = str(name) self.image = Image.from_json(image, logger) - self.privileged = privileged is not None and bool(privileged) - self.read_only = read_only is not None and bool(read_only) - self.replace = replace is not None and bool(replace) - self.pull_policy = str(pull_policy) - self.restart = str(restart) - self.timezone = str(timezone) + self.ct_opts = ct_opts self.network = Network.from_json(network, logger) self.dns = Dns.from_json(dns, logger) self.ports = Ports.from_json(ports, logger) @@ -702,15 +746,7 @@ class Container: cmd = f"# Create container {self.name}\n" cmd += "podman container create \\\n" cmd += f"\t--name={self.name} \\\n" - if self.privileged: - cmd += "\t--privileged \\\n" - if self.replace: - cmd += "\t--replace \\\n" - if self.read_only: - cmd += "\t--read-only \\\n" - cmd += f"\t--restart={self.restart} \\\n" - cmd += f"\t--pull={self.pull_policy} \\\n" - cmd += f"\t--tz={self.timezone} \\\n" + cmd += f"{self.ct_opts.command()}" cmd += f"{self.network.command()}" cmd += f"{self.dns.command()}" cmd += f"{self.ports.command()}"