py3: Switch almost all shebang lines to use `python3`.
This causes `upgrade-zulip-from-git`, as well as a no-option run of
`tools/build-release-tarball`, to produce a Zulip install running
Python 3, rather than Python 2. In particular this means that the
virtualenv we create, in which all application code runs, is Python 3.
One shebang line, on `zulip-ec2-configure-interfaces`, explicitly
keeps Python 2, and at least one external ops script, `wal-e`, also
still runs on Python 2. See discussion on the respective previous
commits that made those explicit. There may also be some other
third-party scripts we use, outside of this source tree and running
outside our virtualenv, that still run on Python 2.
2017-08-02 23:15:16 +02:00
|
|
|
#!/usr/bin/env python3
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2017-08-21 19:41:19 +02:00
|
|
|
import argparse
|
2016-01-10 20:58:11 +01:00
|
|
|
import datetime
|
2016-01-10 20:36:38 +01:00
|
|
|
import errno
|
2017-08-18 06:36:37 +02:00
|
|
|
import logging
|
2013-05-16 18:02:25 +02:00
|
|
|
import os
|
2013-11-13 21:54:30 +01:00
|
|
|
import pwd
|
2017-06-21 14:32:22 +02:00
|
|
|
import re
|
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:
|
2017-08-18 20:34:00 +02:00
|
|
|
from typing import Sequence, Set, Text, 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'
|
2017-01-11 17:07:12 +01:00
|
|
|
BLACKONYELLOW = '\x1b[0;30;43m'
|
|
|
|
WHITEONRED = '\x1b[0;37;41m'
|
2017-07-06 06:09:45 +02:00
|
|
|
BOLDRED = '\x1B[1;31m'
|
|
|
|
|
|
|
|
GREEN = '\x1b[32m'
|
|
|
|
YELLOW = '\x1b[33m'
|
|
|
|
BLUE = '\x1b[34m'
|
|
|
|
MAGENTA = '\x1b[35m'
|
|
|
|
CYAN = '\x1b[36m'
|
2013-11-13 20:57:31 +01:00
|
|
|
|
2017-08-21 19:41:19 +02:00
|
|
|
# Parent parser for cache cleaning scripts.
|
|
|
|
GENERIC_CACHE_SCRIPT_PARSER = argparse.ArgumentParser(add_help=False)
|
|
|
|
GENERIC_CACHE_SCRIPT_PARSER.add_argument(
|
|
|
|
"--threshold", dest="threshold_days", type=int, default=14,
|
|
|
|
nargs="?", metavar="<days>", help="Any cache which is not in "
|
|
|
|
"use by a deployment not older than threshold days(current "
|
|
|
|
"installation in dev) and older than threshold days will be "
|
|
|
|
"deleted. (defaults to 14)")
|
|
|
|
GENERIC_CACHE_SCRIPT_PARSER.add_argument(
|
|
|
|
"--dry-run", dest="dry_run", action="store_true",
|
|
|
|
help="If specified then script will only print the caches "
|
|
|
|
"that it will delete/keep back. It will not delete any cache.")
|
|
|
|
|
2017-06-21 14:32:22 +02:00
|
|
|
def get_deployment_version(extract_path):
|
|
|
|
# type: (str) -> str
|
|
|
|
version = '0.0.0'
|
|
|
|
for item in os.listdir(extract_path):
|
|
|
|
item_path = os.path.join(extract_path, item)
|
|
|
|
if item.startswith('zulip-server') and os.path.isdir(item_path):
|
|
|
|
with open(os.path.join(item_path, 'version.py')) as f:
|
|
|
|
result = re.search('ZULIP_VERSION = "(.*)"', f.read())
|
|
|
|
if result:
|
|
|
|
version = result.groups()[0]
|
|
|
|
break
|
|
|
|
return version
|
|
|
|
|
|
|
|
def is_invalid_upgrade(current_version, new_version):
|
|
|
|
# type: (str, str) -> bool
|
|
|
|
if new_version > '1.4.3' and current_version <= '1.3.10':
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
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:
|
2017-01-24 05:50:04 +01: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:
|
2017-01-24 05:50:04 +01:00
|
|
|
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)
|
2016-01-10 20:58:11 +01:00
|
|
|
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):
|
2017-03-03 20:30:49 +01:00
|
|
|
# type: (Sequence[str], **Any) -> None
|
2016-04-06 17:15:31 +02:00
|
|
|
# Output what we're doing in the `set -x` style
|
|
|
|
print("+ %s" % (" ".join(args)))
|
2016-09-23 11:27:14 +02:00
|
|
|
|
|
|
|
if kwargs.get('shell'):
|
|
|
|
# With shell=True we can only pass string to Popen
|
|
|
|
args = " ".join(args)
|
|
|
|
|
2016-10-27 21:07:55 +02:00
|
|
|
try:
|
|
|
|
subprocess.check_call(args, **kwargs)
|
|
|
|
except subprocess.CalledProcessError:
|
2017-01-11 17:07:12 +01:00
|
|
|
print()
|
2017-01-24 05:50:04 +01:00
|
|
|
print(WHITEONRED + "Error running a subcommand of %s: %s" % (sys.argv[0], " ".join(args)) +
|
|
|
|
ENDC)
|
|
|
|
print(WHITEONRED + "Actual error output for the subcommand is just above this." +
|
|
|
|
ENDC)
|
2017-01-11 17:07:12 +01:00
|
|
|
print()
|
2016-10-27 21:07:55 +02:00
|
|
|
raise
|
2017-08-18 06:36:37 +02:00
|
|
|
|
|
|
|
def log_management_command(cmd, log_path):
|
|
|
|
# type: (Text, Text) -> None
|
|
|
|
log_dir = os.path.dirname(log_path)
|
|
|
|
if not os.path.exists(log_dir):
|
|
|
|
os.makedirs(log_dir)
|
|
|
|
|
|
|
|
formatter = logging.Formatter("%(asctime)s: %(message)s")
|
|
|
|
file_handler = logging.FileHandler(log_path)
|
|
|
|
file_handler.setFormatter(formatter)
|
|
|
|
logger = logging.getLogger("zulip.management")
|
|
|
|
logger.addHandler(file_handler)
|
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
|
|
|
|
logger.info("Ran '%s'" % (cmd,))
|
2017-08-18 19:14:09 +02:00
|
|
|
|
|
|
|
def get_environment():
|
|
|
|
# type: () -> Text
|
|
|
|
if os.path.exists(DEPLOYMENTS_DIR):
|
|
|
|
return "prod"
|
|
|
|
if os.environ.get("TRAVIS"):
|
|
|
|
return "travis"
|
|
|
|
return "dev"
|
2017-08-18 20:34:00 +02:00
|
|
|
|
|
|
|
def get_recent_deployments(threshold_days):
|
|
|
|
# type: (int) -> Set[Text]
|
|
|
|
# Returns a list of deployments not older than threshold days
|
|
|
|
# including `/root/zulip` directory if it exists.
|
|
|
|
recent = set()
|
|
|
|
threshold_date = datetime.datetime.now() - datetime.timedelta(days=threshold_days)
|
|
|
|
for dir_name in os.listdir(DEPLOYMENTS_DIR):
|
|
|
|
try:
|
|
|
|
date = datetime.datetime.strptime(dir_name, TIMESTAMP_FORMAT)
|
|
|
|
if date >= threshold_date:
|
|
|
|
recent.add(os.path.join(DEPLOYMENTS_DIR, dir_name))
|
|
|
|
except ValueError:
|
|
|
|
# Always include deployments whose name is not in the format of a timestamp.
|
|
|
|
recent.add(os.path.join(DEPLOYMENTS_DIR, dir_name))
|
|
|
|
if os.path.exists("/root/zulip"):
|
|
|
|
recent.add("/root/zulip")
|
|
|
|
return recent
|
2017-08-19 20:13:44 +02:00
|
|
|
|
|
|
|
def get_threshold_timestamp(threshold_days):
|
|
|
|
# type: (int) -> int
|
|
|
|
# Given number of days, this function returns timestamp corresponding
|
|
|
|
# to the time prior to given number of days.
|
|
|
|
threshold = datetime.datetime.now() - datetime.timedelta(days=threshold_days)
|
|
|
|
threshold_timestamp = int(time.mktime(threshold.utctimetuple()))
|
|
|
|
return threshold_timestamp
|
2017-08-20 00:07:31 +02:00
|
|
|
|
|
|
|
def get_caches_to_be_purged(caches_dir, caches_in_use, threshold_days):
|
|
|
|
# type: (Text, Set[Text], int) -> Set[Text]
|
|
|
|
# Given a directory containing caches, a list of caches in use
|
|
|
|
# and threshold days, this function return a list of caches
|
|
|
|
# which can be purged. Remove the cache only if it is:
|
|
|
|
# 1: Not in use by the current installation(in dev as well as in prod).
|
|
|
|
# 2: Not in use by a deployment not older than `threshold_days`(in prod).
|
|
|
|
# 3: Not in use by '/root/zulip'.
|
|
|
|
# 4: Not older than `threshold_days`.
|
|
|
|
caches_to_purge = set()
|
|
|
|
threshold_timestamp = get_threshold_timestamp(threshold_days)
|
|
|
|
for cache_dir_base in os.listdir(caches_dir):
|
|
|
|
cache_dir = os.path.join(caches_dir, cache_dir_base)
|
|
|
|
if cache_dir in caches_in_use:
|
|
|
|
# Never purge a cache which is in use.
|
|
|
|
continue
|
|
|
|
if os.path.getctime(cache_dir) < threshold_timestamp:
|
|
|
|
caches_to_purge.add(cache_dir)
|
|
|
|
return caches_to_purge
|
2017-08-23 22:44:28 +02:00
|
|
|
|
|
|
|
def purge_unused_caches(caches_dir, caches_in_use, threshold_days, dry_run, cache_type):
|
|
|
|
# type: (Text, Set[Text], int, bool, Text) -> None
|
|
|
|
all_caches = set([os.path.join(caches_dir, cache) for cache in os.listdir(caches_dir)])
|
|
|
|
caches_to_purge = get_caches_to_be_purged(caches_dir, caches_in_use, threshold_days)
|
|
|
|
caches_to_keep = all_caches - caches_to_purge
|
|
|
|
|
|
|
|
if dry_run:
|
|
|
|
print("Performing a dry run...")
|
|
|
|
else:
|
|
|
|
print("Cleaning unused %s caches..." % (cache_type,))
|
|
|
|
|
|
|
|
for cache_dir in caches_to_purge:
|
|
|
|
print("Cleaning unused %s cache: %s" % (cache_type, cache_dir))
|
|
|
|
if not dry_run:
|
|
|
|
subprocess.check_call(["sudo", "rm", "-rf", cache_dir])
|
|
|
|
|
|
|
|
for cache_dir in caches_to_keep:
|
|
|
|
print("Keeping used %s cache: %s" % (cache_type, cache_dir))
|
|
|
|
|
|
|
|
print("Done!\n")
|