소스 검색

make services work

Tobias Simetsreiter 4 년 전
부모
커밋
a0a768ece6

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

@@ -2,6 +2,7 @@
 def get_form(inp):
     import os
     import json
+    import traceback
     form_dir = os.path.realpath(os.path.join(__file__,"..","..","form"))
     print(form_dir)
     resp = {
@@ -10,6 +11,7 @@ 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":
@@ -18,6 +20,7 @@ def get_form(inp):
             with open(jsfile) as fd:
                 js = json.load(fd)
                 js["name"] = f
+                js = mod.get_form(js)
                 js["ok"] = True;
                 resp["js"].append(js)
         except Exception as ex:
@@ -25,24 +28,35 @@ def get_form(inp):
                 "name": f,
                 "ok": False,
                 "err": str(ex),
+                "trace": traceback.format_exc(),
             })
     return resp
 
-def submit_form(inp):
+def get_form_module(name):
     import importlib.machinery
     import os
+    base = os.path.basename(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
+
+def submit_form(inp):
+    import traceback
+    import os
 
     try:
-        base = os.path.basename(inp["name"])
-        b, ext = os.path.splitext(base)
-        modpath = os.path.join(__file__,"..","..","form", b+".py")
-        modpath = os.path.realpath(modpath)
-        mod = importlib.machinery.SourceFileLoader("bootstrap_portal.form."+b, modpath).load_module()
+        mod = get_form_module(inp["name"])
         return mod.submit_form(inp, {})
     except Exception as ex:
         return {
             "ok": False,
             "err": str(ex),
+            "trace": traceback.format_exc(),
         }
 
 def get_data(inp):

+ 13 - 8
src/bootstrap_captive/form/services.json

@@ -1,21 +1,26 @@
 {
     "schema": {
         "service": {
-            "type": "string",
+            "type": "array",
             "title": "Service",
             "required": true,
-            "enum": [
-                "ssh",
-                "hostapd",
-                "dhcpcd",
-                "networking"
-            ]
+            "items": {
+                "type": "string",
+                "title": "Option",
+                "enum": [
+                    "ssh",
+                    "hostapd",
+                    "dhcpcd",
+                    "networking"
+                ]
+            }
         },
         "action": {
             "type": "string",
             "title": "Action",
             "required": true,
             "enum": [
+                "status",
                 "enable",
                 "disable",
                 "start",
@@ -26,7 +31,7 @@
     "form": [
         {
             "key": "service",
-            "type": "radiobuttons",
+            "type": "checkboxes",
             "activeClass": "btn-success"
         },
         {

+ 12 - 2
src/bootstrap_captive/form/services.py

@@ -1,12 +1,22 @@
 
+def get_form(form):
+    return form
+
 def submit_form(inp, form):
+    from subprocess import run, PIPE
     resp = {
         "ok": True,
     }
     try:
-        print("WOLOLO")
+        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()
     except Exception as ex:
-        resp["err"].append(str(ex))
+        resp["ok"] = False
+        resp["err"] = str(ex)
 
     return resp
 

+ 13 - 15
src/bootstrap_captive/form/wifi.py

@@ -1,28 +1,26 @@
 
+def get_form(form):
+    return form
+
 def submit_form(inp, form):
     import re
     resp = {
         "ok":True,
     }
     inp_conf = inp["wifi_config"]
-    try:
-        ssid = inp_conf["ssid"].replace('"','\"')
-        pw = inp_conf["password"].replace('"','\"')
-        wifi_file = "/etc/wpa_supplicant/wpa_supplicant.conf"
-        with open(wifi_file) as fd:
-            wifi_conf = fd.read()
-
-        wifi_conf = re.sub(r'ssid=".*','ssid="'+ssid+'"', wifi_conf)
-        wifi_conf = re.sub(r'psk=".*','psk="'+pw+'"', wifi_conf)
+    ssid = inp_conf["ssid"].replace('"','\"')
+    pw = inp_conf["password"].replace('"','\"')
+    wifi_file = "/etc/wpa_supplicant/wpa_supplicant.conf"
+    with open(wifi_file) as fd:
+        wifi_conf = fd.read()
 
-        with open(wifi_file, "w") as fd:
-            fd.write(wifi_conf)
+    wifi_conf = re.sub(r'ssid=".*','ssid="'+ssid+'"', wifi_conf)
+    wifi_conf = re.sub(r'psk=".*','psk="'+pw+'"', wifi_conf)
 
-        resp["wifi_conf"] = wifi_conf
+    with open(wifi_file, "w") as fd:
+        fd.write(wifi_conf)
 
-    except Exception as ex:
-        resp["ok"] = False
-        resp["err"] = "Could not set Wifi: "+str(ex)
+    resp["wifi_conf"] = wifi_conf
 
     return resp
 

+ 1 - 1
src/http/index.html

@@ -14,6 +14,7 @@
      <div class="progress" style="height: 30px;">
        <div id="progress_bar" class="progress-bar" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: 100%"></div>
      </div>
+     <pre id="error_box"></pre>
      <div class="form-group">
        <div id="form_div">
        </div>
@@ -21,7 +22,6 @@
      <div class="form-group">
        <button onclick="portal.disablePortal()" class="btn btn-warning btn-lg form-control">Disable Portal</button>
      </div>
-     <pre id="error_box"></pre>
    </div>
  </body>
 </html>

+ 2 - 4
src/http/portal.js

@@ -95,19 +95,17 @@ portal = new function(){
                 "title": "submit",
             });
             full_form["onSubmit"] = async (errors, values) => {
-                    event.preventDefault();
                     console.log(values);
                     if (errors){
                         console.log(errors);
                     } else {
+                        event.preventDefault();
                         var data = {
                             "submit_form": values,
                         };
                         data.submit_form["name"] = full_form.name;
                         var resp = await postData("", data);
-                        var err_dom = document.createElement("pre");
-                        err_dom.innerText = JSON.stringify(resp);
-                        getId("form_div").appendChild(err_dom);
+                        getId("error_box").innerText = JSON.stringify(resp, null, 2);
                     }
                 }
             console.log(full_form);