2 # -*- coding: utf-8 -*-
4 # ------------------------------------------------------------
7 # turn the opensecurityd into a Windows Service
9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
11 # Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology
18 # https://www.x-net.at
20 # AIT Austrian Institute of Technology
21 # Donau City Strasse 1
24 # http://www.ait.ac.at
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
31 # http://www.apache.org/licenses/LICENSE-2.0
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 # ------------------------------------------------------------
41 # ------------------------------------------------------------
51 import win32serviceutil
53 from opensecurity_util import logger
57 # ------------------------------------------------------------
61 class OpenSecurityService(win32serviceutil.ServiceFramework):
63 """This is the OpenSecurity Windows Service,"""
65 _svc_name_ = 'OpenSecurityService'
66 _svc_display_name_ = 'OpenSecurity Service'
67 _svc_description_ = 'OpenSecurity Daemon Service'
71 def __init__(self, args):
73 """Init the Service"""
76 win32serviceutil.ServiceFramework.__init__(self, args)
77 self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
82 """Stopping the Service"""
84 self.log('OpenSecurity Service stopping...\n')
86 self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
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 ...
93 # TODO: find a better secure way to do that
95 r = urllib2.urlopen('http://localhost:8080/terminate', timeout=1)
99 win32event.SetEvent(self.hWaitStop)
100 self.log('OpenSecurity Service stopped...\n')
105 """Run the Service"""
107 self.log('OpenSecurity Service started now')
108 servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
109 servicemanager.PYS_SERVICE_STARTED,
110 (self._svc_name_, ''))
117 """log some string to the log file"""
127 """Main service method"""
131 if __name__ == '__main__':
133 win32serviceutil.HandleCommandLine(OpenSecurityService)