Exemple #1
0
    def install(self):
        """Installs the service so it can be started and stopped (if it's not installed yet).

        Returns:
            True when installing the service was a success and false
            when it failed.
        """

        # Build up wrapper script
        script_name = self.service.__class__.__name__
        module_name = win32serviceutil.GetServiceClassString(self.service.__class__)
        module_name = module_name.replace('.' + script_name, '')
        print(module_name)
        print(script_name)

        # Enable auto-start if needed
        service_start_type = None
        if self.auto_start:
            win32api.SetConsoleCtrlHandler(lambda control_handler: True, True)
            service_start_type = win32service.SERVICE_AUTO_START

        # Install the service
        win32serviceutil.InstallService(
            self.class_name,
            self.name,
            self.name,
            startType = service_start_type,
            description = self.description
        )
Exemple #2
0
 def __init__(self, cls):
     if not issubclass(cls, Daemon):
         raise TypeError
     self.cls = cls
     self.svc_name = cls._svc_name_
     self.svc_clss = win32serviceutil.GetServiceClassString(cls)
     self.svc_disp = cls._svc_display_name_ if hasattr(cls, "_svc_display_name_") else self.svc_name
     self.svc_desp = cls._svc_description_ if hasattr(cls, "_svc_description_") else None
     self.svc_deps = cls._svc_deps_ if hasattr(cls, "_svc_deps_") else None
     self.exe_name = cls._exe_name_ if hasattr(cls, "_exe_name_") else None
     self.exe_args = cls._exe_args_ if hasattr(cls, "_exe_args_") else None
Exemple #3
0
 def install(cls, username=None, password=None, start_type=None):
     if hasattr(cls, '_svc_reg_class_'):
         svc_class = cls._svc_reg_class_
     else:
         svc_class = win32serviceutil.GetServiceClassString(cls)
     win32serviceutil.InstallService(
         svc_class,
         cls._svc_name_,
         cls._svc_display_name_,
         description=cls._svc_description_,
         userName=username,
         password=password,
         startType=start_type,
     )
Exemple #4
0
 def setup(cls, cmd, user=None, password=None, startup='manual', cwd=None, wait=0):
     from gramex.config import app_log
     name, service_name = cls._svc_display_name_, cls._svc_name_
     port = getattr(cls, '_svc_port_', None)
     if cwd is None:
         cwd = os.getcwd()
     info = (name, cwd, 'port %s' % port if port is not None else '')
     service_class = win32serviceutil.GetServiceClassString(cls)
     startup = cls.startup_map[startup]
     running = win32service.SERVICE_RUNNING
     if cmd[0] == 'install':
         win32serviceutil.InstallService(
             service_class, service_name, displayName=name, description=cls._svc_description_,
             startType=startup, userName=user, password=password)
         win32serviceutil.SetServiceCustomOption(cls._svc_name_, 'cwd', cwd)
         app_log.info('Installed service. %s will run from %s %s' % info)
     elif cmd[0] in {'update', 'change'}:
         win32serviceutil.ChangeServiceConfig(
             service_class, service_name, displayName=name, description=cls._svc_description_,
             startType=startup, userName=user, password=password)
         win32serviceutil.SetServiceCustomOption(cls._svc_name_, 'cwd', cwd)
         app_log.info('Updated service. %s will run from %s %s' % info)
     elif cmd[0] in {'remove', 'uninstall'}:
         try:
             win32serviceutil.StopService(service_name)
         except pywintypes.error as e:
             if e.args[0] != winerror.ERROR_SERVICE_NOT_ACTIVE:
                 raise
         win32serviceutil.RemoveService(service_name)
         app_log.info('Removed service. %s ran from %s %s' % info)
     elif cmd[0] == 'start':
         win32serviceutil.StartService(service_name, cmd[1:])
         if wait:
             win32serviceutil.WaitForServiceStatus(service_name, running, wait)
         app_log.info('Started service %s at %s %s' % info)
     elif cmd[0] == 'stop':
         if wait:
             win32serviceutil.StopServiceWithDeps(service_name, waitSecs=wait)
         else:
             win32serviceutil.StopService(service_name)
         app_log.info('Stopped service %s at %s %s' % info)
     elif cmd[0]:
         app_log.error('Unknown command: %s' % cmd[0])
Exemple #5
0
    def __init__(self, *args, **kwargs):
        """Initializes a new instance of the PyServiceWindows class.

        Args:
            name (str):
                The name of the service, this name is used when installing or looking
                for the service.
            description (str):
                Small sentence, describing this service.
            auto_start (bool):
                True when this service needs to be started automatically when the system
                starts or when the service crashes.
        """

        # Call the constructors of both base classes
        PyServicePlatformBase.__init__(self, *args)

        # Get the service class name (not sure why this is needed but whatever)
        self.compete_name = win32serviceutil.GetServiceClassString(self.service.__class__)
Exemple #6
0
def insert_service(user, cmd, arg):
    from os import environ as env
    from os import getcwd as pwd
    from shutil import which
    # get servive name
    name = WinService.name()
    # get servise display name
    display = WinService.display_name()
    try:
        # install Servive
        wu.InstallService(wu.GetServiceClassString(WinService),
                          name,
                          display,
                          userName=user)
        # success
        click.echo(f'INSTALLED: {name}')
    except ws.error as ex:
        raise click.ClickException(ex.strerror)
    except Exception as ex:
        raise click.ClickException(ex.strerror)
Exemple #7
0
def getServiceClassString(o, argv):
    return win32serviceutil.GetServiceClassString(o, argv)
Exemple #8
0
    def SvcDoRun(self):
        app.run(host='0.0.0.0', port=5008)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOPPED)
        exit()


if __name__ == '__main__':
    try:
        servicemanager.Initialize()
        servicemanager.PrepareToHostSingle(BackendService)
        servicemanager.StartServiceCtrlDispatcher()
    except:
        try:
            win32serviceutil.StopService("IP_BACKEND")
        except:
            pass

        try:
            win32serviceutil.InstallService(
                win32serviceutil.GetServiceClassString(BackendService),
                BackendService._svc_name_,
                BackendService._svc_display_name_,
                startType=win32service.SERVICE_AUTO_START)
        except:
            pass

        win32serviceutil.StartService("IP_BACKEND")