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())
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 undeploy(self, test_obj): command = Command([ "ansible-playbook", "-i", self.get_inventory(test_obj), self._get_deployment_playbook(test_obj, deploy=False) ], stdout=True, stderr=True) ex = ExecutorLocal(name="Undeployer") undeployment = ex.execute(command) undeployment.wait() assert undeployment.completed_successfully() or log.error( undeployment.read_stdout()) return undeployment
def login(self, timeout=TIMEOUT) -> Execution: """ Log in to the defined OpenShift URL using the related token. It waits for a max of "timeout" seconds before timing out. :param timeout: :return: The execution result. """ cmd_login = Command(args=['oc', 'login', self.url, '--token', '%s' % self.token, '--insecure-skip-tls-verify=true'], timeout=timeout, stderr=True, stdout=True) execution: Execution = self.executor.execute(cmd_login) execution.wait() if not execution.completed_successfully(): self._logger.debug("Login has failed against %s: %s" % (self.url, execution.read_stdout())) return execution
def test_scale_down_router(router: Dispatch): """ Scale down the number of PODs to 1. Expects that the scale down command completes successfully. :param router: :return: """ cmd_scale_up = Command(args=['oc', 'scale', '--replicas=1', 'dc', 'amq-interconnect'], timeout=TIMEOUT) execution: Execution = router.execute(cmd_scale_up) execution.wait() # If debug enabled, logging stdout (or stderr when using local executor) if logger.isEnabledFor(logging.DEBUG) and not execution.completed_successfully(): logger.debug("Scale down has failed, stdout: %s" % (execution.read_stdout())) assert execution.completed_successfully()
def scale(self, replicas: int, deployment: str) -> Execution: """ Perform oc scale, setting the number of replicas provided for the given deployment name. It enforces that the "oc login" is executed first. :param replicas: :param deployment: :return: The execution result. """ cmd_scale_up = Command(args=['oc', 'scale', '--replicas=%d' % replicas, 'dc', deployment], timeout=30, stderr=True, stdout=True) execution: Execution = self.executor.execute(cmd_scale_up) execution.wait() if not execution.completed_successfully(): self._logger.debug("Scaling deployment %s (replicas: %d) failed: %s" % (deployment, replicas, execution.read_stderr())) return execution
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='systemd', stdout=True, timeout=self.TIMEOUT) else: state = service_state.system_state return Command(['systemctl', '--no-pager', state, self.name], stdout=True, timeout=self.TIMEOUT)
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) if not execution.read_stdout(): ServiceArtemis._logger.debug("Service: %s - Status: FAILED" % self.name) return ServiceStatus.FAILED service_output = execution.read_stdout() if re.search('(is running|\(running\)|Running)', service_output): ServiceArtemis._logger.debug("Service: %s - Status: RUNNING" % self.name) return ServiceStatus.RUNNING elif re.search('(is stopped|\(dead\)|Stopped)', service_output): ServiceArtemis._logger.debug("Service: %s - Status: STOPPED" % self.name) return ServiceStatus.STOPPED ServiceArtemis._logger.debug("Service: %s - Status: UNKNOWN" % self.name) return ServiceStatus.UNKNOWN
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): state = service_state.ansible_state return CommandAnsible(command, ansible_module='command', stdout=True, timeout=self.TIMEOUT) else: state = service_state.system_state return Command(command.split(), stdout=True, timeout=self.TIMEOUT)
def test_scale_up_router(router: Dispatch): """ Executes "oc" command to scale up the number of PODs according to value defined in MESH_SIZE constant. It also uses 'amq-interconnect' as the deployment config name (standard in official templates). Test passes if command is executed without errors. Note: the oc command expects that current session is logged to Openshift cluster (you can do it manually, but it will be also done through the CI job). :param router: :return: """ cmd_scale_up = Command(args=['oc', 'scale', '--replicas=%d' % MESH_SIZE, 'dc', 'amq-interconnect'], timeout=TIMEOUT, stderr=True, stdout=True) execution: Execution = router.execute(cmd_scale_up) execution.wait() # If debug enabled, logging stdout (or stderr when using local executor) if logger.isEnabledFor(logging.DEBUG) and not execution.completed_successfully(): logger.debug("Scale up has failed, stdout: %s" % (execution.read_stdout())) assert execution.completed_successfully()