Commit 29624c22 authored by Muhammad Umair Khan's avatar Muhammad Umair Khan
Browse files

Update deployment configurations and user management UI

- Restrict manual user creation to local provider and admin role
- Simplify Add New User UI panel and adjust tab ordering
- Resolve PyInfra curl error during NVM installation
- Improve PyInfra path exports by moving them from .bashrc to .profile for SSH sessions
- Fix GRUB kernel update regex for compatibility with PyInfra
parent f7c21cd5
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -39,6 +39,8 @@ image:
  env:
    MEEP_SVC_PATH: /auth/v1
    MEEP_MAX_SESSIONS: "10"
    MEEP_DEFAULT_ADMIN_PROVIDER: ""
    MEEP_DEFAULT_ADMIN_USERNAME: ""
    MEEP_OAUTH_GITHUB_ENABLED: "false"
    MEEP_OAUTH_GITHUB_AUTH_URL: ""
    MEEP_OAUTH_GITHUB_TOKEN_URL: ""
+21 −12
Original line number Diff line number Diff line
import shlex

from pyinfra import host
from pyinfra.api import operation, StringCommand
from pyinfra.facts.files import File
@@ -35,8 +37,13 @@ def install_golangci_lint(version, gocode_bin_dir):
    if current_version_output and clean_version in current_version_output:
        return
        
    gopath = f"{gocode_bin_dir}/.."
    gocache = f"{gopath}/cache"
    golangci_cache = f"{gopath}/cache/golangci-lint"
    cmd = (
        f"/usr/local/go/bin/go env -w GOPATH={gocode_bin_dir}/.. && "
        f"export GOPATH={gopath} GOCACHE={gocache} GOLANGCI_LINT_CACHE={golangci_cache} && "
        f"mkdir -p {gocache} {golangci_cache} && "
        f"/usr/local/go/bin/go env -w GOPATH={gopath} && "
        f"curl --retry 3 --retry-delay 5 -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b {gocode_bin_dir} {version}"
    )
    yield StringCommand(cmd)
@@ -45,19 +52,21 @@ def install_golangci_lint(version, gocode_bin_dir):
@operation()
def install_nvm(version, target_home):
    """
    install NVM with retry resilience.
    Install NVM with retry resilience.

    Pipe curl to bash (nvm's documented installer) instead of pyinfra
    files.download. files.download writes with `curl -o` into a hashed temp
    file under TmpDir, which is memoized from earlier sudo operations and can
    be unwritable for this unprivileged step (curl error 23).
    """
    if host.get_fact(File, path=f"{target_home}/.nvm/nvm.sh"):
    nvm_dir = f"{target_home}/.nvm"
    if host.get_fact(File, path=f"{nvm_dir}/nvm.sh"):
        return

    yield from files.download._inner(
        src=f"https://raw.githubusercontent.com/nvm-sh/nvm/{version}/install.sh",
        dest=f"{target_home}/install_nvm.sh"
    )
    yield StringCommand(f"bash {target_home}/install_nvm.sh")
    yield from files.file._inner(
        path=f"{target_home}/install_nvm.sh",
        present=False
    url = f"https://raw.githubusercontent.com/nvm-sh/nvm/{version}/install.sh"
    yield StringCommand(
        f"export HOME={shlex.quote(target_home)} NVM_DIR={shlex.quote(nvm_dir)} && "
        f"curl --retry 3 --retry-delay 5 -fsSL {shlex.quote(url)} | bash"
    )

@operation()
+19 −2
Original line number Diff line number Diff line
@@ -13,8 +13,25 @@ _IMPORT_SCENARIOS_SCRIPT = os.path.normpath(os.path.join(_SCRIPT_DIR, "../script
def _build_env_prefix(target_home, node_version):
    """Creates a bash command prefix that sets all required environment variables for meepctl to function."""
    sudo_pass = host.data.get("sudo_password") or ""
    path = f"/usr/local/go/bin:{target_home}/gocode/bin:{target_home}/.nvm/versions/node/v{node_version}/bin:/snap/bin:/usr/local/bin:/usr/bin:/bin"
    return f"export PATH={path} GOPATH={target_home}/gocode HOME={target_home} KUBECONFIG={target_home}/.kube/config SUDO_PASSWORD='{sudo_pass}' &&"
    gopath = f"{target_home}/gocode"
    gocache = f"{gopath}/cache"
    gomodcache = f"{gopath}/pkg/mod"
    golangci_cache = f"{gopath}/cache/golangci-lint"
    node_bin = f"{target_home}/.nvm/versions/node/v{node_version}/bin"
    path = f"/usr/local/go/bin:{gopath}/bin:{node_bin}:/snap/bin:/usr/local/bin:/usr/bin:/bin"
    # Tool caches live under GOPATH so `go` / golangci-lint never touch a
    # root-owned ~/.cache left behind by earlier sudo builds.
    return (
        f"export PATH={shlex.quote(path)} "
        f"GOPATH={shlex.quote(gopath)} "
        f"GOCACHE={shlex.quote(gocache)} "
        f"GOMODCACHE={shlex.quote(gomodcache)} "
        f"GOLANGCI_LINT_CACHE={shlex.quote(golangci_cache)} "
        f"HOME={shlex.quote(target_home)} "
        f"KUBECONFIG={shlex.quote(target_home + '/.kube/config')} "
        f"SUDO_PASSWORD={shlex.quote(sudo_pass)} && "
        f"mkdir -p {shlex.quote(gocache)} {shlex.quote(gomodcache)} {shlex.quote(golangci_cache)} &&"
    )

@operation()
def validate_mec_host_address(ip_address):
+20 −4
Original line number Diff line number Diff line
@@ -52,11 +52,27 @@ files.directory(
    _sudo=True
)

files.directory(
    name="Create GOCACHE directory",
    path=f"{target_home}/gocode/cache",
    user=target_user,
    group=target_user,
    mode="0755",
    present=True,
    _sudo=True
)

files.block(
    name="Setup Go environment in .bashrc",
    path=f"{target_home}/.bashrc",
    marker="# {mark} PYINFRA MANAGED - Go environment setup",
    content="export GOPATH=$HOME/gocode\nexport PATH=$PATH:$GOPATH/bin:/usr/local/go/bin"
    name="Setup Development environment in .profile",
    path=f"{target_home}/.profile",
    marker="# {mark} PYINFRA MANAGED - Development environment setup",
    content=(
        "export GOPATH=$HOME/gocode\n"
        "export GOCACHE=$GOPATH/cache\n"
        "export GOMODCACHE=$GOPATH/pkg/mod\n"
        "export GOLANGCI_LINT_CACHE=$GOPATH/cache/golangci-lint\n"
        f"export PATH=$PATH:$GOPATH/bin:/usr/local/go/bin:{target_home}/.nvm/versions/node/v{node_version}/bin"
    )
)

dev.install_golangci_lint(
+20 −12
Original line number Diff line number Diff line
@@ -69,17 +69,25 @@ systemd.daemon_reload(
    _sudo=True
)

# Disable IPv6 at the kernel level via GRUB
# Disable IPv6 at the kernel level via GRUB.
# files.replace uses sed (not Python regex), so the pattern must be BRE/ERE:
# no lookaheads. Skip when the file is missing or already has ipv6.disable=1.
grub_cmdline = host.get_fact(
    Command,
    "grep -E '^GRUB_CMDLINE_LINUX=' /etc/default/grub 2>/dev/null || true",
)

if grub_cmdline and "ipv6.disable=1" not in grub_cmdline:
    files.replace(
        name="Disable IPv6 in GRUB",
        path="/etc/default/grub",
    text=r'^GRUB_CMDLINE_LINUX="((?!.*ipv6\.disable=1).*)"$',
        text=r'^GRUB_CMDLINE_LINUX="(.*)"$',
        replace=r'GRUB_CMDLINE_LINUX="\1 ipv6.disable=1"',
    _sudo=True
        extended_regex=True,
        _sudo=True,
    )

    server.shell(
        name="Update GRUB",
        commands=["update-grub"],
    _sudo=True
        _sudo=True,
    )
Loading