Beispiel #1
0
    def get_ip(self):
        """Get ip of Ansible node"""
        if self.ip:
            return self.ip

        cmd_ping: CommandAnsible = CommandAnsible(
            ansible_module='setup',
            ansible_args='filter=ansible_default_ipv4',
            stdout=True,
            stderr=True,
            timeout=20,
        )
        execution: Execution = self.executor.execute(cmd_ping)
        execution.wait()

        if not execution.completed_successfully() or not execution.read_stdout(
        ):
            return None

        ip_addr_out: Union[list, str] = execution.read_stdout()
        ip_addresses: list = re.findall(r'([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)',
                                        ip_addr_out, re.MULTILINE)

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

        return ip_addresses[0]
    def _create_command(self, service_state: ServiceDockerState):
        """
        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
            restart = 'no'
            if service_state == self.ServiceDockerState.RESTARTED:
                restart = 'yes'

            print('name=%s state=%s restart=%s docker_host=%s' %
                  (self.name, state, restart, self.docker_host))

            docker_host_opt = 'docker_host=%s' % self.docker_host if self.docker_host else ''
            return CommandAnsible('name=%s state=%s restart=%s %s' %
                                  (self.name, state, restart, docker_host_opt),
                                  ansible_module='docker_container',
                                  stdout=True,
                                  timeout=self.TIMEOUT)
        elif isinstance(self.executor, ExecutorContainer):
            state = service_state.system_state
            return CommandContainer([],
                                    docker_command=state,
                                    stdout=True,
                                    timeout=self.TIMEOUT)
Beispiel #3
0
    def ping(self) -> bool:
        """Send ping to Ansible node"""
        cmd_ping: CommandAnsible = CommandAnsible(ansible_module='ping',
                                                  stdout=True,
                                                  timeout=20)
        execution: 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())
    def copy_configuration_files(self):
        cmd_copy_files = None
        if isinstance(self.component.node, NodeAnsible):
            cmd_copy_files = CommandAnsible(
                ansible_module="synchronize",
                ansible_args='src=%s dest=%s' %
                             (self.local_config_dir,
                              self.node_config_dir),
                stdout=True,
                stderr=True,
                timeout=20
            )
        elif isinstance(self.component.node, NodeLocal):
            cmd_copy_files = Command([], stdout=True, timeout=20)
        elif isinstance(self.component.node, NodeDocker):
            raise IQAConfigurationException("Unable to change configuration on docker node")

        return self.component.node.execute(cmd_copy_files)
Beispiel #5
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
         )