2016-04-07 15:03:22 +02:00
|
|
|
#!/usr/bin/env python
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2016-01-10 20:58:11 +01:00
|
|
|
import datetime
|
2016-01-10 20:36:38 +01:00
|
|
|
import errno
|
2013-05-16 18:02:25 +02:00
|
|
|
import os
|
2013-11-13 21:54:30 +01:00
|
|
|
import pwd
|
2016-01-10 20:58:11 +01:00
|
|
|
import shutil
|
2016-04-06 17:15:31 +02:00
|
|
|
import subprocess
|
2016-01-10 20:58:11 +01:00
|
|
|
import sys
|
|
|
|
import time
|
2013-02-19 02:36:59 +01:00
|
|
|
|
2016-07-12 17:08:35 +02:00
|
|
|
if False:
|
2016-08-18 13:50:36 +02:00
|
|
|
from typing import Sequence, Any
|
2016-07-12 17:08:35 +02:00
|
|
|
|
2013-10-04 19:19:57 +02:00
|
|
|
DEPLOYMENTS_DIR = "/home/zulip/deployments"
|
2013-05-16 18:02:25 +02:00
|
|
|
LOCK_DIR = os.path.join(DEPLOYMENTS_DIR, "lock")
|
|
|
|
TIMESTAMP_FORMAT = '%Y-%m-%d-%H-%M-%S'
|
2013-06-05 00:21:47 +02:00
|
|
|
|
|
|
|
# Color codes
|
|
|
|
OKBLUE = '\033[94m'
|
|
|
|
OKGREEN = '\033[92m'
|
|
|
|
WARNING = '\033[93m'
|
|
|
|
FAIL = '\033[91m'
|
|
|
|
ENDC = '\033[0m'
|
2013-11-13 20:57:31 +01:00
|
|
|
|
2016-07-12 16:55:20 +02:00
|
|
|
def subprocess_text_output(args):
|
2016-07-12 17:08:35 +02:00
|
|
|
# type: (Sequence[str]) -> str
|
2016-07-12 16:55:20 +02:00
|
|
|
return subprocess.check_output(args, universal_newlines=True).strip()
|
|
|
|
|
2013-11-13 21:54:30 +01:00
|
|
|
def su_to_zulip():
|
2016-07-12 17:08:35 +02:00
|
|
|
# type: () -> None
|
2013-11-13 21:54:30 +01:00
|
|
|
pwent = pwd.getpwnam("zulip")
|
|
|
|
os.setgid(pwent.pw_gid)
|
|
|
|
os.setuid(pwent.pw_uid)
|
2016-07-29 22:35:07 +02:00
|
|
|
os.environ['HOME'] = os.path.abspath(os.path.join(DEPLOYMENTS_DIR, '..'))
|
2013-11-13 21:54:30 +01:00
|
|
|
|
2013-11-13 20:57:31 +01:00
|
|
|
def make_deploy_path():
|
2016-07-12 17:08:35 +02:00
|
|
|
# type: () -> str
|
2013-11-13 20:57:31 +01:00
|
|
|
timestamp = datetime.datetime.now().strftime(TIMESTAMP_FORMAT)
|
|
|
|
return os.path.join(DEPLOYMENTS_DIR, timestamp)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
cmd = sys.argv[1]
|
|
|
|
if cmd == 'make_deploy_path':
|
2015-11-01 17:11:06 +01:00
|
|
|
print(make_deploy_path())
|
2016-01-10 20:36:38 +01:00
|
|
|
|
|
|
|
def mkdir_p(path):
|
2016-07-12 17:08:35 +02:00
|
|
|
# type: (str) -> None
|
2016-01-10 20:36:38 +01:00
|
|
|
# Python doesn't have an analog to `mkdir -p` < Python 3.2.
|
|
|
|
try:
|
|
|
|
os.makedirs(path)
|
2016-03-10 13:53:26 +01:00
|
|
|
except OSError as e:
|
2016-01-10 20:36:38 +01:00
|
|
|
if e.errno == errno.EEXIST and os.path.isdir(path):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise
|
2016-01-10 20:58:11 +01:00
|
|
|
|
|
|
|
def get_deployment_lock(error_rerun_script):
|
2016-07-12 17:08:35 +02:00
|
|
|
# type: (str) -> None
|
2016-01-10 20:58:11 +01:00
|
|
|
start_time = time.time()
|
|
|
|
got_lock = False
|
|
|
|
while time.time() - start_time < 300:
|
|
|
|
try:
|
|
|
|
os.mkdir(LOCK_DIR)
|
|
|
|
got_lock = True
|
|
|
|
break
|
|
|
|
except OSError:
|
2016-07-23 21:11:23 +02:00
|
|
|
print(WARNING + "Another deployment in progress; waiting for lock... "
|
|
|
|
+ "(If no deployment is running, rmdir %s)" % (LOCK_DIR,) + ENDC)
|
2016-01-10 21:03:02 +01:00
|
|
|
sys.stdout.flush()
|
2016-01-10 20:58:11 +01:00
|
|
|
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():
|
2016-07-12 17:08:35 +02:00
|
|
|
# type: () -> None
|
2016-01-10 20:58:11 +01:00
|
|
|
shutil.rmtree(LOCK_DIR)
|
2016-04-06 17:15:31 +02:00
|
|
|
|
2016-08-18 13:50:36 +02:00
|
|
|
def run(args, **kwargs):
|
|
|
|
# type: (Sequence[str], **Any) -> int
|
2016-04-06 17:15:31 +02:00
|
|
|
# Output what we're doing in the `set -x` style
|
|
|
|
print("+ %s" % (" ".join(args)))
|
2016-08-18 13:50:36 +02:00
|
|
|
process = subprocess.Popen(args, **kwargs)
|
2016-04-06 17:15:31 +02:00
|
|
|
rc = process.wait()
|
|
|
|
if rc:
|
2016-07-02 14:24:00 +02:00
|
|
|
raise subprocess.CalledProcessError(rc, args) # type: ignore # https://github.com/python/typeshed/pull/329
|
2016-04-06 17:15:31 +02:00
|
|
|
return 0
|