def get_info(self): args = "lxc list {name} --format json".format(name=self.name) status, output = utils.cmd_output(args) if status: raise CoreCommandError(status, args, output) data = json.loads(output) if not data: raise CoreCommandError( status, args, "LXC({name}) not present".format(name=self.name)) return data[0]
def get_info(self): args = "docker inspect {name}".format(name=self.name) status, output = utils.cmd_output(args) if status: raise CoreCommandError(status, args, output) data = json.loads(output) if not data: raise CoreCommandError( status, args, "docker({name}) not present".format(name=self.name)) return data[0]
def copy_file(self, source, destination): if destination[0] != "/": destination = os.path.join("/root/", destination) args = "lxc file push {source} {name}/{destination}".format( source=source, name=self.name, destination=destination) status, output = utils.cmd_output(args) if status: raise CoreCommandError(status, args, output)
def get_pid(self): args = "docker inspect -f '{{{{.State.Pid}}}}' {name}".format( name=self.name) status, output = utils.cmd_output(args) if status: raise CoreCommandError(status, args, output) self.pid = output logging.debug("node(%s) pid: %s", self.name, self.pid) return output
def network_cmd(self, args): if not self.up: logging.debug("node down, not running network command: %s", args) return 0 status, output = self.client.ns_cmd(args) if status: raise CoreCommandError(status, args, output) return output
def check_cmd(self, args): """ Runs shell command on node. :param list[str]|str args: command to run :return: combined stdout and stderr :rtype: str :raises CoreCommandError: when a non-zero exit status occurs """ status, output = self.cmd_output(args) if status: raise CoreCommandError(status, args, output) return output.strip()
def check_cmd(self, args): """ Run command and return exit status and combined stdout and stderr. :param list[str]|str args: command to run :return: combined stdout and stderr :rtype: str :raises core.CoreCommandError: when there is a non-zero exit status """ status, output = self.cmd_output(args) if status != 0: raise CoreCommandError(status, args, output) return output.strip()
def check_cmd(args, **kwargs): """ Execute a command on the host and return a tuple containing the exit status and result string. stderr output is folded into the stdout result string. :param list[str]|str args: command arguments :param dict kwargs: keyword arguments to pass to subprocess.Popen :return: combined stdout and stderr :rtype: str :raises CoreCommandError: when there is a non-zero exit status or the file to execute is not found """ kwargs["stdout"] = subprocess.PIPE kwargs["stderr"] = subprocess.STDOUT args = split_args(args) logger.debug("command: %s", args) try: p = subprocess.Popen(args, **kwargs) stdout, _ = p.communicate() status = p.wait() if status != 0: raise CoreCommandError(status, args, stdout) return stdout.strip() except OSError: raise CoreCommandError(-1, args)
def addfile(self, srcname, filename): """ Add a file. :param str srcname: source file name :param str filename: file name to add :return: nothing :raises CoreCommandError: when a non-zero exit status occurs """ logging.info("adding file from %s to %s", srcname, filename) directory = os.path.dirname(filename) cmd = 'mkdir -p "%s" && mv "%s" "%s" && sync' % (directory, srcname, filename) status, output = self.client.shcmd_result(cmd) if status: raise CoreCommandError(status, cmd, output)
def mount(self, source, target): """ Create and mount a directory. :param str source: source directory to mount :param str target: target directory to create :return: nothing :raises CoreCommandError: when a non-zero exit status occurs """ source = os.path.abspath(source) logging.info("node(%s) mounting: %s at %s", self.name, source, target) cmd = 'mkdir -p "%s" && %s -n --bind "%s" "%s"' % (target, constants.MOUNT_BIN, source, target) status, output = self.client.shcmd_result(cmd) if status: raise CoreCommandError(status, cmd, output) self._mounts.append((source, target))
def cmd(args, wait=True): """ Runs a command on and returns the exit status. :param list[str]|str args: command arguments :param bool wait: wait for command to end or not :return: command status :rtype: int """ args = split_args(args) logger.debug("command: %s", args) try: p = subprocess.Popen(args) if not wait: return 0 return p.wait() except OSError: raise CoreCommandError(-1, args)
def cmd_output(args): """ Execute a command on the host and return a tuple containing the exit status and result string. stderr output is folded into the stdout result string. :param list[str]|str args: command arguments :return: command status and stdout :rtype: tuple[int, str] :raises CoreCommandError: when the file to execute is not found """ args = split_args(args) logging.debug("command: %s", args) try: p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout, _ = p.communicate() status = p.wait() return status, stdout.decode("utf-8").strip() except OSError: raise CoreCommandError(-1, args)
def copy_file(self, source, destination): args = "docker cp {source} {name}:{destination}".format( source=source, name=self.name, destination=destination) status, output = utils.cmd_output(args) if status: raise CoreCommandError(status, args, output)