added #!s

This commit is contained in:
R Dittberner 2026-02-05 11:44:34 +01:00
parent ee4dc7c230
commit 7e6b127c37
2 changed files with 52 additions and 27 deletions

74
main.py
View file

@ -1,54 +1,76 @@
#! /usr/bin/python3
import os import os
import subprocess import subprocess
import yaml import yaml
import argparse import argparse
def main(): def main():
parser = argparse.ArgumentParser(prog='podman-tool', parser = argparse.ArgumentParser(
description='Utility for managing podman containers', prog="podman-tool",
epilog=""" commands: create: create services specified in ptconfig.yml without starting; description="Utility for managing podman containers",
epilog=""" commands: create: create services specified in ptconfig.yml without starting;
start: start one specific service or all services at once; start: start one specific service or all services at once;
stop: stop one specific service or all services at once; stop: stop one specific service or all services at once;
shell: run a shell in an existing service; shell: run a shell in an existing service;
karaf: run a karaf shell in an existing service (only useful for openHAB); karaf: run a karaf shell in an existing service (only useful for openHAB);
down: stop all services and delete their pods""") down: stop all services and delete their pods""",
)
if "REPO" in os.environ: if "REPO" in os.environ:
repo = os.environ['REPO'] + '/' repo = os.environ["REPO"] + "/"
print(f"Your current repository path is {repo}. To change it, change the REPO environment variable.") print(
f"Your current repository path is {repo}. To change it, change the REPO environment variable."
)
else: else:
repo = '' repo = ""
print("The tool will use the config file from the directory it is running in. If you want to change this," print(
"set the REPO environment variable.") "The tool will use the config file from the directory it is running in. If you want to change this,"
"set the REPO environment variable."
)
config = repo + 'ptconfig.yml' config = repo + "ptconfig.yml"
with open(config, 'r') as ptcfg: with open(config, "r") as ptcfg:
read_config = yaml.safe_load(ptcfg) read_config = yaml.safe_load(ptcfg)
compose_config = repo + "podman-compose.yml" compose_config = repo + "podman-compose.yml"
services = read_config['services'] services = read_config["services"]
parser.add_argument("command", help="command to run (create, start, stop, shell, karaf, down)") parser.add_argument(
parser.add_argument("service", help="name of service if applicable, or leave empty for all services", nargs='?', default='') "command", help="command to run (create, start, stop, shell, karaf, down)"
parser.add_argument("-y", action="store_true", help="automatically confirm any queries") )
parser.add_argument(
"service",
help="name of service if applicable, or leave empty for all services",
nargs="?",
default="",
)
parser.add_argument(
"-y", action="store_true", help="automatically confirm any queries"
)
args = parser.parse_args() args = parser.parse_args()
def create(): def create():
subprocess.run(["rm", "-rf", compose_config]) subprocess.run(["rm", "-rf", compose_config])
invalid_options = ['addons', 'bindings', 'commands'] invalid_options = ["addons", "bindings", "commands"]
with open(config) as oldfile, open(compose_config, 'w') as newfile: with open(config) as oldfile, open(compose_config, "w") as newfile:
for line in oldfile: for line in oldfile:
if not any(invalid_option in line for invalid_option in invalid_options): if not any(
invalid_option in line for invalid_option in invalid_options
):
newfile.write(line) newfile.write(line)
subprocess.run(["podman-compose", "-f", compose_config, "up", "--no-start"]) subprocess.run(["podman-compose", "-f", compose_config, "up", "--no-start"])
for service in list(read_config['services']): for service in list(read_config["services"]):
for index in list(read_config['services'][service]['addons']['bindings']['commands']): for index in list(
read_config["services"][service]["addons"]["bindings"]["commands"]
):
subprocess.run([index]) subprocess.run([index])
def start(): def start():
start_service = args.service start_service = args.service
if start_service == '': if start_service == "":
start_service = list(services) start_service = list(services)
for i in range(len(start_service)): for i in range(len(start_service)):
subprocess.run(["podman-compose", "start", start_service[i]]) subprocess.run(["podman-compose", "start", start_service[i]])
@ -59,7 +81,7 @@ def main():
def stop(): def stop():
stop_service = args.service stop_service = args.service
if stop_service == '': if stop_service == "":
if args.y: if args.y:
confirm = "y" confirm = "y"
else: else:
@ -73,14 +95,16 @@ def main():
print("aborting...") print("aborting...")
exit(0) exit(0)
else: else:
subprocess.run(["podman_compose", "stop", stop_service]) subprocess.run(["podman-compose", "stop", stop_service])
exit(0) exit(0)
def shell(): def shell():
subprocess.run(["podman-compose", "exec", args.service, "sh"]) subprocess.run(["podman-compose", "exec", args.service, "sh"])
def karaf(): def karaf():
subprocess.run(["podman-compose", "exec", args.service, "/openhab/runtime/bin/client"]) subprocess.run(
["podman-compose", "exec", args.service, "/openhab/runtime/bin/client"]
)
def down(): def down():
if args.y: if args.y:
@ -94,7 +118,6 @@ def main():
print("aborting...") print("aborting...")
exit(0) exit(0)
if args.command == "create": if args.command == "create":
create() create()
elif args.command == "start": elif args.command == "start":
@ -109,6 +132,5 @@ def main():
down() down()
if __name__ == "__main__": if __name__ == "__main__":
main() main()

3
podman-tool Executable file
View file

@ -0,0 +1,3 @@
#! /bin/sh
python3 ./main.py