def run(self, command="ls /", **kwargs): """ Run command inside module, all params what allows avocado are passed inside shell,ignore_status, etc. :param command: str :param kwargs: dict :return: avocado.process.run """ return self.runHost( 'docker exec %s bash -c "%s"' % (self.docker_id, common.sanitize_cmd(command)), **kwargs)
def run(self, command="ls /", **kwargs): """ Run command inside OpenShift POD, all params what allows avocado are passed inside shell,ignore_status, etc. https://docs.openshift.com/container-platform/3.6/dev_guide/executing_remote_commands.html :param command: str :param kwargs: dict :return: avocado.process.run """ return self.runHost( "oc exec %s %s" % (self.pod_id, common.sanitize_cmd(command)), **kwargs)
def status(self, command="ls /"): """ Function returns whether the application exists and is Running in OpenShift environment :param command: Do not use it directly (It is defined in config.yaml) :return: bool """ status = False if self._app_exists(): command = self.info.get('start') or command return self.runHost('oc exec %s %s' % (self.pod_id, common.sanitize_cmd(command)))
def run_systemdrun(self, command, internal_background=False, **kwargs): """ execute command via systemd-run inside container :param command: :param internal_background: :param kwargs: :return: """ if not kwargs: kwargs = {} self.__machined_restart() add_sleep_infinite = "" unit_name = common.generate_unique_name() lpath = "/var/tmp/{}".format(unit_name) if self.__systemd_wait_support: add_wait_var = "--wait" else: # keep service exist after it finish, to be able to read exit code add_wait_var = "-r" if internal_background: add_wait_var = "" add_sleep_infinite = "&& sleep infinity" opts = " --unit {unitname} {wait} -M {machine}".format(wait=add_wait_var, machine=self.name, unitname=unit_name ) try: comout = process.run("""systemd-run {opts} /bin/bash -c "({comm})>{pin}.stdout 2>{pin}.stderr {sleep}" """.format( opts=opts, comm=common.sanitize_cmd(command), pin=lpath, sleep=add_sleep_infinite), **kwargs) if not internal_background: if not self.__systemd_wait_support: comout.exit_status = self.__systemctl_wait_until_finish(self.name,unit_name) with open("{chroot}{pin}.stdout".format(chroot=self.location, pin=lpath), 'r') as content_file: comout.stdout = content_file.read() with open("{chroot}{pin}.stderr".format(chroot=self.location, pin=lpath), 'r') as content_file: comout.stderr = content_file.read() comout.command = command os.remove("{chroot}{pin}.stdout".format(chroot=self.location, pin=lpath)) os.remove("{chroot}{pin}.stderr".format(chroot=self.location, pin=lpath)) self.logger.debug(comout) if not self.__systemd_wait_support and not kwargs.get("ignore_status") and comout.exit_status != 0: raise process.CmdError(comout.command, comout) return comout except process.CmdError as e: raise e
def run_machinectl(self, command, **kwargs): """ execute command via machinectl shell inside container :param command: :param kwargs: :return: """ self.__machined_restart() lpath = "/var/tmp" if not kwargs: kwargs = {} should_ignore = kwargs.get("ignore_status") kwargs["ignore_status"] = True comout = process.run("""machinectl shell root@{machine} /bin/bash -c "({comm})>{pin}/stdout 2>{pin}/stderr; echo $?>{pin}/retcode; sleep {defaultsleep}" """.format( machine=self.name, comm=common.sanitize_cmd(command), pin=lpath, defaultsleep=self.__default_command_sleep ), **kwargs) if comout.exit_status != 0: raise mtfexceptions.NspawnExc("This command should not fail anyhow inside NSPAWN:", command) try: kwargs["verbose"] = False b = process.run( 'bash -c "cat {chroot}{pin}/stdout; cat {chroot}{pin}/stderr > /dev/stderr; exit `cat {chroot}{pin}/retcode`"'.format( chroot=self.location, pin=lpath), **kwargs) finally: comout.stdout = b.stdout comout.stderr = b.stderr comout.exit_status = b.exit_status removesworkaround = re.search('[^(]*\((.*)\)[^)]*', comout.command) if removesworkaround: comout.command = removesworkaround.group(1) if comout.exit_status == 0 or should_ignore: return comout else: raise process.CmdError(comout.command, comout)