Skip to content

Commit

Permalink
feat: add shared cache setting
Browse files Browse the repository at this point in the history
  • Loading branch information
eitsupi committed May 12, 2024
1 parent d771d02 commit 4017fc4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/r-dependent-packages/NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ See [the reference of the `pak` package](https://pak.r-lib.org/reference/pak-con
}
```

## Cache directory and cache volume

The package cache directory in the container is set to `/pak/cache`.

This directory is stored in a volume named `devcontainer-pak-cache`
and is shared among multiple containers.

## References

- [pak](https://pak.r-lib.org/)
10 changes: 10 additions & 0 deletions src/r-dependent-packages/devcontainer-feature.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@
]
}
},
"containerEnv": {
"PKG_PACKAGE_CACHE_DIR": "/pak/cache"
},
"mounts": [
{
"source": "devcontainer-pak-cache",
"target": "/pak/cache",
"type": "volume"
}
],
"onCreateCommand": {
"r-dependent-packages": "/usr/local/share/rocker-devcontainer-features/r-dependent-packages/scripts/oncreate.sh"
},
Expand Down
20 changes: 20 additions & 0 deletions src/r-dependent-packages/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ ROOT=${MANIFESTROOT:-"."}
REPOS=${ADDITIONALREPOSITORIES:-""}
DEPS=${DEPENDENCYTYPES:-"all"}

PKG_PACKAGE_CACHE_DIR=${PKG_PACKAGE_CACHE_DIR:-"/pak/cache"}

USERNAME=${USERNAME:-${_REMOTE_USER}}

LIFECYCLE_SCRIPTS_DIR="/usr/local/share/rocker-devcontainer-features/r-dependent-packages/scripts"
Expand All @@ -17,6 +19,22 @@ if [ "$(id -u)" -ne 0 ]; then
exit 1
fi

create_cache_dir() {
if [ -d "$1" ]; then
echo "Cache directory $1 already exists. Skip creation..."
else
echo "Create cache directory $1..."
mkdir -p "$1"
fi

if [ -z "$2" ]; then
echo "No username provided. Skip chown..."
else
echo "Change owner of $1 to $2..."
chown -R "$2:$2" "$1"
fi
}

check_r() {
if [ ! -x "$(command -v R)" ]; then
echo "(!) Cannot run R. Please install R before installing this Feature."
Expand Down Expand Up @@ -44,6 +62,8 @@ install_pak() {

export DEBIAN_FRONTEND=noninteractive

create_cache_dir "${PKG_PACKAGE_CACHE_DIR}" "${USERNAME}"

# Set Lifecycle scripts
mkdir -p "${LIFECYCLE_SCRIPTS_DIR}"

Expand Down
17 changes: 17 additions & 0 deletions src/r-dependent-packages/lifecycle_script.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
#!/usr/bin/env bash

PKG_PACKAGE_CACHE_DIR=${PKG_PACKAGE_CACHE_DIR:-"/pak/cache"}

set -e

fix_permissions() {
local dir
dir="${1}"

if [ ! -w "${dir}" ]; then
echo "Fixing permissions of '${dir}'..."
sudo chown -R "$(id -u):$(id -g)" "${dir}"
echo "Done!"
else
echo "Permissions of '${dir}' are OK!"
fi
}

fix_permissions "${PKG_PACKAGE_CACHE_DIR}"

echo "Install dependent R packages..."

R -q -e \
Expand Down

0 comments on commit 4017fc4

Please sign in to comment.