Compare commits
No commits in common. "acacf19a1286fa3e97f37e22f50a4ee092b7fd41" and "07bd99d38e54f00a8d5e5e9b680172fa74ba09a1" have entirely different histories.
acacf19a12
...
07bd99d38e
3 changed files with 61 additions and 13 deletions
|
@ -78,6 +78,22 @@ class Cgroup:
|
||||||
namespace = maybe_or(val, "namespace", "")
|
namespace = maybe_or(val, "namespace", "")
|
||||||
how = maybe_or(val, "how", "")
|
how = maybe_or(val, "how", "")
|
||||||
|
|
||||||
|
if not isinstance(config, list):
|
||||||
|
logger.log_warning("Config key in cgroup Config is invalid!")
|
||||||
|
config = []
|
||||||
|
|
||||||
|
if not isinstance(parent, str):
|
||||||
|
logger.log_warning("Parent key in cgroup Config is invalid!")
|
||||||
|
parent = ""
|
||||||
|
|
||||||
|
if not isinstance(namespace, str):
|
||||||
|
logger.log_warning("Namespace key in cgroup Config is invalid!")
|
||||||
|
namespace = ""
|
||||||
|
|
||||||
|
if not isinstance(how, str):
|
||||||
|
logger.log_warning("How key in cgroup Config is invalid!")
|
||||||
|
how = ""
|
||||||
|
|
||||||
if how == "split" and parent != "":
|
if how == "split" and parent != "":
|
||||||
logger.log_warning(
|
logger.log_warning(
|
||||||
"Split cgroups can not be combined with a cgroup parent!"
|
"Split cgroups can not be combined with a cgroup parent!"
|
||||||
|
|
|
@ -34,21 +34,23 @@ def load_container_config(file: Path, log: Log) -> dict | None:
|
||||||
data = json.load(fp)
|
data = json.load(fp)
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
log.log_error(f"{file.name} is not a valid JSON file!")
|
log.log_error(f"{file.name} is not a valid JSON file!")
|
||||||
log.log_error(f"Exception: {e}")
|
log.log_debug(f"Exception: {e}")
|
||||||
return None
|
return None
|
||||||
except UnicodeDecodeError as e:
|
except UnicodeDecodeError as e:
|
||||||
log.log_error(f"{file.name} is not a valid UTF8 file!")
|
log.log_error(f"{file.name} is not a valid UTF8 file!")
|
||||||
log.log_error(f"Exception: {e}")
|
log.log_debug(f"Exception: {e}")
|
||||||
return None
|
return None
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
log.log_error(f"{file.name} could not be read!")
|
log.log_error(f"{file.name} could not be read!")
|
||||||
log.log_error(f"Exception: {e}")
|
log.log_debug(f"Exception: {e}")
|
||||||
return None
|
return None
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def create_container_from_config(data: dict, log: Log) -> Container | None:
|
def create_container_from_config(data: dict, log: Log) -> Container | None:
|
||||||
"""Create a container object."""
|
"""Create a container object."""
|
||||||
|
log.log_info("Creating container...")
|
||||||
|
log.log_debug(f"Container config is:\n{data}")
|
||||||
ct: Container | None = None
|
ct: Container | None = None
|
||||||
try:
|
try:
|
||||||
ct = Container(data)
|
ct = Container(data)
|
||||||
|
@ -80,8 +82,10 @@ def main() -> None:
|
||||||
if data is None:
|
if data is None:
|
||||||
logger.log_error(f"{config_file} is invalid, aborting!")
|
logger.log_error(f"{config_file} is invalid, aborting!")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
try:
|
||||||
ct = create_container_from_config(data, logger)
|
ct = create_container_from_config(data, logger)
|
||||||
if ct is None:
|
except ConfigError as e:
|
||||||
|
logger.log_error(e)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
scripts = {
|
scripts = {
|
||||||
"create-volumes": ct.create_volumes,
|
"create-volumes": ct.create_volumes,
|
||||||
|
|
|
@ -8,11 +8,21 @@
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import TypeAlias
|
||||||
|
|
||||||
|
UNKNOWN = -1
|
||||||
|
ERROR = 0
|
||||||
|
WARNING = 1
|
||||||
|
INFO = 2
|
||||||
|
DEBUG = 3
|
||||||
|
|
||||||
|
LogLevel: TypeAlias = int
|
||||||
|
|
||||||
|
|
||||||
class Log:
|
class Log:
|
||||||
"""Class for Logging."""
|
"""Class for Logging."""
|
||||||
|
|
||||||
|
level: LogLevel = ERROR
|
||||||
messages: list = []
|
messages: list = []
|
||||||
logfile: Path
|
logfile: Path
|
||||||
|
|
||||||
|
@ -24,6 +34,7 @@ class Log:
|
||||||
|
|
||||||
def log_error(self, msg: str) -> None:
|
def log_error(self, msg: str) -> None:
|
||||||
"""Log an error."""
|
"""Log an error."""
|
||||||
|
if self.level >= ERROR:
|
||||||
now = self.timestamp()
|
now = self.timestamp()
|
||||||
prefix = "EE"
|
prefix = "EE"
|
||||||
log_message = f"[{now}] ({prefix}) {msg}"
|
log_message = f"[{now}] ({prefix}) {msg}"
|
||||||
|
@ -31,11 +42,28 @@ class Log:
|
||||||
|
|
||||||
def log_warning(self, msg: str) -> None:
|
def log_warning(self, msg: str) -> None:
|
||||||
"""Log a warning."""
|
"""Log a warning."""
|
||||||
|
if self.level >= WARNING:
|
||||||
now = self.timestamp()
|
now = self.timestamp()
|
||||||
prefix = "WW"
|
prefix = "WW"
|
||||||
log_message = f"[{now}] ({prefix}) {msg}"
|
log_message = f"[{now}] ({prefix}) {msg}"
|
||||||
self.write_message(log_message)
|
self.write_message(log_message)
|
||||||
|
|
||||||
|
def log_info(self, msg: str) -> None:
|
||||||
|
"""Log an information."""
|
||||||
|
if self.level >= INFO:
|
||||||
|
now = self.timestamp()
|
||||||
|
prefix = "II"
|
||||||
|
log_message = f"[{now}] ({prefix}) {msg}"
|
||||||
|
self.write_message(log_message)
|
||||||
|
|
||||||
|
def log_debug(self, msg: str) -> None:
|
||||||
|
"""Log a debug message."""
|
||||||
|
if self.level >= DEBUG:
|
||||||
|
now = self.timestamp()
|
||||||
|
prefix = "DD"
|
||||||
|
log_message = f"[{now}] ({prefix}) {msg}"
|
||||||
|
self.write_message(log_message)
|
||||||
|
|
||||||
def write_message(self, msg: str) -> None:
|
def write_message(self, msg: str) -> None:
|
||||||
"""Write the message."""
|
"""Write the message."""
|
||||||
print(msg)
|
print(msg)
|
||||||
|
|
Loading…
Add table
Reference in a new issue