cleanup #2243
This file contains hidden or 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
| name: cleanup | |
| on: | |
| schedule: | |
| # Every 12h, at 03:00 and 15:00 UTC | |
| - cron: "0 3,15 * * *" | |
| jobs: | |
| cleanup: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Delete old logs on the server | |
| uses: appleboy/ssh-action@0ff4204d59e8e51228ff73bce53f80d53301dee2 # v1.2.5 | |
| 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_QUIC }}" -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_QUIC }}" | 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_QUIC }}" -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_QUIC }}/logs.json" | |
| rm -f "$TEMP_FILE" |