Compare commits
No commits in common. "main" and "v0.0.4" have entirely different histories.
6 changed files with 162 additions and 320 deletions
19
Makefile
19
Makefile
|
@ -1,19 +1,18 @@
|
|||
BINDIR ?= $(DESTDIR)/usr/bin
|
||||
VARDIR ?= $(DESTDIR)/var/lib/containerctl
|
||||
DESTDIR ?= /usr
|
||||
BINDIR ?= $(DESTDIR)/bin
|
||||
VARDIR ?= /var/lib/containerctl
|
||||
GENERATEDIR ?= $(VARDIR)/generate
|
||||
CONFIGDIR ?= $(VARDIR)/configs
|
||||
CONTAINERDIR ?= $(VARDIR)/containers
|
||||
ENVDIR ?= $(VARDIR)/environment-files
|
||||
|
||||
install:
|
||||
mkdir -p \
|
||||
$(PREFIX)$(GENERATEDIR) \
|
||||
$(PREFIX)$(CONFIGDIR) \
|
||||
$(PREFIX)$(CONTAINERDIR) \
|
||||
$(PREFIX)$(ENVDIR) \
|
||||
$(PREFIX)$(BINDIR)
|
||||
install -m755 containerctl $(PREFIX)$(BINDIR)
|
||||
cp -t $(PREFIX)$(GENERATEDIR) \
|
||||
$(PREFIX)/$(GENERATEDIR) \
|
||||
$(PREFIX)/$(CONFIGDIR) \
|
||||
$(PREFIX)/$(CONTAINERDIR) \
|
||||
$(PREFIX)/$(BINDIR)
|
||||
install -m755 containerctl $(PREFIX)/$(BINDIR)
|
||||
cp -t $(PREFIX)/$(GENERATEDIR) \
|
||||
generate/container.py \
|
||||
generate/log.py \
|
||||
generate/generate.py
|
||||
|
|
52
containerctl
52
containerctl
|
@ -11,46 +11,24 @@ 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}" 2> /dev/null
|
||||
printf '[%b] (EE) %b\n' "${TODAY}" "${@}" | tee -a "${LOG}"
|
||||
}
|
||||
|
||||
list_dir()
|
||||
{
|
||||
find "${1}" -mindepth 1 -type d
|
||||
}
|
||||
|
||||
list_containers()
|
||||
{
|
||||
find "${CONTAINERDIR}" -mindepth 1 -type d
|
||||
list_dir "${CONTAINERDIR}"
|
||||
}
|
||||
|
||||
list_configs()
|
||||
{
|
||||
find "${CONFIGDIR}" -mindepth 1 -type f
|
||||
list_dir "${CONFIGDIR}"
|
||||
}
|
||||
|
||||
exec_script()
|
||||
|
@ -133,8 +111,7 @@ generate_container()
|
|||
exit 1
|
||||
fi
|
||||
|
||||
mypython="$(get_python_path)"
|
||||
/usr/bin/env "${mypython}" "${BASEDIR}/generate/generate.py" \
|
||||
/usr/bin/env python3 "${BASEDIR}/generate/generate.py" \
|
||||
"${CONFIGDIR}/${config}" "${LOG}" "${CONTAINERDIR}"
|
||||
|
||||
if [ "${?}" = 1 ]
|
||||
|
@ -148,21 +125,14 @@ generate_all()
|
|||
{
|
||||
list_configs | while read -r config
|
||||
do
|
||||
generate_container "$(printf '%b' "${config}" | sed -e "s|${CONFIGDIR}||g")"
|
||||
generate_config "$(printf '%b' "${config}" | sed -e "s|${CONFIGDIR}||g")"
|
||||
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}" ;;
|
||||
"generate") shift; generate_config "${@}" ;;
|
||||
*) exec_script "${@}" ;;
|
||||
esac
|
||||
|
|
|
@ -16,9 +16,6 @@
|
|||
},
|
||||
"tag": {
|
||||
"type": "string"
|
||||
},
|
||||
"command": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
|
@ -55,7 +52,11 @@
|
|||
"type": "array",
|
||||
"items": {}
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"mode",
|
||||
"options"
|
||||
]
|
||||
},
|
||||
"dns": {
|
||||
"type": "object",
|
||||
|
@ -71,7 +72,11 @@
|
|||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"search",
|
||||
"servers"
|
||||
]
|
||||
},
|
||||
"ports": {
|
||||
"type": "object",
|
||||
|
@ -92,7 +97,11 @@
|
|||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"tcp",
|
||||
"udp"
|
||||
]
|
||||
},
|
||||
"env": {
|
||||
"type": "object"
|
||||
|
@ -123,7 +132,11 @@
|
|||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"add",
|
||||
"drop"
|
||||
]
|
||||
},
|
||||
"accounting": {
|
||||
"type": "object",
|
||||
|
|
|
@ -186,7 +186,6 @@ 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)
|
||||
|
||||
|
@ -245,26 +244,17 @@ class Volume:
|
|||
"""Container Volume."""
|
||||
|
||||
name: str
|
||||
path: list
|
||||
path: str
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, val: ConfigValue, logger: Log) -> list:
|
||||
def from_json(cls, val: ConfigValue, logger: Log) -> list | None:
|
||||
"""Create from JSON."""
|
||||
if val is None:
|
||||
return []
|
||||
return None
|
||||
if not isinstance(val, dict):
|
||||
logger.log_warning("Volume key is malformed.")
|
||||
return []
|
||||
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)
|
||||
logger.log_warning("Volume key is present, but malformed.")
|
||||
return None
|
||||
return [cls(key, value) for key, value in val.items()]
|
||||
|
||||
def is_host_volume(self) -> bool:
|
||||
"""Check if this Volume is a named or a host volume."""
|
||||
|
@ -272,10 +262,7 @@ class Volume:
|
|||
|
||||
def command(self) -> str:
|
||||
"""Option for podman container create."""
|
||||
cmd = ""
|
||||
for path in self.path:
|
||||
cmd += f"\t--volume {self.name}:{path} \\\n"
|
||||
return cmd
|
||||
return f"--volume {self.name}:{self.path}"
|
||||
|
||||
def create(self) -> str:
|
||||
"""Create volume, if it does not exist."""
|
||||
|
@ -311,17 +298,17 @@ class Secret:
|
|||
|
||||
name: str
|
||||
secret_type: str
|
||||
target: list
|
||||
target: str
|
||||
options: str
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, val: ConfigValue, logger: Log) -> list:
|
||||
def from_json(cls, val: ConfigValue, logger: Log) -> list | None:
|
||||
"""Create from JSON."""
|
||||
if val is None:
|
||||
return []
|
||||
return None
|
||||
if not isinstance(val, dict):
|
||||
logger.log_warning("Secret key is malformed!")
|
||||
return []
|
||||
logger.log_warning("Secret key is present, but malformed!")
|
||||
return None
|
||||
secrets = []
|
||||
for key in val:
|
||||
if not isinstance(val[key], dict):
|
||||
|
@ -329,36 +316,26 @@ class Secret:
|
|||
continue
|
||||
name = key
|
||||
secret_type = maybe_or(val[key], "type", "")
|
||||
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 = []
|
||||
target = maybe_or(val[key], "target", "")
|
||||
options = maybe_or(val[key], "options", "")
|
||||
if options is None:
|
||||
options = ""
|
||||
secrets.append(cls(name, str(secret_type), target, str(options)))
|
||||
secrets.append(
|
||||
cls(name, str(secret_type), str(target), str(options))
|
||||
)
|
||||
|
||||
return secrets
|
||||
|
||||
def command(self) -> str:
|
||||
"""Option for podman container create."""
|
||||
cmd = ""
|
||||
for target in self.target:
|
||||
cmd += (
|
||||
f"\t--secret {self.name},:"
|
||||
f"type={self.secret_type},"
|
||||
f"target={target}"
|
||||
)
|
||||
# Not a password, ruff...
|
||||
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"
|
||||
cmd = (
|
||||
f"--secret {self.name},:"
|
||||
f"type={self.secret_type},"
|
||||
f"target={self.target}"
|
||||
)
|
||||
# Not a password, ruff...
|
||||
if self.secret_type == "mount" and self.options != "": # noqa: S105
|
||||
cmd = f"{cmd},{self.options}"
|
||||
|
||||
return cmd
|
||||
|
||||
|
@ -397,29 +374,22 @@ class Environment:
|
|||
if val is None:
|
||||
return cls([], "")
|
||||
if not isinstance(val, dict):
|
||||
logger.log_warning("Environment key is malformed!")
|
||||
logger.log_warning("Environment key is present, but malformed!")
|
||||
return cls([], "")
|
||||
return cls([f"{key}='{value}'" for key, value in val.items()], "")
|
||||
|
||||
def command(self) -> str:
|
||||
"""Option for podman container create."""
|
||||
if len(self.variables) == 0:
|
||||
return ""
|
||||
return f"\t--env-file={self.file} \\\n"
|
||||
return f"--env-file={self.file}"
|
||||
|
||||
def create(self) -> str:
|
||||
"""Create env file."""
|
||||
cmd = ""
|
||||
header = f"# Create env-file {self.file}\n"
|
||||
|
||||
cmd = f"# Create env-file {self.file}\n"
|
||||
for var in self.variables:
|
||||
escaped_var = var.replace("'", "%b")
|
||||
cmd += f"printf '{escaped_var}\\n' \"'\" \"'\" >> '{self.file}'\n"
|
||||
|
||||
if cmd == "":
|
||||
return ""
|
||||
|
||||
return header + f"printf '\\n' > {self.file}\n" + cmd
|
||||
return cmd
|
||||
|
||||
def remove(self) -> str:
|
||||
"""Remove env file."""
|
||||
|
@ -444,45 +414,31 @@ class Ports:
|
|||
if val is None:
|
||||
return cls([], [])
|
||||
if not isinstance(val, dict):
|
||||
logger.log_warning("Ports key is malformed!")
|
||||
logger.log_warning("Ports key is present, but malformed!")
|
||||
return cls([], [])
|
||||
tcp_ports = maybe(val, "tcp")
|
||||
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):
|
||||
logger.log_warning("tcp_ports configuration is malformed!")
|
||||
return cls([], udp_ports)
|
||||
logger.log_warning("Key tcp_ports is not an array!")
|
||||
return cls([], [])
|
||||
if not isinstance(udp_ports, list):
|
||||
logger.log_warning("udp_ports configuration is malformed!")
|
||||
return cls(tcp_ports, [])
|
||||
logger.log_warning("Key udp_ports is not an array!")
|
||||
return cls([], [])
|
||||
return cls(tcp_ports, udp_ports)
|
||||
|
||||
def command(self) -> str:
|
||||
"""Option for podman container create."""
|
||||
ports = ""
|
||||
seperator = " \\\n"
|
||||
tcp_ports = seperator.join(
|
||||
[f"\t--publish {port}/tcp" for port in self.tcp_ports]
|
||||
ports += seperator.join(
|
||||
[f"\t--port {port}/tcp" for port in self.tcp_ports]
|
||||
)
|
||||
udp_ports = seperator.join(
|
||||
[f"\t--publish {port}/udp" for port in self.udp_ports]
|
||||
ports += seperator
|
||||
ports += seperator.join(
|
||||
[f"\t--port {port}/udp" for port in self.udp_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
|
||||
ports += seperator
|
||||
return ports
|
||||
|
||||
|
||||
@dataclass
|
||||
|
@ -495,31 +451,26 @@ class Network:
|
|||
@classmethod
|
||||
def from_json(cls, val: ConfigValue, logger: Log) -> Self:
|
||||
"""Create from JSON."""
|
||||
if val is None:
|
||||
return cls("", [])
|
||||
|
||||
if not isinstance(val, dict):
|
||||
logger.log_warning("Network configuration is malformed!")
|
||||
if val is None or not isinstance(val, dict):
|
||||
logger.log_error("Network configuration is missing or malformed!")
|
||||
return cls("", [])
|
||||
mode = maybe(val, "mode")
|
||||
options = maybe(val, "options")
|
||||
if mode is None or not isinstance(mode, str):
|
||||
if mode is None or options is None or not isinstance(options, list):
|
||||
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(mode, [])
|
||||
return cls(mode, options)
|
||||
return cls(str(mode), options)
|
||||
|
||||
def command(self) -> str:
|
||||
"""Option for podman container create."""
|
||||
if self.mode == "":
|
||||
return ""
|
||||
cmd = f"\t--network={self.mode}"
|
||||
cmd = f"--network={self.mode}"
|
||||
opts = ",".join(self.options)
|
||||
if opts != "":
|
||||
cmd += f":{opts}"
|
||||
return cmd + " \\\n"
|
||||
return cmd
|
||||
|
||||
|
||||
@dataclass
|
||||
|
@ -529,7 +480,6 @@ class Image:
|
|||
registry: str
|
||||
image: str
|
||||
tag: str
|
||||
cmd: str
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, val: ConfigValue, logger: Log) -> Self | None:
|
||||
|
@ -540,13 +490,10 @@ class Image:
|
|||
registry = maybe_or(val, "registry", "")
|
||||
image = maybe_or(val, "image", "")
|
||||
tag = maybe_or(val, "tag", "")
|
||||
cmd = maybe_or(val, "command", "")
|
||||
return cls(registry, image, tag, cmd)
|
||||
return cls(str(registry), str(image), str(tag))
|
||||
|
||||
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}"
|
||||
|
||||
|
||||
|
@ -558,13 +505,13 @@ class Capability:
|
|||
mode: str
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, val: ConfigValue, logger: Log) -> list:
|
||||
def from_json(cls, val: ConfigValue, logger: Log) -> list | None:
|
||||
"""Create from JSON."""
|
||||
if val is None:
|
||||
return []
|
||||
if not isinstance(val, dict):
|
||||
logger.log_warning("Capabilities key is malformed!")
|
||||
return []
|
||||
if val is None or not isinstance(val, dict):
|
||||
logger.log_warning(
|
||||
"Capabilities key is either missing or malformed!"
|
||||
)
|
||||
return None
|
||||
add = [cls(value, "add") for value in val["add"]]
|
||||
drop = [cls(value, "drop") for value in val["drop"]]
|
||||
return add + drop
|
||||
|
@ -584,133 +531,49 @@ class Dns:
|
|||
@classmethod
|
||||
def from_json(cls, val: ConfigValue, logger: Log) -> Self:
|
||||
"""Create from JSON."""
|
||||
if val is None:
|
||||
return cls([], "")
|
||||
|
||||
if not isinstance(val, dict):
|
||||
logger.log_warning("DNS Key is malformed!")
|
||||
if val is None or not isinstance(val, dict):
|
||||
logger.log_error("DNS Key is either missing or malformed!")
|
||||
return cls([], "")
|
||||
search = maybe_or(val, "search", "")
|
||||
servers = maybe(val, "servers")
|
||||
if not isinstance(servers, list):
|
||||
logger.log_warning("Servers key is not an array!")
|
||||
return cls([], search)
|
||||
return cls(servers, search)
|
||||
logger.log_error("Servers key is not an array!")
|
||||
return cls([], "")
|
||||
return cls(servers, str(search))
|
||||
|
||||
def command(self) -> str:
|
||||
"""Option for podman container create."""
|
||||
if len(self.servers) == 0 and self.search == "":
|
||||
return ""
|
||||
if len(self.servers) == 0:
|
||||
return f"\t--dns-search={self.search} \\\n"
|
||||
return f"--dns-search={self.search}"
|
||||
if self.search == "":
|
||||
return f"\t--dns={','.join(self.servers)} \\\n"
|
||||
return f"--dns={','.join(self.servers)}"
|
||||
|
||||
cmd = f"\t--dns-search={self.search} \\\n\t--dns="
|
||||
cmd = f"--dns-search={self.search} \\\n\t--dns="
|
||||
cmd += ",".join(self.servers)
|
||||
|
||||
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
|
||||
ct_opts: ContainerOptions
|
||||
ct_network: ContainerNetwork
|
||||
env: Environment
|
||||
secrets: list
|
||||
volumes: list
|
||||
capabilities: list
|
||||
privileged: bool
|
||||
read_only: bool
|
||||
replace: bool
|
||||
restart: str
|
||||
pull_policy: str
|
||||
timezone: str
|
||||
network: Network
|
||||
dns: Dns
|
||||
ports: Ports
|
||||
env: Environment | None
|
||||
secrets: list | None
|
||||
volumes: list | None
|
||||
capabilities: list | None
|
||||
accounting: Accounting
|
||||
|
||||
def __init__(self, json: dict, logger: Log | None = None) -> None:
|
||||
|
@ -720,30 +583,20 @@ class Container:
|
|||
name = maybe(json, "name")
|
||||
if name is None:
|
||||
logger.log_error("No container name set, aborting!")
|
||||
raise ConfigError("Container has no name")
|
||||
return
|
||||
image = maybe(json, "image")
|
||||
if image is None:
|
||||
logger.log_error("No image set, aborting!")
|
||||
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?")
|
||||
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")
|
||||
env = maybe(json, "env")
|
||||
secrets = maybe(json, "secrets")
|
||||
volumes = maybe(json, "volumes")
|
||||
|
@ -751,11 +604,18 @@ class Container:
|
|||
accounting = maybe(json, "accounting")
|
||||
self.name = str(name)
|
||||
self.image = Image.from_json(image, logger)
|
||||
self.ct_opts = ct_opts
|
||||
self.ct_network = ContainerNetwork.from_json(json, 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.env = Environment.from_json(env, logger)
|
||||
self.env.file = "/var/lib/containerctl/environment-files/"
|
||||
self.env.file += f"{self.name}"
|
||||
self.env.file = "/var/lib/containerctl/containers/"
|
||||
self.env.file += f"{self.name}/environment"
|
||||
self.secrets = Secret.from_json(secrets, logger)
|
||||
self.volumes = Volume.from_json(volumes, logger)
|
||||
self.capabilities = Capability.from_json(capabilities, logger)
|
||||
|
@ -788,15 +648,26 @@ class Container:
|
|||
def create_container(self) -> str:
|
||||
"""Generate podman container create command."""
|
||||
cmd = f"# Create container {self.name}\n"
|
||||
cmd += "podman container create \\\n"
|
||||
cmd += "podman container crate \\\n"
|
||||
cmd += f"\t--name={self.name} \\\n"
|
||||
cmd += f"{self.ct_opts.command()}"
|
||||
cmd += f"{self.ct_network.command()}"
|
||||
cmd += f"{self.env.command()}"
|
||||
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"\t{self.network.command()} \\\n"
|
||||
cmd += f"\t{self.dns.command()} \\\n"
|
||||
cmd += f"{self.ports.command()}"
|
||||
if self.env is not None:
|
||||
cmd += f"\t{self.env.command()} \\\n"
|
||||
for secret in self.secrets:
|
||||
cmd += f"{secret.command()}"
|
||||
cmd += f"\t{secret.command()} \\\n"
|
||||
for volume in self.volumes:
|
||||
cmd += f"{volume.command()}"
|
||||
cmd += f"\t{volume.command()} \\\n"
|
||||
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.15"
|
||||
GENERATE_VERSION = "0.0.4"
|
||||
HEADER = f"""#!/bin/sh
|
||||
# This script was generated by containerctl v{GENERATE_VERSION}
|
||||
# Report bugs with _this script_ to <tenno+containerctl@suij.in>
|
||||
|
@ -76,12 +76,10 @@ def main() -> None:
|
|||
if len(sys.argv) > log_threshold:
|
||||
base = sys.argv[3]
|
||||
logger = Log(log_file)
|
||||
conf = Path(config_file)
|
||||
data = load_container_config(conf, logger)
|
||||
data = load_container_config(Path(config_file), logger)
|
||||
if data is None:
|
||||
logger.log_error(f"{conf.name} is invalid, aborting!")
|
||||
logger.log_error(f"{config_file} 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)
|
||||
|
@ -111,7 +109,7 @@ def main() -> None:
|
|||
script_content += ct.create_environment()
|
||||
script_content += method()
|
||||
if script_content == "":
|
||||
script_content = "true\n"
|
||||
script_content = "true"
|
||||
with s.open("w+", encoding="utf-8") as f:
|
||||
f.write(HEADER)
|
||||
f.write(script_content)
|
||||
|
|
|
@ -15,7 +15,6 @@ class Log:
|
|||
|
||||
messages: list = []
|
||||
logfile: Path
|
||||
prefix: str = ""
|
||||
|
||||
def __init__(self, path: str) -> None:
|
||||
"""Init for Log."""
|
||||
|
@ -26,19 +25,15 @@ class Log:
|
|||
def log_error(self, msg: str) -> None:
|
||||
"""Log an error."""
|
||||
now = self.timestamp()
|
||||
prefix = "(EE)"
|
||||
if self.prefix != "":
|
||||
prefix += f" {self.prefix}:"
|
||||
log_message = f"[{now}] {prefix} {msg}"
|
||||
prefix = "EE"
|
||||
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)"
|
||||
if self.prefix != "":
|
||||
prefix += f" {self.prefix}:"
|
||||
log_message = f"[{now}] {prefix} {msg}"
|
||||
prefix = "WW"
|
||||
log_message = f"[{now}] ({prefix}) {msg}"
|
||||
self.write_message(log_message)
|
||||
|
||||
def write_message(self, msg: str) -> None:
|
||||
|
@ -54,7 +49,3 @@ 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