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
|
2019-07-23 23:58:11 +02:00
|
|
|
from typing import Set
|
2017-08-22 09:44:33 +02:00
|
|
|
|
|
|
|
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
sys.path.append(ZULIP_PATH)
|
2021-05-13 21:53:04 +02:00
|
|
|
from scripts.lib.zulip_tools import (
|
|
|
|
DEPLOYMENTS_DIR,
|
|
|
|
get_recent_deployments,
|
|
|
|
may_be_perform_purging,
|
|
|
|
su_to_zulip,
|
|
|
|
)
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2021-05-14 04:32:02 +02:00
|
|
|
LOCAL_GIT_CACHE_DIR = "/srv/zulip.git"
|
|
|
|
|
2017-08-22 09:44:33 +02:00
|
|
|
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def parse_args() -> argparse.Namespace:
|
2017-08-22 09:44:33 +02:00
|
|
|
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 "
|
2021-02-12 08:19:30 +01:00
|
|
|
"examined and removed.",
|
|
|
|
)
|
2017-08-22 09:44:33 +02:00
|
|
|
parser.add_argument(
|
2021-02-12 08:19:30 +01:00
|
|
|
"--threshold",
|
|
|
|
dest="threshold_days",
|
|
|
|
type=int,
|
|
|
|
default=14,
|
2021-02-12 03:52:14 +01:00
|
|
|
metavar="<days>",
|
2021-02-12 08:19:30 +01:00
|
|
|
help="Deployments older than threshold days will be deleted. (defaults to 14)",
|
|
|
|
)
|
2017-08-22 09:44:33 +02:00
|
|
|
parser.add_argument(
|
2021-02-12 08:19:30 +01:00
|
|
|
"--dry-run",
|
|
|
|
action="store_true",
|
2017-08-22 09:44:33 +02:00
|
|
|
help="If specified then script will only print the deployments and "
|
2021-02-12 08:19:30 +01:00
|
|
|
"caches that it will delete/keep back. It will not delete anything.",
|
|
|
|
)
|
2017-09-23 21:08:04 +02:00
|
|
|
parser.add_argument(
|
2021-02-12 08:19:30 +01:00
|
|
|
"--verbose",
|
|
|
|
action="store_true",
|
|
|
|
help="If specified then script will print a detailed report of what is going on.",
|
|
|
|
)
|
2017-09-23 21:08:04 +02:00
|
|
|
|
2017-08-22 09:44:33 +02:00
|
|
|
args = parser.parse_args()
|
2021-02-12 08:19:30 +01: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
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def get_deployments_to_be_purged(recent_deployments: Set[str]) -> Set[str]:
|
2021-02-12 08:19:30 +01:00
|
|
|
all_deployments = {
|
|
|
|
os.path.join(DEPLOYMENTS_DIR, deployment) for deployment in os.listdir(DEPLOYMENTS_DIR)
|
|
|
|
}
|
2017-08-22 09:44:33 +02:00
|
|
|
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
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def main() -> None:
|
2017-08-22 09:44:33 +02:00
|
|
|
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(
|
2021-02-12 08:19:30 +01:00
|
|
|
deployments_to_purge, deployments_to_keep, "deployment", args.dry_run, args.verbose, True
|
|
|
|
)
|
2017-08-22 09:44:33 +02:00
|
|
|
|
|
|
|
if not args.dry_run:
|
2021-05-14 04:32:02 +02:00
|
|
|
if os.path.exists(LOCAL_GIT_CACHE_DIR):
|
|
|
|
subprocess.check_call(
|
|
|
|
["git", "worktree", "prune"], cwd=LOCAL_GIT_CACHE_DIR, preexec_fn=su_to_zulip
|
|
|
|
)
|
2021-05-13 21:53:04 +02:00
|
|
|
|
2017-08-22 09:44:33 +02:00
|
|
|
print("Deployments cleaned successfully...")
|
|
|
|
print("Cleaning orphaned/unused caches...")
|
|
|
|
|
|
|
|
# Call 'clean-unused-caches' script to clean any orphaned/unused caches.
|
2021-02-12 08:19:30 +01: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
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-08-22 09:44:33 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|