reformat, add router shell intercept, and other tooling

This commit is contained in:
Didier Slof 2022-12-13 13:39:51 +01:00
parent 244a7d471d
commit 6591b66247
Signed by: didier
GPG key ID: 01E71F18AA4398E5
6 changed files with 57 additions and 1 deletions

109
tools/manager/main.py Normal file
View file

@ -0,0 +1,109 @@
import glob
import os
import argparse
from savefile import SaveFile
# Spaghetti code to replace the shell script
PROJECT_NAME = os.environ.get("PROJECT_NAME", "neo")
files = glob.glob("docker-compose.*.yml")
def setup_handler(what: str):
types = {
"def": lambda: os.system("sh ./utils/setup.sh"),
"ports": lambda: os.system("sh ./utils/setup-ports.sh ./utils/rulelist.rules")
}.get(what, lambda: print("Invalid setup type"))
def dc(cmd: str):
fopts = ""
for f in files:
fopts += f"-f {f} "
# print(f"docker-compose {fopts}{cmd}")
os.system(f"docker-compose -p {PROJECT_NAME} {fopts} {cmd}")
def main():
global files
ap = argparse.ArgumentParser()
ap.add_argument("--savefile", help="The savefile to use",
default=os.path.expanduser("~/.local/faulty/neo.save"))
ap.add_argument("--del-savefile", "--rm-save", help="Delete the savefile",
action="store_true")
ap.add_argument("--ignore-savefile", "--no-load", help="Ignore the savefile",
action="store_true")
# -x, --exclude: e.g. -x this -x that
ap.add_argument("-x", "--exclude", help="Exclude a file from the list",
action="append", default=[])
# -i, --include: e.g. -i this -i that
ap.add_argument("-i", "--include", help="Include a file from the list",
action="append", default=[])
ap.add_argument('--save', help="Save the list of files to the savefile", action="store_true")
ap.add_argument('action', help="The action to perform on the files",
nargs="?", default="up")
ap.add_argument('args', help="Extra arguments for the action",
nargs=argparse.REMAINDER)
args = ap.parse_args()
if args.del_savefile:
os.remove(args.savefile)
return
if not args.ignore_savefile:
savefile = SaveFile(args.savefile)
else:
savefile = SaveFile(os.path.expanduser("~/.local/faulty/neo.save"))
# load the savefile
if not args.ignore_savefile:
files = savefile.get("files", files)
if len(args.include) > 0:
files = []
for i in args.include:
files.append(f"docker-compose.{i}.yml")
if len(args.exclude) > 0:
for i in args.exclude:
files.remove(f"docker-compose.{i}.yml")
if args.save:
savefile["files"] = files
def handle_aliases(fnlist: dict, name: str):
return fnlist.get(name, lambda: print("does not exist"))
def dump_data():
print(f"savefile: {args.savefile}")
print(f"del_savefile: {args.del_savefile}")
print(f"ignore_savefile: {args.ignore_savefile}")
print(f"exclude: {args.exclude}")
print(f"include: {args.include}")
print(f"save: {args.save}")
print(f"action: {args.action}")
print(f"args: {args.args}")
print(f"files: {files}")
fnlist = {
"up": lambda: dc(f"up -d {' '.join(args.args)}"),
"upr": lambda: dc(f"up -d --build {' '.join(args.args)}"),
"logs": lambda: dc(f"logs --tail=20 -f {' '.join(args.args)}"),
"default": lambda: dc(f"{' '.join(args.args)}"),
"setup": lambda: setup_handler(args.args[0]),
"delete-project-files": lambda: os.system(f"sudo rm -rf /srv/{args.args[0]}"),
"_dump": lambda: dump_data(),
"dpf": lambda: handle_aliases(fnlist, "delete-project-files")()
}
fnlist.get(args.action, lambda: dc(f"{args.action} {' '.join(args.args)}"))()
if __name__ == "__main__":
main()