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}") def update(): os.system("git pull") os.system("git submodule update --init") 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]}"), "pull": lambda: update(), "_dump": lambda: dump_data(), "backup": lambda: os.system(f"tar {args.args[0]} /srv /docker") "dpf": lambda: handle_aliases(fnlist, "delete-project-files")() } fnlist.get(args.action, lambda: dc(f"{args.action} {' '.join(args.args)}"))() if __name__ == "__main__": main()