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