Browse Source

add async systemd commands

Tobias Simetsreiter 4 years ago
parent
commit
01fda845fd

+ 2 - 6
src/bootstrap_captive/api/__init__.py

@@ -11,7 +11,6 @@ def get_form(inp):
     }
     for f in os.listdir(form_dir):
         try:
-            mod = get_form_module(f)
             jsfile = os.path.join(form_dir, f)
             print(jsfile)
             if not f[-5:].lower() == ".json":
@@ -21,6 +20,7 @@ def get_form(inp):
                 js = json.load(fd)
                 js["name"] = f[:-5]
                 try:
+                    mod = get_form_module(f)
                     js = mod.get_form(js)
                 except Exception as ex:
                     js["setup_err"] = str(ex)
@@ -42,11 +42,7 @@ def get_form_module(name):
     b, ext = os.path.splitext(base)
     modpath = os.path.join(__file__,"..","..","form", b+".py")
     modpath = os.path.realpath(modpath)
-    try:
-        return importlib.machinery.SourceFileLoader("bootstrap_portal.form."+b, modpath).load_module()
-    except Exception as ex:
-        print(ex)
-        return None
+    return importlib.machinery.SourceFileLoader("bootstrap_portal.form."+b, modpath).load_module()
 
 def submit_form(inp):
     import traceback

+ 8 - 0
src/bootstrap_captive/form/42-systemd.json

@@ -16,6 +16,10 @@
                 ]
             }
         },
+        "async_flag": {
+            "type": "boolean",
+            "default": false
+        },
         "action": {
             "type": "string",
             "title": "Action",
@@ -36,6 +40,10 @@
             "type": "checkboxes",
             "activeClass": "btn-success"
         },
+        {
+            "key": "async_flag",
+            "inlinetitle": "disregard command result (when connection loss is expected)"
+        },
         {
             "key": "action",
             "type": "radiobuttons",

+ 21 - 6
src/bootstrap_captive/form/42-systemd.py

@@ -3,21 +3,36 @@ def get_form(form):
     return form
 
 def submit_form(inp, form):
-    from subprocess import run, PIPE
+    from subprocess import run, PIPE, Popen, DEVNULL
+    import signal
     resp = {
         "ok": True,
+        "out": "",
+        "err": "",
     }
     try:
         service = inp["service"]
         action = inp["action"]
-        proc = run(["systemctl", action ]+service, stderr=PIPE, stdout=PIPE)
-        resp["ok"] = proc.returncode == 0
-        resp["err"] = proc.stderr.decode()
-        resp["out"] = proc.stdout.decode()
+        async_flag = inp["async_flag"]
+        CMD = ["systemctl", action ]+service
+        if async_flag:
+            def preexec():
+                # os.setpgrp()
+                signal.signal(signal.SIGINT, signal.SIG_IGN)
+
+            proc = Popen(CMD,
+                  stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL,
+                  preexec_fn=preexec)
+            resp["ok"] = True
+            resp["out"] = "Signal Sent: "+ " ".join(CMD)
+        else:
+            proc = run(CMD, stderr=PIPE, stdout=PIPE)
+            resp["ok"] = proc.returncode == 0
+            resp["err"] = proc.stderr.decode()
+            resp["out"] = proc.stdout.decode()
     except Exception as ex:
         resp["ok"] = False
         resp["err"] = str(ex)
 
     return resp
 
-

+ 3 - 0
src/bootstrap_captive/install.py

@@ -1,4 +1,7 @@
 
+def com_update(args):
+    pass
+
 def com_unpack(args):
     import os
     if not "UNSHARXZ_INSTALLER" in os.environ:

+ 5 - 1
src/main.py

@@ -2,7 +2,7 @@
 
 import os
 import sys
-from bootstrap_captive.install import com_install,com_unpack
+from bootstrap_captive.install import com_install,com_unpack,com_update
 from bootstrap_captive.wificonfig import set_wifi
 realp = os.path.realpath(os.path.dirname(__file__))
 
@@ -37,6 +37,10 @@ def parser():
     p_install.set_defaults(func=com_install)
     p_install.add_argument("-i", "--install_dir", default="/opt/eu.tsimnet.git/dasimmet/bootstrap_captive")
 
+    p_update = sub.add_parser("install")
+    p_update.set_defaults(func=com_update)
+    p_update.add_argument("-i", "--install_dir", default="/opt/eu.tsimnet.git/dasimmet/bootstrap_captive")
+
     p_start = sub.add_parser("start")
     p_start.set_defaults(func=com_start)