|
1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
18 from __future__ import print_function |
|
19 |
|
20 import os |
|
21 import sys |
|
22 |
|
23 try: |
|
24 import win32serviceutil |
|
25 import win32service |
|
26 except ImportError: |
|
27 print('Win32 extensions for Python are likely not installed.') |
|
28 sys.exit(3) |
|
29 |
|
30 from os.path import join |
|
31 |
|
32 from cubicweb.etwist.server import (CubicWebRootResource, reactor, server) |
|
33 |
|
34 from logilab.common.shellutils import rm |
|
35 |
|
36 import logging |
|
37 from logging import getLogger, handlers |
|
38 from cubicweb import set_log_methods |
|
39 from cubicweb.cwconfig import CubicWebConfiguration as cwcfg |
|
40 |
|
41 def _check_env(env): |
|
42 env_vars = ('CW_INSTANCES_DIR', 'CW_INSTANCES_DATA_DIR', 'CW_RUNTIME_DIR') |
|
43 for var in env_vars: |
|
44 if var not in env: |
|
45 raise Exception('The environment variables %s must be set.' % \ |
|
46 ', '.join(env_vars)) |
|
47 if not env.get('USERNAME'): |
|
48 env['USERNAME'] = 'cubicweb' |
|
49 |
|
50 class CWService(object, win32serviceutil.ServiceFramework): |
|
51 _svc_name_ = None |
|
52 _svc_display_name_ = None |
|
53 instance = None |
|
54 |
|
55 def __init__(self, *args, **kwargs): |
|
56 win32serviceutil.ServiceFramework.__init__(self, *args, **kwargs) |
|
57 cwcfg.load_cwctl_plugins() |
|
58 logger = getLogger('cubicweb') |
|
59 set_log_methods(CubicWebRootResource, logger) |
|
60 |
|
61 def SvcStop(self): |
|
62 self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) |
|
63 logger = getLogger('cubicweb.twisted') |
|
64 logger.info('stopping %s service' % self.instance) |
|
65 reactor.stop() |
|
66 self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) |
|
67 |
|
68 def SvcDoRun(self): |
|
69 self.ReportServiceStatus(win32service.SERVICE_START_PENDING) |
|
70 logger = getLogger('cubicweb.twisted') |
|
71 handler = handlers.NTEventLogHandler('cubicweb') |
|
72 handler.setLevel(logging.INFO) |
|
73 logger.addHandler(handler) |
|
74 logger.info('starting %s service' % self.instance) |
|
75 try: |
|
76 _check_env(os.environ) |
|
77 # create the site |
|
78 config = cwcfg.config_for(self.instance) |
|
79 config.init_log(force=True) |
|
80 config.debugmode = False |
|
81 logger.info('starting cubicweb instance %s ', self.instance) |
|
82 config.info('clear ui caches') |
|
83 for cachedir in ('uicache', 'uicachehttps'): |
|
84 rm(join(config.appdatahome, cachedir, '*')) |
|
85 root_resource = CubicWebRootResource(config, config.repository()) |
|
86 website = server.Site(root_resource) |
|
87 # serve it via standard HTTP on port set in the configuration |
|
88 port = config['port'] or 8080 |
|
89 logger.info('listening on port %s' % port) |
|
90 reactor.listenTCP(port, website) |
|
91 root_resource.init_publisher() |
|
92 root_resource.start_service() |
|
93 logger.info('instance started on %s', root_resource.base_url) |
|
94 self.ReportServiceStatus(win32service.SERVICE_RUNNING) |
|
95 reactor.run() |
|
96 except Exception as e: |
|
97 logger.error('service %s stopped (cause: %s)' % (self.instance, e)) |
|
98 logger.exception('what happened ...') |
|
99 self.ReportServiceStatus(win32service.SERVICE_STOPPED) |