Compare commits
22 commits
Author | SHA1 | Date | |
---|---|---|---|
717d6a63b3 | |||
b9a8b87e7f | |||
8e4deb7d36 | |||
a8d0148f72 | |||
589a9125f4 | |||
259c2ec8d7 | |||
d5ea5e64ee | |||
9805419555 | |||
b3fe3b8a10 | |||
94fcd6828f | |||
d67137363f | |||
2e793fd31f | |||
11d6b574f3 | |||
e3125ea4fe | |||
8f156e9f70 | |||
aa13b77758 | |||
147e5630aa | |||
e4ec47401e | |||
45d2e3a3d1 | |||
7a794197f4 | |||
8b7221363c | |||
486a38440f |
5 changed files with 233 additions and 105 deletions
43
containerctl
43
containerctl
|
@ -11,9 +11,36 @@ LOGDIR="/var/log/containerctl"
|
|||
TODAY="$(date '+%F')"
|
||||
LOG="${LOGDIR}/${TODAY}"
|
||||
|
||||
get_python_path()
|
||||
{
|
||||
py="python3"
|
||||
pyver="$(/usr/bin/env "${py}" -c 'import sys; print(sys.version_info.minor)')"
|
||||
if [ "${pyver}" -lt "11" ]
|
||||
then
|
||||
pyver="13"
|
||||
py="python3.${pyver}"
|
||||
else
|
||||
printf '%b' "${py}"
|
||||
return
|
||||
fi
|
||||
|
||||
while [ "${pyver}" -ge 11 ]
|
||||
do
|
||||
if /usr/bin/env "${py}" -c "print('${py}')" 2> /dev/null
|
||||
then
|
||||
return
|
||||
fi
|
||||
pyver=$((pyver - 1))
|
||||
py="python3.${pyver}"
|
||||
done
|
||||
|
||||
log_error 'containerctl needs at least Python 3.11 to run!'
|
||||
exit 1
|
||||
}
|
||||
|
||||
log_error()
|
||||
{
|
||||
printf '[%b] (EE) %b\n' "${TODAY}" "${@}" | tee -a "${LOG}"
|
||||
printf '[%b] (EE) %b\n' "${TODAY}" "${@}" | tee -a "${LOG}" 2> /dev/null
|
||||
}
|
||||
|
||||
list_containers()
|
||||
|
@ -106,12 +133,7 @@ generate_container()
|
|||
exit 1
|
||||
fi
|
||||
|
||||
mypython="python3"
|
||||
pyver="$(/usr/bin/env "${mypython}" -c 'import sys; print(sys.version_info.minor)')"
|
||||
if [ "${pyver}" -gt "11" ]
|
||||
then
|
||||
mypython="python3.11"
|
||||
fi
|
||||
mypython="$(get_python_path)"
|
||||
/usr/bin/env "${mypython}" "${BASEDIR}/generate/generate.py" \
|
||||
"${CONFIGDIR}/${config}" "${LOG}" "${CONTAINERDIR}"
|
||||
|
||||
|
@ -130,10 +152,17 @@ generate_all()
|
|||
done
|
||||
}
|
||||
|
||||
usage()
|
||||
{
|
||||
printf '%b list-containers|list-configs|generate CONFIG-FILE|generate-all|CONTAINER-NAME ACTION\n' "${0}"
|
||||
}
|
||||
|
||||
case "${1}" in
|
||||
"list-containers") list_containers ;;
|
||||
"list-configs") list_configs ;;
|
||||
"generate-all") generate_all ;;
|
||||
"generate") shift; generate_container "${@}" ;;
|
||||
"help") usage "${0}" ;;
|
||||
"usage") usage "${0}" ;;
|
||||
*) exec_script "${@}" ;;
|
||||
esac
|
||||
|
|
|
@ -16,6 +16,9 @@
|
|||
},
|
||||
"tag": {
|
||||
"type": "string"
|
||||
},
|
||||
"command": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
|
@ -52,11 +55,7 @@
|
|||
"type": "array",
|
||||
"items": {}
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"mode",
|
||||
"options"
|
||||
]
|
||||
}
|
||||
},
|
||||
"dns": {
|
||||
"type": "object",
|
||||
|
@ -72,11 +71,7 @@
|
|||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"search",
|
||||
"servers"
|
||||
]
|
||||
}
|
||||
},
|
||||
"ports": {
|
||||
"type": "object",
|
||||
|
@ -97,11 +92,7 @@
|
|||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"tcp",
|
||||
"udp"
|
||||
]
|
||||
}
|
||||
},
|
||||
"env": {
|
||||
"type": "object"
|
||||
|
@ -132,11 +123,7 @@
|
|||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"add",
|
||||
"drop"
|
||||
]
|
||||
}
|
||||
},
|
||||
"accounting": {
|
||||
"type": "object",
|
||||
|
|
|
@ -186,6 +186,7 @@ class Memory:
|
|||
reservation = maybe_or(val, "reservation", "")
|
||||
swap = maybe_or(val, "swap", "")
|
||||
if limit == "":
|
||||
logger.log_warning("No limit set, memory config is not needed")
|
||||
return cls("", "", "")
|
||||
return cls(limit, reservation, swap)
|
||||
|
||||
|
@ -244,7 +245,7 @@ class Volume:
|
|||
"""Container Volume."""
|
||||
|
||||
name: str
|
||||
path: str
|
||||
path: list
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, val: ConfigValue, logger: Log) -> list:
|
||||
|
@ -252,9 +253,18 @@ class Volume:
|
|||
if val is None:
|
||||
return []
|
||||
if not isinstance(val, dict):
|
||||
logger.log_warning("Volume key is present, but malformed.")
|
||||
logger.log_warning("Volume key is malformed.")
|
||||
return []
|
||||
return [cls(key, value) for key, value in val.items()]
|
||||
return [
|
||||
Volume.from_json_entry(key, value) for key, value in val.items()
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def from_json_entry(cls, key: str, value: str | list) -> Self:
|
||||
"""Create from JSON entry."""
|
||||
if isinstance(value, str):
|
||||
return cls(key, [value])
|
||||
return cls(key, value)
|
||||
|
||||
def is_host_volume(self) -> bool:
|
||||
"""Check if this Volume is a named or a host volume."""
|
||||
|
@ -262,7 +272,10 @@ class Volume:
|
|||
|
||||
def command(self) -> str:
|
||||
"""Option for podman container create."""
|
||||
return f"--volume {self.name}:{self.path}"
|
||||
cmd = ""
|
||||
for path in self.path:
|
||||
cmd += f"\t--volume {self.name}:{path} \\\n"
|
||||
return cmd
|
||||
|
||||
def create(self) -> str:
|
||||
"""Create volume, if it does not exist."""
|
||||
|
@ -298,7 +311,7 @@ class Secret:
|
|||
|
||||
name: str
|
||||
secret_type: str
|
||||
target: str
|
||||
target: list
|
||||
options: str
|
||||
|
||||
@classmethod
|
||||
|
@ -307,7 +320,7 @@ class Secret:
|
|||
if val is None:
|
||||
return []
|
||||
if not isinstance(val, dict):
|
||||
logger.log_warning("Secret key is present, but malformed!")
|
||||
logger.log_warning("Secret key is malformed!")
|
||||
return []
|
||||
secrets = []
|
||||
for key in val:
|
||||
|
@ -316,26 +329,36 @@ class Secret:
|
|||
continue
|
||||
name = key
|
||||
secret_type = maybe_or(val[key], "type", "")
|
||||
target = maybe_or(val[key], "target", "")
|
||||
target = maybe(val[key], "target")
|
||||
if isinstance(target, str):
|
||||
target = [target]
|
||||
if not isinstance(target, list):
|
||||
logger.log_warning(
|
||||
f"Secret {name} has no target and will be ignored"
|
||||
)
|
||||
target = []
|
||||
options = maybe_or(val[key], "options", "")
|
||||
if options is None:
|
||||
options = ""
|
||||
secrets.append(
|
||||
cls(name, str(secret_type), str(target), str(options))
|
||||
)
|
||||
secrets.append(cls(name, str(secret_type), target, str(options)))
|
||||
|
||||
return secrets
|
||||
|
||||
def command(self) -> str:
|
||||
"""Option for podman container create."""
|
||||
cmd = (
|
||||
f"--secret {self.name},:"
|
||||
cmd = ""
|
||||
for target in self.target:
|
||||
cmd += (
|
||||
f"\t--secret {self.name},:"
|
||||
f"type={self.secret_type},"
|
||||
f"target={self.target}"
|
||||
f"target={target}"
|
||||
)
|
||||
# Not a password, ruff...
|
||||
if self.secret_type == "mount" and self.options != "": # noqa: S105
|
||||
has_option = self.secret_type == "mount" # noqa: S105
|
||||
has_option = has_option and self.options != ""
|
||||
if has_option:
|
||||
cmd = f"{cmd},{self.options}"
|
||||
cmd += " \\\n"
|
||||
|
||||
return cmd
|
||||
|
||||
|
@ -374,7 +397,7 @@ class Environment:
|
|||
if val is None:
|
||||
return cls([], "")
|
||||
if not isinstance(val, dict):
|
||||
logger.log_warning("Environment key is present, but malformed!")
|
||||
logger.log_warning("Environment key is malformed!")
|
||||
return cls([], "")
|
||||
return cls([f"{key}='{value}'" for key, value in val.items()], "")
|
||||
|
||||
|
@ -396,7 +419,7 @@ class Environment:
|
|||
if cmd == "":
|
||||
return ""
|
||||
|
||||
return header + cmd
|
||||
return header + f"printf '\\n' > {self.file}\n" + cmd
|
||||
|
||||
def remove(self) -> str:
|
||||
"""Remove env file."""
|
||||
|
@ -421,7 +444,7 @@ class Ports:
|
|||
if val is None:
|
||||
return cls([], [])
|
||||
if not isinstance(val, dict):
|
||||
logger.log_warning("Ports key is present, but malformed!")
|
||||
logger.log_warning("Ports key is malformed!")
|
||||
return cls([], [])
|
||||
tcp_ports = maybe(val, "tcp")
|
||||
udp_ports = maybe(val, "udp")
|
||||
|
@ -480,13 +503,13 @@ class Network:
|
|||
return cls("", [])
|
||||
mode = maybe(val, "mode")
|
||||
options = maybe(val, "options")
|
||||
if mode is None:
|
||||
if mode is None or not isinstance(mode, str):
|
||||
err = "Network configuration is missing or has malformed elements!"
|
||||
logger.log_error(err)
|
||||
return cls("", [])
|
||||
if options is None or not isinstance(options, list):
|
||||
return cls(str(mode), [])
|
||||
return cls(str(mode), options)
|
||||
return cls(mode, [])
|
||||
return cls(mode, options)
|
||||
|
||||
def command(self) -> str:
|
||||
"""Option for podman container create."""
|
||||
|
@ -506,6 +529,7 @@ class Image:
|
|||
registry: str
|
||||
image: str
|
||||
tag: str
|
||||
cmd: str
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, val: ConfigValue, logger: Log) -> Self | None:
|
||||
|
@ -516,10 +540,13 @@ class Image:
|
|||
registry = maybe_or(val, "registry", "")
|
||||
image = maybe_or(val, "image", "")
|
||||
tag = maybe_or(val, "tag", "")
|
||||
return cls(str(registry), str(image), str(tag))
|
||||
cmd = maybe_or(val, "command", "")
|
||||
return cls(registry, image, tag, cmd)
|
||||
|
||||
def command(self) -> str:
|
||||
"""Option for podman container create."""
|
||||
if self.cmd != "":
|
||||
return f"{self.registry}/{self.image}:{self.tag} {self.cmd}"
|
||||
return f"{self.registry}/{self.image}:{self.tag}"
|
||||
|
||||
|
||||
|
@ -566,9 +593,9 @@ class Dns:
|
|||
search = maybe_or(val, "search", "")
|
||||
servers = maybe(val, "servers")
|
||||
if not isinstance(servers, list):
|
||||
logger.log_error("Servers key is not an array!")
|
||||
return cls([], "")
|
||||
return cls(servers, str(search))
|
||||
logger.log_warning("Servers key is not an array!")
|
||||
return cls([], search)
|
||||
return cls(servers, search)
|
||||
|
||||
def command(self) -> str:
|
||||
"""Option for podman container create."""
|
||||
|
@ -585,20 +612,101 @@ 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 ""
|
||||
|
||||
|
||||
@dataclass
|
||||
class ContainerNetwork:
|
||||
"""Wrapper for Network, Dns and Ports."""
|
||||
|
||||
network: Network
|
||||
dns: Dns
|
||||
ports: Ports
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, json: ConfigValue, logger: Log) -> Self:
|
||||
"""Create from JSON."""
|
||||
network_config = maybe(json, "network")
|
||||
dns_config = maybe(json, "dns")
|
||||
ports_config = maybe(json, "ports")
|
||||
|
||||
network = Network.from_json(network_config, logger)
|
||||
dns = Dns.from_json(dns_config, logger)
|
||||
ports = Ports.from_json(ports_config, logger)
|
||||
|
||||
return cls(network, dns, ports)
|
||||
|
||||
def command(self) -> str:
|
||||
"""Option for podman container create."""
|
||||
cmd = ""
|
||||
cmd += self.network.command()
|
||||
cmd += self.dns.command()
|
||||
cmd += self.ports.command()
|
||||
|
||||
return cmd
|
||||
|
||||
|
||||
class Container:
|
||||
"""Container."""
|
||||
|
||||
name: str
|
||||
image: Image
|
||||
privileged: bool
|
||||
read_only: bool
|
||||
replace: bool
|
||||
restart: str
|
||||
pull_policy: str
|
||||
timezone: str
|
||||
network: Network
|
||||
dns: Dns
|
||||
ports: Ports
|
||||
ct_opts: ContainerOptions
|
||||
ct_network: ContainerNetwork
|
||||
env: Environment
|
||||
secrets: list
|
||||
volumes: list
|
||||
|
@ -612,20 +720,30 @@ class Container:
|
|||
name = maybe(json, "name")
|
||||
if name is None:
|
||||
logger.log_error("No container name set, aborting!")
|
||||
return
|
||||
raise ConfigError("Container has no name")
|
||||
image = maybe(json, "image")
|
||||
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")
|
||||
network = maybe(json, "network")
|
||||
dns = maybe(json, "dns")
|
||||
ports = maybe(json, "ports")
|
||||
raise ConfigError("Container has no image")
|
||||
|
||||
self.image = Image.from_json(image, logger)
|
||||
image_valid = True
|
||||
if self.image.image == "":
|
||||
logger.log_error("Image has no image set!")
|
||||
image_valid = False
|
||||
if self.image.registry == "":
|
||||
logger.log_error("Image has no registry set!")
|
||||
image_valid = False
|
||||
if self.image.tag == "":
|
||||
logger.log_error("Image has no tag set!")
|
||||
image_valid = False
|
||||
if not image_valid:
|
||||
raise ConfigError("Image is missing required keys!")
|
||||
|
||||
self.name = name
|
||||
ct_opts = ContainerOptions.from_json(json, logger)
|
||||
if not ct_opts.is_valid:
|
||||
raise ConfigError("Config seems to be invalid?")
|
||||
env = maybe(json, "env")
|
||||
secrets = maybe(json, "secrets")
|
||||
volumes = maybe(json, "volumes")
|
||||
|
@ -633,15 +751,8 @@ 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.network = Network.from_json(network, logger)
|
||||
self.dns = Dns.from_json(dns, logger)
|
||||
self.ports = Ports.from_json(ports, logger)
|
||||
self.ct_opts = ct_opts
|
||||
self.ct_network = ContainerNetwork.from_json(json, logger)
|
||||
self.env = Environment.from_json(env, logger)
|
||||
self.env.file = "/var/lib/containerctl/environment-files/"
|
||||
self.env.file += f"{self.name}"
|
||||
|
@ -679,23 +790,13 @@ 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.network.command()}"
|
||||
cmd += f"{self.dns.command()}"
|
||||
cmd += f"{self.ports.command()}"
|
||||
cmd += f"{self.ct_opts.command()}"
|
||||
cmd += f"{self.ct_network.command()}"
|
||||
cmd += f"{self.env.command()}"
|
||||
for secret in self.secrets:
|
||||
cmd += f"\t{secret.command()} \\\n"
|
||||
cmd += f"{secret.command()}"
|
||||
for volume in self.volumes:
|
||||
cmd += f"\t{volume.command()} \\\n"
|
||||
cmd += f"{volume.command()}"
|
||||
for capability in self.capabilities:
|
||||
cmd += f"\t{capability.command()} \\\n"
|
||||
cmd += f"{self.accounting.command()}"
|
||||
|
|
|
@ -14,7 +14,7 @@ from pathlib import Path
|
|||
from container import ConfigError, Container
|
||||
from log import Log
|
||||
|
||||
GENERATE_VERSION = "0.0.9"
|
||||
GENERATE_VERSION = "0.0.15"
|
||||
HEADER = f"""#!/bin/sh
|
||||
# This script was generated by containerctl v{GENERATE_VERSION}
|
||||
# Report bugs with _this script_ to <tenno+containerctl@suij.in>
|
||||
|
@ -76,10 +76,12 @@ def main() -> None:
|
|||
if len(sys.argv) > log_threshold:
|
||||
base = sys.argv[3]
|
||||
logger = Log(log_file)
|
||||
data = load_container_config(Path(config_file), logger)
|
||||
conf = Path(config_file)
|
||||
data = load_container_config(conf, logger)
|
||||
if data is None:
|
||||
logger.log_error(f"{config_file} is invalid, aborting!")
|
||||
logger.log_error(f"{conf.name} is invalid, aborting!")
|
||||
sys.exit(1)
|
||||
logger.set_prefix(conf.name)
|
||||
ct = create_container_from_config(data, logger)
|
||||
if ct is None:
|
||||
sys.exit(1)
|
||||
|
|
|
@ -15,6 +15,7 @@ class Log:
|
|||
|
||||
messages: list = []
|
||||
logfile: Path
|
||||
prefix: str = ""
|
||||
|
||||
def __init__(self, path: str) -> None:
|
||||
"""Init for Log."""
|
||||
|
@ -25,15 +26,19 @@ class Log:
|
|||
def log_error(self, msg: str) -> None:
|
||||
"""Log an error."""
|
||||
now = self.timestamp()
|
||||
prefix = "EE"
|
||||
log_message = f"[{now}] ({prefix}) {msg}"
|
||||
prefix = "(EE)"
|
||||
if self.prefix != "":
|
||||
prefix += f" {self.prefix}:"
|
||||
log_message = f"[{now}] {prefix} {msg}"
|
||||
self.write_message(log_message)
|
||||
|
||||
def log_warning(self, msg: str) -> None:
|
||||
"""Log a warning."""
|
||||
now = self.timestamp()
|
||||
prefix = "WW"
|
||||
log_message = f"[{now}] ({prefix}) {msg}"
|
||||
prefix = "(WW)"
|
||||
if self.prefix != "":
|
||||
prefix += f" {self.prefix}:"
|
||||
log_message = f"[{now}] {prefix} {msg}"
|
||||
self.write_message(log_message)
|
||||
|
||||
def write_message(self, msg: str) -> None:
|
||||
|
@ -49,3 +54,7 @@ class Log:
|
|||
return datetime.datetime.now(tz=datetime.UTC).strftime(
|
||||
"%Y-%m-%d %H:%M:%S",
|
||||
)
|
||||
|
||||
def set_prefix(self, prefix: str) -> None:
|
||||
"""Set a prefix."""
|
||||
self.prefix = prefix
|
||||
|
|
Loading…
Add table
Reference in a new issue