Skip to content
Snippets Groups Projects
Commit e0b1ed0f authored by Martin Mareš's avatar Martin Mareš
Browse files

Colors and service helpers

parent 081c78ea
No related branches found
No related tags found
No related merge requests found
- colors - more colors
- "shc CONTAINER start" vs. "shc CONTAINER shell" - "shc CONTAINER start" vs. "shc CONTAINER shell"
Albireo cleanup: Albireo cleanup:
......
...@@ -10,7 +10,7 @@ Build-Depends: debhelper (>= 12.0), debhelper-compat (= 13), ...@@ -10,7 +10,7 @@ Build-Depends: debhelper (>= 12.0), debhelper-compat (= 13),
Package: kam-shipcat Package: kam-shipcat
Architecture: any Architecture: any
Depends: podman, adduser Depends: podman, adduser, python3-termcolor
Suggests: rsync Suggests: rsync
Description: The Ship's Cat manages containers Description: The Ship's Cat manages containers
A simple tool for managing podman containers at KAM. A simple tool for managing podman containers at KAM.
...@@ -9,9 +9,8 @@ Type=forking ...@@ -9,9 +9,8 @@ Type=forking
TimeoutStopSec=60 TimeoutStopSec=60
PIDFile=%t/shc/%i.pid PIDFile=%t/shc/%i.pid
ExecStartPre=/bin/rm -f %t/shc/%i.pid ExecStart=/usr/bin/shipcat-start %i
ExecStart=/usr/bin/podman start %i ExecStop=/usr/bin/shipcat-stop %i
ExecStop=/usr/bin/podman stop --ignore %i
RuntimeDirectory=shc RuntimeDirectory=shc
RuntimeDirectoryPreserve=yes RuntimeDirectoryPreserve=yes
......
...@@ -13,6 +13,9 @@ authors = [ ...@@ -13,6 +13,9 @@ authors = [
maintainers = [ maintainers = [
{name="Martin Mareš", email="mj@ucw.cz"}, {name="Martin Mareš", email="mj@ucw.cz"},
] ]
dependencies = ["termcolor"]
[project.scripts] [project.scripts]
shipcat = "shipcat.main:main" shipcat = "shipcat.main:main"
shipcat-start = "shipcat.main:main_service_start"
shipcat-stop = "shipcat.main:main_service_stop"
...@@ -7,40 +7,25 @@ import os ...@@ -7,40 +7,25 @@ import os
from pathlib import Path from pathlib import Path
from pwd import getpwnam from pwd import getpwnam
import socket import socket
import subprocess
import sys import sys
from typing import NoReturn, List, Optional from typing import List, Optional
from shipcat.config import GlobalConfig, ContainerConfig, ConfigError from shipcat.config import GlobalConfig, ContainerConfig, ConfigError
from shipcat.util import die, verbose, set_verbose, run_command
config: GlobalConfig config: GlobalConfig
verbose: int
def die(msg: str) -> NoReturn:
print(msg, file=sys.stderr)
sys.exit(1)
def progress(msg: str) -> None: def progress(msg: str) -> None:
print(msg, file=sys.stderr, end="", flush=True) print(msg, file=sys.stderr, end="", flush=True)
def run_command(cmd: List[str], *args, **kwargs) -> None:
if verbose:
print('RUN: ' + " ".join(cmd))
res = subprocess.run(cmd, *args, **kwargs)
if res.returncode != 0:
sys.exit(res.returncode)
def load_config() -> GlobalConfig: def load_config() -> GlobalConfig:
try: try:
return GlobalConfig.load('/etc/shipcat/shipcat.toml') return GlobalConfig.load('/etc/shipcat/shipcat.toml')
except ConfigError as e: except ConfigError as e:
print(e, file=sys.stderr) die(str(e))
sys.exit(1)
def setup_container(args: argparse.Namespace, require_root: bool, container: Optional[str] = None) -> ContainerConfig: def setup_container(args: argparse.Namespace, require_root: bool, container: Optional[str] = None) -> ContainerConfig:
...@@ -311,6 +296,33 @@ def cmd_list(args: argparse.Namespace) -> None: ...@@ -311,6 +296,33 @@ def cmd_list(args: argparse.Namespace) -> None:
print(cc.container) print(cc.container)
def main_service_start():
# Service helper for service start, called by root
if len(sys.argv) != 2:
die("Expected arguments: container")
config = load_config()
cc = ContainerConfig.load(config, sys.argv[1])
pid_file = f'/run/shc/{cc.container}.pid'
Path(pid_file).unlink(missing_ok=True)
run_command(['podman', 'start', cc.container])
def main_service_stop():
# Service helper for service stop, called by root
if len(sys.argv) != 2:
die("Expected arguments: container")
config = load_config()
cc = ContainerConfig.load(config, sys.argv[1])
run_command(['podman', 'stop', '--ignore', cc.container])
def parse_int_list(s: str) -> List[int]: def parse_int_list(s: str) -> List[int]:
return list(map(int, s.split(','))) return list(map(int, s.split(',')))
...@@ -382,8 +394,8 @@ def main() -> None: ...@@ -382,8 +394,8 @@ def main() -> None:
'stop': cmd_stop, 'stop': cmd_stop,
} }
global config, verbose global config
config = load_config() config = load_config()
verbose = args.verbose set_verbose(args.verbose)
actions[args.action](args) actions[args.action](args)
# Utility functions
import subprocess
import sys
from termcolor import cprint
from typing import NoReturn, List
verbose = 0
def die(msg: str) -> NoReturn:
cprint(msg, 'red', file=sys.stderr)
sys.exit(1)
def fatal(msg: str) -> NoReturn:
die('FATAL: ' + msg)
def error(msg: str) -> NoReturn:
die('ERROR: ' + msg)
def warn(msg: str) -> None:
cprint('WARNING: ' + msg, 'red', file=sys.stderr)
def progress(msg: str) -> None:
cprint(msg, 'green', file=sys.stderr)
def note(msg: str) -> None:
cprint(msg, 'yellow', file=sys.stderr)
def debug(msg: str, level: int = 1) -> None:
if verbose >= level:
print(msg)
def set_verbose(level: int) -> None:
global verbose
verbose = level
def run_command(cmd: List[str], *args, **kwargs) -> None:
if verbose:
print('RUN: ' + " ".join(cmd))
res = subprocess.run(cmd, *args, **kwargs)
if res.returncode != 0:
sys.exit(res.returncode)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment