Fix default logging parameters
and add logging.basicConfig.
This commit is contained in:
parent
2c41d962db
commit
62a1566ab0
27
main.py
27
main.py
|
@ -1,4 +1,5 @@
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
import sys
|
import sys
|
||||||
|
@ -9,13 +10,13 @@ from queue import SimpleQueue
|
||||||
from signal import signal, SIGHUP, SIGINT, SIGTERM, setitimer, SIGALRM, ITIMER_REAL, SIGUSR1, SIGUSR2, strsignal
|
from signal import signal, SIGHUP, SIGINT, SIGTERM, setitimer, SIGALRM, ITIMER_REAL, SIGUSR1, SIGUSR2, strsignal
|
||||||
|
|
||||||
import click
|
import click
|
||||||
import sh as shex
|
import sh
|
||||||
# noinspection PyUnresolvedReferences
|
|
||||||
from sh import podman
|
from sh import podman
|
||||||
|
|
||||||
SERVICES_BASE_PATH = "/docker/services/"
|
SERVICES_BASE_PATH = "/docker/services/"
|
||||||
|
|
||||||
sh = shex(_out=sys.stdout, _err=sys.stderr)
|
# noinspection PyCallingNonCallable
|
||||||
|
shlog = sh(_out=sys.stdout, _err=sys.stderr)
|
||||||
sdnotify = sh.Command("systemd-notify")
|
sdnotify = sh.Command("systemd-notify")
|
||||||
|
|
||||||
|
|
||||||
|
@ -69,11 +70,11 @@ class PodKeeper:
|
||||||
os.chdir(self.podhome)
|
os.chdir(self.podhome)
|
||||||
if self.replace and podman.pod.exists(self.podname, _ok_code=[0, 1]).exit_code == 0:
|
if self.replace and podman.pod.exists(self.podname, _ok_code=[0, 1]).exit_code == 0:
|
||||||
print(f"Replacing existing pod {self.podname}", file=sys.stderr, flush=True)
|
print(f"Replacing existing pod {self.podname}", file=sys.stderr, flush=True)
|
||||||
podman.pod.stop(self.podname)
|
shlog.podman.pod.stop(self.podname)
|
||||||
podman.pod.rm("-f", self.podname)
|
shlog.podman.pod.rm("-f", self.podname)
|
||||||
|
|
||||||
print(f"Starting pod {self.podname} at {self.last_check}", file=sys.stderr, flush=True)
|
print(f"Starting pod {self.podname} at {self.last_check}", file=sys.stderr, flush=True)
|
||||||
podman.play.kube(self.podyaml, *self.podnet_args)
|
shlog.podman.play.kube(self.podyaml, *self.podnet_args)
|
||||||
try:
|
try:
|
||||||
if 'NOTIFY_SOCKET' in os.environ:
|
if 'NOTIFY_SOCKET' in os.environ:
|
||||||
sdnotify("--ready", f"--pid={os.getpid()}", "--status=Monitoring pod...")
|
sdnotify("--ready", f"--pid={os.getpid()}", "--status=Monitoring pod...")
|
||||||
|
@ -104,7 +105,7 @@ class PodKeeper:
|
||||||
def signal_pod(self, signum):
|
def signal_pod(self, signum):
|
||||||
print(f"Sending signal '{strsignal(signum)}' to pod {self.podname}", file=sys.stderr, flush=True)
|
print(f"Sending signal '{strsignal(signum)}' to pod {self.podname}", file=sys.stderr, flush=True)
|
||||||
try:
|
try:
|
||||||
podman.pod.kill("--signal", str(signum), self.podname)
|
shlog.podman.pod.kill("--signal", str(signum), self.podname)
|
||||||
except sh.ErrorReturnCode:
|
except sh.ErrorReturnCode:
|
||||||
print("Error signaling pod", file=sys.stderr, flush=True)
|
print("Error signaling pod", file=sys.stderr, flush=True)
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
@ -117,28 +118,28 @@ class PodKeeper:
|
||||||
if container["State"] != "running":
|
if container["State"] != "running":
|
||||||
print(f"Container {container['Name']} exited", file=sys.stderr, flush=True)
|
print(f"Container {container['Name']} exited", file=sys.stderr, flush=True)
|
||||||
logs_since = self.last_check - timedelta(seconds=10)
|
logs_since = self.last_check - timedelta(seconds=10)
|
||||||
logs = podman.logs('--since', logs_since.isoformat(), container['Name'])
|
print(f"Log since last check (-10s):\n", file=sys.stderr, flush=True)
|
||||||
print(f"Log since last check (-10s):\n{logs}", file=sys.stderr, flush=True)
|
shlog.podman.logs('--since', logs_since.isoformat(), container['Name'], _out=sys.stderr)
|
||||||
self.stopping.set()
|
self.stopping.set()
|
||||||
self.last_check = new_timestamp
|
self.last_check = new_timestamp
|
||||||
|
|
||||||
def stop_pod(self):
|
def stop_pod(self):
|
||||||
print("Stopping pod", self.podname, file=sys.stderr, flush=True)
|
print("Stopping pod", self.podname, file=sys.stderr, flush=True)
|
||||||
try:
|
try:
|
||||||
podman.pod.stop("-t", "19", self.podname)
|
shlog.podman.pod.stop("-t", "19", self.podname)
|
||||||
successful_stopped = True
|
successful_stopped = True
|
||||||
except sh.ErrorReturnCode:
|
except sh.ErrorReturnCode:
|
||||||
print(f"First stop of {self.podname} was not successful!", file=sys.stderr, flush=True)
|
print(f"First stop of {self.podname} was not successful!", file=sys.stderr, flush=True)
|
||||||
successful_stopped = False
|
successful_stopped = False
|
||||||
try:
|
try:
|
||||||
podman.pod.stop("-t", "5", self.podname)
|
shlog.podman.pod.stop("-t", "5", self.podname)
|
||||||
except sh.ErrorReturnCode:
|
except sh.ErrorReturnCode:
|
||||||
if not successful_stopped:
|
if not successful_stopped:
|
||||||
print(f"Second stop of {self.podname} was not successful!", file=sys.stderr, flush=True)
|
print(f"Second stop of {self.podname} was not successful!", file=sys.stderr, flush=True)
|
||||||
|
|
||||||
if self.remove:
|
if self.remove:
|
||||||
try:
|
try:
|
||||||
podman.pod.rm(self.podname)
|
shlog.podman.pod.rm(self.podname)
|
||||||
except sh.ErrorReturnCode:
|
except sh.ErrorReturnCode:
|
||||||
print(f"Removal of {self.podname} was not successful!", file=sys.stderr, flush=True)
|
print(f"Removal of {self.podname} was not successful!", file=sys.stderr, flush=True)
|
||||||
|
|
||||||
|
@ -152,6 +153,8 @@ class PodKeeper:
|
||||||
@click.option("--remove/--keep", default=True, help="Controls removal of pod after stopping")
|
@click.option("--remove/--keep", default=True, help="Controls removal of pod after stopping")
|
||||||
@click.argument("identifier")
|
@click.argument("identifier")
|
||||||
def main(network, log_driver, log_level, replace, remove, identifier):
|
def main(network, log_driver, log_level, replace, remove, identifier):
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
keeper = PodKeeper(
|
keeper = PodKeeper(
|
||||||
network=network,
|
network=network,
|
||||||
log_driver=log_driver,
|
log_driver=log_driver,
|
||||||
|
|
Loading…
Reference in a new issue