def child_test(zfs, iocroot, name, _type, force=False): """Tests for dependent children""" if _type == "jail": path = f"{iocroot}/jails/{name}/root" else: # RELEASE path = f"{iocroot}/releases/{name}" # While we would like to catch the zfs exception, it still prints to the # display, this is the next best test. if os.path.isdir(path): children = zfs.get_dataset_by_path(path).snapshots_recursive else: if not force: ioc_common.logit({ "level": "WARNING", "message": "Partial UUID/NAME supplied, cannot check for " "dependant jails." }) if not click.confirm("\nProceed?"): return children = [] else: return _children = [] for child in children: _name = child.name.rsplit("@", 1)[-1] if _type == "jail": path = path.replace(name, _name) if os.path.isdir(path): # We only want jails, not every snap they have. _children.append(f" {_name}\n") else: _children.append(f" {_name}\n") sort = ioc_common.ioc_sort("", "name", data=_children) _children.sort(key=sort) if len(_children) != 0: if not force: ioc_common.logit({ "level": "WARNING", "message": f"\n{name} has dependent jails," " use --force to destroy: " }) ioc_common.logit({ "level": "WARNING", "message": "".join(_children) }) exit(1) else: return
def child_test(zfs, iocroot, name, _type, force=False, recursive=False): """Tests for dependent children""" path = None children = [] paths = [ f"{iocroot}/jails/{name}/root", f"{iocroot}/releases/{name}", f"{iocroot}/templates/{name}/root" ] for p in paths: if os.path.isdir(p): path = p children = zfs.get_dataset_by_path(path).snapshots_recursive break if path is None: if not force: ioc_common.logit({ "level": "WARNING", "message": "Partial UUID/NAME supplied, cannot check for " "dependant jails." }) if not click.confirm("\nProceed?"): exit() else: return _children = [] for child in children: _name = child.name.rsplit("@", 1)[-1] _children.append(f" {_name}\n") sort = ioc_common.ioc_sort("", "name", exit_on_error=True, data=_children) _children.sort(key=sort) if len(_children) != 0: if not force and not recursive: ioc_common.logit({ "level": "WARNING", "message": f"\n{name} has dependent jails" " (who may also have dependents)," " use --recursive to destroy: " }) ioc_common.logit({ "level": "WARNING", "message": "".join(_children) }) exit(1) else: return
def df_cmd(header, _long, _sort): """Allows a user to show resource usage of all jails.""" lgr = ioc_logger.Logger('ioc_cli_df').getLogger() jails, paths = IOCList("uuid").list_datasets() pool = IOCJson().json_get_value("pool") jail_list = [] table = Texttable(max_width=0) for jail in jails: full_uuid = jails[jail] if not _long: uuid = full_uuid[:8] else: uuid = full_uuid path = paths[jail] conf = IOCJson(path).json_load() zconf = ["zfs", "get", "-H", "-o", "value"] mountpoint = f"{pool}/iocage/jails/{full_uuid}" tag = conf["tag"] template = conf["type"] if template == "template": mountpoint = f"{pool}/iocage/templates/{tag}" compressratio = Popen( zconf + ["compressratio", mountpoint], stdout=PIPE).communicate()[0].decode("utf-8").strip() reservation = Popen( zconf + ["reservation", mountpoint], stdout=PIPE).communicate()[0].decode("utf-8").strip() quota = Popen(zconf + ["quota", mountpoint], stdout=PIPE).communicate()[0].decode("utf-8").strip() used = Popen(zconf + ["used", mountpoint], stdout=PIPE).communicate()[0].decode("utf-8").strip() available = Popen( zconf + ["available", mountpoint], stdout=PIPE).communicate()[0].decode("utf-8").strip() jail_list.append( [uuid, compressratio, reservation, quota, used, available, tag]) sort = ioc_common.ioc_sort("df", _sort) jail_list.sort(key=sort) if header: jail_list.insert(0, ["UUID", "CRT", "RES", "QTA", "USE", "AVA", "TAG"]) # We get an infinite float otherwise. table.set_cols_dtype(["t", "t", "t", "t", "t", "t", "t"]) table.add_rows(jail_list) lgr.info(table.draw()) else: for jail in jail_list: print("\t".join(jail))
def list_bases(self, datasets): """Lists all bases.""" base_list = ioc_sort("list_release", "release", data=datasets) table = Texttable(max_width=0) if self.header: base_list.insert(0, ["Bases fetched"]) table.add_rows(base_list) # We get an infinite float otherwise. table.set_cols_dtype(["t"]) return table.draw() else: flat_base = [b for b in base_list for b in b] return flat_base
def cli(header, _long, _sort): """Allows a user to show resource usage of all jails.""" table = texttable.Texttable(max_width=0) jail_list = ioc.IOCage().df(long=_long) sort = ioc_common.ioc_sort("df", _sort) jail_list.sort(key=sort) if header: jail_list.insert(0, ["NAME", "CRT", "RES", "QTA", "USE", "AVA"]) # We get an infinite float otherwise. table.set_cols_dtype(["t", "t", "t", "t", "t", "t"]) table.add_rows(jail_list) ioc_common.logit({"level": "INFO", "message": table.draw()}) else: for jail in jail_list: ioc_common.logit({"level": "INFO", "message": "\t".join(jail)})
def list_all(self, jails): """List all jails.""" table = Texttable(max_width=0) jail_list = [] for jail in jails: jail = jail.properties["mountpoint"].value conf = IOCJson(jail).json_load() uuid = conf["host_hostuuid"] full_ip4 = conf["ip4_addr"] ip6 = conf["ip6_addr"] jail_root = "{}/iocage/jails/{}/root".format(self.pool, uuid) try: short_ip4 = full_ip4.split("|")[1].split("/")[0] except IndexError: short_ip4 = "-" tag = conf["tag"] boot = conf["boot"] jail_type = conf["type"] full_release = conf["release"] if "HBSD" in full_release: full_release = "{}-STABLE-HBSD".format(full_release.split( ".")[0]) short_release = "{}-STABLE".format(full_release.rsplit("-")[0]) else: short_release = "-".join(full_release.rsplit("-")[:2]) if full_ip4 == "none": full_ip4 = "-" status, jid = self.list_get_jid(uuid) if status: state = "up" else: state = "down" if conf["type"] == "template": template = "-" else: try: template = checkoutput(["zfs", "get", "-H", "-o", "value", "origin", jail_root]).split("/")[3] except IndexError: template = "-" if "release" in template.lower() or "stable" in template.lower(): template = "-" # Append the JID and the UUID to the table if self.full: jail_list.append([jid, uuid, boot, state, tag, jail_type, full_release, full_ip4, ip6, template]) else: jail_list.append([jid, uuid[:8], state, tag, short_release, short_ip4]) list_type = "list_full" if self.full else "list_short" sort = ioc_sort(list_type, self.sort, data=jail_list) jail_list.sort(key=sort) # Prints the table if self.header: if self.full: # We get an infinite float otherwise. table.set_cols_dtype(["t", "t", "t", "t", "t", "t", "t", "t", "t", "t"]) jail_list.insert(0, ["JID", "UUID", "BOOT", "STATE", "TAG", "TYPE", "RELEASE", "IP4", "IP6", "TEMPLATE"]) else: # We get an infinite float otherwise. table.set_cols_dtype(["t", "t", "t", "t", "t", "t"]) jail_list.insert(0, ["JID", "UUID", "STATE", "TAG", "RELEASE", "IP4"]) table.add_rows(jail_list) return table.draw() else: flat_jail = [j for j in jail_list] return flat_jail