def nsDaemonStop(ns, *args, **kw): nsconsole(ns, "Stop requested") f = lf(ns) V = f("V") if V("/etc/daemonize") is True: nsconsole(ns, "No can do: +daemonize is requested.") return f("/usr/local/bin/_stop")(*args, **kw)
def nsEnvInit(ns, *args, **kw): f = lf(ns) nsMkdir(ns, "/sys/env") nsSet(ns, "/sys/env/id", str(uuid.uuid4())) nsMkdir(ns, "/sys/env/variables") nsMkdir(ns, "/sys/env/platform") nsEnvVars(ns) nsSet(ns, "/sys/env/platform/architecture", platform.architecture()[0]) nsSet(ns, "/sys/env/platform/machine", platform.machine()) nsSet(ns, "/sys/env/platform/node", platform.node()) nsSet(ns, "/sys/env/platform/platform", platform.platform()) nsSet(ns, "/sys/env/platform/python", platform.python_version().split('.')) nsSet(ns, "/sys/env/platform/system", platform.system()) nsSet(ns, "/sys/env/platform/uname", platform.uname()) nsSet(ns, "/sys/env/uid", os.getuid()) nsSet(ns, "/sys/env/user", getpass.getuser()) nsSet(ns, "/sys/env/home", str(Path.home())) nsSet(ns, "/sys/env/cwd", os.getcwd()) home_total, home_used, home_free = shutil.disk_usage( nsGet(ns, "/sys/env/home")) nsSet(ns, "/sys/env/home.disk.size", home_total) nsSet(ns, "/sys/env/home.disk.used", home_used) nsSet(ns, "/sys/env/home.disk.free", home_free) nsSet(ns, "/sys/env/home.disk.free.percent", (home_free / home_total) * 100) nsSet(ns, "/sys/env/bootTimestamp", time.time()) nsSet(ns, "/sys/env/pid", os.getpid()) nsSet(ns, "/sys/env/me", psutil.Process(os.getpid())) nsMkdir(ns, "/sys/env/proc") try: nsSet(ns, "/sys/env/ip.addr", socket.gethostbyname(socket.gethostname())) nsSet(ns, "/sys/env/ip.addr.list", socket.gethostbyname_ex(socket.gethostname())[2]) except socket.gaierror: nsSet(ns, "/sys/env/ip.addr", '127.0.0.1') nsSet(ns, "/sys/env/ip.addr.list", ['127.0.0.1']) if nsGet(ns, "/etc/flags/truename", False) is True: nsSet(ns, "/etc/hostname", nsGet(ns, "/sys/env/platform/node")) nsEnvLoadLocalBS(ns) for k in kw: _k = "/" + k[4:].replace("_", "/") if fnmatch.fnmatch(k, "__V_*") is True: _k = "/" + k[4:].replace("_", "/") nsSet(ns, _k, kw[k]) if k[0] == "/": nsSet(ns, k, kw[k]) elif fnmatch.fnmatch(k, "__F_*") is True: dir = os.path.dirname(_k) nsMkdir(ns, dir) I(ns, _k, kw[k]) elif fnmatch.fnmatch(k, "__C_*") is True: nsCfgLoad(ns, kw[k]) elif fnmatch.fnmatch(k, "__B_*") is True: nsCfgFSLoad(ns, kw[k]) else: pass
def nsEnvRemovePid(ns): daemonFile = "{}.daemon".format(nsGet(ns, "/sys/env/pidFile")) if os.path.exists(daemonFile) and os.path.isfile(daemonFile): return f = lf(ns) V = f("V") if nsGet(ns, "/etc/daemonize") is True: return nsconsole(ns, "Removing PID file") if os.path.exists(nsGet(ns, "/sys/env/pidFile")) and os.path.isfile( nsGet(ns, "/sys/env/pidFile")): os.remove(nsGet(ns, "/sys/env/pidFile"))
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 nsDaemonStatus(ns, *args, **kw): f = lf(ns) V = f("V") pid = nsEnvLoadPid(ns) if pid is None: nsconsole(ns, "No process was found") nsDaemonCleanUp(ns) return me = psutil.Process(pid) if me.is_running() is True: nsconsole(ns, "Process is running with PID %d" % pid) else: nsconsole(ns, "Process is not running") nsDaemonCleanUp(ns)
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 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 nsGeventTick(ns): f = lf(ns) V = f("V") V("/dev/time", time.time())
def send_event(ns): f = lf(ns) nsconsole( ns, "SEND: {}".format(f("/tasks/gate/test/in")(random.randint(1, 100))))
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")()))
def testMain(ns, *args, **kw): f = lf(ns) V = f("V") V("/home/a", 42) return ns
def log_JSON(ns): time.sleep(10) f = lf(ns) f("V")("/etc/logConsoleAsJSON", True) f("/bin/info")("Test of info log message as JSON")