From 3f83b843c24a7fecabff709847cd9bf286039ce9 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Thu, 13 May 2021 12:53:04 -0700 Subject: [PATCH] upgrade-zulip-from-git: Create deployment directories with git worktree. Signed-off-by: Anders Kaseorg --- scripts/lib/upgrade-zulip-from-git | 12 ++++++++---- scripts/purge-old-deployments | 11 ++++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/scripts/lib/upgrade-zulip-from-git b/scripts/lib/upgrade-zulip-from-git index 42d318f86e..6701bb51a8 100755 --- a/scripts/lib/upgrade-zulip-from-git +++ b/scripts/lib/upgrade-zulip-from-git @@ -63,7 +63,7 @@ try: logging.info("Cloning the repository") subprocess.check_call( ["git", "clone", "-q", remote_url, "--mirror", LOCAL_GIT_CACHE_DIR], - stdout=open("/dev/null", "w"), + stdout=subprocess.DEVNULL, ) if os.stat(LOCAL_GIT_CACHE_DIR).st_uid == 0: subprocess.check_call(["chown", "-R", "zulip:zulip", LOCAL_GIT_CACHE_DIR]) @@ -83,13 +83,17 @@ try: logging.info("Fetching the latest commits") subprocess.check_call(["git", "fetch", "-q", "--tags", "--all"], preexec_fn=su_to_zulip) - # Generate the deployment directory via git clone from our local repository. + # Generate the deployment directory via git worktree from our local repository. subprocess.check_call( - ["git", "clone", "-q", "-b", refname, LOCAL_GIT_CACHE_DIR, deploy_path], - stdout=open("/dev/null", "w"), + ["git", "worktree", "add", "--detach", deploy_path, refname], + stdout=subprocess.DEVNULL, preexec_fn=su_to_zulip, ) os.chdir(deploy_path) + subprocess.check_call( + ["git", "checkout", "-qtb", "deployment-" + os.path.basename(deploy_path), refname], + preexec_fn=su_to_zulip, + ) overwrite_symlink("/etc/zulip/settings.py", "zproject/prod_settings.py") diff --git a/scripts/purge-old-deployments b/scripts/purge-old-deployments index 953feb9891..0e6f68a71a 100755 --- a/scripts/purge-old-deployments +++ b/scripts/purge-old-deployments @@ -7,7 +7,12 @@ from typing import Set ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(ZULIP_PATH) -from scripts.lib.zulip_tools import DEPLOYMENTS_DIR, get_recent_deployments, may_be_perform_purging +from scripts.lib.zulip_tools import ( + DEPLOYMENTS_DIR, + get_recent_deployments, + may_be_perform_purging, + su_to_zulip, +) def parse_args() -> argparse.Namespace: @@ -70,6 +75,10 @@ def main() -> None: ) if not args.dry_run: + subprocess.check_call( + ["git", "worktree", "prune"], cwd="/srv/zulip.git", preexec_fn=su_to_zulip + ) + print("Deployments cleaned successfully...") print("Cleaning orphaned/unused caches...")