enforce use of environment variables when running as a service stable
authorAlexandre Fayolle <alexandre.fayolle@logilab.fr>
Tue, 27 Apr 2010 10:32:25 +0000
branchstable
changeset 5414 6eeec78c65f9
parent 5413 dc896e698ab0
child 5416 cd95e4660864
enforce use of environment variables when running as a service CW_INSTANCES_DIR, CW_INSTANCES_DATA_DIR, CW_RUNTIME_DIR must be positionned at the system level. This will ensure that cubicweb-ctl commands issued on the computer will use the proper directories.
etwist/service.py
--- a/etwist/service.py	Tue Apr 27 07:49:47 2010 +0000
+++ b/etwist/service.py	Tue Apr 27 10:32:25 2010 +0000
@@ -1,23 +1,30 @@
 import os
+import sys
 
-import win32serviceutil
-import win32service
-import win32event
+try:
+    import win32serviceutil
+    import win32service
+except ImportError:
+    print 'Win32 extensions for Python are likely not installed.'
+    sys.exit(3)
+
 
 from cubicweb.etwist.server import (CubicWebRootResource, reactor, server,
                                     parsePOSTData, channel)
 
+import logging
 from logging import getLogger, handlers
 from cubicweb import set_log_methods
 from cubicweb.cwconfig import CubicWebConfiguration as cwcfg
 
-logger = getLogger('cubicweb.twisted')
-logger.handlers = [handlers.NTEventLogHandler('cubicweb')]
-
-if not os.environ.get('CW_INSTANCES_DIR'):
-    os.environ['CW_INSTANCES_DIR'] = r'C:\etc\cubicweb.d'
-if not os.environ.get('USERNAME'):
-    os.environ['USERNAME'] = 'cubicweb'
+def _check_env(env):
+    env_vars = ('CW_INSTANCES_DIR', 'CW_INSTANCES_DATA_DIR', 'CW_RUNTIME_DIR')
+    for var in env_vars:
+        if var not in env:
+            raise Exception('The environment variables %s must be set.' % \
+                            ', '.join(env_vars))            
+    if not env.get('USERNAME'):
+        env['USERNAME'] = 'cubicweb'
 
 class CWService(object, win32serviceutil.ServiceFramework):
     _svc_name_ = None
@@ -27,11 +34,13 @@
     def __init__(self, *args, **kwargs):
         win32serviceutil.ServiceFramework.__init__(self, *args, **kwargs)
         cwcfg.load_cwctl_plugins()
+        logger = getLogger('cubicweb')
         set_log_methods(CubicWebRootResource, logger)
         server.parsePOSTData = parsePOSTData
 
     def SvcStop(self):
         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
+        logger = getLogger('cubicweb.twisted')
         logger.info('stopping %s service' % self.instance)
         reactor.stop()
         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
@@ -39,8 +48,12 @@
     def SvcDoRun(self):
         self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
         logger = getLogger('cubicweb.twisted')
+        handler = handlers.NTEventLogHandler('cubicweb')
+        handler.setLevel(logging.INFO)
+        logger.addHandler(handler)
         logger.info('starting %s service' % self.instance)
         try:
+            _check_env(os.environ)
             # create the site
             config = cwcfg.config_for(self.instance)
             root_resource = CubicWebRootResource(config, False)