Skip to content

cleanup

cleanup #908

Workflow file for this run

name: cleanup
on:
schedule:
- cron: "0 */8 * * *" # every 8h
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Delete old logs on the server
uses: appleboy/ssh-action@25ce8cbbcb08177468c7ff7ec5cbfa236f9341e1 # v1.1.0
with:
host: interop.seemann.io
username: ${{ secrets.INTEROP_SEEMANN_IO_USER }}
key: ${{ secrets.INTEROP_SEEMANN_IO_SSH_KEY }}
script: |
delete_oldest_folder() {
OLDEST_DIR=$(find "${{ vars.LOG_DIR }}" -mindepth 1 -maxdepth 1 -type d -printf '%T+ %p\n' | sort | head -n 1 | cut -d" " -f2-)
if [[ -n "$OLDEST_DIR" ]]; then
echo "Deleting oldest directory: $OLDEST_DIR"
rm -rf "$OLDEST_DIR"
fi
}
# Loop until enough space is available or no directories left to delete
while true; do
AVAILABLE_SPACE_GB=$(df -BG "${{ vars.LOG_DIR }}" | tail -n 1 | awk '{print $4}' | sed 's/G//')
echo "Available Space: $AVAILABLE_SPACE_GB GB"
if [[ "$AVAILABLE_SPACE_GB" -lt 50 ]]; then
echo "Less than 50 GB available. Trying to clean up..."
delete_oldest_folder
else
echo "Enough space available."
break
fi
done
TEMP_FILE=$(mktemp)
find "${{ vars.LOG_DIR }}" -mindepth 1 -maxdepth 1 -type d -not -name 'lost+found' -exec basename {} \; | sort > "$TEMP_FILE"
jq -R -s 'split("\n") | map(select(. != ""))' "$TEMP_FILE" > "${{ vars.LOG_DIR }}/logs.json"
rm -f "$TEMP_FILE"