let the server and the service gracefully shutdown
authorOliver Maurhart <oliver.maurhart@ait.ac.at>
Wed, 05 Mar 2014 10:15:34 +0100
changeset 88f4f813ef9b33
parent 87 d5b04809faca
child 89 7a925dd96e2d
let the server and the service gracefully shutdown
.hgignore
OpenSecurity/bin/opensecurity_service.pyw
OpenSecurity/bin/opensecurityd.pyw
     1.1 --- a/.hgignore	Tue Mar 04 16:54:51 2014 +0100
     1.2 +++ b/.hgignore	Wed Mar 05 10:15:34 2014 +0100
     1.3 @@ -1,7 +1,15 @@
     1.4 +# what not to track in mercurial
     1.5 +syntax: glob
     1.6 +
     1.7 +# no python compiles
     1.8 +*.pyc
     1.9 +
    1.10 +# no generated setup files
    1.11 +OpenSecurity?Setup?*.exe
    1.12 +OpenSecurity?Setup?Manifest.txt
    1.13 +
    1.14 +# specific foolders and files 
    1.15  OpenSecurity/cygwin64/bin
    1.16 -OpenSecurity/cygwin64/Cygwin-Terminal.ico
    1.17 -OpenSecurity/cygwin64/Cygwin.bat
    1.18 -OpenSecurity/cygwin64/Cygwin.ico
    1.19  OpenSecurity/cygwin64/dev
    1.20  OpenSecurity/cygwin64/etc
    1.21  OpenSecurity/cygwin64/home
    1.22 @@ -9,6 +17,10 @@
    1.23  OpenSecurity/cygwin64/tmp
    1.24  OpenSecurity/cygwin64/usr
    1.25  OpenSecurity/cygwin64/var
    1.26 +OpenSecurity/cygwin64/Cygwin-Terminal.ico
    1.27 +OpenSecurity/cygwin64/Cygwin.bat
    1.28 +OpenSecurity/cygwin64/Cygwin.ico
    1.29 +OpenSecurity/log/OpenSecurity.log
    1.30  OpenSecurity/python27/DLLs
    1.31  OpenSecurity/python27/Lib
    1.32  OpenSecurity/python27/libs
     2.1 --- a/OpenSecurity/bin/opensecurity_service.pyw	Tue Mar 04 16:54:51 2014 +0100
     2.2 +++ b/OpenSecurity/bin/opensecurity_service.pyw	Wed Mar 05 10:15:34 2014 +0100
     2.3 @@ -36,6 +36,7 @@
     2.4  import os.path
     2.5  import time
     2.6  import servicemanager
     2.7 +import urllib2
     2.8  import win32event
     2.9  import win32service
    2.10  import win32serviceutil
    2.11 @@ -74,6 +75,15 @@
    2.12          self.log('OpenSecurity Service stopping...\n') 
    2.13          self._run = False
    2.14          self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
    2.15 +
    2.16 +        # as we are not running inside the
    2.17 +        # original service process space
    2.18 +        # we got to have some means to shutdown
    2.19 +        # the REST server from a distance ...
    2.20 +        #
    2.21 +        # TODO: find a better secure way to do that
    2.22 +        r = urllib2.urlopen('http://localhost:8080/terminate', timeout=1)
    2.23 +
    2.24          win32event.SetEvent(self.hWaitStop)
    2.25          self.log('OpenSecurity Service stopped...\n') 
    2.26  
     3.1 --- a/OpenSecurity/bin/opensecurityd.pyw	Tue Mar 04 16:54:51 2014 +0100
     3.2 +++ b/OpenSecurity/bin/opensecurityd.pyw	Wed Mar 05 10:15:34 2014 +0100
     3.3 @@ -107,7 +107,7 @@
     3.4          global gvm_mgr
     3.5          res = "'os_server': { "
     3.6          res += "'version': '" + __version__ + "', "
     3.7 -        res += "'machine_folder': '" + gvm_mgr.getDefaultMachineFolder() + "' "
     3.8 +        res += "'machine_folder': '" + gvm_mgr.machineFolder + "' "
     3.9          res += "}"
    3.10          return res
    3.11  
    3.12 @@ -230,6 +230,7 @@
    3.13      def GET(self):
    3.14          log_call(web.ctx.environ)
    3.15          global gvm_mgr
    3.16 +        gvm_mgr.cleanup()
    3.17          sys.exit(0)
    3.18          return None
    3.19  
    3.20 @@ -295,6 +296,16 @@
    3.21      logger.debug('Stopped OpenSecurity REST server')
    3.22  
    3.23  
    3.24 +def stop():
    3.25 +    """stop the opensecuirityd"""
    3.26 +
    3.27 +    # calling sys.exit() raises a SystemExit exception
    3.28 +    # of the WSGI Server to let it wind down
    3.29 +    # gravefully
    3.30 +    sys.exit(0)
    3.31 +
    3.32 +
    3.33 +
    3.34  # start
    3.35  if __name__ == "__main__":
    3.36      main()