Skip to content

Commit

Permalink
Deployment script deletes old deployments (#710)
Browse files Browse the repository at this point in the history
* Added pruning functionality and flag for old deployments

* Amended help comment for prune deployments flag

* Uncommented setup_env line

* refactor: Change max_deployment scope and default prune deployments flag

* chore: clarify comment in _prune_deployments()

---------

Co-authored-by: Shihab Suliman <rye74444@ws557.diamond.ac.uk>
Co-authored-by: olliesilvester <122091460+olliesilvester@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 17, 2024
1 parent 0042db5 commit 48f9299
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion utility_scripts/deploy/deploy_mx_bluesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import argparse
import os
import re
import shutil
import subprocess
import sys
from typing import NamedTuple
Expand All @@ -24,6 +25,8 @@

DEV_DEPLOY_LOCATION = "/scratch/30day_tmp/mx-bluesky_release_test/bluesky"

MAX_DEPLOYMENTS = 4

help_message = f"""
To deploy mx_bluesky on a specific beamline, using the control machine to create the \
environment and without kubernetes, only the beamline argument needs to be passed.
Expand All @@ -40,8 +43,10 @@ class Options(NamedTuple):
print_release_dir: bool = False
quiet: bool = False
dev_mode: bool = False
use_control_machine: bool = True
prune_deployments: bool = True

# NOTE For i24 for now will need to set it to false from the command line
use_control_machine: bool = True


class Deployment:
Expand Down Expand Up @@ -134,6 +139,42 @@ def _create_environment_from_control_machine(
process.kill()


def _prune_old_deployments(release_area: str):
def get_creation_time(deployment):
# Warning: getctime gives time since last metadata change, not the creation time.
return os.path.getctime(os.path.join(release_area, deployment))

deployments = os.listdir(release_area)
set_of_deployments = set(deployments)

# Seperates symlinks and deployments
symlinks = set()
for item in deployments:
if os.path.islink(os.path.join(release_area, item)):
symlinks.add(item)
set_of_deployments.remove(item)

sorted_deployments = sorted(set_of_deployments, key=get_creation_time, reverse=True)

# Excludes most recent deployments
if len(sorted_deployments) > MAX_DEPLOYMENTS:
full_path_old_deployments = {
os.path.join(release_area, deployment)
for deployment in sorted_deployments[MAX_DEPLOYMENTS:]
}

# Excludes deployments that are symlinked
for link in symlinks:
link_path = os.path.dirname(os.readlink(os.path.join(release_area, link)))
if link_path in full_path_old_deployments:
full_path_old_deployments.remove(link_path)

# Deletes old deployments
for old_deployment in full_path_old_deployments:
print(f"Deleting old deployment {os.path.basename(old_deployment)}")
shutil.rmtree(old_deployment)


def main(beamline: str, options: Options):
release_area = options.release_dir
this_repo_top = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
Expand Down Expand Up @@ -248,6 +289,9 @@ def create_symlink_by_tmp_and_rename(dirname, target, linkname):
else:
print("Quitting without latest version being updated")

if options.prune_deployments:
_prune_old_deployments(release_area)


# Get the release directory based off the beamline and the latest mx-bluesky version
def _parse_options() -> tuple[str, Options]:
Expand Down Expand Up @@ -284,6 +328,14 @@ def _parse_options() -> tuple[str, Options]:
action="store_false",
help="Do not create environment running from the control machine.",
)
parser.add_argument(
"-pd",
"--prune-deployments",
required=False,
default=True,
help="Delete deployments which are older than the latest four if they aren't being used in any symlinks",
)

args = parser.parse_args()
if args.dev:
print("Running as dev")
Expand All @@ -297,10 +349,12 @@ def _parse_options() -> tuple[str, Options]:
print_release_dir=args.print_release_dir,
quiet=args.print_release_dir,
dev_mode=args.dev,
prune_deployments=args.prune_deployments,
use_control_machine=args.no_control,
)


if __name__ == "__main__":
beamline, options = _parse_options()
print(options.prune_deployments)
main(beamline, options)

0 comments on commit 48f9299

Please sign in to comment.