Skip to content

Commit 49e21fd

Browse files
authored
feat: add cleanup feature for user caches and temporary files (#69)
1 parent f000166 commit 49e21fd

File tree

3 files changed

+114
-15
lines changed

3 files changed

+114
-15
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"id": "cleanup",
3+
"version": "1.0.0",
4+
"name": "Cleanup",
5+
"description": "Remove user caches and temporary files (for example ~/.cache/R). Intended to run last.",
6+
"installsAfter": [
7+
"ghcr.io/devcontainers/features/common-utils",
8+
"./quarto-computing-dependencies",
9+
"./uv",
10+
"./chrome",
11+
"./decktape",
12+
"./tinytex",
13+
"ghcr.io/rocker-org/devcontainer-features/quarto-cli",
14+
"ghcr.io/devcontainers/features/github-cli"
15+
]
16+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
USERNAME=${USERNAME:-${_REMOTE_USER:-"automatic"}}
6+
7+
# Resolve username
8+
if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
9+
USERNAME=""
10+
POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F":" '$3==val{print $1}' /etc/passwd)")
11+
for CURRENT_USER in "${POSSIBLE_USERS[@]}"; do
12+
if id -u "${CURRENT_USER}" >/dev/null 2>&1; then
13+
USERNAME=${CURRENT_USER}
14+
break
15+
fi
16+
done
17+
if [ "${USERNAME}" = "" ]; then
18+
USERNAME=root
19+
fi
20+
elif ! id -u "${USERNAME}" >/dev/null 2>&1; then
21+
USERNAME=root
22+
fi
23+
24+
echo "[cleanup] Running cleanup feature as user: ${USERNAME}"
25+
26+
# Determine home directory for the target user
27+
if [ "${USERNAME}" = "root" ]; then
28+
USER_HOME="/root"
29+
else
30+
USER_HOME=$(eval echo "~${USERNAME}")
31+
fi
32+
33+
echo "[cleanup] Target home: ${USER_HOME}"
34+
35+
cleanup_paths=(
36+
"${USER_HOME}/.cache"
37+
"${USER_HOME}/.local/share/Trash"
38+
"${USER_HOME}/tmp"
39+
"${USER_HOME}/.npm/_cacache"
40+
)
41+
42+
for path in "${cleanup_paths[@]}"; do
43+
if [ -e "${path}" ]; then
44+
echo "[cleanup] Removing: ${path}"
45+
rm -rf "${path}" || true
46+
else
47+
echo "[cleanup] Not found: ${path}"
48+
fi
49+
done
50+
51+
echo "[cleanup] Cleaning system temporary files"
52+
if [ -d "/tmp" ]; then
53+
echo "[cleanup] Removing: /tmp/*"
54+
rm -rf /tmp/* || true
55+
fi
56+
57+
echo "[cleanup] Cleaning package manager caches"
58+
if command -v apt-get >/dev/null 2>&1; then
59+
echo "[cleanup] Running: apt-get clean"
60+
apt-get clean || true
61+
62+
if [ -d "/var/lib/apt/lists" ]; then
63+
echo "[cleanup] Removing: /var/lib/apt/lists/*"
64+
rm -rf /var/lib/apt/lists/* || true
65+
fi
66+
fi
67+
68+
echo "[cleanup] Done."

.github/.devcontainer/devcontainer.json

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,34 @@
77
"IMAGE": "${localEnv:IMAGE}"
88
},
99
"options": [
10-
"--label", "org.opencontainers.image.title=${localEnv:ANNOTATION_TITLE}",
11-
"--annotation", "org.opencontainers.image.title=${localEnv:ANNOTATION_TITLE}",
12-
"--label", "org.opencontainers.image.description=${localEnv:ANNOTATION_DESCRIPTION}",
13-
"--annotation", "org.opencontainers.image.description=${localEnv:ANNOTATION_DESCRIPTION}",
14-
"--label", "org.opencontainers.image.authors=${localEnv:ANNOTATION_AUTHORS}",
15-
"--annotation", "org.opencontainers.image.authors=${localEnv:ANNOTATION_AUTHORS}",
16-
"--label", "org.opencontainers.image.url=${localEnv:ANNOTATION_URL}",
17-
"--annotation", "org.opencontainers.image.url=${localEnv:ANNOTATION_URL}",
18-
"--label", "org.opencontainers.image.source=${localEnv:ANNOTATION_URL}",
19-
"--annotation", "org.opencontainers.image.source=${localEnv:ANNOTATION_URL}",
20-
"--label", "org.opencontainers.image.version=${localEnv:ANNOTATION_VERSION}",
21-
"--annotation", "org.opencontainers.image.version=${localEnv:ANNOTATION_VERSION}",
22-
"--label", "org.opencontainers.image.licenses=${localEnv:ANNOTATION_LICENSE}",
23-
"--annotation", "org.opencontainers.image.licenses=${localEnv:ANNOTATION_LICENSE}"
10+
"--label",
11+
"org.opencontainers.image.title=${localEnv:ANNOTATION_TITLE}",
12+
"--annotation",
13+
"org.opencontainers.image.title=${localEnv:ANNOTATION_TITLE}",
14+
"--label",
15+
"org.opencontainers.image.description=${localEnv:ANNOTATION_DESCRIPTION}",
16+
"--annotation",
17+
"org.opencontainers.image.description=${localEnv:ANNOTATION_DESCRIPTION}",
18+
"--label",
19+
"org.opencontainers.image.authors=${localEnv:ANNOTATION_AUTHORS}",
20+
"--annotation",
21+
"org.opencontainers.image.authors=${localEnv:ANNOTATION_AUTHORS}",
22+
"--label",
23+
"org.opencontainers.image.url=${localEnv:ANNOTATION_URL}",
24+
"--annotation",
25+
"org.opencontainers.image.url=${localEnv:ANNOTATION_URL}",
26+
"--label",
27+
"org.opencontainers.image.source=${localEnv:ANNOTATION_URL}",
28+
"--annotation",
29+
"org.opencontainers.image.source=${localEnv:ANNOTATION_URL}",
30+
"--label",
31+
"org.opencontainers.image.version=${localEnv:ANNOTATION_VERSION}",
32+
"--annotation",
33+
"org.opencontainers.image.version=${localEnv:ANNOTATION_VERSION}",
34+
"--label",
35+
"org.opencontainers.image.licenses=${localEnv:ANNOTATION_LICENSE}",
36+
"--annotation",
37+
"org.opencontainers.image.licenses=${localEnv:ANNOTATION_LICENSE}"
2438
]
2539
},
2640
"remoteUser": "${localEnv:USER}",
@@ -55,6 +69,7 @@
5569
"installTinyTex": "false",
5670
"installChromium": "false"
5771
},
58-
"ghcr.io/devcontainers/features/github-cli:1": {}
72+
"ghcr.io/devcontainers/features/github-cli:1": {},
73+
"./cleanup": {}
5974
}
6075
}

0 commit comments

Comments
 (0)