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:
Tim Abbott
2016-01-10 11:58:11 -08:00
parent f871090bb6
commit 26e9d55e16
3 changed files with 38 additions and 47 deletions

View File

@@ -4,15 +4,13 @@ import shutil
import sys import sys
import subprocess import subprocess
import logging import logging
import shutil
import time
TARBALL_ARCHIVE_PATH="/home/zulip/archives" TARBALL_ARCHIVE_PATH="/home/zulip/archives"
os.environ["PYTHONUNBUFFERED"] = "y" os.environ["PYTHONUNBUFFERED"] = "y"
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..')) sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))
from zulip_tools import DEPLOYMENTS_DIR, LOCK_DIR, FAIL, WARNING, ENDC, \ from zulip_tools import DEPLOYMENTS_DIR, FAIL, WARNING, ENDC, \
mkdir_p, su_to_zulip mkdir_p, su_to_zulip, get_deployment_lock, release_deployment_lock
logging.basicConfig(format="%(asctime)s upgrade-zulip: %(message)s", logging.basicConfig(format="%(asctime)s upgrade-zulip: %(message)s",
level=logging.INFO) level=logging.INFO)
@@ -27,25 +25,8 @@ if len(sys.argv) != 2:
tarball_path = sys.argv[1] tarball_path = sys.argv[1]
start_time = time.time() error_rerun_script = "%s/current/scripts/upgrade-zulip %s" % (DEPLOYMENTS_DIR, tarball_path)
got_lock = False get_deployment_lock(error_rerun_script)
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)
try: try:
# Copy the release tarball to an archival path that's readable by # Copy the release tarball to an archival path that's readable by
@@ -72,4 +53,4 @@ try:
logging.info("Deployment complete") logging.info("Deployment complete")
finally: finally:
shutil.rmtree(LOCK_DIR) release_deployment_lock()

View File

@@ -3,14 +3,12 @@ import os
import sys import sys
import subprocess import subprocess
import logging import logging
import datetime
import shutil
import time
os.environ["PYTHONUNBUFFERED"] = "y" os.environ["PYTHONUNBUFFERED"] = "y"
sys.path.append(os.path.join(os.path.dirname(__file__), '..')) 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", logging.basicConfig(format="%(asctime)s update-deployment: %(message)s",
level=logging.INFO) level=logging.INFO)
@@ -25,22 +23,8 @@ subprocess.check_call(["mkdir", '-p',
DEPLOYMENTS_DIR, DEPLOYMENTS_DIR,
'/home/zulip/logs']) '/home/zulip/logs'])
start_time = time.time() error_rerun_script = "%s/current/tools/update-deployment %s" % (DEPLOYMENTS_DIR, refname)
got_lock = False get_deployment_lock(error_rerun_script)
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)
try: try:
deploy_path = make_deploy_path() deploy_path = make_deploy_path()
@@ -66,4 +50,4 @@ try:
logging.info("Deployment complete") logging.info("Deployment complete")
subprocess.check_call(["./scripts/purge-old-deployments"]) subprocess.check_call(["./scripts/purge-old-deployments"])
finally: finally:
shutil.rmtree(LOCK_DIR) release_deployment_lock()

View File

@@ -1,10 +1,12 @@
#!/usr/bin/env python2.7 #!/usr/bin/env python2.7
from __future__ import print_function from __future__ import print_function
import datetime
import errno import errno
import os import os
import sys
import datetime
import pwd import pwd
import shutil
import sys
import time
DEPLOYMENTS_DIR = "/home/zulip/deployments" DEPLOYMENTS_DIR = "/home/zulip/deployments"
LOCK_DIR = os.path.join(DEPLOYMENTS_DIR, "lock") LOCK_DIR = os.path.join(DEPLOYMENTS_DIR, "lock")
@@ -40,3 +42,27 @@ def mkdir_p(path):
pass pass
else: else:
raise 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)