def shell(): res = send_command("status") if not res: print("server not running") while res: cmd = input("$> ").split() switch = Switch() switch.add_case(lambda : len(cmd) == 1 and cmd[0] == 'exit', lambda : exit(0)) switch.add_case(lambda : len(cmd) == 1 and cmd[0] == 'status', status) switch.add_case(lambda : len(cmd) == 2 and cmd[0] == 'start', lambda : start_process(cmd[1])) switch.add_case(lambda : len(cmd) == 2 and cmd[0] == 'stop', lambda : stop_process(cmd[1])) switch.add_case(lambda : len(cmd) == 2 and cmd[0] == 'exit', lambda : exit(0)) switch.add_case(lambda : len(cmd) == 1, lambda : help(cmd[0])) switch.switch()
def main(): try: server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((HOST, PORT)) except OSError as e: print(e) exit(1) jobs = load_file(sys.argv[1]) run_all_jobs(jobs) while True: server.listen() conn, _ = server.accept() req = read(conn).split(' ') switch = Switch() switch.add_case(lambda: req[0] == 'getpid', lambda: get_pid(conn)) switch.add_case(lambda: req[0] == 'stop', lambda: stop_proccess(jobs, req[1])) switch.add_case(lambda: req[0] == 'start', lambda: start_proccess(jobs, req[1])) switch.add_case(lambda: req[0] == 'signal', lambda: send_signal(jobs, req[1])) switch.add_case(lambda: req[0] == 'status', lambda: status(jobs)) switch.add_case(lambda: req[0] == 'close', lambda: exit(0)) switch.switch() conn.close()
parser = OptionParser() parser.add_option('--create', dest='create', action='store_true', help="create configuration file", default=False) parser.add_option('--startserver', dest='startserver', help='start taskmaster deamon with configuration file', metavar='FILE') parser.add_option('--stopserver', dest='stopserver', action='store_true', help="stop taskmaster deamon", default=False) parser.add_option('--status', dest='status', action='store_true', help='show list of process', default=False) parser.add_option('--stop', dest='stopprocess', default=False, help='stop proccess', metavar='COMMAND') parser.add_option('--start', dest='startprocess', default=False, help='start proccess', metavar='COMMAND') parser.add_option('--shell', dest="shell", action='store_true', help='start a stable shell', default=False) if '--create' in sys.argv: (options, args) = parser.parse_args(sys.argv[0:2]) else: (options, args) = parser.parse_args() switch = Switch() switch.add_case(lambda : options.create, lambda : manage_configuration_file(standalone='create')) switch.add_case(lambda : options.startserver, lambda : start_server(options.startserver)) switch.add_case(lambda : options.stopserver, stop_server) switch.add_case(lambda : options.status, status) switch.add_case(lambda : options.stopprocess, lambda : stop_process(options.stopprocess)) switch.add_case(lambda : options.startprocess, lambda : start_process(options.startprocess)) switch.add_case(lambda : options.shell, shell) switch.switch()
def manage_configuration_file(standalone=None): parser = OptionParser( usage= F'Usage: %prog {standalone if standalone else ""} -c configurationfile [options]' ) if standalone: parser.add_option(F'--{standalone}', dest='standalone', action='store_true') parser.add_option('-c', '--configurationfile', dest='configurationfile', help='create configuration file | add new configuration', metavar='FILE') parser.add_option('-s', '--show', default=False, action='store_true', dest='show', help='show configuration file') parser.add_option('-C', '--command', dest='cmd', help='command to launch', metavar='COMMAND') parser.add_option('-N', '--nprocess', default=None, dest='nprocess', help='number of process', metavar='NUMBER') parser.add_option('-S', '--autostart', default=None, dest='autostart', help='start at launch', metavar='yes/no') parser.add_option('-O', '--stdout', default=None, dest='stdout', help='standard output redirect file', metavar='FILE') parser.add_option('-E', '--stderr', default=None, dest='stderr', help='standard error redirect file', metavar='FILE') parser.add_option('-U', '--umask', default=None, dest='umask', help='umask to set before launching the program', metavar='NUMBER') parser.add_option('--name', dest='name', help='name of command', metavar='NAME') parser.add_option('--restart', default=None, dest='restart', help='should Restarted', metavar='always/never/unexpcted_exit') parser.add_option('--exitcodes', default=None, dest='exitcodes', help='return code represent expected exit', metavar='NUMBER,...') parser.add_option('--starttime', default=None, dest='starttime', help='time to consider successfully started in seconds', metavar='NUMBER_SECONDS') parser.add_option('--stoptime', default=None, dest='stoptime', help='time to consider successfully stopped in seconds', metavar='NUMBER_SECONDS') parser.add_option('--startretries', default=None, dest='startretries', help='restart attempts before aborted', metavar='NUMBER') parser.add_option('--stopsignal', default=None, dest='stopsignal', help='signal to stop program', metavar='SIGNAME,...') parser.add_option('--env', default=None, dest='env', help='environment variables', metavar='KEY=VAL,...') parser.add_option('--workingdir', default=None, dest='workingdir', help='working directory', metavar='STRING') (options, _) = parser.parse_args() (file, mustShow, configuration) = clean_options(options) switch = Switch() switch.add_case(lambda: mustShow, lambda: surround_exit(show_file, file)) switch.add_case(lambda: path.exists(file), lambda: surround_exit(update_file, file, configuration)) switch.add_case(lambda: not path.exists(file), lambda: surround_exit(create_file, file, configuration)) switch.switch()