Exemple #1
0
def get_mounts_windows():
    output, error_code, log_output = execute_system_command(["net", "use"])

    if error_code:
        handle_error(logger, Texts.MOUNTS_LIST_COMMAND_ERROR_MSG,
                     Texts.MOUNTS_LIST_COMMAND_ERROR_MSG)
        exit(1)
    host = get_kubectl_host(with_port=False)
    data = output.split("\n")

    start_analyzing = False
    ret_list = []
    first_line = None
    second_line = None

    for item in data:
        if start_analyzing:
            if "The command completed successfully." in item:
                break
            else:
                if not first_line:
                    first_line = item
                elif not second_line:
                    second_line = item

                if first_line and second_line:
                    if host in first_line:
                        split_first_line = first_line.split()
                        status = None
                        remote_location = None
                        if len(split_first_line) == 3:
                            status = split_first_line[0].strip()
                            local_folder = split_first_line[1].strip()
                            remote_location = split_first_line[2].strip()
                        elif len(split_first_line) == 2:
                            status = split_first_line[0].strip()
                            local_folder = ""
                            remote_location = split_first_line[1].strip()
                        network = second_line.strip()
                        if status and remote_location:
                            ret_list.append(
                                ShareData(remote_share=remote_location,
                                          local_share=local_folder,
                                          status=status,
                                          network=network))
                    first_line = None
                    second_line = None
        elif "--------" in item:
            start_analyzing = True

    ret_list.sort(key=lambda x: x.remote_share, reverse=False)

    click.echo(
        tabulate([x.windows_tabular_format() for x in ret_list],
                 headers=ShareData.WIN_MOUNTS_LIST_HEADERS,
                 tablefmt=TBLT_TABLE_FORMAT))
Exemple #2
0
def get_mounts_linux_osx(username: str = "",
                         is_admin: bool = False,
                         osx: bool = False):
    output, error_code, log_output = execute_system_command(["mount"])

    if error_code:
        handle_error(logger, Texts.MOUNTS_LIST_COMMAND_ERROR_MSG,
                     Texts.MOUNTS_LIST_COMMAND_ERROR_MSG)
        exit(1)
    host = get_kubectl_host(with_port=False)
    if osx:
        username_string = f"//{username}@"
    else:
        username_string = f"username={username},"

    if osx:
        mnt_regex = "//(.*)@(.*) on (.*) \("
    else:
        mnt_regex = "(.*) on (.*) type"

    ret_list = []

    if output:
        for item in [
                nauta_item for nauta_item in output.split("\n")
                if host in nauta_item and (
                    is_admin or username_string in nauta_item)
        ]:
            try:
                mount_data = re.search(mnt_regex, item)
                if osx:
                    # Type checking is disabled here - we are catching AttributeError exception anyway
                    username = mount_data.group(1)  # type: ignore
                    remote_location = mount_data.group(2)  # type: ignore
                    local_folder = mount_data.group(3)  # type: ignore
                else:
                    remote_location = mount_data.group(1)  # type: ignore
                    local_folder = mount_data.group(2)  # type: ignore
                    username = re.search("username=(.*?),",
                                         item).group(1)  # type: ignore

                ret_list.append(
                    ShareData(remote_share=remote_location,
                              local_share=local_folder,
                              username=username))
            except Exception:
                handle_error(logger, Texts.MOUNTS_LIST_COMMAND_ERROR_MSG,
                             Texts.MOUNTS_LIST_COMMAND_ERROR_MSG)

    ret_list.sort(key=lambda x: x.username, reverse=False)

    click.echo(
        tabulate([x.linux_osx_tabular_format() for x in ret_list],
                 headers=ShareData.LINUX_MOUNTS_LIST_HEADERS,
                 tablefmt=TBLT_TABLE_FORMAT))
Exemple #3
0
def get_mount_command() -> str:
    adr = get_kubectl_host(with_port=False)
    usr = get_current_user()
    psw = get_users_samba_password(usr)

    if platform.system() == 'Linux':
        return get_mount_command_linux(usr, psw, adr)
    elif platform.system() == 'Windows':
        return get_mount_command_windows(usr, psw, adr)
    else:  # OSX
        return get_mount_command_osx(usr, psw, adr)
Exemple #4
0
def get_inference_instance_url(inference_instance: Run, model_name: str = None) -> str:
    """
    Get URL to inference instance.
    """
    service_name = inference_instance.name
    model_name = model_name if model_name else inference_instance.metadata['annotations']['modelName']
    k8s_host = get_kubectl_host(replace_https=False)
    k8s_namespace = get_kubectl_current_context_namespace()

    proxy_url = f'{k8s_host}/api/v1/namespaces/{k8s_namespace}/' \
                f'services/{service_name}:rest-port/proxy/v1/models/{model_name}'

    return proxy_url
Exemple #5
0
def create(state: State, username: str, list_only: bool, filename: str):
    """
    Adds a new user with a name given as a parameter.

    :param username: name of a new user
    """

    if list_only and filename:
        handle_error(user_msg=Texts.F_L_OPTIONS_EXCLUSION_ERROR_MSG)
        exit(1)

    try:
        try:
            validate_user_name(username)
        except ValueError as exe:
            handle_error(
                logger,
                Texts.NAME_VALIDATION_ERROR_MSG.format(username=username),
                str(exe),
                add_verbosity_msg=state.verbosity == 0)
            exit(1)

        if not is_current_user_administrator():
            handle_error(logger, Texts.USER_NOT_ADMIN_ERROR_MSG,
                         Texts.USER_NOT_ADMIN_ERROR_MSG)
            exit(1)

        user_state = check_users_presence(username)

        if user_state == UserState.ACTIVE:
            handle_error(
                logger,
                Texts.USER_ALREADY_EXISTS_ERROR_MSG.format(username=username),
                Texts.USER_ALREADY_EXISTS_ERROR_MSG.format(username=username))
            exit(1)

        if user_state == UserState.TERMINATING:
            handle_error(
                logger,
                Texts.USER_BEING_REMOVED_ERROR_MSG.format(username=username),
                Texts.USER_BEING_REMOVED_ERROR_MSG.format(username=username))
            exit(1)

    except Exception:
        handle_error(
            logger,
            Texts.USER_VERIFICATION_ERROR_MSG.format(username=username),
            Texts.USER_VERIFICATION_ERROR_MSG.format(username=username),
            add_verbosity_msg=state.verbosity == 0)
        exit(1)

    try:
        with spinner(text=Texts.CREATING_USER_PROGRESS_MSG.format(
                username=username)):
            chart_location = os.path.join(Config().config_path,
                                          ADD_USER_CHART_NAME)

            nauta_config_map = NAUTAConfigMap()

            tiller_location = nauta_config_map.image_tiller
            tensorboard_service_location = nauta_config_map.image_tensorboard_service

            add_user_command = [
                "helm", "install", "--wait", "--namespace", username, "--name",
                username, chart_location, "--set", "global.nauta=nauta",
                "--set", f"username={username}", "--set",
                "TillerImage={}".format(tiller_location), "--set",
                f"TensorboardServiceImage={tensorboard_service_location}"
            ]
            env = os.environ.copy()
            env['PATH'] = Config().config_path + os.pathsep + env['PATH']
            _, err_code, log_output = execute_system_command(
                ' '.join(add_user_command), env=env, shell=True)

        if err_code:
            handle_error(logger,
                         log_output,
                         Texts.USER_ADD_ERROR_MSG,
                         add_verbosity_msg=state.verbosity == 0)

            if not delete_user(username):
                handle_error(user_msg=Texts.REMOVE_USER_ERROR_MSG.format(
                    username=username))
            sys.exit(1)

        try:
            users_password = get_users_token(username)
        except Exception:
            handle_error(logger,
                         Texts.PASSWORD_GATHER_ERROR_MSG,
                         Texts.PASSWORD_GATHER_ERROR_MSG,
                         add_verbosity_msg=state.verbosity == 0)
            users_password = ""

        try:
            cert = get_certificate(username)
        except Exception:
            handle_error(logger,
                         Texts.CERT_GATHER_ERROR_MSG,
                         Texts.CERT_GATHER_ERROR_MSG,
                         add_verbosity_msg=state.verbosity == 0)
            cert = ""

    except Exception:
        handle_error(logger,
                     Texts.USER_ADD_ERROR_MSG.format(username=username),
                     Texts.USER_ADD_ERROR_MSG.format(username=username),
                     add_verbosity_msg=state.verbosity == 0)
        if not delete_user(username):
            handle_error(user_msg=Texts.REMOVE_USER_ERROR_MSG.format(
                username=username))
        sys.exit(1)

    if is_user_created(username, 90):
        click.echo(Texts.USER_CREATION_SUCCESS_MSG.format(username=username))
    else:
        # if during 90 seconds a user hasn't been created - app displays information about it
        # but don't step processing the command - config file generated here my be useful later
        # when user has been created
        click.echo(Texts.USER_NOT_READY_ERROR_MSG.format(username=username))

    try:
        kubeconfig = generate_kubeconfig(username, username,
                                         get_kubectl_host(), users_password,
                                         cert)
    except Exception:
        handle_error(logger,
                     Texts.CONFIG_CREATION_ERROR_MSG,
                     Texts.CONFIG_CREATION_ERROR_MSG,
                     add_verbosity_msg=state.verbosity == 0)
        exit(1)

    if list_only:
        click.echo(Texts.LIST_ONLY_HEADER)
        click.echo(kubeconfig)
    else:
        if not filename:
            filename = DEFAULT_FILENAME.format(username)
        try:
            with open(filename, "w") as file:
                file.write(kubeconfig)

            click.echo(Texts.CONFIG_SAVE_SUCCESS_MSG.format(filename=filename))
        except Exception:
            handle_error(logger,
                         Texts.CONFIG_SAVE_FAIL_MSG,
                         Texts.CONFIG_SAVE_FAIL_MSG,
                         add_verbosity_msg=state.verbosity == 0)
            click.echo(Texts.CONFIG_SAVE_FAIL_INSTRUCTIONS_MSG)
            click.echo(kubeconfig)
            sys.exit(1)
Exemple #6
0
def compute_hash_of_k8s_env_address():
    nauta_hostname = get_kubectl_host()
    nauta_hostname_hash = hashlib.md5(
        nauta_hostname.encode('utf-8')).hexdigest()
    return nauta_hostname_hash
Exemple #7
0
def test_get_k8s_host_wo_port(mocked_k8s_config):
    k8s_host = get_kubectl_host(with_port=False)

    assert k8s_host == '127.0.0.1'
Exemple #8
0
def test_get_k8s_host_w_port(mocked_k8s_config):
    k8s_host = get_kubectl_host()

    assert k8s_host == '127.0.0.1:8080'
Exemple #9
0
def test_get_k8s_host_wo_port(mocked_k8s_config):
    k8s_host = get_kubectl_host(replace_https=False, with_port=False)

    assert k8s_host == 'https://127.0.0.1'
Exemple #10
0
def test_get_k8s_host_w_port_replace_https(mocked_k8s_config):
    k8s_host = get_kubectl_host(replace_https=True, with_port=True)

    assert k8s_host == '127.0.0.1:8080'