2015-11-16 17:40:32 +01:00
|
|
|
#!/usr/bin/env python2.7
|
2013-11-14 06:32:49 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
import logging
|
|
|
|
import shutil
|
|
|
|
import time
|
|
|
|
|
2015-11-16 17:40:32 +01:00
|
|
|
os.environ["PYTHONUNBUFFERED"] = "y"
|
|
|
|
|
2013-11-14 06:32:49 +01:00
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..'))
|
|
|
|
from zulip_tools import DEPLOYMENTS_DIR, LOCK_DIR, FAIL, WARNING, ENDC, \
|
|
|
|
su_to_zulip
|
|
|
|
|
|
|
|
logging.basicConfig(format="%(asctime)s upgrade-zulip: %(message)s",
|
|
|
|
level=logging.INFO)
|
|
|
|
|
|
|
|
if os.getuid() != 0:
|
|
|
|
logging.error("Must be run as root.")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if len(sys.argv) != 2:
|
|
|
|
print FAIL + "Usage: %s <tarball>" % (sys.argv[0],) + ENDC
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
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:
|
2013-11-14 17:53:50 +01:00
|
|
|
print WARNING + "Another deployment in progress; waiting for lock... (If no deployment is running, rmdir %s)" % (LOCK_DIR,) + ENDC
|
2016-01-10 19:46:09 +01:00
|
|
|
time.sleep(3)
|
2013-11-14 06:32:49 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2016-01-10 19:46:09 +01:00
|
|
|
try:
|
|
|
|
logging.info("Unpacking the tarball")
|
|
|
|
unpack_zulip = os.path.realpath(os.path.join(os.path.dirname(__file__), 'unpack-zulip'))
|
|
|
|
deploy_path = subprocess.check_output([unpack_zulip, tarball_path], preexec_fn=su_to_zulip)
|
2013-11-14 06:32:49 +01:00
|
|
|
|
2016-01-10 19:46:09 +01:00
|
|
|
deploy_path = deploy_path.strip()
|
|
|
|
os.chdir(deploy_path)
|
2013-11-14 06:32:49 +01:00
|
|
|
|
2016-01-10 19:46:09 +01:00
|
|
|
subprocess.check_call(["./scripts/lib/upgrade-zulip-stage-2", deploy_path])
|
|
|
|
|
|
|
|
logging.info("Deployment complete")
|
|
|
|
finally:
|
|
|
|
shutil.rmtree(LOCK_DIR)
|