Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/scripts/service_hub.py
Views: 687
#!/usr/bin/env python12import os, socket, time34os.chdir(os.environ['SALVUS_ROOT'])567def cmd(s):8print(s)9if os.WEXITSTATUS(os.system(s)):10raise RuntimeError111213def hub(command, server_id):14cmd("hub {command} {args} ".format(command=command,15args=hub_args(server_id)))161718def hub_args(server_id):19if server_id != '':20port = 5000 + 2 * int(server_id)21proxy_port = port + 122else:23if args.port:24port = int(args.port)25elif args.port == -1:26port = 500027else:28port = 029if args.proxy_port:30proxy_port = int(args.proxy_port)31elif args.proxy_port == -1:32proxy_port = 500133else:34proxy_port = 035if args.share_port:36share_port = int(args.share_port)37else:38share_port = 03940agent_port = int(args.agent_port) if args.agent_port else 04142s = "--host={hostname} --websocket-server --agent_port {agent_port} {mentions} --share_path {share_path} ".format(43hostname=args.hostname,44server_id=server_id,45agent_port=agent_port,46mentions="--mentions" if args.mentions else "",47share_path=args.share_path)4849if args.database_nodes:50s += ' --database_nodes {database_nodes} '.format(51database_nodes=args.database_nodes)5253if args.kucalc:54s += ' --kucalc '5556if args.lti:57s += ' --lti '5859if args.landing:60s += ' --landing'6162if args.dev:63s += ' --dev '6465if args.single:66s += ' --single '676869if args.personal:70s += ' --personal '7172if args.update:73s += ' --update '7475if args.test:76s += ' --test '7778if args.foreground:79s += ' --foreground '80else:81logpath = "%s/../logs" % os.environ['SALVUS_ROOT']82pidpath = "%s/../pids" % os.environ['SALVUS_ROOT']83if not os.path.exists(logpath):84os.makedirs(logpath)85if not os.path.exists(pidpath):86os.makedirs(pidpath)87logfile = "%s/hub%s.log" % (logpath, server_id)88pidfile = "%s/hub%s.pid" % (pidpath, server_id)89s += " --logfile {logfile} --pidfile {pidfile} ".format(90logfile=logfile, pidfile=pidfile)9192if server_id:93s += ' --id ' + server_id9495return s969798def start_hub(server_id):99if args.foreground:100hub('', server_id)101else:102hub('start', server_id)103104105def stop_hub(server_id):106hub('stop', server_id)107108109def restart_hub(server_id):110hub('stop', server_id)111time.sleep(1)112hub('start', server_id)113114115def gap():116print("waiting %s seconds before restarting next hub" % args.gap)117time.sleep(args.gap)118119120def start(args):121for server_id in args.id.split(','):122start_hub(server_id)123124125def stop(args):126for server_id in args.id.split(','):127stop_hub(server_id)128129130def restart(args):131v = args.id.split(',')132for i, server_id in enumerate(v):133restart_hub(server_id)134if i < len(v) - 1:135gap()136137138if __name__ == "__main__":139140import argparse141parser = argparse.ArgumentParser(description="Control hub servers")142subparsers = parser.add_subparsers(help='sub-command help')143144parser.add_argument(145"--id",146help="comma separated list ids of servers to start/stop",147dest="id",148default="",149type=str)150151parser.add_argument('--database_nodes',152help="",153dest='database_nodes',154default='')155156parser.add_argument('--foreground',157help="foreground",158dest='foreground',159action="store_const",160const=True,161default=False)162163parser.add_argument('--dev',164help="dev",165dest='dev',166action="store_const",167const=True,168default=False)169170parser.add_argument('--kucalc',171help="kucalc",172dest='kucalc',173action="store_const",174const=True,175default=False)176177parser.add_argument('--lti',178help="lti",179dest='lti',180action="store_const",181const=True,182default=False)183184parser.add_argument('--landing',185help="landing",186dest='landing',187action="store_const",188const=True,189default=False)190191parser.add_argument('--single',192help="single",193dest='single',194action="store_const",195const=True,196default=False)197198parser.add_argument('--personal',199help="personal",200dest='personal',201action="store_const",202const=True,203default=False)204205parser.add_argument('--update',206help="update",207dest='update',208action="store_const",209const=True,210default=False)211212parser.add_argument('--test',213help="test",214dest='test',215action="store_const",216const=True,217default=False)218219parser.add_argument('--agent_port', dest='agent_port', type=int, default=0)220221parser.add_argument('--mentions',222help="mentions",223dest='mentions',224action="store_const",225const=True,226default=False)227228parser.add_argument('--share_path',229dest='share_path',230type=str,231default='')232233parser.add_argument(234"--hostname",235help="hostname to listen on [default: hostname of computer]",236dest="hostname",237default=socket.gethostname(),238type=str)239240parser.add_argument(241"--gap",242help=243"time (in seconds) to wait before restarting each hub [default: 10]",244dest="gap",245default=10,246type=int)247248parser_stop = subparsers.add_parser('stop', help='stop the hubs')249parser_stop.set_defaults(func=stop)250251parser_start = subparsers.add_parser('start', help='start the hubs')252parser_start.set_defaults(func=start)253254parser_restart = subparsers.add_parser('restart', help='restart the hubs')255parser_restart.set_defaults(func=restart)256257args = parser.parse_args()258args.func(args)259260261