Add --remove option to disable removal after stop

This commit is contained in:
Ben 2020-12-30 20:15:02 +01:00
parent 11bca0af53
commit 6bf57bf761
Signed by: ben
GPG key ID: 0F54A7ED232D3319

10
main.py
View file

@ -19,9 +19,10 @@ sdnotify = sh.Command("systemd-notify")
class PodKeeper: class PodKeeper:
def __init__(self, network, replace, identifier): def __init__(self, network, replace, remove, identifier):
self.podnet_args = ("--network", network) if network else () self.podnet_args = ("--network", network) if network else ()
self.replace = replace self.replace = replace
self.remove = remove
identifier_path = pathlib.PurePath(identifier) identifier_path = pathlib.PurePath(identifier)
if len(identifier_path.parts) != 1: if len(identifier_path.parts) != 1:
raise ValueError(f"identifier has path parts: {identifier_path}") raise ValueError(f"identifier has path parts: {identifier_path}")
@ -129,6 +130,8 @@ class PodKeeper:
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:
try: try:
podman.pod.rm(self.podname) podman.pod.rm(self.podname)
except sh.ErrorReturnCode: except sh.ErrorReturnCode:
@ -138,9 +141,10 @@ class PodKeeper:
@click.command() @click.command()
@click.option("--network", default="brodge", help="Network for the created pod") @click.option("--network", default="brodge", help="Network for the created pod")
@click.option("--replace", default=True, help="Replace previously running pod with the same name") @click.option("--replace", default=True, help="Replace previously running pod with the same name")
@click.option("--remove", default=True, help="Remove pod after stopping")
@click.argument("identifier") @click.argument("identifier")
def main(network, replace, identifier): def main(network, replace, remove, identifier):
keeper = PodKeeper(network, replace, identifier) keeper = PodKeeper(network, replace, remove, identifier)
signal(SIGINT, keeper.destroy) signal(SIGINT, keeper.destroy)
signal(SIGTERM, keeper.destroy) signal(SIGTERM, keeper.destroy)