mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	deployments: Refactor locking libraries into zulip_tools.py.
The code in update-deployment and upgrade-zulip for managing the deployment lock was nearly identical.
This commit is contained in:
		@@ -4,15 +4,13 @@ import shutil
 | 
			
		||||
import sys
 | 
			
		||||
import subprocess
 | 
			
		||||
import logging
 | 
			
		||||
import shutil
 | 
			
		||||
import time
 | 
			
		||||
 | 
			
		||||
TARBALL_ARCHIVE_PATH="/home/zulip/archives"
 | 
			
		||||
os.environ["PYTHONUNBUFFERED"] = "y"
 | 
			
		||||
 | 
			
		||||
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))
 | 
			
		||||
from zulip_tools import DEPLOYMENTS_DIR, LOCK_DIR, FAIL, WARNING, ENDC, \
 | 
			
		||||
    mkdir_p, su_to_zulip
 | 
			
		||||
from zulip_tools import DEPLOYMENTS_DIR, FAIL, WARNING, ENDC, \
 | 
			
		||||
    mkdir_p, su_to_zulip, get_deployment_lock, release_deployment_lock
 | 
			
		||||
 | 
			
		||||
logging.basicConfig(format="%(asctime)s upgrade-zulip: %(message)s",
 | 
			
		||||
                    level=logging.INFO)
 | 
			
		||||
@@ -27,25 +25,8 @@ if len(sys.argv) != 2:
 | 
			
		||||
 | 
			
		||||
tarball_path = sys.argv[1]
 | 
			
		||||
 | 
			
		||||
start_time = time.time()
 | 
			
		||||
got_lock = False
 | 
			
		||||
while time.time() - start_time < 300:
 | 
			
		||||
    try:
 | 
			
		||||
        os.mkdir(LOCK_DIR)
 | 
			
		||||
        got_lock = True
 | 
			
		||||
        break
 | 
			
		||||
    except OSError:
 | 
			
		||||
        print WARNING + "Another deployment in progress; waiting for lock... (If no deployment is running, rmdir %s)" % (LOCK_DIR,) + ENDC
 | 
			
		||||
        time.sleep(3)
 | 
			
		||||
 | 
			
		||||
if not got_lock:
 | 
			
		||||
    print FAIL + "Deployment already in progress.  Please run\n" \
 | 
			
		||||
               + "  %s/current/scripts/upgrade-zulip %s\n" % (DEPLOYMENTS_DIR, tarball_path) \
 | 
			
		||||
               + "manually when the previous deployment finishes, or run\n" \
 | 
			
		||||
               + "  rmdir %s\n"  % (LOCK_DIR,) \
 | 
			
		||||
               + "if the previous deployment crashed." \
 | 
			
		||||
               + ENDC
 | 
			
		||||
    sys.exit(1)
 | 
			
		||||
error_rerun_script = "%s/current/scripts/upgrade-zulip %s" % (DEPLOYMENTS_DIR, tarball_path)
 | 
			
		||||
get_deployment_lock(error_rerun_script)
 | 
			
		||||
 | 
			
		||||
try:
 | 
			
		||||
    # Copy the release tarball to an archival path that's readable by
 | 
			
		||||
@@ -72,4 +53,4 @@ try:
 | 
			
		||||
 | 
			
		||||
    logging.info("Deployment complete")
 | 
			
		||||
finally:
 | 
			
		||||
    shutil.rmtree(LOCK_DIR)
 | 
			
		||||
    release_deployment_lock()
 | 
			
		||||
 
 | 
			
		||||
@@ -3,14 +3,12 @@ import os
 | 
			
		||||
import sys
 | 
			
		||||
import subprocess
 | 
			
		||||
import logging
 | 
			
		||||
import datetime
 | 
			
		||||
import shutil
 | 
			
		||||
import time
 | 
			
		||||
 | 
			
		||||
os.environ["PYTHONUNBUFFERED"] = "y"
 | 
			
		||||
 | 
			
		||||
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
 | 
			
		||||
from zulip_tools import DEPLOYMENTS_DIR, LOCK_DIR, FAIL, WARNING, ENDC, make_deploy_path
 | 
			
		||||
from zulip_tools import DEPLOYMENTS_DIR, FAIL, WARNING, ENDC, make_deploy_path, \
 | 
			
		||||
    get_deployment_lock, release_deployment_lock
 | 
			
		||||
 | 
			
		||||
logging.basicConfig(format="%(asctime)s update-deployment: %(message)s",
 | 
			
		||||
                    level=logging.INFO)
 | 
			
		||||
@@ -25,22 +23,8 @@ subprocess.check_call(["mkdir", '-p',
 | 
			
		||||
                       DEPLOYMENTS_DIR,
 | 
			
		||||
                       '/home/zulip/logs'])
 | 
			
		||||
 | 
			
		||||
start_time = time.time()
 | 
			
		||||
got_lock = False
 | 
			
		||||
while time.time() - start_time < 300:
 | 
			
		||||
    try:
 | 
			
		||||
        os.mkdir(LOCK_DIR)
 | 
			
		||||
        got_lock = True
 | 
			
		||||
        break
 | 
			
		||||
    except OSError:
 | 
			
		||||
        print WARNING + "Another deployment in progress; waiting for lock..." + ENDC
 | 
			
		||||
        time.sleep(3)
 | 
			
		||||
 | 
			
		||||
if not got_lock:
 | 
			
		||||
    print FAIL + "Deployment already in progress.  Please run\n" \
 | 
			
		||||
               + "  %s/current/tools/update-deployment %s\n" % (DEPLOYMENTS_DIR, refname) \
 | 
			
		||||
               + "manually when the current deployment finishes." + ENDC
 | 
			
		||||
    sys.exit(1)
 | 
			
		||||
error_rerun_script = "%s/current/tools/update-deployment %s" % (DEPLOYMENTS_DIR, refname)
 | 
			
		||||
get_deployment_lock(error_rerun_script)
 | 
			
		||||
 | 
			
		||||
try:
 | 
			
		||||
    deploy_path = make_deploy_path()
 | 
			
		||||
@@ -66,4 +50,4 @@ try:
 | 
			
		||||
    logging.info("Deployment complete")
 | 
			
		||||
    subprocess.check_call(["./scripts/purge-old-deployments"])
 | 
			
		||||
finally:
 | 
			
		||||
    shutil.rmtree(LOCK_DIR)
 | 
			
		||||
    release_deployment_lock()
 | 
			
		||||
 
 | 
			
		||||
@@ -1,10 +1,12 @@
 | 
			
		||||
#!/usr/bin/env python2.7
 | 
			
		||||
from __future__ import print_function
 | 
			
		||||
import datetime
 | 
			
		||||
import errno
 | 
			
		||||
import os
 | 
			
		||||
import sys
 | 
			
		||||
import datetime
 | 
			
		||||
import pwd
 | 
			
		||||
import shutil
 | 
			
		||||
import sys
 | 
			
		||||
import time
 | 
			
		||||
 | 
			
		||||
DEPLOYMENTS_DIR = "/home/zulip/deployments"
 | 
			
		||||
LOCK_DIR = os.path.join(DEPLOYMENTS_DIR, "lock")
 | 
			
		||||
@@ -40,3 +42,27 @@ def mkdir_p(path):
 | 
			
		||||
            pass
 | 
			
		||||
        else:
 | 
			
		||||
            raise
 | 
			
		||||
 | 
			
		||||
def get_deployment_lock(error_rerun_script):
 | 
			
		||||
    start_time = time.time()
 | 
			
		||||
    got_lock = False
 | 
			
		||||
    while time.time() - start_time < 300:
 | 
			
		||||
        try:
 | 
			
		||||
            os.mkdir(LOCK_DIR)
 | 
			
		||||
            got_lock = True
 | 
			
		||||
            break
 | 
			
		||||
        except OSError:
 | 
			
		||||
            print(WARNING + "Another deployment in progress; waiting for lock... (If no deployment is running, rmdir %s)" % (LOCK_DIR,) + ENDC)
 | 
			
		||||
            time.sleep(3)
 | 
			
		||||
 | 
			
		||||
    if not got_lock:
 | 
			
		||||
        print(FAIL + "Deployment already in progress.  Please run\n"
 | 
			
		||||
              + "  %s\n" % (error_rerun_script,)
 | 
			
		||||
              + "manually when the previous deployment finishes, or run\n"
 | 
			
		||||
              + "  rmdir %s\n"  % (LOCK_DIR,)
 | 
			
		||||
              + "if the previous deployment crashed."
 | 
			
		||||
              + ENDC)
 | 
			
		||||
        sys.exit(1)
 | 
			
		||||
 | 
			
		||||
def release_deployment_lock():
 | 
			
		||||
    shutil.rmtree(LOCK_DIR)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user