Skip to content

Commit c8dd19b

Browse files
authored
Add docker pruning script for GHA usage (#384)
1 parent 0962bcf commit c8dd19b

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

.config/dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pkgmgr
2525
pylibssh
2626
seccomp
2727
signingkey
28+
subdirs
2829
testenv
2930
uninstallation
3031
unmarshal

.github/workflows/tox.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ jobs:
2525
build-image:
2626
runs-on: ${{ matrix.builder }}
2727
name: ${{ matrix.name }}
28-
needs: tox
2928
services:
3029
registry:
3130
image: registry:2
@@ -54,6 +53,9 @@ jobs:
5453
username: ${{ github.actor }}
5554
password: ${{ secrets.GITHUB_TOKEN }}
5655

56+
- name: Prune docker system
57+
run: sudo ./final/docker-prune.sh
58+
5759
- name: Build the container image for ${{ matrix.platform }} and test it
5860
uses: ./.github/actions/build-test
5961
# this needs to be passed only when on release pipeline:

final/docker-prune.sh

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/bin/bash
2+
# Cleanup Docker orphan overlay2 directories which do pile up over time
3+
# https://github.com/docker/buildx/issues/2061
4+
set -eu
5+
6+
# Define the overlay2 directory
7+
overlay_dir="/var/lib/docker/overlay2"
8+
9+
# Verify that the overlay2 directory exists
10+
if [ ! -d "$overlay_dir" ]; then
11+
echo "The directory $overlay_dir does not exist. Please check the path."
12+
exit 1
13+
fi
14+
15+
# Get all layer IDs associated with current containers (MergedDir, LowerDir, UpperDir, WorkDir)
16+
container_layer_ids=$(docker ps -qa | xargs -r docker inspect --format '{{ .GraphDriver.Data.MergedDir }} {{ .GraphDriver.Data.LowerDir }} {{ .GraphDriver.Data.UpperDir }} {{ .GraphDriver.Data.WorkDir }}' | tr ' ' '\n' | tr ':' '\n' | awk -F'/' '{print $(NF-1)}' | sort | uniq)
17+
18+
# Get all layer IDs associated with images
19+
image_layer_ids=$(docker images -qa | xargs -r docker inspect --format '{{ .GraphDriver.Data.MergedDir }} {{ .GraphDriver.Data.LowerDir }} {{ .GraphDriver.Data.UpperDir }} {{ .GraphDriver.Data.WorkDir }}' | tr ' ' '\n' | tr ':' '\n' | awk -F'/' '{print $(NF-1)}' | sort | uniq)
20+
21+
# Get all cache IDs of type source.local
22+
source_local_cache_ids=$(docker system df -v | grep 'source.local' | awk '{print $1}' | sort | uniq)
23+
24+
# Combine the layer IDs of containers, images, and source.local caches
25+
all_layer_ids=$(echo -e "$container_layer_ids\n$image_layer_ids" | sort | uniq)
26+
27+
echo "source_local_cache_ids:"
28+
echo "$source_local_cache_ids"
29+
30+
echo "all_layer_ids:"
31+
echo "$all_layer_ids"
32+
33+
# List all subdirectories in overlay2
34+
overlay_subdirs=$(ls -1 $overlay_dir)
35+
36+
# Find and remove orphan directories that are not in the list of active layers or caches
37+
echo "Searching for and removing orphan directories in $overlay_dir..."
38+
39+
for dir in $overlay_subdirs; do
40+
# Ignore directories ending in "-init" and the "l" directory
41+
if [[ "$dir" == *"-init" ]] || [[ "$dir" == "l" ]]; then
42+
echo "Ignoring special directory: $overlay_dir/$dir"
43+
continue
44+
fi
45+
46+
# Check if the directory name starts with any of the source.local cache IDs
47+
preserve_dir=false
48+
for cache_id in $source_local_cache_ids; do
49+
if [[ "$dir" == "$cache_id"* ]]; then
50+
preserve_dir=true
51+
break
52+
fi
53+
done
54+
55+
# If directory should be preserved, skip it
56+
if $preserve_dir; then
57+
echo "Preserving cache directory: $overlay_dir/$dir"
58+
continue
59+
fi
60+
61+
# Check if the directory is associated with an active container or image
62+
if ! echo "$all_layer_ids" | grep -q "$dir"; then
63+
echo "Removing orphan directory: $overlay_dir/$dir"
64+
rm -rf "$overlay_dir/$dir"
65+
fi
66+
done
67+
68+
echo "Process completed."

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ allow-init-docstring = true
7373
arg-type-hints-in-docstring = false
7474
baseline = ".config/pydoclint-baseline.txt"
7575
check-return-types = false
76-
exclude = '\.git|\.tox|build|out|venv'
76+
exclude = '\.git|\.tox|build|collections|out|venv'
7777
should-document-private-class-attributes = true
7878
show-filenames-in-every-violation-message = true
7979
skip-checking-short-docstrings = false

0 commit comments

Comments
 (0)