-
-
Notifications
You must be signed in to change notification settings - Fork 81
42 lines (38 loc) · 1.67 KB
/
cleanup.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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@029f5b4aeeeb58fdfe1410a5d17f967dacf36262 # v1.0.3
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"