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", "")
|
||||
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 != "":
|
||||
logger.log_warning(
|
||||
"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)
|
||||
except json.JSONDecodeError as e:
|
||||
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
|
||||
except UnicodeDecodeError as e:
|
||||
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
|
||||
except OSError as e:
|
||||
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 data
|
||||
|
||||
|
||||
def create_container_from_config(data: dict, log: Log) -> Container | None:
|
||||
"""Create a container object."""
|
||||
log.log_info("Creating container...")
|
||||
log.log_debug(f"Container config is:\n{data}")
|
||||
ct: Container | None = None
|
||||
try:
|
||||
ct = Container(data)
|
||||
|
@ -80,8 +82,10 @@ def main() -> None:
|
|||
if data is None:
|
||||
logger.log_error(f"{config_file} is invalid, aborting!")
|
||||
sys.exit(1)
|
||||
ct = create_container_from_config(data, logger)
|
||||
if ct is None:
|
||||
try:
|
||||
ct = create_container_from_config(data, logger)
|
||||
except ConfigError as e:
|
||||
logger.log_error(e)
|
||||
sys.exit(1)
|
||||
scripts = {
|
||||
"create-volumes": ct.create_volumes,
|
||||
|
|
|
@ -8,11 +8,21 @@
|
|||
|
||||
import datetime
|
||||
from pathlib import Path
|
||||
from typing import TypeAlias
|
||||
|
||||
UNKNOWN = -1
|
||||
ERROR = 0
|
||||
WARNING = 1
|
||||
INFO = 2
|
||||
DEBUG = 3
|
||||
|
||||
LogLevel: TypeAlias = int
|
||||
|
||||
|
||||
class Log:
|
||||
"""Class for Logging."""
|
||||
|
||||
level: LogLevel = ERROR
|
||||
messages: list = []
|
||||
logfile: Path
|
||||
|
||||
|
@ -24,17 +34,35 @@ class Log:
|
|||
|
||||
def log_error(self, msg: str) -> None:
|
||||
"""Log an error."""
|
||||
now = self.timestamp()
|
||||
prefix = "EE"
|
||||
log_message = f"[{now}] ({prefix}) {msg}"
|
||||
self.write_message(log_message)
|
||||
if self.level >= ERROR:
|
||||
now = self.timestamp()
|
||||
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"
|
||||
log_message = f"[{now}] ({prefix}) {msg}"
|
||||
self.write_message(log_message)
|
||||
if self.level >= WARNING:
|
||||
now = self.timestamp()
|
||||
prefix = "WW"
|
||||
log_message = f"[{now}] ({prefix}) {msg}"
|
||||
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:
|
||||
"""Write the message."""
|
||||
|
|
Loading…
Add table
Reference in a new issue