Esempio n. 1
0
def cli(dataset_type, header, _long, remote, http, plugins, _sort):
    """This passes the arg and calls the jail_datasets function."""
    freebsd_version = ioc_common.checkoutput(["freebsd-version"])

    if dataset_type is None:
        dataset_type = "all"

    if remote:
        if "HBSD" in freebsd_version:
            hardened = True
        else:
            hardened = False

        ioc_fetch.IOCFetch("", http=http,
                           hardened=hardened).fetch_release(_list=True)
    elif plugins:
        ioc_fetch.IOCFetch("").fetch_plugin_index("", _list=True)
    else:
        _list = ioc.IOCage().list(dataset_type, header, _long, _sort)

        if not header:
            if dataset_type == "base":
                for item in _list:
                    ioc_common.logit({"level": "INFO", "message": item})
            else:
                for item in _list:
                    ioc_common.logit({
                        "level": "INFO",
                        "message": "\t".join(item)
                    })
        else:
            ioc_common.logit({"level": "INFO", "message": _list})
Esempio n. 2
0
    def create(self,
               release,
               props,
               count=0,
               pkglist=None,
               template=False,
               short=False,
               uuid=None,
               basejail=False,
               empty=False,
               clone=None):
        if short and uuid:
            uuid = uuid[:8]

            if len(uuid) != 8:
                ioc_common.logit(
                    {
                        "level":
                        "EXCEPTION",
                        "message":
                        "Need a minimum of 8 characters to use --short"
                        " (-s) and --uuid (-u) together!"
                    },
                    _callback=self.callback,
                    silent=self.silent)

        if not template and not release and not empty and not clone:
            ioc_common.logit(
                {
                    "level":
                    "EXCEPTION",
                    "message":
                    "Must supply either --template (-t) or"
                    " --release (-r)!"
                },
                _callback=self.callback,
                silent=self.silent)

        if not os.path.isdir(
                f"{self.iocroot}/releases/{release}") and not template and \
                not empty and not clone:
            freebsd_version = ioc_common.checkoutput(["freebsd-version"])

            if "HBSD" in freebsd_version:
                hardened = True
            else:
                hardened = False

            ioc_fetch.IOCFetch(release, hardened=hardened).fetch_release()

        if clone:
            _, clone_uuid, _ = self.__check_jail_existence__()
            status, _ = self.list("jid", uuid=clone_uuid)
            if status:
                ioc_common.logit(
                    {
                        "level":
                        "EXCEPTION",
                        "message":
                        f"Jail: {self.jail} must not be running to be"
                        " cloned!"
                    },
                    _callback=self.callback,
                    silent=self.silent)

            release = clone_uuid
            clone = self.jail

        try:
            ioc_create.IOCCreate(release,
                                 props,
                                 count,
                                 pkglist,
                                 template=template,
                                 short=short,
                                 uuid=uuid,
                                 basejail=basejail,
                                 empty=empty,
                                 clone=clone).create_jail()
        except RuntimeError as err:
            return True, err

        return False, None
Esempio n. 3
0
def cli(jail):
    """Runs update with the command given inside the specified jail."""
    # TODO: Move to API
    jails = ioc_list.IOCList("uuid").list_datasets()
    _jail = {
        uuid: path
        for (uuid, path) in jails.items() if uuid.startswith(jail)
    }

    if len(_jail) == 1:
        uuid, path = next(iter(_jail.items()))
    elif len(_jail) > 1:
        ioc_common.logit({
            "level": "ERROR",
            "message": f"Multiple jails found for {jail}:"
        })
        for u, p in sorted(_jail.items()):
            ioc_common.logit({"level": "ERROR", "message": f"  {u} ({p})"})
        exit(1)
    else:
        ioc_common.logit({"level": "ERROR", "message": f"{jail} not found!"})
        exit(1)

    freebsd_version = ioc_common.checkoutput(["freebsd-version"])
    status, jid = ioc_list.IOCList.list_get_jid(uuid)
    conf = ioc_json.IOCJson(path).json_load()
    started = False

    if conf["type"] == "jail":
        if not status:
            ioc_start.IOCStart(uuid, path, conf, silent=True)
            status, jid = ioc_list.IOCList.list_get_jid(uuid)
            started = True
    elif conf["type"] == "basejail":
        ioc_common.logit({
            "level":
            "ERROR",
            "message":
            "Please run \"iocage migrate\" before trying"
            f" to update {uuid}"
        })
        exit(1)
    elif conf["type"] == "template":
        ioc_common.logit({
            "level":
            "ERROR",
            "message":
            "Please convert back to a jail before trying"
            f" to update {uuid}"
        })
        exit(1)
    else:
        ioc_common.logit({
            "level":
            "ERROR",
            "message":
            f"{conf['type']} is not a supported jail type."
        })
        exit(1)

    if "HBSD" in freebsd_version:
        su.Popen(["hbsd-update", "-j", jid]).communicate()

        if started:
            ioc_stop.IOCStop(uuid, path, conf, silent=True)
    else:
        ioc_fetch.IOCFetch(conf["cloned_release"]).fetch_update(True, uuid)

        if started:
            ioc_stop.IOCStop(uuid, path, conf, silent=True)
Esempio n. 4
0
    def fetch(self, **kwargs):
        """Fetches a release or plugin."""
        release = kwargs.pop("release", None)
        name = kwargs.pop("name", None)
        props = kwargs.pop("props", ())
        plugins = kwargs.pop("plugins", False)
        plugin_file = kwargs.pop("plugin_file", False)
        count = kwargs.pop("count", 1)
        accept = kwargs.pop("accept", False)

        freebsd_version = ioc_common.checkoutput(["freebsd-version"])
        arch = os.uname()[4]

        if not kwargs["files"]:
            if arch == "arm64":
                kwargs["files"] = ("MANIFEST", "base.txz", "doc.txz")
            else:
                kwargs["files"] = ("MANIFEST", "base.txz", "lib32.txz",
                                   "doc.txz")

        if "HBSD" in freebsd_version:
            if kwargs["server"] == "ftp.freebsd.org":
                kwargs["hardened"] = True
            else:
                kwargs["hardened"] = False
        else:
            kwargs["hardened"] = False

        if plugins or plugin_file:
            ip = [
                x for x in props
                if x.startswith("ip4_addr") or x.startswith("ip6_addr")
            ]
            if not ip:
                ioc_common.logit(
                    {
                        "level":
                        "EXCEPTION",
                        "message":
                        "An IP address is needed to fetch a plugin!\n"
                        "Please specify ip(4|6)"
                        "_addr=\"[INTERFACE|]IPADDRESS\"!"
                    },
                    _callback=self.callback,
                    silent=self.silent)

            if plugins:
                ioc_fetch.IOCFetch(release, plugin=name,
                                   **kwargs).fetch_plugin_index(
                                       props, accept_license=accept)
                return

            if count == 1:
                ioc_fetch.IOCFetch(release, **kwargs).fetch_plugin(
                    name, props, 0, accept)
            else:
                for j in range(1, count + 1):
                    ioc_fetch.IOCFetch(release, **kwargs).fetch_plugin(
                        name, props, j, accept)
        else:
            ioc_fetch.IOCFetch(release, **kwargs).fetch_release()
Esempio n. 5
0
    def create(self,
               release,
               props,
               count=0,
               pkglist=None,
               template=False,
               short=False,
               _uuid=None,
               basejail=False,
               empty=False,
               clone=None,
               skip_batch=False):
        """Creates the jail dataset"""
        count = 0 if count == 1 and not skip_batch else count

        if short and _uuid:
            _uuid = _uuid[:8]

            if len(_uuid) != 8:
                ioc_common.logit(
                    {
                        "level":
                        "EXCEPTION",
                        "message":
                        "Need a minimum of 8 characters to use --short"
                        " (-s) and --uuid (-u) together!"
                    },
                    _callback=self.callback,
                    silent=self.silent)

        if not template and not release and not empty and not clone:
            ioc_common.logit(
                {
                    "level":
                    "EXCEPTION",
                    "message":
                    "Must supply either --template (-t) or"
                    " --release (-r)!"
                },
                _callback=self.callback,
                silent=self.silent)

        if not os.path.isdir(
                f"{self.iocroot}/releases/{release}") and not template and \
                not empty and not clone:
            freebsd_version = ioc_common.checkoutput(["freebsd-version"])

            if "HBSD" in freebsd_version:
                hardened = True
            else:
                hardened = False

            ioc_fetch.IOCFetch(release, hardened=hardened,
                               silent=self.silent).fetch_release()

        if clone:
            clone_uuid, _ = self.__check_jail_existence__()
            status, _ = self.list("jid", uuid=clone_uuid)
            if status:
                ioc_common.logit(
                    {
                        "level":
                        "EXCEPTION",
                        "message":
                        f"Jail: {self.jail} must not be running to be"
                        " cloned!"
                    },
                    _callback=self.callback,
                    silent=self.silent)

            release = clone_uuid
            clone = self.jail

        try:
            if count > 1 and not skip_batch:
                for j in range(1, count + 1):
                    try:
                        if _uuid is not None:
                            uuid.UUID(_uuid, version=4)

                        count_uuid = _uuid  # Is a UUID
                    except ValueError:
                        # This will allow named jails to use count
                        # This can probably be smarter
                        count_uuid = f"{_uuid}_{j}"

                    self.create(release,
                                props,
                                j,
                                pkglist=pkglist,
                                template=template,
                                short=short,
                                _uuid=count_uuid,
                                basejail=basejail,
                                empty=empty,
                                clone=clone,
                                skip_batch=True)
            else:
                ioc_create.IOCCreate(release,
                                     props,
                                     count,
                                     pkglist,
                                     template=template,
                                     short=short,
                                     uuid=_uuid,
                                     basejail=basejail,
                                     empty=empty,
                                     clone=clone,
                                     silent=self.silent).create_jail()
        except RuntimeError:
            raise

        return False, None
Esempio n. 6
0
def cli(http, _file, server, user, password, auth, verify, release, plugins,
        plugin_file, root_dir, props, count, update, eol, files):
    """CLI command that calls fetch_release()"""
    freebsd_version = ioc_common.checkoutput(["freebsd-version"])
    arch = os.uname()[4]

    if not files:
        if arch == "arm64":
            files = ("MANIFEST", "base.txz", "doc.txz")
        else:
            files = ("MANIFEST", "base.txz", "lib32.txz", "doc.txz")

    if "HBSD" in freebsd_version:
        if server == "ftp.freebsd.org":
            hardened = True
        else:
            hardened = False
    else:
        hardened = False

    if plugins or plugin_file:
        if plugin_file:
            try:
                with open(plugin_file) as f:
                    json.load(f)
            except FileNotFoundError:
                ioc_common.logit({
                    "level": "ERROR",
                    "message": "File was not found!"
                })
                exit(1)
            except json.decoder.JSONDecodeError:
                ioc_common.logit({
                    "level":
                    "ERROR",
                    "message":
                    "Invalid JSON file supplied, please supply a "
                    "correctly formatted JSON file."
                })
                exit(1)

        ip = [
            x for x in props
            if x.startswith("ip4_addr") or x.startswith("ip6_addr")
        ]
        if not ip:
            ioc_common.logit({
                "level":
                "ERROR",
                "message":
                "An IP address is needed to fetch a plugin!\n"
                "Please specify ip(4|6)"
                "_addr=\"INTERFACE|IPADDRESS\"!"
            })
            exit(1)
        if plugins:
            ioc_fetch.IOCFetch(release=None,
                               http=http,
                               _file=_file,
                               verify=verify,
                               hardened=hardened,
                               update=update,
                               eol=eol,
                               files=files).fetch_plugin_index(props)
            exit()

        if count == 1:
            ioc_fetch.IOCFetch("",
                               server,
                               user,
                               password,
                               auth,
                               root_dir,
                               http=http,
                               _file=_file,
                               verify=verify,
                               hardened=hardened,
                               update=update,
                               eol=eol,
                               files=files).fetch_plugin(
                                   plugin_file, props, 0)
        else:
            for j in range(1, count + 1):
                ioc_fetch.IOCFetch("",
                                   server,
                                   user,
                                   password,
                                   auth,
                                   root_dir,
                                   http=http,
                                   _file=_file,
                                   verify=verify,
                                   hardened=hardened,
                                   update=update,
                                   eol=eol,
                                   files=files).fetch_plugin(
                                       plugin_file, props, j)
    else:
        ioc_fetch.IOCFetch(release,
                           server,
                           user,
                           password,
                           auth,
                           root_dir,
                           http=http,
                           _file=_file,
                           verify=verify,
                           hardened=hardened,
                           update=update,
                           eol=eol,
                           files=files).fetch_release()