Commit d3e9737a authored by Muhammad Umair Khan's avatar Muhammad Umair Khan
Browse files

fix(deploy): update pyinfra deployment scripts for v3 compatibility and idempotency

This commit modernizes the deployment scripts for compatibility with the latest version of Pyinfra and improves the idempotency of several operations:
- Replaced inline sed commands with proper files.line operations for managing apt sources.list.
- Replaced bare server.shell executions (e.g., holding apt packages, changing socket group ownership) with stateful host.get_fact(Command, ...) checks to ensure they only run when necessary.
- Added --protocol argument to update_repocfg.py to dynamically handle https-only ingress configurations.
- Removed redundant ownership assignment of gopath which caused permissions errors.
parent 8b20da57
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -20,6 +20,9 @@ K8S_WORKERS=""
# e.g., 192.168.1.100 or mec.example.com
MEC_HOST_ADDRESS=""

# The protocol to use for the ingress (http or https)
MEC_PROTOCOL="https"

# ----------------------------------------------------
# GitHub OAuth Secrets
# ----------------------------------------------------
+26 −6
Original line number Diff line number Diff line
from pyinfra import host
from pyinfra.api import operation, StringCommand
from pyinfra.facts.files import File
from pyinfra.operations import files
from pyinfra.facts.server import Command

@operation()
def install_go(version, url):
@@ -10,17 +12,29 @@ def install_go(version, url):
    if host.get_fact(File, path="/usr/local/go/bin/go"):
        return
        
    yield StringCommand(f"wget --tries=3 --timeout=15 -O /tmp/go{version}.linux-amd64.tar.gz {url}")
    yield from files.download._inner(
        src=url,
        dest=f"/tmp/go{version}.linux-amd64.tar.gz"
    )
    yield StringCommand("rm -rf /usr/local/go")
    yield StringCommand(f"tar -C /usr/local -xzf /tmp/go{version}.linux-amd64.tar.gz")
    yield StringCommand(f"rm /tmp/go{version}.linux-amd64.tar.gz")
    yield from files.file._inner(
        path=f"/tmp/go{version}.linux-amd64.tar.gz",
        present=False
    )

@operation()
def install_golangci_lint(version, gocode_bin_dir):
    """
    Install golangci-lint at the specified version with retry resilience.
    Always reinstalls to ensure the correct version.
    Install golangci-lint at the specified version.
    """
    binary_path = f"{gocode_bin_dir}/golangci-lint"
    current_version_output = host.get_fact(Command, f"{binary_path} --version 2>/dev/null || echo missing")
    
    clean_version = version.lstrip('v')
    if current_version_output and clean_version in current_version_output:
        return
        
    cmd = (
        f"/usr/local/go/bin/go env -w GOPATH={gocode_bin_dir}/.. && "
        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}"
@@ -36,9 +50,15 @@ def install_nvm(version, target_home):
    if host.get_fact(File, path=f"{target_home}/.nvm/nvm.sh"):
        return
        
    yield StringCommand(f"curl --retry 3 --retry-delay 5 -o {target_home}/install_nvm.sh https://raw.githubusercontent.com/nvm-sh/nvm/{version}/install.sh")
    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 StringCommand(f"rm {target_home}/install_nvm.sh")
    yield from files.file._inner(
        path=f"{target_home}/install_nvm.sh",
        present=False
    )

@operation()
def install_node_and_packages(node_version, npm_version, eslint_version, target_home):
+6 −2
Original line number Diff line number Diff line
@@ -111,7 +111,7 @@ def deploy_dep(mec_sandbox_dir, target_home, node_version, force=True):
    # if not host.get_fact(File, path=f"{target_home}/.meep/.02_deps_deployed"):
    prefix = _build_env_prefix(target_home, node_version)
    force_flag = "-f " if force else ""
    yield StringCommand(f"{prefix} meepctl deploy dep all -v {force_flag} > /dev/tty 2>&1 || true")
    yield StringCommand(f"{prefix} meepctl deploy dep all {force_flag} > /dev/tty 2>&1 || true")
    # yield StringCommand(f"mkdir -p {target_home}/.meep && touch {target_home}/.meep/.02_deps_deployed")


@@ -185,11 +185,15 @@ def update_meepctl_repocfg(repocfg_path, host_address, github_enabled=True, gitl
        add_deploy_dir=False,
        mode="0755"
    )
    
    protocol = os.environ.get("MEC_PROTOCOL", "https").strip().lower()

    path = shlex.quote(repocfg_path)
    host_addr = shlex.quote(host_address)
    prot = shlex.quote(protocol)
    gh_flag = "--github-enabled" if github_enabled else ""
    gl_flag = "--gitlab-enabled" if gitlab_enabled else ""
    cmd = f"python3 /tmp/meep_update_repocfg.py --path {path} --host {host_addr} {gh_flag} {gl_flag}".strip()
    cmd = f"python3 /tmp/meep_update_repocfg.py --path {path} --host {host_addr} --protocol {prot} {gh_flag} {gl_flag}".strip()
    yield StringCommand(cmd)


+7 −1
Original line number Diff line number Diff line
#!/usr/bin/env python3
"""
Idempotently updates ingress host, redirect URIs, and provider enabled status in .meepctl-repocfg.yaml.
Updates ingress host, redirect URIs, and provider enabled status in .meepctl-repocfg.yaml.
"""
import argparse
import os
@@ -13,6 +13,7 @@ def main():
    parser = argparse.ArgumentParser(description="Update .meepctl-repocfg.yaml")
    parser.add_argument("--path", required=True, help="Path to .meepctl-repocfg.yaml")
    parser.add_argument("--host", required=True, help="MEC host address")
    parser.add_argument("--protocol", default="https", help="MEC protocol (http or https)")
    parser.add_argument("--github-enabled", action="store_true", help="Enable GitHub OAuth")
    parser.add_argument("--gitlab-enabled", action="store_true", help="Enable GitLab OAuth")
    args = parser.parse_args()
@@ -53,6 +54,11 @@ def main():
            ingress["ca"] = "self-signed"
            changed = True
            
        is_https_only = (args.protocol != "http")
        if ingress.get("https-only") != is_https_only:
            ingress["https-only"] = is_https_only
            changed = True

    auth = deploy.get("auth", {})

    gh = auth.get("github", {})
+0 −9
Original line number Diff line number Diff line
@@ -52,15 +52,6 @@ files.directory(
    _sudo=True
)

server.shell(
    name="Reclaim GOPATH ownership from root",
    commands=[
        f"chown -R {target_user}:{target_user} {target_home}/gocode || true"
    ],
    _sudo=True
)


files.block(
    name="Setup Go environment in .bashrc",
    path=f"{target_home}/.bashrc",
Loading