Files
gitlab-instance-0a899031_cn…/scripts/ssh-git-pull-prod.py
2026-06-08 06:05:31 -05:00

60 lines
2.0 KiB
Python

#!/usr/bin/env python3
"""Git pull on production VPS and restart API."""
from __future__ import annotations
import os
import sys
import paramiko
from load_dev_env import ssh_config
HOST, USER, SSH_PASSWORD = ssh_config()
REPO = "/home/ubuntu/gitlab-instance-0a899031_cnomadcna"
GIT_USER = os.getenv("GIT_USER", "eric")
GIT_PASSWORD = os.getenv("GIT_PASSWORD", "")
GIT_REMOTE = os.getenv(
"GIT_REMOTE",
"https://gitea.dsx2020.com/eric/gitlab-instance-0a899031_cnomadcna.git",
)
BRANCH = os.getenv("GIT_BRANCH", "test2")
def run(c: paramiko.SSHClient, cmd: str, timeout: int = 180) -> int:
safe = cmd.replace(GIT_PASSWORD, "***").replace(SSH_PASSWORD, "***")
print(f"\n$ {safe}")
_, o, e = c.exec_command(f"echo '{SSH_PASSWORD}' | sudo -S bash -lc \"{cmd}\"", timeout=timeout, get_pty=True)
out = o.read().decode("utf-8", errors="replace")
err = e.read().decode("utf-8", errors="replace")
code = o.channel.recv_exit_status()
if out:
sys.stdout.buffer.write(out.encode("utf-8", errors="replace"))
if err:
sys.stdout.buffer.write(err.encode("utf-8", errors="replace"))
return code
def main() -> int:
auth_url = GIT_REMOTE.replace("https://", f"https://{GIT_USER}:{GIT_PASSWORD}@")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(HOST, username=USER, password=SSH_PASSWORD, timeout=30)
try:
run(c, f"cp {REPO}/.env.local /tmp/nomadcna.env.local.bak 2>/dev/null || true")
run(c, f"cd {REPO} && git fetch {auth_url} {BRANCH}")
run(c, f"cd {REPO} && git checkout {BRANCH}")
run(c, f"cd {REPO} && git reset --hard FETCH_HEAD")
run(c, f"cp /tmp/nomadcna.env.local.bak {REPO}/.env.local 2>/dev/null || true")
run(c, "systemctl restart cnomadcna-api.service")
run(c, "systemctl is-active cnomadcna-api.service")
run(c, f"cd {REPO} && git log -1 --oneline")
return 0
finally:
c.close()
if __name__ == "__main__":
sys.exit(main())