Always stop pod

This commit is contained in:
Benedikt Ziemons 2020-11-14 18:59:47 +01:00
parent fd60cef69b
commit cd06cc445e
Signed by: ben
GPG key ID: 0F54A7ED232D3319

48
main.py
View file

@ -53,31 +53,33 @@ class PodKeeper:
last_check = datetime.utcnow() last_check = datetime.utcnow()
print(f"Starting pod {self.podname} at {last_check}", file=sys.stderr, flush=True) print(f"Starting pod {self.podname} at {last_check}", file=sys.stderr, flush=True)
podman.play.kube(self.podyaml, *self.podnet_args) podman.play.kube(self.podyaml, *self.podnet_args)
sdnotify("--ready") try:
while not self.stopping.is_set(): sdnotify("--ready")
self.waiter.wait() while not self.stopping.is_set():
self.waiter.clear() self.waiter.wait()
if self.checking.is_set(): self.waiter.clear()
self.checking.clear() if self.checking.is_set():
new_timestamp = datetime.utcnow() self.checking.clear()
pod_description = json.loads(podman.pod.inspect(self.podname)) new_timestamp = datetime.utcnow()
for container in pod_description["Containers"]: pod_description = json.loads(podman.pod.inspect(self.podname))
if container["State"] != "running": for container in pod_description["Containers"]:
print(f"Container {container['name']} exited", file=sys.stderr, flush=True) if container["State"] != "running":
print(f"Log since last check:\n{podman.logs('--since', last_check.isoformat(), container['name'])}", file=sys.stderr, flush=True) print(f"Container {container['name']} exited", file=sys.stderr, flush=True)
self.destroy() print(f"Log since last check:\n{podman.logs('--since', last_check.isoformat(), container['name'])}", file=sys.stderr, flush=True)
last_check = new_timestamp self.destroy()
last_check = new_timestamp
if self.reloading.is_set(): if self.reloading.is_set():
self.reloading.clear() self.reloading.clear()
print("Reloading pod", self.podname, file=sys.stderr, flush=True) print("Reloading pod", self.podname, file=sys.stderr, flush=True)
try: try:
podman.pod.kill("--signal", "HUP", self.podname) podman.pod.kill("--signal", "HUP", self.podname)
except sh.ErrorReturnCode: except sh.ErrorReturnCode:
print("Error reloading pod", file=sys.stderr, flush=True) print("Error reloading pod", file=sys.stderr, flush=True)
traceback.print_exc() traceback.print_exc()
self.stop_sequence() finally:
self.stop_sequence()
def stop_sequence(self): def stop_sequence(self):
print("Stopping pod", self.podname, file=sys.stderr, flush=True) print("Stopping pod", self.podname, file=sys.stderr, flush=True)