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
|
2017-08-22 09:44:33 +02:00
|
|
|
import argparse
|
2013-05-16 18:03:01 +02:00
|
|
|
import os
|
2017-08-22 09:44:33 +02:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
if False:
|
|
|
|
from typing import Set, Text
|
|
|
|
|
|
|
|
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
sys.path.append(ZULIP_PATH)
|
|
|
|
from scripts.lib.zulip_tools import DEPLOYMENTS_DIR, get_recent_deployments, \
|
2017-09-16 17:47:52 +02:00
|
|
|
may_be_perform_purging, TIMESTAMP_FORMAT
|
2017-08-22 09:44:33 +02:00
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
# type: () -> argparse.Namespace
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="This script can be used for cleaning old unused deployments.",
|
|
|
|
epilog="Orphaned/unused caches older than threshold days will be automatically "
|
|
|
|
"examined and removed.")
|
|
|
|
parser.add_argument(
|
|
|
|
"--threshold", dest="threshold_days", type=int, default=14,
|
|
|
|
nargs="?", metavar="<days>", help="Deployments older than "
|
|
|
|
"threshold days will be deleted. (defaults to 14)")
|
|
|
|
parser.add_argument(
|
|
|
|
"--dry-run", dest="dry_run", action="store_true",
|
|
|
|
help="If specified then script will only print the deployments and "
|
|
|
|
"caches that it will delete/keep back. It will not delete anything.")
|
2017-09-23 21:08:04 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--verbose", dest="verbose", action="store_true",
|
|
|
|
help="If specified then script will print a detailed report "
|
|
|
|
"of what is going on.")
|
|
|
|
|
2017-08-22 09:44:33 +02:00
|
|
|
args = parser.parse_args()
|
2017-09-23 21:08:04 +02:00
|
|
|
args.verbose |= args.dry_run # Always print a detailed report in case of dry run.
|
2017-08-22 09:44:33 +02:00
|
|
|
return args
|
|
|
|
|
|
|
|
def get_deployments_to_be_purged(recent_deployments):
|
|
|
|
# type: (Set[Text]) -> Set[Text]
|
|
|
|
all_deployments = set([os.path.join(DEPLOYMENTS_DIR, deployment)
|
|
|
|
for deployment in os.listdir(DEPLOYMENTS_DIR)])
|
|
|
|
deployments_to_purge = set()
|
|
|
|
for deployment in all_deployments:
|
2017-09-23 21:38:47 +02:00
|
|
|
# Deployments whose name is not in the format of a timestamp are
|
|
|
|
# always included in the recent_deployments and are not deleted.
|
2017-09-16 17:47:52 +02:00
|
|
|
if not os.path.isdir(deployment):
|
|
|
|
# Skip things like uwsgi sockets.
|
|
|
|
continue
|
2017-09-26 00:11:18 +02:00
|
|
|
if not os.path.exists(os.path.join(deployment, "zerver")):
|
|
|
|
# Skip things like "lock" that aren't actually a deployment directory
|
|
|
|
continue
|
2017-08-22 09:44:33 +02:00
|
|
|
if deployment not in recent_deployments:
|
|
|
|
deployments_to_purge.add(deployment)
|
|
|
|
return deployments_to_purge
|
|
|
|
|
|
|
|
def main():
|
|
|
|
# type: () -> None
|
|
|
|
args = parse_args()
|
|
|
|
deployments_to_keep = get_recent_deployments(args.threshold_days)
|
|
|
|
deployments_to_purge = get_deployments_to_be_purged(deployments_to_keep)
|
|
|
|
|
2017-09-23 21:15:13 +02:00
|
|
|
may_be_perform_purging(
|
|
|
|
deployments_to_purge, deployments_to_keep, "deployment", args.dry_run, args.verbose)
|
2017-08-22 09:44:33 +02:00
|
|
|
|
|
|
|
if not args.dry_run:
|
|
|
|
print("Deployments cleaned successfully...")
|
|
|
|
print("Cleaning orphaned/unused caches...")
|
|
|
|
|
|
|
|
# Call 'clean-unused-caches' script to clean any orphaned/unused caches.
|
2017-09-26 01:10:11 +02:00
|
|
|
subprocess.check_call([os.path.join(ZULIP_PATH, "scripts/lib/clean-unused-caches")] + sys.argv[1:])
|
2017-09-26 01:12:29 +02:00
|
|
|
print("Done!")
|
2017-08-22 09:44:33 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|