OpenSecurity/bin/opensecurity_service.pyw
author Oliver Maurhart <oliver.maurhart@ait.ac.at>
Wed, 29 Oct 2014 15:18:22 +0100
changeset 240 d7ef04254e9c
parent 91 a26757850ea9
permissions -rwxr-xr-x
lizenz fixed in all files
     1 #!/bin/env python
     2 # -*- coding: utf-8 -*-
     3 
     4 # ------------------------------------------------------------
     5 # opensecurity_service
     6 # 
     7 # turn the opensecurityd into a Windows Service
     8 #
     9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    10 #
    11 # Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology
    12 # 
    13 # 
    14 #     X-Net Services GmbH
    15 #     Elisabethstrasse 1
    16 #     4020 Linz
    17 #     AUSTRIA
    18 #     https://www.x-net.at
    19 # 
    20 #     AIT Austrian Institute of Technology
    21 #     Donau City Strasse 1
    22 #     1220 Wien
    23 #     AUSTRIA
    24 #     http://www.ait.ac.at
    25 # 
    26 # 
    27 # Licensed under the Apache License, Version 2.0 (the "License");
    28 # you may not use this file except in compliance with the License.
    29 # You may obtain a copy of the License at
    30 # 
    31 #    http://www.apache.org/licenses/LICENSE-2.0
    32 # 
    33 # Unless required by applicable law or agreed to in writing, software
    34 # distributed under the License is distributed on an "AS IS" BASIS,
    35 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    36 # See the License for the specific language governing permissions and
    37 # limitations under the License.
    38 # ------------------------------------------------------------
    39 
    40 
    41 # ------------------------------------------------------------
    42 # imports
    43 
    44 import os
    45 import os.path
    46 import time
    47 import servicemanager
    48 import urllib2
    49 import win32event
    50 import win32service
    51 import win32serviceutil
    52 
    53 from opensecurity_util import logger
    54 import opensecurityd
    55 
    56 
    57 # ------------------------------------------------------------
    58 # code
    59 
    60 
    61 class OpenSecurityService(win32serviceutil.ServiceFramework):
    62     
    63     """This is the OpenSecurity Windows Service,"""
    64 
    65     _svc_name_ = 'OpenSecurityService'
    66     _svc_display_name_ = 'OpenSecurity Service'
    67     _svc_description_ = 'OpenSecurity Daemon Service'
    68 
    69     _run = True
    70     
    71     def __init__(self, args):
    72 
    73         """Init the Service"""
    74         
    75         # super call 
    76         win32serviceutil.ServiceFramework.__init__(self, args)
    77         self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
    78 
    79 
    80     def SvcStop(self):
    81         
    82         """Stopping the Service"""
    83 
    84         self.log('OpenSecurity Service stopping...\n') 
    85         self._run = False
    86         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
    87 
    88         # as we are not running inside the
    89         # original service process space
    90         # we got to have some means to shutdown
    91         # the REST server from a distance ...
    92         #
    93         # TODO: find a better secure way to do that
    94         try:
    95             r = urllib2.urlopen('http://localhost:8080/terminate', timeout=1)
    96         except:
    97             pass
    98 
    99         win32event.SetEvent(self.hWaitStop)
   100         self.log('OpenSecurity Service stopped...\n') 
   101 
   102 
   103     def SvcDoRun(self):
   104     
   105         """Run the Service"""
   106 
   107         self.log('OpenSecurity Service started now')
   108         servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
   109                               servicemanager.PYS_SERVICE_STARTED,
   110                               (self._svc_name_, ''))
   111         self._run = True
   112         self.main()
   113 
   114 
   115     def log(self, s):
   116 
   117         """log some string to the log file"""
   118 
   119         try:
   120             logger.debug(s)
   121         except:
   122             pass
   123 
   124 
   125     def main(self):
   126 
   127         """Main service method"""
   128         opensecurityd.main()
   129 
   130 
   131 if __name__ == '__main__':
   132     """startup"""
   133     win32serviceutil.HandleCommandLine(OpenSecurityService)
   134 
   135