Skip to content

Commit db9b62d

Browse files
committed
add persistence feature
1 parent 8c1385e commit db9b62d

8 files changed

Lines changed: 140 additions & 0 deletions

File tree

.github/workflows/test.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ jobs:
5353
- google-cloud-cli
5454
- krew
5555
- luarocks
56+
- persistence
5657
steps:
5758
- uses: actions/checkout@v3
5859

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"id": "persistence",
3+
"version": "1.0.7",
4+
"name": "Persistance",
5+
"documentationURL": "http://github.com/lugoues/devcontainers-features/tree/main/src/persistence",
6+
"description": "Store data you with to persist between cntainer rebuilds.",
7+
"options": {
8+
"paths": {
9+
"default": "",
10+
"description": "Colon separated list of directory paths to persist.",
11+
"type": "string"
12+
}
13+
},
14+
"mounts": [
15+
{
16+
"source": "persistence-${devcontainerId}",
17+
"target": "/mnt/persistence_data",
18+
"type": "volume"
19+
}
20+
],
21+
"postCreateCommand": "bash /usr/local/share/persistence_post_create",
22+
"installsAfter": [
23+
"ghcr.io/devcontainers/features/common-utils"
24+
]
25+
}

src/persistence/install.sh

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
sudo_if() {
6+
COMMAND="$*"
7+
8+
if [ "$(id -u)" -ne 0 ]; then
9+
sudo $COMMAND
10+
else
11+
$COMMAND
12+
fi
13+
}
14+
15+
PERSIST_DIR="/mnt/persistence_data"
16+
PERSIST_POST_CREATE="/usr/local/share/persistence_post_create"
17+
18+
tee "$PERSIST_POST_CREATE" > /dev/null \
19+
<< EOF
20+
#!/bin/sh
21+
set -e
22+
23+
SUDO_CMD=""
24+
[ "\${USER}" != "root" ] && SUDO_CMD="sudo"
25+
26+
echo "Setting owner for ${PERSIST_DIR} to \${USER}"
27+
\${SUDO_CMD} chown -R \$USER "${PERSIST_DIR}"
28+
29+
EOF
30+
31+
chmod +x "${PERSIST_POST_CREATE}"
32+
33+
IFS=':' read -ra PATH_ARRAY <<< "${PATHS}"
34+
for path in "${PATH_ARRAY[@]}"; do
35+
[[ -z "${path}" ]] && continue
36+
37+
# Replace ~ or $HOME with $_REMOTE_USER_HOME
38+
path="${path/#\~/${_REMOTE_USER_HOME}}"
39+
path="${path/#\$HOME/${_REMOTE_USER_HOME}}"
40+
41+
# Build safe persist_path
42+
original_path=$(realpath -m "${path}")
43+
safe_name="${original_path//\//_}"
44+
persist_path="${PERSIST_DIR}/${safe_name#_}"
45+
46+
# Create target directory
47+
sudo_if sudo mkdir -p "${persist_path}"
48+
49+
# Create parent directory if needed
50+
parent_dir="$(dirname "${original_path}")"
51+
mkdir -p "${parent_dir}"
52+
53+
54+
# Backup and remove existing directory if it exists
55+
if [[ -d "${original_path}" ]]; then
56+
mv "${original_path}" "${original_path}.backup"
57+
fi
58+
59+
ln -s "${persist_path}" "${original_path}"
60+
w
61+
echo "\${SUDO_CMD} chown -R \$USER \"${parent_dir}\"" >> "${PERSIST_POST_CREATE}"
62+
done
63+

src/persistence/post-install.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
set -e
3+
echo "(*) Executing post-installation steps..."
4+
5+
sudo chown ${USER} /mnt/persistence_data
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
source dev-container-features-test-lib
6+
7+
# from scenario
8+
source="/mnt/persistence_data"
9+
home="/home/vscode"
10+
11+
12+
check "source exists" test -d "${source}"
13+
check "target symlink" test -L "${home}/.config/tests/nested/deep"
14+
check "write test file to new linked dir" touch "${home}/.config/tests/nested/deep/test_file"
15+
check "ensure it exists in target" test -f "${source}/home_vscode_.config_tests_nested_deep/test_file"
16+
check "ensure user owns target" bash -xc "[[ \$(stat -c '%U' ${source}/home_vscode_.config_tests_nested_deep/test_file) == vscode ]]"
17+
18+
reportResults

test/persistence/scenarios.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"persitence_single_link": {
3+
"image": "mcr.microsoft.com/devcontainers/base:ubuntu",
4+
"features": {
5+
"persistence": {
6+
"paths": "~/.config/tests/nested/deep"
7+
}
8+
},
9+
"containerUser": "vscode",
10+
"onCreateCommand": "mkdir -p /mnt/persistence_data"
11+
}
12+
}

test/persistence/test.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
source dev-container-features-test-lib
4+
5+
6+
reportResults

test/uv/test.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
4+
set -e
5+
6+
source dev-container-features-test-lib
7+
8+
check "uv version" uv --version
9+
10+
reportResults

0 commit comments

Comments
 (0)