def exec(self, jail, command, options): """Issues a command inside a jail.""" from iocage.lib.ioc_exec import IOCExec tag, uuid, path = self.check_jail_existence(jail) host_user = options["host_user"] jail_user = options["jail_user"] # We may be getting ';', '&&' and so forth. Adding the shell for # safety. if len(command) == 1: command = ["/bin/sh", "-c"] + command msg, _ = IOCExec(command, uuid, tag, path, host_user, jail_user).exec_jail() return msg.decode("utf-8")
def exec_cmd(command, jail, host_user, jail_user): """Runs the command given inside the specified jail as the supplied user.""" lgr = ioc_logger.Logger('ioc_cli_exec').getLogger() # We may be getting ';', '&&' and so forth. Adding the shell for safety. if len(command) == 1: command = ("/bin/sh", "-c") + command if jail.startswith("-"): lgr.critical("Please specify a jail first!") exit(1) if host_user and jail_user: lgr.critical("Please only specify either host_user or" " jail_user, not both!") exit(1) jails, paths = IOCList("uuid").list_datasets() _jail = { tag: uuid for (tag, uuid) in jails.items() if uuid.startswith(jail) or tag == jail } if len(_jail) == 1: tag, uuid = next(iter(_jail.items())) path = paths[tag] elif len(_jail) > 1: lgr.error("Multiple jails found for" " {}:".format(jail)) for t, u in sorted(_jail.items()): lgr.error(" {} ({})".format(u, t)) raise RuntimeError() else: lgr.critical("{} not found!".format(jail)) exit(1) msg, err = IOCExec(command, uuid, tag, path, host_user, jail_user).exec_jail() if err: err = indent_lines(msg) lgr.error("{}".format(err)) else: lgr.info(msg.decode("utf-8"))
def create_install_packages(self, jail_uuid, location, _tag, config): """ Takes a list of pkg's to install into the target jail. The resolver property is required for pkg to have network access. """ status, jid = IOCList().list_get_jid(jail_uuid) err = False if not status: IOCStart(jail_uuid, _tag, location, config, silent=True) resolver = config["resolver"] if resolver != "/etc/resolv.conf" and resolver != "none": with open("{}/etc/resolv.conf".format(location), "w") as resolv_conf: for line in resolver.split(";"): resolv_conf.write(line + "\n") else: copy(resolver, "{}/root/etc/resolv.conf".format(location)) status, jid = IOCList().list_get_jid(jail_uuid) if not self.plugin: with open(self.pkglist, "r") as j: self.pkglist = json.load(j)["pkgs"] self.lgr.info("\nInstalling pkg... ") # To avoid a user being prompted about pkg. Popen(["pkg-static", "-j", jid, "install", "-q", "-y", "pkg"], stderr=PIPE).communicate() # We will have mismatched ABI errors from earlier, this is to be safe. os.environ["ASSUME_ALWAYS_YES"] = "yes" cmd = ("pkg-static", "upgrade", "-f", "-q", "-y") pkg_upgrade = IOCExec(cmd, jail_uuid, _tag, location, plugin=self.plugin).exec_jail() if pkg_upgrade: self.lgr.error("ERROR: {}".format(pkg_upgrade.decode("utf-8"))) err = True self.lgr.info("Installing supplied packages:") for pkg in self.pkglist: self.lgr.info(" - {}... ".format(pkg)) cmd = ("pkg", "install", "-q", "-y", pkg) pkg_install = IOCExec(cmd, jail_uuid, _tag, location, plugin=self.plugin).exec_jail() if pkg_install: self.lgr.error("ERROR: {}".format(pkg_install)) err = True os.remove("{}/root/etc/resolv.conf".format(location)) if status: IOCStop(jail_uuid, _tag, location, config, silent=True) if self.plugin and err: return err