예제 #1
0
파일: iocage.py 프로젝트: gronke/iocage
    def start(self, jail=None):
        """Checks jails type and existence, then starts the jail"""
        if self.rc or self._all:
            if not jail:
                self.__jail_order__("start")
        else:
            tag, uuid, path = self.__check_jail_existence__()
            conf = ioc_json.IOCJson(path).json_load()
            err, msg = self.__check_jail_type__(conf["type"], uuid, tag)

            if not err:
                ioc_start.IOCStart(uuid,
                                   tag,
                                   path,
                                   conf,
                                   callback=self.callback,
                                   silent=self.silent)

                return False, None
            else:
                if jail:
                    return err, msg
                else:
                    self.callback({"level": "ERROR", "message": msg})
                    exit(1)
예제 #2
0
    def start(self, jail=None):
        """Checks jails type and existence, then starts the jail"""
        if self.rc or self._all:
            if not jail:
                self.__jail_order__("start")
        else:
            uuid, path = self.__check_jail_existence__()
            conf = ioc_json.IOCJson(path, silent=self.silent).json_load()
            err, msg = self.__check_jail_type__(conf["type"], uuid)
            depends = conf["depends"].split()

            if not err:
                for depend in depends:
                    if depend != "none":
                        self.jail = depend
                        self.start()

                ioc_start.IOCStart(uuid,
                                   path,
                                   conf,
                                   silent=self.silent,
                                   callback=self.callback)

                return False, None
            else:
                if jail:
                    return err, msg
                else:
                    self.callback({"level": "ERROR", "message": msg})
                    exit(1)
예제 #3
0
def test_should_return_default_mtu_if_no_members(mock_checkoutput):
    mock_checkoutput.side_effect = [
        bridge_with_no_members_if_config, member_if_config
    ]

    mtu = ioc_start.IOCStart("", "", "").find_bridge_mtu('bridge0')
    assert mtu == '1500'
    mock_checkoutput.called_with(["ifconfig", "bridge0"])
예제 #4
0
def test_should_return_mtu_of_first_member(mock_checkoutput):
    mock_checkoutput.side_effect = [bridge_if_config, member_if_config]

    mtu = ioc_start.IOCStart("", "", "").find_bridge_mtu('bridge0')
    assert mtu == '1500'
    mock_checkoutput.assert_has_calls(
        [mock.call(["ifconfig", "bridge0"]),
         mock.call(["ifconfig", "bge0"])])
예제 #5
0
    def __jail_start__(self, jail, silent=False):
        """Checks jails type and existence, then starts the jail"""
        tag, uuid, path = self.__check_jail_existence__(jail)
        conf = ioc_json.IOCJson(path).json_load()
        err, msg = self.__check_jail_type__(conf, uuid, tag)

        if not err:
            ioc_start.IOCStart(uuid, tag, path, conf, silent)

            return False, None
        else:
            return err, msg
예제 #6
0
파일: console.py 프로젝트: gronke/iocage
def cli(jail, force):
    """
    Runs jexec to login into the specified jail. Accepts a force flag that
    will attempt to start the jail if it is not already running.
    """
    jails, paths = ioc_list.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]

        iocjson = ioc_json.IOCJson(path)
        conf = iocjson.json_load()
        login_flags = conf["login_flags"].split()
        exec_fib = conf["exec_fib"]
        status, _ = ioc_list.IOCList().list_get_jid(uuid)
    elif len(_jail) > 1:
        ioc_common.logit({
            "level": "ERROR",
            "message": f"Multiple jails found for {jail}"
        })
        for t, u in sorted(_jail.items()):
            ioc_common.logit({"level": "ERROR", "message": f"  {u} ({t})"})
        exit(1)
    else:
        ioc_common.logit({"level": "ERROR", "message": f"{jail} not found!"})
        exit(1)

    if not status and not force:
        ioc_common.logit({
            "level": "ERROR",
            "message": f"{uuid} ({tag}) is not running!"
        })
        exit(1)

    if not status and force:
        ioc_common.logit({
            "level":
            "INFO",
            "message":
            f"{uuid} ({tag}) is not running, starting jail."
        })
        if conf["type"] == "jail":
            ioc_start.IOCStart(uuid, jail, path, conf, silent=True)
            status = True
        elif conf["type"] == "basejail":
            ioc_common.logit({
                "level":
                "ERROR",
                "message":
                "Please run \"iocage migrate\" before trying to"
                f" start {uuid} ({tag})"
            })
            exit(1)
        elif conf["type"] == "template":
            ioc_common.logit({
                "level":
                "ERROR",
                "message":
                "Please convert back to a jail before trying to"
                f" start {uuid} ({tag})"
            })
            exit(1)
        else:
            ioc_common.logit({
                "level":
                "ERROR",
                "message":
                f"{conf['type']} is not a supported jail type."
            })
            exit(1)

    if status:
        su.Popen(["setfib", exec_fib, "jexec", f"ioc-{uuid}", "login"] +
                 login_flags).communicate()
예제 #7
0
파일: upgrade.py 프로젝트: wxcafe/iocage
def cli(jail, release):
    """Runs upgrade with the command given inside the specified jail."""
    # TODO: Move to API
    release = release.rsplit("-", 1)[0].rsplit("-", 1)[0]
    host_release = os.uname()[2].rsplit("-", 1)[0].rsplit("-", 1)[0]

    if release is not None:
        if host_release < release:
            ioc_common.logit({
                "level":
                "EXCEPTION",
                "message":
                f"\nHost: {host_release} is not greater than"
                f" target: {release}\nThis is unsupported."
            })

    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()))
        root_path = f"{path}/root"
    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": "EXCEPTION",
                "message": f"{jail} not found!"
            },
            exit_on_error=True)

    status, jid = ioc_list.IOCList.list_get_jid(uuid)
    conf = ioc_json.IOCJson(path).json_load()
    jail_release = conf["release"]

    if release in jail_release:
        ioc_common.logit(
            {
                "level": "EXCEPTION",
                "message": f"Jail: {uuid} is already at version {release}!"
            },
            exit_on_error=True)

    started = False

    if conf["release"] == "EMPTY":
        ioc_common.logit(
            {
                "level": "EXCEPTION",
                "message": "Upgrading is not supported for empty jails."
            },
            exit_on_error=True)

    if conf["type"] == "jail":
        if not status:
            ioc_start.IOCStart(uuid, path, conf, silent=True)
            started = True

        new_release = ioc_upgrade.IOCUpgrade(conf, release,
                                             root_path).upgrade_jail()
    elif conf["type"] == "basejail":
        ioc_common.logit(
            {
                "level":
                "EXCEPTION",
                "message":
                "Please run \"iocage migrate\" before trying"
                f" to upgrade {uuid}"
            },
            exit_on_error=True)
    elif conf["type"] == "template":
        ioc_common.logit(
            {
                "level":
                "EXCEPTION",
                "message":
                "Please convert back to a jail before trying"
                f" to upgrade {uuid}"
            },
            exit_on_error=True)
    else:
        ioc_common.logit(
            {
                "level": "EXCEPTION",
                "message": f"{conf['type']} is not a supported jail type."
            },
            exit_on_error=True)

    if started:
        ioc_stop.IOCStop(uuid, path, conf, silent=True)

        ioc_common.logit({
            "level":
            "INFO",
            "message":
            f"\n{uuid} successfully upgraded from"
            f" {jail_release} to {new_release}!"
        })
예제 #8
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)
예제 #9
0
파일: restart.py 프로젝트: gronke/iocage
def __hard_restart__(uuid, jail, path, conf):
    """Stops and then starts the jail."""
    ioc_stop.IOCStop(uuid, jail, path, conf)
    ioc_start.IOCStart(uuid, jail, path, conf)
예제 #10
0
def cli(jail, release):
    """Runs upgrade with the command given inside the specified jail."""
    jails, paths = ioc_list.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]
        root_path = "{}/root".format(path)
    elif len(_jail) > 1:
        ioc_common.logit({
            "level": "ERROR",
            "message": f"Multiple jails found for {jail}:"
        })
        for t, u in sorted(_jail.items()):
            ioc_common.logit({"level": "ERROR", "message": f"  {u} ({t})"})
        exit(1)
    else:
        ioc_common.logit({"level": "ERROR", "message": f"{jail} not found!"})
        exit(1)

    status, jid = ioc_list.IOCList.list_get_jid(uuid)
    conf = ioc_json.IOCJson(path).json_load()
    jail_release = conf["release"]
    started = False

    if conf["release"] == "EMPTY":
        ioc_common.logit({
            "level":
            "ERROR",
            "message":
            "Upgrading is not supported for empty jails."
        })
        exit(1)
    if conf["type"] == "jail":
        if not status:
            ioc_start.IOCStart(uuid, tag, path, conf, silent=True)
            started = True

            new_release = ioc_upgrade.IOCUpgrade(conf, release,
                                                 root_path).upgrade_jail()
    elif conf["type"] == "basejail":
        ioc_common.logit({
            "level":
            "ERROR",
            "message":
            "Please run \"iocage migrate\" before trying"
            f" to upgrade {uuid} ({tag})"
        })
        exit(1)
    elif conf["type"] == "template":
        ioc_common.logit({
            "level":
            "ERROR",
            "message":
            "Please convert back to a jail before trying"
            f" to upgrade {uuid} ({tag})"
        })
        exit(1)
    else:
        ioc_common.logit({
            "level":
            "ERROR",
            "message":
            f"{conf['type']} is not a supported jail type."
        })
        exit(1)

    if started:
        ioc_stop.IOCStop(uuid, tag, path, conf, silent=True)

        ioc_common.logit({
            "level":
            "INFO",
            "message":
            f"\n{uuid} ({tag}) successfully upgraded from"
            f" {jail_release} to {new_release}!"
        })