예제 #1
0
    def run(self):
        initCfg()

        logger.debug('Running Daemon')
        set_proctitle('UDSActorDaemon')

        # Linux daemon will continue running unless something is requested to
        if self.interactWithBroker() is False:
            logger.debug('Interact with broker returned false, stopping service after a while')
            return

        if self.isAlive is False:
            logger.debug('The service is not alive after broker interaction, stopping it')
            return

        if self.rebootRequested is True:
            logger.debug('Reboot has been requested, stopping service')
            return

        self.initIPC()

        # *********************
        # * Main Service loop *
        # *********************
        # Counter used to check ip changes only once every 10 seconds, for
        # example
        counter = 0
        while self.isAlive:
            counter += 1
            if counter % 10 == 0:
                self.checkIpsChanged()
            # In milliseconds, will break
            self.doWait(1000)

        self.endIPC()
        self.endAPI()

        self.notifyStop()
예제 #2
0
    def SvcDoRun(self):
        '''
        Main service loop
        '''
        try:
            initCfg()

            logger.debug('running SvcDoRun')
            servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                                  servicemanager.PYS_SERVICE_STARTED,
                                  (self._svc_name_, ''))

            # call the CoInitialize to allow the registration to run in an other
            # thread
            logger.debug('Initializing com...')
            pythoncom.CoInitialize()

            # ********************************************************
            # * Ask brokers what to do before proceding to main loop *
            # ********************************************************
            while True:
                brokerConnected = self.interactWithBroker()
                if brokerConnected is False:
                    logger.debug(
                        'Interact with broker returned false, stopping service after a while'
                    )
                    self.notifyStop()
                    win32event.WaitForSingleObject(self.hWaitStop, 5000)
                    return
                elif brokerConnected is True:
                    break

                # If brokerConnected returns None, repeat the cycle
                self.doWait(16000)  # Wait for a looong while

            if self.interactWithBroker() is False:
                logger.debug(
                    'Interact with broker returned false, stopping service after a while'
                )
                self.notifyStop()
                win32event.WaitForSingleObject(self.hWaitStop, 5000)
                return

            if self.isAlive is False:
                logger.debug(
                    'The service is not alive after broker interaction, stopping it'
                )
                self.notifyStop()
                return

            if self.rebootRequested is True:
                logger.debug('Reboot has been requested, stopping service')
                self.notifyStop()
                return

            self.initIPC()
        except Exception:  # Any init exception wil be caught, service must be then restarted
            logger.exception()
            logger.debug('Exiting service with failure status')
            os._exit(-1)  # pylint: disable=protected-access

        # ********************************
        # * Registers SENS subscriptions *
        # ********************************
        logevent('Registering ISensLogon')
        subscription_guid = '{41099152-498E-11E4-8FD3-10FEED05884B}'
        sl = SensLogon(self)
        subscription_interface = pythoncom.WrapObject(sl)

        event_system = win32com.client.Dispatch(PROGID_EventSystem)

        event_subscription = win32com.client.Dispatch(PROGID_EventSubscription)
        event_subscription.EventClassID = SENSGUID_EVENTCLASS_LOGON
        event_subscription.PublisherID = SENSGUID_PUBLISHER
        event_subscription.SubscriptionName = 'UDS Actor subscription'
        event_subscription.SubscriptionID = subscription_guid
        event_subscription.SubscriberInterface = subscription_interface

        event_system.Store(PROGID_EventSubscription, event_subscription)

        logger.debug('Registered SENS, running main loop')

        # Execute script in c:\\windows\\post-uds.bat after interacting with broker, if no reboot is requested ofc
        # This will be executed only when machine gets "ready"
        try:
            if os.path.isfile(POST_CMD):
                subprocess.call([
                    POST_CMD,
                ])
            else:
                logger.info('POST file not found & not executed')
        except Exception as e:
            # Ignore output of execution command
            logger.error('Executing post command give')

        # *********************
        # * Main Service loop *
        # *********************
        # Counter used to check ip changes only once every 10 seconds, for
        # example
        counter = 0
        while self.isAlive:
            counter += 1
            # Process SENS messages, This will be a bit asyncronous (1 second
            # delay)
            pythoncom.PumpWaitingMessages()
            if counter >= 15:  # Once every 15 seconds
                counter = 0
                try:
                    self.checkIpsChanged()
                except Exception as e:
                    logger.error('Error checking ip change: {}'.format(e))
            # In milliseconds, will break
            win32event.WaitForSingleObject(self.hWaitStop, 1000)

        logger.debug('Exited main loop, deregistering SENS')

        # *******************************************
        # * Remove SENS subscription before exiting *
        # *******************************************
        event_system.Remove(PROGID_EventSubscription,
                            "SubscriptionID == " + subscription_guid)

        self.endIPC()  # Ends IPC servers
        self.endAPI()  # And deinitializes REST api if needed

        self.notifyStop()
예제 #3
0
            counter += 1
            # Process SENS messages, This will be a bit asyncronous (1 second
            # delay)
            pythoncom.PumpWaitingMessages()
            if counter >= 15:  # Once every 15 seconds
                counter = 0
                try:
                    self.checkIpsChanged()
                except Exception as e:
                    logger.error('Error checking ip change: {}'.format(e))
            # In milliseconds, will break
            win32event.WaitForSingleObject(self.hWaitStop, 1000)

        logger.debug('Exited main loop, deregistering SENS')

        # *******************************************
        # * Remove SENS subscription before exiting *
        # *******************************************
        event_system.Remove(PROGID_EventSubscription,
                            "SubscriptionID == " + subscription_guid)

        self.endIPC()  # Ends IPC servers
        self.endAPI()  # And deinitializes REST api if needed

        self.notifyStop()


if __name__ == '__main__':
    initCfg()
    win32serviceutil.HandleCommandLine(UDSActorSvc)
예제 #4
0
    def run(self):
        cfg = initCfg()  # Gets a local copy of config to get "reboot"

        logger.debug('CFG: {}'.format(cfg))

        if cfg is not None:
            self.rebootMachineAfterOp = cfg.get('reboot', True)
        else:
            self.rebootMachineAfterOp = False

        logger.info('Reboot after is {}'.format(self.rebootMachineAfterOp))

        logger.debug('Running Daemon')
        set_proctitle('UDSActorDaemon')

        # Linux daemon will continue running unless something is requested to
        while True:
            brokerConnected = self.interactWithBroker()
            if brokerConnected is False:
                logger.debug('Interact with broker returned false, stopping service after a while')
                return
            elif brokerConnected is True:
                break

            # If brokerConnected returns None, repeat the cycle
            self.doWait(16000)  # Wait for a looong while

        if self.isAlive is False:
            logger.debug('The service is not alive after broker interaction, stopping it')
            return

        if self.rebootRequested is True:
            logger.debug('Reboot has been requested, stopping service')
            return

        # Execute script in /etc/udsactor/post after interacting with broker, if no reboot is requested ofc
        # This will be executed only when machine gets "ready"
        try:

            if os.path.isfile(POST_CMD):
                if (os.stat(POST_CMD).st_mode & stat.S_IXUSR) != 0:
                    subprocess.call([POST_CMD, ])
                else:
                    logger.info('POST file exists but it it is not executable (needs execution permission by root)')
            else:
                logger.info('POST file not found & not executed')
        except Exception as e:
            # Ignore output of execution command
            logger.error('Executing post command give')

        self.initIPC()

        # *********************
        # * Main Service loop *
        # *********************
        # Counter used to check ip changes only once every 10 seconds, for
        # example
        counter = 0
        while self.isAlive:
            counter += 1
            if counter % 10 == 0:
                self.checkIpsChanged()
            # In milliseconds, will break
            self.doWait(1000)

        self.endIPC()
        self.endAPI()

        self.notifyStop()
예제 #5
0
    def SvcDoRun(self):
        '''
        Main service loop
        '''
        try:
            initCfg()

            logger.debug('running SvcDoRun')
            servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                                  servicemanager.PYS_SERVICE_STARTED,
                                  (self._svc_name_, ''))

            # call the CoInitialize to allow the registration to run in an other
            # thread
            logger.debug('Initializing com...')
            pythoncom.CoInitialize()

            # ********************************************************
            # * Ask brokers what to do before proceding to main loop *
            # ********************************************************
            if self.interactWithBroker() is False:
                logger.debug(
                    'Interact with broker returned false, stopping service after a while'
                )
                self.notifyStop()
                win32event.WaitForSingleObject(self.hWaitStop, 5000)
                return

            if self.isAlive is False:
                logger.debug(
                    'The service is not alive after broker interaction, stopping it'
                )
                self.notifyStop()
                return

            if self.rebootRequested is True:
                logger.debug('Reboot has been requested, stopping service')
                self.notifyStop()
                return

            self.initIPC()
        except Exception:  # Any init exception wil be caught, service must be then restarted
            logger.exception()
            logger.debug('Exiting service with failure status')
            os._exit(-1)  # pylint: disable=protected-access

        # ********************************
        # * Registers SENS subscriptions *
        # ********************************
        logevent('Registering ISensLogon')
        subscription_guid = '{41099152-498E-11E4-8FD3-10FEED05884B}'
        sl = SensLogon(self)
        subscription_interface = pythoncom.WrapObject(sl)

        event_system = win32com.client.Dispatch(PROGID_EventSystem)

        event_subscription = win32com.client.Dispatch(PROGID_EventSubscription)
        event_subscription.EventClassID = SENSGUID_EVENTCLASS_LOGON
        event_subscription.PublisherID = SENSGUID_PUBLISHER
        event_subscription.SubscriptionName = 'UDS Actor subscription'
        event_subscription.SubscriptionID = subscription_guid
        event_subscription.SubscriberInterface = subscription_interface

        event_system.Store(PROGID_EventSubscription, event_subscription)

        logger.debug('Registered SENS, running main loop')

        # *********************
        # * Main Service loop *
        # *********************
        # Counter used to check ip changes only once every 10 seconds, for
        # example
        counter = 0
        while self.isAlive:
            counter += 1
            # Process SENS messages, This will be a bit asyncronous (1 second
            # delay)
            pythoncom.PumpWaitingMessages()
            if counter % 10 == 0:
                self.checkIpsChanged()
            # In milliseconds, will break
            win32event.WaitForSingleObject(self.hWaitStop, 1000)

        logger.debug('Exited main loop, deregistering SENS')

        # *******************************************
        # * Remove SENS subscription before exiting *
        # *******************************************
        event_system.Remove(PROGID_EventSubscription,
                            "SubscriptionID == " + subscription_guid)

        self.endIPC()  # Ends IPC servers
        self.endAPI()  # And deinitializes REST api if needed

        self.notifyStop()
예제 #6
0
    def SvcDoRun(self):
        '''
        Main service loop
        '''
        try:
            initCfg()

            logger.debug('running SvcDoRun')
            servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                                  servicemanager.PYS_SERVICE_STARTED,
                                  (self._svc_name_, ''))

            # call the CoInitialize to allow the registration to run in an other
            # thread
            logger.debug('Initializing com...')
            pythoncom.CoInitialize()

            # ********************************************************
            # * Ask brokers what to do before proceding to main loop *
            # ********************************************************
            if self.interactWithBroker() is False:
                logger.debug('Interact with broker returned false, stopping service after a while')
                self.notifyStop()
                win32event.WaitForSingleObject(self.hWaitStop, 5000)
                return

            if self.isAlive is False:
                logger.debug('The service is not alive after broker interaction, stopping it')
                self.notifyStop()
                return

            if self.rebootRequested is True:
                logger.debug('Reboot has been requested, stopping service')
                self.notifyStop()
                return

            self.initIPC()
        except Exception:  # Any init exception wil be caught, service must be then restarted
            logger.exception()
            logger.debug('Exiting service with failure status')
            os._exit(-1)  # pylint: disable=protected-access

        # ********************************
        # * Registers SENS subscriptions *
        # ********************************
        logevent('Registering ISensLogon')
        subscription_guid = '{41099152-498E-11E4-8FD3-10FEED05884B}'
        sl = SensLogon(self)
        subscription_interface = pythoncom.WrapObject(sl)

        event_system = win32com.client.Dispatch(PROGID_EventSystem)

        event_subscription = win32com.client.Dispatch(PROGID_EventSubscription)
        event_subscription.EventClassID = SENSGUID_EVENTCLASS_LOGON
        event_subscription.PublisherID = SENSGUID_PUBLISHER
        event_subscription.SubscriptionName = 'UDS Actor subscription'
        event_subscription.SubscriptionID = subscription_guid
        event_subscription.SubscriberInterface = subscription_interface

        event_system.Store(PROGID_EventSubscription, event_subscription)

        logger.debug('Registered SENS, running main loop')

        # *********************
        # * Main Service loop *
        # *********************
        # Counter used to check ip changes only once every 10 seconds, for
        # example
        counter = 0
        while self.isAlive:
            counter += 1
            # Process SENS messages, This will be a bit asyncronous (1 second
            # delay)
            pythoncom.PumpWaitingMessages()
            if counter % 10 == 0:
                self.checkIpsChanged()
            # In milliseconds, will break
            win32event.WaitForSingleObject(self.hWaitStop, 1000)

        logger.debug('Exited main loop, deregistering SENS')

        # *******************************************
        # * Remove SENS subscription before exiting *
        # *******************************************
        event_system.Remove(
            PROGID_EventSubscription, "SubscriptionID == " + subscription_guid)

        self.endIPC()  # Ends IPC servers
        self.endAPI()  # And deinitializes REST api if needed

        self.notifyStop()
예제 #7
0
        # Counter used to check ip changes only once every 10 seconds, for
        # example
        counter = 0
        while self.isAlive:
            counter += 1
            # Process SENS messages, This will be a bit asyncronous (1 second
            # delay)
            pythoncom.PumpWaitingMessages()
            if counter % 10 == 0:
                self.checkIpsChanged()
            # In milliseconds, will break
            win32event.WaitForSingleObject(self.hWaitStop, 1000)

        logger.debug('Exited main loop, deregistering SENS')

        # *******************************************
        # * Remove SENS subscription before exiting *
        # *******************************************
        event_system.Remove(
            PROGID_EventSubscription, "SubscriptionID == " + subscription_guid)

        self.endIPC()  # Ends IPC servers
        self.endAPI()  # And deinitializes REST api if needed

        self.notifyStop()


if __name__ == '__main__':
    initCfg()
    win32serviceutil.HandleCommandLine(UDSActorSvc)
예제 #8
0
    def SvcDoRun(self):  # pylint: disable=too-many-statements, too-many-branches
        '''
        Main service loop
        '''
        try:
            initCfg()

            logger.debug('running SvcDoRun')
            servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                                  servicemanager.PYS_SERVICE_STARTED,
                                  (self._svc_name_, ''))

            # call the CoInitialize to allow the registration to run in an other
            # thread
            logger.debug('Initializing com...')
            pythoncom.CoInitialize()

            # ********************************************************
            # * Ask brokers what to do before proceding to main loop *
            # ********************************************************
            while True:
                brokerConnected = self.interactWithBroker()
                if brokerConnected is False:
                    logger.debug('Interact with broker returned false, stopping service after a while')
                    self.notifyStop()
                    win32event.WaitForSingleObject(self.hWaitStop, 5000)
                    return
                elif brokerConnected is True:
                    break

                # If brokerConnected returns None, repeat the cycle
                self.doWait(16000)  # Wait for a looong while

            if self.interactWithBroker() is False:
                logger.debug('Interact with broker returned false, stopping service after a while')
                self.notifyStop()
                win32event.WaitForSingleObject(self.hWaitStop, 5000)
                return

            if self.isAlive is False:
                logger.debug('The service is not alive after broker interaction, stopping it')
                self.notifyStop()
                return

            if self.rebootRequested is True:
                logger.debug('Reboot has been requested, stopping service')
                self.notifyStop()
                return

            self.initIPC()
        except Exception:  # Any init exception wil be caught, service must be then restarted
            logger.exception()
            logger.debug('Exiting service with failure status')
            os._exit(-1)  # pylint: disable=protected-access

        # ********************************
        # * Registers SENS subscriptions *
        # ********************************
        logevent('Registering ISensLogon')
        subscription_guid = '{41099152-498E-11E4-8FD3-10FEED05884B}'
        sl = SensLogon(self)
        subscription_interface = pythoncom.WrapObject(sl)

        event_system = win32com.client.Dispatch(PROGID_EventSystem)

        event_subscription = win32com.client.Dispatch(PROGID_EventSubscription)
        event_subscription.EventClassID = SENSGUID_EVENTCLASS_LOGON
        event_subscription.PublisherID = SENSGUID_PUBLISHER
        event_subscription.SubscriptionName = 'UDS Actor subscription'
        event_subscription.SubscriptionID = subscription_guid
        event_subscription.SubscriberInterface = subscription_interface

        event_system.Store(PROGID_EventSubscription, event_subscription)

        logger.debug('Registered SENS, running main loop')

        # Execute script in c:\\windows\\post-uds.bat after interacting with broker, if no reboot is requested ofc
        # This will be executed only when machine gets "ready"
        try:
            if os.path.isfile(POST_CMD):
                subprocess.call([POST_CMD, ])
            else:
                logger.info('POST file not found & not executed')
        except Exception as e:
            # Ignore output of execution command
            logger.error('Executing post command give')

        # *********************
        # * Main Service loop *
        # *********************
        # Counter used to check ip changes only once every 10 seconds, for
        # example
        counter = 0
        while self.isAlive:
            counter += 1
            # Process SENS messages, This will be a bit asyncronous (1 second
            # delay)
            pythoncom.PumpWaitingMessages()
            if counter >= 15:  # Once every 15 seconds
                counter = 0
                try:
                    self.checkIpsChanged()
                except Exception as e:
                    logger.error('Error checking ip change: {}'.format(e))
            # In milliseconds, will break
            win32event.WaitForSingleObject(self.hWaitStop, 1000)

        logger.debug('Exited main loop, deregistering SENS')

        # *******************************************
        # * Remove SENS subscription before exiting *
        # *******************************************
        event_system.Remove(
            PROGID_EventSubscription, "SubscriptionID == " + subscription_guid)

        self.endIPC()  # Ends IPC servers
        self.endAPI()  # And deinitializes REST api if needed

        self.notifyStop()