Example #1
0
    def ping(self) -> bool:
        """Send ping to node"""
        cmd_ping = Command([], stdout=True, timeout=20)

        # If unable to determine ip address, then do not perform ping
        if self.get_ip() is None:
            return False
        cmd_ping.args = ['ping', '-c', '1', self.get_ip()]

        execution = self.executor.execute(cmd_ping)

        # True if completed with exit code 0 and stdout has some data
        return execution.completed_successfully() and bool(
            execution.read_stdout())
Example #2
0
    def get_ip(self):
        """Get ip of node"""
        if self.ip is not None:
            return self.ip

        cmd_ip = Command(['ip', 'addr', 'list'], stdout=True, timeout=10)
        execution = self.execute(cmd_ip)

        # If execution failed, skip it
        if not execution.completed_successfully():
            return None

        # Parsing stdout and retrieving the IP
        ip_addr_out = execution.read_stdout()
        if not ip_addr_out:
            return None

        # Parse all returned ip addresses
        ip_addresses = re.findall(r'([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)',
                                  ip_addr_out, re.MULTILINE)
        try:
            ip_addresses.remove('127.0.0.1')
        except ValueError:
            return None

        # If only loop back defined, skip it
        if not ip_addresses:
            return None

        return ip_addresses[0]
 def _create_command(self, service_state: ServiceSystemState):
     """
     Creates a Command instance based on executor type and state
     that is specific to each type of command.
     :param service_state:
     :return:
     """
     if isinstance(self.executor, ExecutorAnsible):
         state = service_state.ansible_state
         return CommandAnsible('name=%s state=%s' % (self.name, state),
                               ansible_module='service',
                               stdout=True,
                               timeout=self.TIMEOUT)
     else:
         state = service_state.system_state
         return Command(['service', self.name, state],
                        stdout=True,
                        timeout=self.TIMEOUT)
Example #4
0
 def _create_command(self, service_state: ServiceSystemState):
     """
     Creates a Command instance based on executor type and state
     that is specific to each type of command.
     :param service_state:
     :return:
     :return:
     """
     command = 'runuser -l %s %s %s' % (self.service_username,
                                        self.service_path,
                                        service_state.system_state)
     if isinstance(self.executor, ExecutorAnsible):
         return CommandAnsible(command,
                               ansible_module='command',
                               stdout=True,
                               timeout=self.TIMEOUT)
     else:
         return Command(command.split(), stdout=True, timeout=self.TIMEOUT)
Example #5
0
    def status(self) -> ServiceStatus:
        """
        Returns the service status based on linux service.
        :return: The status of this specific service
        :rtype: ServiceStatus
        """
        # service output :
        # is running
        # is stopped

        # systemctl output:
        # (running)
        # (dead)

        # On RHEL7> service is automatically redirected to systemctl
        cmd_status = Command([
            'runuser', '-l', self.service_username,
            '%s status' % self.service_path
        ],
                             stdout=True,
                             timeout=self.TIMEOUT)
        execution = self.executor.execute(cmd_status)

        service_output = execution.read_stdout()

        if not service_output:
            ServiceFakeArtemis._logger.debug("Service: %s - Status: FAILED" %
                                             self.name)
            return ServiceStatus.FAILED

        if re.search(r'(is running|\(running\)|Running)', service_output):
            ServiceFakeArtemis._logger.debug("Service: %s - Status: RUNNING" %
                                             self.name)
            return ServiceStatus.RUNNING
        elif re.search(r'(is stopped|\(dead\)|Stopped)', service_output):
            ServiceFakeArtemis._logger.debug("Service: %s - Status: STOPPED" %
                                             self.name)
            return ServiceStatus.STOPPED

        ServiceFakeArtemis._logger.debug("Service: %s - Status: UNKNOWN" %
                                         self.name)
        return ServiceStatus.UNKNOWN