|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# GitHub App for the EESSI project |
| 4 | +# |
| 5 | +# A bot to help with requests to add software installations to the EESSI software layer, |
| 6 | +# see https://github.com/EESSI/software-layer |
| 7 | + |
| 8 | +# This script cleans up (deletes) all build artefacts and temporary storage tarballs of a given PR |
| 9 | +# |
| 10 | +# author: Thomas Roeblitz (@trz42) |
| 11 | +# |
| 12 | +# license: GPLv2 |
| 13 | +# |
| 14 | + |
| 15 | +SCRIPT_DIR=$(dirname $(realpath $BASH_SOURCE)) |
| 16 | + |
| 17 | +function display_help |
| 18 | +{ |
| 19 | + echo "Usage: $0 [OPTIONS] <PR number>" >&2 |
| 20 | + echo " -b | --jobs-base-dir DIRECTORY - jobs base directory [default: reads" >&2 |
| 21 | + echo " value from bot config file app.cfg or .]" >&2 |
| 22 | + echo " -D | --dry-run - only show commands that would be run" >&2 |
| 23 | + echo " [default: false]" >&2 |
| 24 | + echo " -h | --help - display this usage information" >&2 |
| 25 | +} |
| 26 | + |
| 27 | +function get_jobs_base_dir |
| 28 | +{ |
| 29 | + app_cfg_path=${1} |
| 30 | + grep jobs_base_dir ${app_cfg_path} | grep -v '^[ ]*#' | sed -e 's/^[^=]*=[ ]*//' |
| 31 | +} |
| 32 | + |
| 33 | +echo |
| 34 | + |
| 35 | +if [[ $# -lt 1 ]]; then |
| 36 | + display_help |
| 37 | + exit 1 |
| 38 | +fi |
| 39 | + |
| 40 | +# process command line args |
| 41 | +POSITIONAL_ARGS=() |
| 42 | + |
| 43 | +jobs_base_dir= |
| 44 | +dry_run=false |
| 45 | + |
| 46 | +while [[ $# -gt 0 ]]; do |
| 47 | + case $1 in |
| 48 | + -b|--jobs-base-dir) |
| 49 | + if [[ $# -gt 1 ]]; then |
| 50 | + jobs_base_dir="$2" |
| 51 | + shift 2 |
| 52 | + else |
| 53 | + echo "Error: missing argument (directory) for parameter '${1}'" |
| 54 | + exit 2 |
| 55 | + fi |
| 56 | + ;; |
| 57 | + -D|--dry-run) |
| 58 | + dry_run=true |
| 59 | + shift 1 |
| 60 | + ;; |
| 61 | + -h|--help) |
| 62 | + display_help |
| 63 | + exit 0 |
| 64 | + ;; |
| 65 | + -*|--*) |
| 66 | + echo "Error: Unknown option: $1" >&2 |
| 67 | + exit 1 |
| 68 | + ;; |
| 69 | + *) # No more options |
| 70 | + POSITIONAL_ARGS+=("$1") # save positional arg |
| 71 | + shift |
| 72 | + ;; |
| 73 | + esac |
| 74 | +done |
| 75 | + |
| 76 | +# restore potentially parsed filename(s) into $* |
| 77 | +set -- "${POSITIONAL_ARGS[@]}" |
| 78 | + |
| 79 | +if [[ $# -ne 1 ]]; then |
| 80 | + echo "Error: exactly one PR number should be provided as argument" |
| 81 | + display_help |
| 82 | + exit 3 |
| 83 | +fi |
| 84 | + |
| 85 | +pull_request=${1} |
| 86 | + |
| 87 | +if ${dry_run} = true ; then |
| 88 | + echo "DRY_RUN: not removing any files" |
| 89 | +fi |
| 90 | + |
| 91 | +# determine jobs base dir if not given explicitly |
| 92 | +# 1. check for file app.cfg in SCRIPT_DIR |
| 93 | +# 2. check for file app.cfg in current dir |
| 94 | +# if found try to obtain value of jobs_base_dir setting |
| 95 | +# if not file not found or jobs_base_dir setting not found (or empty) --> error & exit |
| 96 | +if [[ -z ${jobs_base_dir} ]]; then |
| 97 | + echo "jobs base directory not given explicitly, trying to determine it" |
| 98 | + if [[ -e ${SCRIPT_DIR}/app.cfg ]]; then |
| 99 | + echo "check for app.cfg in '${SCRIPT_DIR}'" |
| 100 | + jobs_base_dir=$(get_jobs_base_dir ${SCRIPT_DIR}/app.cfg) |
| 101 | + else |
| 102 | + if [[ -e ./app.cfg ]]; then |
| 103 | + echo "check for app.cfg in '${PWD}' (current directory)" |
| 104 | + jobs_base_dir=$(get_jobs_base_dir ./app.cfg) |
| 105 | + fi |
| 106 | + fi |
| 107 | +fi |
| 108 | +if [[ -z ${jobs_base_dir} ]]; then |
| 109 | + echo "Error: jobs base directory is empty, please specify it as argument" |
| 110 | + display_help |
| 111 | + exit 4 |
| 112 | +fi |
| 113 | + |
| 114 | +echo "processing all directories for PR ${pull_request}:" |
| 115 | +find ${jobs_base_dir}/* -maxdepth 1 -type d -wholename */pr_${pull_request} | sed -e 's/^/ /' |
| 116 | + |
| 117 | +echo |
| 118 | +echo "disk usage of directories for PR ${pull_request} BEFORE removing build artefacts and tmp storage" |
| 119 | +for d in $(find ${jobs_base_dir}/* -maxdepth 1 -type d -wholename */pr_${pull_request}); do du -sh $d; done |
| 120 | + |
| 121 | +echo |
| 122 | +echo "$([[ ${dry_run} = true ]] && echo "DRY_RUN: ")removing tmp storage tarballs for PR ${pull_request}" |
| 123 | +for d in $(find ${jobs_base_dir}/* -maxdepth 1 -type d -wholename */pr_${pull_request}) |
| 124 | +do |
| 125 | + for f in $(find $d -type f -wholename "*[0-9].tgz") |
| 126 | + do |
| 127 | + if ${dry_run} = true ; then |
| 128 | + echo "DRY_RUN: rm '$f' ($(ls -lh $f | awk '{print $5}'))" |
| 129 | + else |
| 130 | + echo "Removing file '$f'" |
| 131 | + rm $f |
| 132 | + fi |
| 133 | + done |
| 134 | +done |
| 135 | + |
| 136 | +echo |
| 137 | +echo "disk usage of directories for PR ${pull_request} AFTER removing tmp storage tarballs" |
| 138 | +for d in $(find ${jobs_base_dir}/* -maxdepth 1 -type d -wholename */pr_${pull_request}); do du -sh $d; done |
| 139 | + |
| 140 | +echo |
| 141 | +echo "$([[ ${dry_run} = true ]] && echo "DRY_RUN: ")removing build artefacts for PR ${pull_request}" |
| 142 | +for d in $(find ${jobs_base_dir}/* -maxdepth 1 -type d -wholename */pr_${pull_request}) |
| 143 | +do |
| 144 | + for f in $(find $d -type f -wholename "*tar.gz") |
| 145 | + do |
| 146 | + if ${dry_run} = true ; then |
| 147 | + echo "DRY_RUN: rm '$f' ($(ls -lh $f | awk '{print $5}'))" |
| 148 | + else |
| 149 | + echo "Removing file '$f'" |
| 150 | + rm $f |
| 151 | + fi |
| 152 | + done |
| 153 | +done |
| 154 | + |
| 155 | +echo |
| 156 | +echo "disk usage of directories for PR ${pull_request} AFTER removing build artefacts and tmp storage" |
| 157 | +for d in $(find ${jobs_base_dir}/* -maxdepth 1 -type d -wholename */pr_${pull_request}); do du -sh $d; done |
| 158 | + |
0 commit comments