Forráskód Böngészése

Add a simple exit method and remove the use of daemon

 * Also, fix crash when user gives wrong password.
   Instead ask for authentication again
oz123 10 éve
szülő
commit
637ece69d2
1 módosított fájl, 21 hozzáadás és 66 törlés
  1. 21 66
      scripts/webui.py

+ 21 - 66
scripts/webui.py

@@ -23,12 +23,11 @@ from pwman.util.crypto import CryptoEngine
 import pwman.data.factory
 from pwman.data.tags import TagNew
 from pwman import parser_options, get_conf_options
-from daemon import Daemon
 from pkg_resources import resource_filename
 import itertools
-import argparse
 import sys
-from os.path import expanduser, join
+from signal import SIGTERM
+import os
 
 templates_path = [resource_filename('pwman', 'ui/templates')]
 statics = [resource_filename('pwman', 'ui/templates/static')][0]
@@ -44,6 +43,11 @@ DB = None
 # WEB GUI shows multiple tags as one tag!
 
 
+@route('/exit', method=['GET'])
+def exit():
+    os.kill(os.getpid(), SIGTERM)
+
+
 def require_auth(fn):
     def check_auth(**kwargs):
         if AUTHENTICATED:
@@ -114,7 +118,13 @@ def is_authenticated():
 
     if request.method == 'POST':
         key = request.POST.get('pwd', '')
-        crypto.auth(key)
+        while True:
+            try:
+                crypto.auth(key)
+                break
+            except Exception:
+                redirect('/auth')
+
         AUTHENTICATED = True
         redirect('/')
     else:
@@ -163,21 +173,10 @@ def server_static(filepath):
     return static_file(filepath, root=statics)
 
 
-class Pwman3WebDaemon(Daemon):
+class Pwman3WebDaemon(object):
 
-    def startd(self):
-        """
-        Start the daemon
-        """
-        # Check for a pidfile to see if the daemon already runs
-        self.exit_running()
-        #if not os.path.exists(self.pidfile):
-        # Start the daemon
-        self.daemonize()
-        # after self.daemonize()
-        # all output is redirected
-        print(open(self.pidfile).read())
-        self.run()
+    def __enter__(self):
+        return self
 
     def run(self):
         global AUTHENTICATED, TAGS, DB
@@ -196,54 +195,10 @@ class Pwman3WebDaemon(Daemon):
         debug(True)
         run(port=9030)
 
+    def __exit__(self, type, value, traceback):
+        return isinstance(value, TypeError)
 
 if __name__ == '__main__':
 
-    parser = argparse.ArgumentParser(
-        description="Start the webui of pwman3",
-        usage='%(prog)s start|stop|restart|status [-p|-d]\n',
-        )
-    parser.add_argument('-D', '--NoDaemon',
-                        help='Do not fork, start in foreground',
-                        action="store_true", default=False)
-
-    ext_usage = ("{prog} start - starts the webui."
-                 "{prog} stop - stops the webui."
-                 "{prog} status - check if the process is already running."
-                 "".format(prog=parser.prog))
-
-    if len(sys.argv) == 1:
-        parser.print_help()
-        sys.exit(1)
-
-    action = sys.argv[1]
-
-    if action not in ['start', 'stop', 'restart', 'status']:
-        parser.print_help()
-        print(ext_usage)
-        sys.exit(1)
-
-    sys.argv = sys.argv[1:]
-
-    path = join(expanduser("~"), '.pwman')
-    daemon = Pwman3WebDaemon(parser.prog, join(path, '%s.pid' % parser.prog),
-                             noisy=True)
-
-    args = parser.parse_args()
-
-    if action == 'status':
-        running = daemon.check_proc()
-        if running:
-            print("%s already runs with pid: %d" % (parser.prog, running))
-    if action == 'start' and args.NoDaemon:
-        try:
-            daemon.start()
-        except KeyboardInterrupt:
-            daemon.set_level("auto")
-            daemon.stop()
-    if action == 'start' and not args.NoDaemon:
-        daemon.startd()
-    if action == "restart":
-        daemon.restart()
-    if action == "stop":
-        daemon.stop()
+    with Pwman3WebDaemon() as webui:
+        webui.run()