def __extract_properties(self): # get working directory self._svc_cwd_ = wu.GetServiceCustomOption(self._svc_name_, 'cwd') # get virtual environment self._svc_env_ = wu.GetServiceCustomOption(self._svc_name_, 'env') # get command line self._svc_cmd_ = wu.GetServiceCustomOption(self._svc_name_, 'cmd')
def __init__(self, args): self._svc_name_, = args settings_file = win32serviceutil.GetServiceCustomOption( self._svc_name_, self.SETTINGS_FILE_PARAM) service_class_path = win32serviceutil.GetServiceCustomOption( self._svc_name_, self.CLASS_PATH_PARAM) service_class = pydoc.locate(service_class_path) self.service = service_class(settings_file) self.log("__init__ BEGIN") super().__init__(args) self.log("__init__ END")
def SvcDoRun(self): # Change to cwd registry option cwd = win32serviceutil.GetServiceCustomOption(self, 'cwd') if cwd is None: typ = servicemanager.EVENTLOG_INFORMATION_TYPE msg = ' at ' + os.getcwd() + ' by default' else: if os.path.exists(cwd): os.chdir(cwd) typ, msg = servicemanager.EVENTLOG_INFORMATION_TYPE, ' at ' + cwd else: typ = servicemanager.EVENTLOG_WARNING_TYPE msg = ' at ' + os.getcwd() + '. Missing directory: ' + cwd servicelogfile = os.path.join(cwd, 'service.log') sys.stdout = sys.stderr = io.open(servicelogfile, 'a', encoding='utf-8') # Log start of service servicemanager.LogMsg(typ, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, msg)) # Run Gramex try: port = self._svc_port_ import gramex gramex.commandline(None if port is None else ['--listen.port=%d' % port]) except Exception: typ = servicemanager.EVENTLOG_ERROR_TYPE msg = [''.join(traceback.format_exc())] servicemanager.LogMsg(typ, servicemanager.PYS_SERVICE_STOPPED, (self._svc_name_, msg))
def SvcDoRun(self): configfile = win32serviceutil.GetServiceCustomOption( self._svc_name_, "config") os.chdir(os.path.dirname(configfile)) self.log("Using config file %s." % configfile) self.log("Logging at %s." % os.getcwd()) self.ReportServiceStatus(win32service.SERVICE_RUNNING) from microscope.device_server import ( serve_devices, validate_devices, DeviceServerOptions, ) options = DeviceServerOptions( config_fpath=configfile, logging_level=logging.INFO, ) try: devices = validate_devices(configfile) serve_devices(devices, options, self.stop_event) except Exception as e: servicemanager.LogErrorMsg(str(e)) # Exit with non-zero error code so Windows will attempt to restart. sys.exit(-1) self.log("Service shutdown complete.") self.ReportServiceStatus(win32service.SERVICE_STOPPED)
def _get_credentials(service_option): if not SERVICE_NAME: return try: return win32serviceutil.GetServiceCustomOption( serviceName=SERVICE_NAME, option=service_option) except Exception as e: manager_logger.error(e)
def getOverrideFromRegistry(svc_name): override = {} try: keys = win32serviceutil.GetServiceCustomOption(svc_name, 'wsgi_override_keys') except: return override for key in keys.split(): try: val = win32serviceutil.GetServiceCustomOption( svc_name, 'wsgi_' + key) except: pass override[key] = val return override
def SvcDoRun(self): home = win32serviceutil.GetServiceCustomOption( WardenService._svc_name_, 'WARDEN_HOME', defaultValue=None) home = AutoConf.get_home(home) servicemanager.LogInfoMsg( "Warden starting up (home directory: %s) ..." % home) self.warden_server = WardenServer.WardenServer(home) self.warden_server._startup() servicemanager.LogInfoMsg("Warden start up complete") win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
def __init__(self, *args): assert self._svc_name_ is None servicename = args[0][0] self.log("Ramona service '{0}' is starting".format(servicename)) # Read working directory from registry and change to it directory = win32serviceutil.GetServiceCustomOption( servicename, 'directory') os.chdir(directory) # Set Ramona config environment variable to ensure proper configuration files load os.environ['RAMONA_CONFIG'] = win32serviceutil.GetServiceCustomOption( servicename, 'config') from ..server.svrapp import server_app self.svrapp = server_app() self.configure() win32serviceutil.ServiceFramework.__init__(self, *args)
def SvcDoRun(self): self.ReportServiceStatus(win32service.SERVICE_START_PENDING) self.ReportServiceStatus(win32service.SERVICE_RUNNING) servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, '')) SID = win32serviceutil.GetServiceCustomOption(SERVICE_NAME, 'sid') servicemanager.LogInfoMsg('SID {}'.format(SID)) while True: try: data = monitor.gather_data(SID) except Exception as e: servicemanager.LogErrorMsg('ERROR: {}'.format(e)) pass if win32event.WaitForSingleObject(self.hWaitStop, 100) == win32event.WAIT_OBJECT_0: break
def __init__(self, args): win32serviceutil.ServiceFramework.__init__(self, args) parser = OptionParser( "usage: \%prog [-d <directory>] [-s <server>] [-l <login>] [-p <password>]" ) parser.add_option("-d", "--directory", dest="directory", help="upload directory", default=".\\") parser.add_option("-s", "--server", dest="server", help="auth server host", default="localhost") parser.add_option("-l", "--login", dest="login", help="login", default="") parser.add_option("-p", "--password", dest="password", help="password", default="") parser.add_option("-c", "--config", dest="config", help="config file", default="") parser.add_option("-f", "--filelog", dest="filelog", help="log file name", default="service.log") argv = eval( win32serviceutil.GetServiceCustomOption( XMLBlasterFileSenderService, "argv", "[]")) (self.options, args) = parser.parse_args(argv) self.setupLog() logging.info("argv=%s", argv)
def SvcDoRun(self): # Change to cwd registry option cwd = win32serviceutil.GetServiceCustomOption(self, 'cwd') if cwd is None: typ = servicemanager.EVENTLOG_INFORMATION_TYPE msg = ' at ' + os.getcwd() + ' by default' else: if os.path.exists(cwd): os.chdir(cwd) typ, msg = servicemanager.EVENTLOG_INFORMATION_TYPE, ' at ' + cwd else: typ = servicemanager.EVENTLOG_WARNING_TYPE msg = ' at ' + os.getcwd() + '. Missing directory: ' + cwd # Log start of service servicemanager.LogMsg(typ, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, msg)) # Run Gramex try: port = self._svc_port_ import gramex gramex.commandline(None if port is None else ['--listen.port=%d' % port]) except Exception: # TODO: log traceback in event log pass
def SvcDoRun(self): import servicemanager servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, '')) configfile = win32serviceutil.GetServiceCustomOption( proxyService._svc_name_, 'config') if configfile == None or configfile == "": self_path = os.path.dirname( unicode(sys.executable, sys.getfilesystemencoding())) configfile = self_path + '\\proxy.ini' try: class winDHCPD(DHCPD): def log(self, level, message): if level == 'info': servicemanager.LogInfoMsg(message) self.logger.info(message) else: self.logger.debug(message) server = winDHCPD(configfile) except socket.error, msg: print "Error initiating on normal port, will try only 4011"
def __init__(self, args): fname = win32serviceutil.GetServiceCustomOption( self, "ConfigurationFile") edna.Server.__init__(self, fname) TCPServerService.TCPServerService.__init__(self, args)
def getCfgNameFromRegistry(svc_name): return win32serviceutil.GetServiceCustomOption(svc_name, 'wsgi_ini_file')
def _checkConfig(self): # Locate our child process runner (but only when run from source) if not is_frozen: # Running from source python_exe = os.path.join(sys.prefix, "python.exe") if not os.path.isfile(python_exe): # for ppl who build Python itself from source. python_exe = os.path.join(sys.prefix, "PCBuild", "python.exe") if not os.path.isfile(python_exe): # virtualenv support python_exe = os.path.join(sys.prefix, "Scripts", "python.exe") if not os.path.isfile(python_exe): self.error("Can not find python.exe to spawn subprocess") return False me = __file__ if me.endswith(".pyc") or me.endswith(".pyo"): me = me[:-1] self.runner_prefix = '"%s" "%s"' % (python_exe, me) else: # Running from a py2exe built executable - our child process is # us (but with the funky cmdline args!) self.runner_prefix = '"' + sys.executable + '"' # Now our arg processing - this may be better handled by a # twisted/buildbot style config file - but as of time of writing, # MarkH is clueless about such things! # Note that the "arguments" you type into Control Panel for the # service do *not* persist - they apply only when you click "start" # on the service. When started by Windows, args are never presented. # Thus, it is the responsibility of the service to persist any args. # so, when args are presented, we save them as a "custom option". If # they are not presented, we load them from the option. self.dirs = [] if len(self.args) > 1: dir_string = os.pathsep.join(self.args[1:]) save_dirs = True else: dir_string = win32serviceutil.GetServiceCustomOption(self, "directories") save_dirs = False if not dir_string: self.error("You must specify the buildbot directories as " "parameters to the service.\nStopping the service.") return False dirs = dir_string.split(os.pathsep) for d in dirs: d = os.path.abspath(d) sentinal = os.path.join(d, "buildbot.tac") if os.path.isfile(sentinal): self.dirs.append(d) else: msg = "Directory '%s' is not a buildbot dir - ignoring" \ % (d, ) self.warning(msg) if not self.dirs: self.error("No valid buildbot directories were specified.\n" "Stopping the service.") return False if save_dirs: dir_string = os.pathsep.join(self.dirs) win32serviceutil.SetServiceCustomOption(self, "directories", dir_string) return True
def get_config_file(cls): return win32serviceutil.GetServiceCustomOption(cls._svc_name_, "config")
class HEDB: HOST = config['HeRecoveryDB']['Host'] NAME = config['HeRecoveryDB']['Name'] USER = win32serviceutil.GetServiceCustomOption(Service.NAME, 'DB_HE_USER') PASS = win32serviceutil.GetServiceCustomOption(Service.NAME, 'DB_HE_PASS')
if win32event.WaitForSingleObject(self.hWaitStop, 100) == win32event.WAIT_OBJECT_0: break def instart (): module_path = os.path.splitext(os.path.realpath(__file__))[0] class_name = "{}.{}".format(module_path, AppServerSvc.__name__) win32serviceutil.InstallService(class_name, SERVICE_NAME, SERVICE_DISPLAY_NAME, startType=win32service.SERVICE_AUTO_START, ) print('Install OK') win32serviceutil.StartService(SERVICE_NAME) if __name__ == '__main__': is_installed = True SID = win32serviceutil.GetServiceCustomOption(SERVICE_NAME, 'sid') if SID is None: # SID is missing, ask for it from user prompt_string = """ ######################################## Hint: You can paste into this window by right clicking the window header and selecting 'Edit' > 'Paste' ######################################## Please enter your Server ID: """ SID = raw_input(prompt_string) win32serviceutil.SetServiceCustomOption(SERVICE_NAME, 'sid', SID) is_installed = False
def getconfig(): return ( win32serviceutil.GetServiceCustomOption('n2txd', 'host'), win32serviceutil.GetServiceCustomOption('n2txd', 'port'), win32serviceutil.GetServiceCustomOption('n2txd', 'key') )