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

76
main.py
View file

@ -1,54 +1,76 @@
#! /usr/bin/python3
import os
import subprocess
import yaml
import argparse
def main():
parser = argparse.ArgumentParser(prog='podman-tool',
description='Utility for managing podman containers',
epilog=""" commands: create: create services specified in ptconfig.yml without starting;
parser = argparse.ArgumentParser(
prog="podman-tool",
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;
stop: stop one specific service or all services at once;
shell: run a shell in an existing service;
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:
repo = os.environ['REPO'] + '/'
print(f"Your current repository path is {repo}. To change it, change the REPO environment variable.")
repo = os.environ["REPO"] + "/"
print(
f"Your current repository path is {repo}. To change it, change the REPO environment variable."
)
else:
repo = ''
print("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.")
repo = ""
print(
"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'
with open(config, 'r') as ptcfg:
config = repo + "ptconfig.yml"
with open(config, "r") as ptcfg:
read_config = yaml.safe_load(ptcfg)
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("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")
parser.add_argument(
"command", help="command to run (create, start, stop, shell, karaf, down)"
)
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()
def create():
subprocess.run(["rm", "-rf", compose_config])
invalid_options = ['addons', 'bindings', 'commands']
with open(config) as oldfile, open(compose_config, 'w') as newfile:
invalid_options = ["addons", "bindings", "commands"]
with open(config) as oldfile, open(compose_config, "w") as newfile:
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)
subprocess.run(["podman-compose", "-f", compose_config, "up", "--no-start"])
for service in list(read_config['services']):
for index in list(read_config['services'][service]['addons']['bindings']['commands']):
for service in list(read_config["services"]):
for index in list(
read_config["services"][service]["addons"]["bindings"]["commands"]
):
subprocess.run([index])
def start():
start_service = args.service
if start_service == '':
if start_service == "":
start_service = list(services)
for i in range(len(start_service)):
subprocess.run(["podman-compose", "start", start_service[i]])
@ -59,7 +81,7 @@ def main():
def stop():
stop_service = args.service
if stop_service == '':
if stop_service == "":
if args.y:
confirm = "y"
else:
@ -73,14 +95,16 @@ def main():
print("aborting...")
exit(0)
else:
subprocess.run(["podman_compose", "stop", stop_service])
subprocess.run(["podman-compose", "stop", stop_service])
exit(0)
def shell():
subprocess.run(["podman-compose", "exec", args.service, "sh"])
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():
if args.y:
@ -94,7 +118,6 @@ def main():
print("aborting...")
exit(0)
if args.command == "create":
create()
elif args.command == "start":
@ -109,6 +132,5 @@ def main():
down()
if __name__ == "__main__":
main()
main()