Example #1
0
def cli(ctx, help):
    try:
        if help and len(ctx.args) == 0:
            show_help()
            exit(0)
        elif help:
            ctx.args.append("--help")

        if len(ctx.args) == 0:
            show_error()
            exit(1)
        if ctx.args[0] == 'install':
            install(ctx.args[1:])
            exit(0)
        elif ctx.args[0] == 'uninstall':
            uninstall()
            exit(0)
        elif ctx.args[0] == 'stop':
            run(ctx.args)
            stop()
            exit(0)
        else:
            run(ctx.args)
            exit(0)

    except BaseError as e:
        Echo.error(str(e))
        exit(e.get_exit_code())
    except Exception as e:
        Echo.error("An unexpected error occurred.")
        Echo.info(str(e))
        Echo.info(traceback.print_exc())
        exit(254)
Example #2
0
def inspect() -> None:
    vm_provider_name = "multipass"
    vm_provider_class = get_provider_for(vm_provider_name)
    echo = Echo()
    try:
        vm_provider_class.ensure_provider()
        instance = vm_provider_class(echoer=echo)
        instance.get_instance_info()

        command = ["microk8s.inspect"]
        output = instance.run(command, hide_output=True)
        tarball_location = None
        host_destination = getcwd()
        if b"Report tarball is at" not in output:
            echo.error("Report tarball not generated")
        else:
            for line_out in output.split(b"\n"):
                line_out = line_out.decode()
                line = line_out.strip()
                if line.startswith("Report tarball is at "):
                    tarball_location = line.split("Report tarball is at ")[1]
                    break
                echo.wrapped(line_out)
            if not tarball_location:
                echo.error("Cannot find tarball file location")
            else:
                instance.pull_file(name=tarball_location,
                                   destination=host_destination)
                echo.wrapped(
                    "The report tarball {} is stored on the current directory".
                    format(tarball_location.split("/")[-1]))

    except ProviderInstanceNotFoundError:
        _not_installed(echo)
        return 1
Example #3
0
def dashboard_proxy() -> None:
    vm_provider_name = "multipass"
    vm_provider_class = get_provider_for(vm_provider_name)
    echo = Echo()
    try:
        vm_provider_class.ensure_provider()
    except ProviderNotFound as provider_error:
        if provider_error.prompt_installable:
            if echo.is_tty_connected():
                echo.warning("MicroK8s is not installed. Please run `microk8s install`.")
            return 1
        else:
            raise provider_error

    instance = vm_provider_class(echoer=echo)

    echo.info("Checking if Dashboard is running.")
    command = ["microk8s.enable", "dashboard"]
    output = instance.run(command, hide_output=True)
    if b"Addon dashboard is already enabled." not in output:
        echo.info("Waiting for Dashboard to come up.")
        command = ["microk8s.kubectl", "-n", "kube-system", "wait", "--timeout=240s",
                   "deployment", "kubernetes-dashboard", "--for", "condition=available"]
        instance.run(command, hide_output=True)

    command = ["microk8s.kubectl", "-n", "kube-system", "get", "secret"]
    output = instance.run(command, hide_output=True)
    secret_name = None
    for line in output.split(b"\n"):
        if line.startswith(b"default-token"):
            secret_name = line.split()[0].decode()
            break

    if not secret_name:
        echo.error("Cannot find the dashboard secret.")

    command = ["microk8s.kubectl", "-n", "kube-system", "describe", "secret", secret_name]
    output = instance.run(command, hide_output=True)
    token = None
    for line in output.split(b"\n"):
        if line.startswith(b"token:"):
            token = line.split()[1].decode()

    if not token:
        echo.error("Cannot find token from secret.")

    ip = instance.get_instance_info().ipv4[0]

    echo.info("Dashboard will be available at https://{}:10443".format(ip))
    echo.info("Use the following token to login:"******"microk8s.kubectl", "port-forward", "-n", "kube-system",
               "service/kubernetes-dashboard", "10443:443", "--address", "0.0.0.0"]

    try:
        instance.run(command)
    except KeyboardInterrupt:
        return
def cli(ctx, help):
    try:
        if help and len(ctx.args) == 0:
            show_help()
            exit(0)
        elif help:
            ctx.args.append("--help")

        if len(ctx.args) == 0:
            show_error()
            exit(1)
        if ctx.args[0] == "install":
            install(ctx.args[1:])
            exit(0)
        elif ctx.args[0] == "uninstall":
            uninstall()
            exit(0)
        elif ctx.args[0] == "start":
            start()
            run(ctx.args)
            exit(0)
        elif ctx.args[0] == "stop":
            run(ctx.args)
            stop()
            exit(0)
        elif ctx.args[0] == "kubectl":
            exit(kubectl(ctx.args[1:]))
        elif ctx.args[0] == "dashboard-proxy":
            dashboard_proxy()
            exit(0)
        elif ctx.args[0] == "inspect":
            inspect()
            exit(0)
        else:
            run(ctx.args)
            exit(0)

    except BaseError as e:
        Echo.error(str(e))
        exit(e.get_exit_code())
    except Exception as e:
        Echo.error("An unexpected error occurred.")
        Echo.info(str(e))
        Echo.info(traceback.print_exc())
        exit(254)
Example #5
0
def install(args) -> None:
    if "--help" in args:
        _show_install_help()
        return
    parser = argparse.ArgumentParser("microk8s install")
    parser.add_argument('--cpu', default=definitions.DEFAULT_CORES, type=int)
    parser.add_argument('--mem', default=definitions.DEFAULT_MEMORY, type=int)
    parser.add_argument('--disk', default=definitions.DEFAULT_DISK, type=int)
    parser.add_argument('--channel',
                        default=definitions.DEFAULT_CHANNEL,
                        type=str)
    parser.add_argument('-y',
                        '--assume-yes',
                        action='store_true',
                        default=definitions.DEFAULT_ASSUME)
    args = parser.parse_args(args)

    echo = Echo()

    if platform == 'win32':
        if not Windows().check_admin():
            echo.error(
                '`microk8s install` must be ran as adminstrator in order to check Hyper-V status.'
            )
            input('Press return key to exit...')
            exit(1)

        if not Windows().is_enough_space():
            echo.error(
                'There is not enough disk space to continue.  You need at least 50GB.'
            )
            input('Press return key to exit...')
            exit(1)

        if not Windows().check_hyperv():
            if args.assume_yes or (echo.is_tty_connected() and echo.confirm(
                    "Hyper-V needs to be enabled. "
                    "Would you like to do that now?")):
                echo.info('Hyper-V will now be enabled.')
                Windows().enable_hyperv()
                echo.info('Hyper-V has been enabled.')
                echo.info(
                    'This host must be restarted.  After restart, run `microk8s install` again to complete setup.'
                )
                input('Press return key to exit...')
                exit(0)
            else:
                echo.error('Cannot continue without enabling Hyper-V')
                exit(1)

    vm_provider_name: str = 'multipass'
    vm_provider_class = get_provider_for(vm_provider_name)
    try:
        vm_provider_class.ensure_provider()
    except ProviderNotFound as provider_error:
        if provider_error.prompt_installable:
            if args.assume_yes or (echo.is_tty_connected() and echo.confirm(
                    "Support for {!r} needs to be set up. "
                    "Would you like to do that it now?".format(
                        provider_error.provider))):
                vm_provider_class.setup_provider(echoer=echo)
            else:
                raise provider_error
        else:
            raise provider_error

    instance = vm_provider_class(echoer=echo)
    instance.launch_instance(vars(args))
    echo.info(
        "MicroK8s is up and running. See the available commands with 'microk8s --help'."
    )
Example #6
0
def install(args) -> None:
    if "--help" in args or "-h" in args:
        _show_install_help()
        return
    parser = argparse.ArgumentParser("microk8s install")
    parser.add_argument("--cpu", default=definitions.DEFAULT_CORES, type=int)
    parser.add_argument("--mem", default=definitions.DEFAULT_MEMORY, type=int)
    parser.add_argument("--disk", default=definitions.DEFAULT_DISK, type=int)
    parser.add_argument("--channel", default=definitions.DEFAULT_CHANNEL, type=str)
    parser.add_argument("-y", "--assume-yes", action="store_true", default=definitions.DEFAULT_ASSUME)
    args = parser.parse_args(args)

    echo = Echo()

    if platform == "win32":
        aux = Windows(args)
        if not aux.check_admin():
            echo.error("`microk8s install` must be ran as adminstrator in order to check Hyper-V status.")
            input("Press return key to exit...")
            exit(1)

        if not aux.is_enough_space():
            echo.warning("VM disk size requested exceeds free space on host.")

        if not aux.check_hyperv():
            if args.assume_yes or (echo.is_tty_connected() and echo.confirm(
                "Hyper-V needs to be enabled. "
                "Would you like to do that now?"
            )):
                echo.info("Hyper-V will now be enabled.")
                aux.enable_hyperv()
                echo.info("Hyper-V has been enabled.")
                echo.info("This host must be restarted.  After restart, run `microk8s install` again to complete setup.")
                input("Press return key to exit...")
                exit(0)
            else:
                echo.error("Cannot continue without enabling Hyper-V")
                exit(1)

    if platform == "darwin":
        aux = MacOS(args)
        if not aux.is_enough_space():
            echo.warning("VM disk size requested exceeds free space on host.")

    vm_provider_name: str = "multipass"
    vm_provider_class = get_provider_for(vm_provider_name)
    try:
        vm_provider_class.ensure_provider()
    except ProviderNotFound as provider_error:
        if provider_error.prompt_installable:
            if args.assume_yes or (echo.is_tty_connected() and echo.confirm(
                "Support for {!r} needs to be set up. "
                "Would you like to do that it now?".format(provider_error.provider)
            )):
                vm_provider_class.setup_provider(echoer=echo)
            else:
                raise provider_error
        else:
            raise provider_error

    instance = vm_provider_class(echoer=echo)
    instance.launch_instance(vars(args))
    echo.info("MicroK8s is up and running. See the available commands with `microk8s --help`.")
Example #7
0
def install(args) -> None:
    if "--help" in args or "-h" in args:
        _show_install_help()
        return

    parser = argparse.ArgumentParser("microk8s install")
    parser.add_argument("--cpu", default=definitions.DEFAULT_CORES, type=cpu)
    parser.add_argument("--mem",
                        default=definitions.DEFAULT_MEMORY_GB,
                        type=memory)
    parser.add_argument("--disk",
                        default=definitions.DEFAULT_DISK_GB,
                        type=disk)
    parser.add_argument("--channel",
                        default=definitions.DEFAULT_CHANNEL,
                        type=str)
    parser.add_argument("-y",
                        "--assume-yes",
                        action="store_true",
                        default=definitions.DEFAULT_ASSUME)
    args = parser.parse_args(args)

    echo = Echo()

    if platform == "win32":
        host = Windows(args)
    elif platform == "darwin":
        host = MacOS(args)
    else:
        host = Linux(args)

    if not host.has_enough_cpus():
        echo.error(
            "VM cpus requested exceed number of available cores on host.")
        exit(1)
    if not host.has_enough_memory():
        echo.warning("VM memory requested exceeds the total memory on host.")
        exit(1)
    if not host.has_enough_disk_space():
        echo.warning("VM disk size requested exceeds free space on host.")

    vm_provider_name: str = "multipass"
    vm_provider_class = get_provider_for(vm_provider_name)
    try:
        vm_provider_class.ensure_provider()
    except ProviderNotFound as provider_error:
        if provider_error.prompt_installable:
            if args.assume_yes or (echo.is_tty_connected() and echo.confirm(
                    "Support for {!r} needs to be set up. "
                    "Would you like to do that now?".format(
                        provider_error.provider))):
                vm_provider_class.setup_provider(echoer=echo)
            else:
                raise provider_error
        else:
            raise provider_error

    instance = vm_provider_class(echoer=echo)
    spec = vars(args)
    spec.update({"kubeconfig": get_kubeconfig_path()})
    instance.launch_instance(spec)
    echo.info(
        "MicroK8s is up and running. See the available commands with `microk8s --help`."
    )