def show_unit(self, unit: str = None): """ Print out the definition of unit files for a unit :param unit: File/cmd/unit to print unit file. Dots will be replaced with _ automatically """ if unit is None: units = ls(self.systempath) unit = choose_unit(self.systempath, units) if unit is None: sys.exit() show(self.systempath, unit)
def edit(self, unit: str = None): """ Opens a unit service file for editing :param unit: File/cmd/unit to edit. When omitted, show choices. """ if unit is None: units = ls(self.systempath) unit = choose_unit(self.systempath, units) if unit is None: sys.exit() unit = unit if unit.endswith(".service") else unit + ".service" os.system("$EDITOR {}/{}".format(self.systempath, unit))
def ls(self): """ Interactively show units and allow viewing them """ while True: units = ls(self.systempath) if units: unit = choose_unit(self.systempath, units) if unit is None: sys.exit() monitor(unit, self.systempath) else: print( "sysdm knows of no units. Why don't you make one? `sysdm create file_i_want_as_service.py`" ) break
def delete(self, unit: str = None): """ Delete a unit :param unit: File/cmd/unit to delete. When omitted, show choices. """ if unit is None: units = ls(self.systempath) unit = choose_unit(self.systempath, units) if unit is None: sys.exit() inp = input( "Are you sure you want to delete '{}'? [y/N]: ".format(unit)) if inp.lower().strip() != "y": print("Aborting") return delete(unit, self.systempath)
def run(self, unit: str = None, debug=False): """ Run the command of a unit once :param unit: File/cmd/unit to run. :param debug: Use debug on error if available """ if unit is None: units = ls(self.systempath) unit = choose_unit(self.systempath, units) if unit is None: sys.exit() with open(self.systempath + "/" + unit + ".service") as f: for line in f: line = line.strip() if line.startswith("ExecStart="): cmd = line.split("ExecStart=")[1] if debug: cmd = cmd.replace("python3 -u", "python3 -u -m pdb") cmd = cmd.replace("python -u", "python -u -m pdb") elif line.startswith("WorkingDirectory="): cwd = line.split("WorkingDirectory=")[1] os.system("cd {!r} && {}".format(cwd, cmd))
def _main(): parser, args = get_argparser() try: if args.systempath is None: args.systempath = "/etc/systemd/system" if IS_SUDO else "~/.config/systemd/user" args.systempath = os.path.expanduser(args.systempath) args.systempath = args.systempath.rstrip("/") try: os.makedirs(args.systempath) except FileExistsError: pass except AttributeError: # most commands have it, but not all pass if args.command == "create": print("Creating systemd unit...") service_name = install(args) print("Done") if not args.nolist: monitor(service_name, args.systempath) elif args.command == "view": service_name = to_sn(args.unit) if not os.path.exists(args.systempath + "/" + service_name + ".service"): print( "Service file does not exist. You can start by running:\n\n sysdm create {}\n\nto create a service or run:\n\n sysdm ls\n\nto see the services already created by sysdm." .format(args.unit)) sys.exit(1) monitor(service_name, args.systempath) elif args.command == "run": if args.unit is None: units = ls(args) unit = choose_unit(args.systempath, units) if unit is None: sys.exit() else: unit = args.unit with open(args.systempath + "/" + unit + ".service") as f: for line in f: line = line.strip() if line.startswith("ExecStart="): cmd = line.split("ExecStart=")[1] if args.debug: cmd = cmd.replace("python3 -u", "python3 -u -m pdb") cmd = cmd.replace("python -u", "python -u -m pdb") elif line.startswith("WorkingDirectory="): cwd = line.split("WorkingDirectory=")[1] os.system("cd {!r} && {}".format(cwd, cmd)) elif args.command == "show_unit": show(args) elif args.command == "reload": systemctl("daemon-reload") elif args.command == "watch": watch(args) elif args.command == "delete": if args.unit is None: units = ls(args) unit = choose_unit(args.systempath, units) if unit is None: sys.exit() inp = input( "Are you sure you want to delete '{}'? [y/N]: ".format(unit)) if inp.lower().strip() != "y": print("Aborting") return else: unit = args.unit delete(unit, args.systempath) elif args.command == "edit": if args.unit is None: units = ls(args) unit = choose_unit(args.systempath, units) if unit is None: sys.exit() else: unit = args.unit unit = unit if unit.endswith(".service") else unit + ".service" os.system("$EDITOR {}/{}".format(args.systempath, unit)) elif args.command == "ls": while True: units = ls(args) if units: unit = choose_unit(args.systempath, units) if unit is None: sys.exit() monitor(unit, args.systempath) else: print( "sysdm knows of no units. Why don't you make one? `sysdm create file_i_want_as_service.py`" ) break else: parser.print_help(sys.stderr) sys.exit(1)