def nsDaemonLoop(ns, *args, **kw): f = lf(ns) V = f("V") if V("/etc/daemonize") is True: nsConsole(ns, "[/bin/loop] No can do: +daemonize is requested.") return pid = nsEnvLoadPid(ns) if pid is not None: me = psutil.Process(pid) if me.is_running() is True: nsconsole(ns, "Process already running with PID %d" % pid) return f("/bin/loop")(*args, **kw)
def nsDaemonStart(ns, *args, **kw): f = lf(ns) V = f("V") if V("/etc/daemonize") is False: nsConsole(ns, "No can do: +daemonize is not requested.") return pid = nsDaemonLoadPid(ns) if pid is not None: nsconsole(ns, "Application already daemonized. No can do.") return nsConsole(ns, "Starting core.NS($etc.name) daemon") nsConsole(ns, "APP name: $etc.name") nsConsole(ns, "HOST name: $etc.hostname") nsConsole(ns, "PID file: $sys.env.pidFile") f("/usr/local/bin/_start")(*args, **kw)
def nsRPCBringupServer(ns, path, host, port, maxconn): name = os.path.basename(path) dev_path = '/dev/rpc/{}'.format(name) if nsRPCisServer(ns, path) is not True: nsError(ns, "RPC service {} misconfigured".format(name)) return False nsInfo(ns, "Configuring RPC server from {}".format(path)) nsMkdir(ns, dev_path) nsMkdir(ns, "{}/root".format(dev_path)) _to_root = nsGet(ns, "{}/jail".format(path), []) for j in _to_root: _n = os.path.basename(j) _dst = "{}/root/{}".format(dev_path, _n) nsInfo(ns, "RPC.JAIL({}): {}".format(name, j)) nsLn(ns, j, _dst) dispatcher = RPCDispatcher() nsSet(ns, "{}/dispatcher".format(dev_path), dispatcher) for h in nsLs(ns, "{}/handlers".format(path)): nsInfo(ns, "Registering {}->{} ".format(name, h)) _fun = nsGet(ns, "{}/handlers/{}".format(path, h)) dispatcher.add_method(partial(_fun, dev_path), h) transport = WsgiServerTransport(queue_class=gevent.queue.Queue) nsSet(ns, "{}/transport".format(dev_path), transport) nsSet(ns, "{}/listen".format(dev_path), host) nsSet(ns, "{}/port".format(dev_path), host) nsConsole( ns, "RPC server {} will be listening on tcp://{}:{}".format( name, host, port)) pool = gevent.pool.Pool(maxconn) nsSet(ns, "{}/pool".format(dev_path), pool) wsgi_server = gevent.pywsgi.WSGIServer((host, port), transport.handle, spawn=pool, log=None) nsSet(ns, "{}/wsgi".format(dev_path), wsgi_server) nsDaemon(ns, "{}:WSGI".format(name), wsgi_server.serve_forever, _raw=True) rpc_server = RPCServerGreenlets(transport, JSONRPCProtocol(), dispatcher) if nsGet(ns, "/config/RPCCatchCalls") is True or nsGet( ns, "/etc/flags/rpctrace", False) is True: _q = gevent.queue.Queue() nsSet(ns, "{}/trace".format(dev_path), _q) rpc_server.trace = partial(nsRPCCatcher, ns, dev_path, _q) nsSet(ns, "{}/rpc", rpc_server) nsDaemon(ns, "{}:RPC".format(name), rpc_server.serve_forever, _raw=True) nsInfo(ns, "RPC server {} is up".format(name)) return True
def nsEnvLoadLocalBS(ns): CNS_HOME = nsGet(ns, "/sys/env/variables/CORENS_HOME") CNS_NAME = nsGet(ns, "/sys/env/variables/CORENS_NAME") if CNS_NAME is None: CNS_NAME = nsGet(ns, "/etc/name") if CNS_HOME is None: _path = [ "{}/.{}".format(nsGet(ns, "/sys/env/cwd"), nsGet(ns, "/etc/name")), "{}/.{}".format(nsGet(ns, "/sys/env/home"), nsGet(ns, "/etc/name")) ] for p in _path: if check_the_path(ns, p) is True: CNS_HOME = p break if CNS_HOME is None: CNS_HOME = "{}/.{}".format(nsGet(ns, "/sys/env/home"), nsGet(ns, "/etc/name")) os.mkdir(CNS_HOME) os.chmod(CNS_HOME, 0o700) nsSet(ns, "/sys/env/apphome", CNS_HOME) cfg_path = nsGet(ns, "/config/cfg.path") cfg_path.append("osfs://{}".format(CNS_HOME)) if check_the_path(ns, "/etc/{}".format(CNS_NAME)) is True: cfg_path.append("osfs:///etc/{}".format(CNS_NAME)) nsSet(ns, "/config/cfg.path", cfg_path) nsSet(ns, "/sys/env/pidFile", "{}/{}.pid".format(CNS_HOME, CNS_NAME)) pidExists = False if os.path.exists(nsGet(ns, "/sys/env/pidFile")) and os.path.isfile( nsGet(ns, "/sys/env/pidFile")): pidExists = True if nsGet(ns, "/etc/singleCopy") is True and pidExists is True: nsConsole(ns, "Application $etc.name already running") nsGlobalError(ns, "Application {} already running.".format(CNS_NAME)) raise SystemExit nsSet(ns, "/sys/env/pidFileExists", pidExists) if nsGet(ns, "/etc/flags/daemonize", False): nsEnvMkPID(ns) for p in cfg_path: nsCfgAppendFs(ns, p)
def nsDaemonMain(ns, *args, **kw): f = lf(ns) nsDaemonProcTitle(ns) nsSet(ns, "/sys/env/pid", os.getpid()) nsSet(ns, "/sys/env/me", psutil.Process(os.getpid())) nsEnvMkPID(ns) nsDaemonMkLock(ns) nsConsole(ns, "PID : $sys.env.pid") isSystem = nsGet(ns, "/etc/flags/system", False) if isSystem is True: nsConsole(ns, "LOOP name: /sbin/loop") nsDaemonProcTitle(ns, "core.NS($etc.name) eventLoop is running as /sbin/loop") f("/sbin/loop")() else: nsConsole(ns, "LOOP name: /bin/loop") nsDaemonProcTitle(ns, "core.NS($etc.name) eventLoop is running as /bin/loop") f("/bin/loop")()
def recv_event(ns): nsConsole(ns, "RECV: {}".format(f("/tasks/emit/test/empty")()))
def send_event(ns): f = lf(ns) nsConsole(ns, "SEND: {}".format(f("/tasks/pass/test/in")(random.randint(1,100))))
def recv_event(ns): nsConsole(ns, "RECV: {}".format(f("/tasks/pass/test/out")()))
def monitor_event(ns): f = lf(ns) nsConsole(ns, "MONITOR: {}".format(f("/tasks/tee/test/monitor")()))
def recv_event(ns): f = lf(ns) nsConsole(ns, "RECV: {}".format(f("/tasks/tee/test/out")()))