-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clean up unused checkouts on dev-desktops #389
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
- name: Copy cleanup script | ||
template: | ||
src: clean-unused-checkouts.sh | ||
dest: /etc/cron.cleanup_disk_space | ||
owner: root | ||
group: root | ||
mode: 0744 | ||
|
||
- name: Set up the cleanup cron job | ||
template: | ||
src: cron_cleanup_disk_space.j2 | ||
dest: /etc/cron.d/cleanup_disk_space | ||
# if the cron job is running right now, keep retrying until it finishes | ||
register: cleanup_cron_result | ||
until: cleanup_cron_result is not failed | ||
retries: 10 | ||
delay: 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
ansible/roles/dev-desktop/templates/clean-unused-checkouts.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#!/usr/bin/env bash | ||
|
||
# | ||
# {{ ansible_managed }} | ||
# | ||
|
||
# Clean up unused checkouts | ||
# | ||
# This script is used to find old checkouts that are no longer in use. Given the | ||
# size of the build directory, regularly cleaning them up can save significant | ||
# amounts of disk space. | ||
|
||
# Enable strict mode for Bash | ||
# http://redsymbol.net/articles/unofficial-bash-strict-mode/ | ||
set -euo pipefail | ||
IFS=$'\n\t' | ||
|
||
# Print directories and their size instead of deleting them | ||
dry_run=false | ||
|
||
# Default to search for checkouts older than 60 days | ||
time="60" | ||
|
||
while [[ $# -gt 0 ]]; do | ||
case $1 in | ||
--dry-run) | ||
dry_run=true | ||
shift # past argument | ||
;; | ||
-t|--time) | ||
time="${2}" | ||
shift # past argument | ||
shift # past value | ||
;; | ||
-*) | ||
echo "Unknown option $1" | ||
exit 1 | ||
;; | ||
esac | ||
done | ||
|
||
# Find all build or target directories created by users | ||
# | ||
# This command combines (`-o`) two different conditions to find all build and | ||
# target directories that users have created. Within each home directory, we | ||
# recursively look for directories that either have a file named `x.py` and a | ||
# directory named `build`, or a file named `Cargo.toml` and a directory named | ||
# `target`. | ||
all_cache_directories=$(find /home -type d \( -name build -execdir test -f "x.py" \; -o -name target -execdir test -f "Cargo.toml" \; \) -print | sort | uniq) | ||
|
||
# For each checkout, we want to determine if the user has been working on it | ||
# within the `$time` number of days. | ||
unused_cache_directories=$(for directory in $all_cache_directories; do | ||
project=$(dirname "${directory}") | ||
|
||
# Find all directories with files that have been modified less than $time days ago | ||
modified=$(find "${project}" -type f -mtime -"${time}" -printf '%h\n' | xargs -r dirname | sort | uniq) | ||
|
||
# If no files have been modified in the last 90 days, then the project is | ||
# considered old. | ||
if [[ -z "${modified}" ]]; then | ||
echo "${directory}" | ||
fi | ||
done) | ||
|
||
# Delete the build directories in the unused checkouts | ||
for directory in $unused_cache_directories; do | ||
if [[ "${dry_run}" == true ]]; then | ||
du -sh "${directory}" | ||
else | ||
echo "Deleting ${directory}" | ||
rm -rf "${directory}" | ||
fi | ||
done |
2 changes: 2 additions & 0 deletions
2
ansible/roles/dev-desktop/templates/cron_cleanup_disk_space.j2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin | ||
0 0 * * * root /etc/cron.cleanup_disk_space |
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may also want to sweep
build-rust-analyzer
too, which is a secondary build dir recommended for r-a workflows: https://rustc-dev-guide.rust-lang.org/building/suggested.html#configuring-rust-analyzer-for-rustcThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll make a note of this in #390, which tracks rewriting the script in Rust. I fear adding more conditions to the
find
command will get unmanageable quickly. 🙈