Exemple #1
0
    def wait_for_daemon_dead(self, name, timeout=10):
        deadline = time.time() + timeout

        while time.time() < deadline:
            stats = utils.get_agent_stats(name, self.celery)
            if not stats:
                return
            self.logger.info('Waiting for daemon {0} to stop...'
                             .format(name))
            time.sleep(1)
        raise RuntimeError('Failed waiting for daemon {0} to stop. Waited '
                           'for {1} seconds'.format(name, timeout))
Exemple #2
0
    def start(self,
              interval=defaults.START_INTERVAL,
              timeout=defaults.START_TIMEOUT,
              delete_amqp_queue=defaults.DELETE_AMQP_QUEUE_BEFORE_START):

        """
        Starts the daemon process.

        :param interval: the interval in seconds to sleep when waiting for
                         the daemon to be ready.
        :param timeout: the timeout in seconds to wait for the daemon to be
                        ready.
        :param delete_amqp_queue: delete any queues with the name of the
                                  current daemon queue in the broker.

        :raise DaemonStartupTimeout: in case the agent failed to start in the
        given amount of time.
        :raise DaemonException: in case an error happened during the agent
        startup.

        """

        if delete_amqp_queue:
            self._logger.debug('Deleting AMQP queues')
            self._delete_amqp_queues()
        start_command = self.start_command()
        self._logger.info('Starting daemon with command: {0}'
                          .format(start_command))
        self._runner.run(start_command)
        end_time = time.time() + timeout
        while time.time() < end_time:
            self._logger.debug('Querying daemon {0} stats'.format(self.name))
            stats = utils.get_agent_stats(self.name, self._celery)
            if stats:
                # make sure the status command recognizes the daemon is up
                status = self.status()
                if status:
                    self._logger.debug('Daemon {0} has started'
                                       .format(self.name))
                    return
            self._logger.debug('Daemon {0} has not started yet. '
                               'Sleeping for {1} seconds...'
                               .format(self.name, interval))
            time.sleep(interval)
        self._logger.debug('Verifying there were no un-handled '
                           'exception during startup')
        self._verify_no_celery_error()
        raise exceptions.DaemonStartupTimeout(timeout, self.name)
Exemple #3
0
    def delete(self, force=defaults.DAEMON_FORCE_DELETE):
        self._logger.debug('Retrieving daemon stats')
        stats = utils.get_agent_stats(self.name, self._celery)
        if stats:
            if not force:
                raise exceptions.DaemonStillRunningException(self.name)
            self.stop()

        self._logger.info('Removing {0} service'.format(
            self.name))
        self._runner.run('{0} remove {1} confirm'.format(
            self.nssm_path,
            self.name))

        self._logger.debug('Deleting {0}'.format(self.config_path))
        if os.path.exists(self.config_path):
            os.remove(self.config_path)
Exemple #4
0
    def stop(self,
             interval=defaults.STOP_INTERVAL,
             timeout=defaults.STOP_TIMEOUT):

        """
        Stops the daemon process.

        :param interval: the interval in seconds to sleep when waiting for
                         the daemon to stop.
        :param timeout: the timeout in seconds to wait for the daemon to stop.

        :raise DaemonShutdownTimeout: in case the agent failed to be stopped
        in the given amount of time.
        :raise DaemonException: in case an error happened during the agent
        shutdown.

        """

        stop_command = self.stop_command()
        self._logger.info('Stopping daemon with command: {0}'
                          .format(stop_command))
        self._runner.run(stop_command)
        end_time = time.time() + timeout
        while time.time() < end_time:
            self._logger.debug('Querying daemon {0} stats'.format(self.name))
            # check the process has shutdown
            stats = utils.get_agent_stats(self.name, self._celery)
            if not stats:
                # make sure the status command also recognizes the
                # daemon is down
                status = self.status()
                if not status:
                    self._logger.debug('Daemon {0} has shutdown'
                                       .format(self.name, interval))
                    self._logger.debug('Deleting AMQP queues')
                    self._delete_amqp_queues()
                    return
            self._logger.debug('Daemon {0} is still running. '
                               'Sleeping for {1} seconds...'
                               .format(self.name, interval))
            time.sleep(interval)
        self._logger.debug('Verifying there were no un-handled '
                           'exception during startup')
        self._verify_no_celery_error()
        raise exceptions.DaemonShutdownTimeout(timeout, self.name)
Exemple #5
0
    def delete(self, force=defaults.DAEMON_FORCE_DELETE):

        self._logger.debug('Retrieving daemon stats')
        stats = utils.get_agent_stats(self.name, self._celery)
        if stats:
            if not force:
                raise exceptions.DaemonStillRunningException(self.name)
            self.stop()

        if os.path.exists(self.script_path):
            self._logger.debug('Deleting {0}'.format(self.script_path))
            self._runner.run('sudo rm {0}'.format(self.script_path))
        if os.path.exists(self.config_path):
            self._logger.debug('Deleting {0}'.format(self.config_path))
            self._runner.run('sudo rm {0}'.format(self.config_path))
        if os.path.exists(self.includes_path):
            self._logger.debug('Deleting {0}'.format(self.includes_path))
            self._runner.run('sudo rm {0}'.format(self.includes_path))
Exemple #6
0
def start(cloudify_agent, installer, **_):

    if ctx.type == context.NODE_INSTANCE:
        remote_execution = _get_remote_execution()
        if remote_execution:
            ctx.logger.info("Starting Agent {0}".format(cloudify_agent["name"]))
            installer.start_agent()
        else:
            # if remote_execution is False, and this operation was invoked
            # (install_agent is True), it means that some other process is
            # installing the agent (e.g userdata). All that is left for us
            # to do is wait for the agent to start.
            stats = utils.get_agent_stats(cloudify_agent["name"], app)
            if stats:
                ctx.logger.info("Agent has started")
            else:
                return ctx.operation.retry(message="Waiting for Agent to start...", retry_after=5)
    else:
        ctx.logger.info("Starting Agent {0}".format(cloudify_agent["name"]))
        installer.start_agent()
Exemple #7
0
 def assert_daemon_dead(self, name):
     stats = utils.get_agent_stats(name, self.celery)
     self.assertTrue(stats is None)