Skip to content

Commit 4a2e519

Browse files
committed
add persistence feature
1 parent 8c1385e commit 4a2e519

8 files changed

Lines changed: 145 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

src/persistence/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
# Persistance (persistence)
3+
4+
Store data you with to persist between cntainer rebuilds.
5+
6+
## Example Usage
7+
8+
```json
9+
"features": {
10+
"ghcr.io/lugoues/devcontainer-features/persistence:1": {}
11+
}
12+
```
13+
14+
## Options
15+
16+
| Options Id | Description | Type | Default Value |
17+
|-----|-----|-----|-----|
18+
| paths | Colon separated list of directory paths to persist. | string | - |
19+
20+
21+
22+
---
23+
24+
_Note: This file was auto-generated from the [devcontainer-feature.json](https://github.com/lugoues/devcontainer-features/blob/main/src/persistence/devcontainer-feature.json). Add additional notes to a `NOTES.md`._
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.14",
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": "/usr/local/share/persistence_post_create",
22+
"installsAfter": [
23+
"ghcr.io/devcontainers/features/common-utils"
24+
]
25+
}

src/persistence/install.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
PERSIST_DIR="/mnt/persistence-data"
6+
PERSIST_POST_CREATE="/usr/local/share/persistence_post_create"
7+
8+
cat > "$PERSIST_POST_CREATE" << EOF
9+
#!/usr/bin/env bash
10+
11+
SUDO_CMD=""
12+
[ "\${USER}" != "root" ] && SUDO_CMD="sudo"
13+
14+
EOF
15+
chmod +x "${PERSIST_POST_CREATE}"
16+
17+
IFS=':' read -ra PATH_ARRAY <<< "${PATHS}"
18+
for path in "${PATH_ARRAY[@]}"; do
19+
[[ -z "${path}" ]] && continue
20+
21+
path="${path/#\~/${_REMOTE_USER_HOME}}"
22+
path="${path/#\$HOME/${_REMOTE_USER_HOME}}"
23+
24+
original_path=$(realpath -m "${path}")
25+
safe_name="${original_path//\//_}"
26+
persist_path="${PERSIST_DIR}/${safe_name#_}"
27+
28+
# Add directory creation to post-create
29+
echo "echo \"Setting up ${path}\"" >> "${PERSIST_POST_CREATE}"
30+
echo "\${SUDO_CMD} mkdir -p \"${persist_path}\"" >> "${PERSIST_POST_CREATE}"
31+
32+
parent_dir="$(dirname "${original_path}")"
33+
mkdir -p "${parent_dir}"
34+
35+
if [[ -d "${original_path}" ]]; then
36+
mv "${original_path}" "${original_path}.backup"
37+
fi
38+
39+
ln -s "${persist_path}" "${original_path}"
40+
41+
echo "\${SUDO_CMD} chown -R \$USER \"${parent_dir}\"" >> "${PERSIST_POST_CREATE}"
42+
done
43+
44+
cat >> "$PERSIST_POST_CREATE" << EOF
45+
46+
echo "Setting owner for ${PERSIST_DIR} to \${USER}"
47+
\${SUDO_CMD} chown -R \$USER "${PERSIST_DIR}"
48+
49+
EOF
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)