Skip to content

Commit

Permalink
refactor: docker setup using devcontainer
Browse files Browse the repository at this point in the history
  • Loading branch information
mcanouil committed Nov 15, 2023
1 parent 7ff8fef commit b03e34a
Show file tree
Hide file tree
Showing 13 changed files with 305 additions and 540 deletions.
4 changes: 2 additions & 2 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ARG TAG=latest
FROM --platform=linux/amd64 ghcr.io/mcanouil/eggla:${TAG}
ARG VARIANT="bullseye"
FROM --platform=linux/amd64 buildpack-deps:${VARIANT}-curl
20 changes: 20 additions & 0 deletions .devcontainer/bcftools/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "BCFtools",
"id": "bcftools",
"version": "1.0.0",
"description": "Installs the BCFtools software.",
"documentationURL": "https://github.com/mcanouil/eggla/tree/main/.devcontainer/bcftools",
"options": {
"version": {
"type": "string",
"proposals": [
"latest"
],
"default": "latest",
"description": "Select version of the BCFtools software, if not 'latest'."
}
},
"installsAfter": [
"ghcr.io/devcontainers/features/common-utils"
]
}
53 changes: 53 additions & 0 deletions .devcontainer/bcftools/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env bash

BCFTOOLS_VERSION=${VERSION:-"1.16"}

set -e

apt_get_update() {
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
echo "Running apt-get update..."
apt-get update -y
fi
}

# Checks if packages are installed and installs them if not
check_packages() {
if ! dpkg -s "$@" >/dev/null 2>&1; then
apt_get_update
apt-get -y install --no-install-recommends "$@"
fi
}

check_packages curl \
build-essential \
libcurl4-openssl-dev \
ca-certificates \
bash-completion \
gzip \
libxt6 \
bzip2 \
wget \
autoconf \
automake \
make \
cmake \
libbz2-dev \
liblzma-dev \
libjpeg-dev \
libxml2-dev \
zlib1g-dev

wget -q -P /tmp/ https://github.com/samtools/bcftools/releases/download/${BCFTOOLS_VERSION}/bcftools-${BCFTOOLS_VERSION}.tar.bz2
tar -C /tmp/ -xjf /tmp/bcftools-${BCFTOOLS_VERSION}.tar.bz2
cd /tmp/bcftools-${BCFTOOLS_VERSION}
autoreconf -i
./configure --prefix=/usr
make
make install
rm -rf /tmp/bcftools-${BCFTOOLS_VERSION}

# Clean up
rm -rf /var/lib/apt/lists/*

echo "Done!"
29 changes: 25 additions & 4 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"build": {
"dockerfile": "Dockerfile",
"args": {
"TAG": "devel"
"VARIANT": "bullseye"
}
},
"remoteUser": "vscode",
Expand All @@ -15,20 +15,41 @@
"userGid": "1001",
"upgradePackages": "true"
},
"ghcr.io/devcontainers/features/docker-from-docker:1": {
"version": "latest"
"ghcr.io/devcontainers/features/git:1": {
"version": "latest",
"ppa": "false"
},
"ghcr.io/rocker-org/devcontainer-features/r-rig:1": {
"version": "4.2.0",
"vscodeRSupport": "full",
"installDevTools": "false",
"installREnv": "true",
"installRMarkdown": "false"
},
"ghcr.io/rocker-org/devcontainer-features/quarto-cli:1": {
"version": "prerelease",
"installTinyTex": "false",
"installChromium": "false"
},
"./bcftools": {
"version": "1.16"
},
"./eggla": {
"version": "main"
}
},
"customizations": {
"vscode": {
"extensions": [
"REditorSupport.r"
]
],
"settings": {
"r.rterm.option": [
"--no-save",
"--no-restore-data",
"--quiet"
]
}
}
}
}
2 changes: 1 addition & 1 deletion inst/setup/lock.R → .devcontainer/eggla/R/lock.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pkgs <- setdiff(rownames(old.packages(repos = "https://cloud.r-project.org/")), "pak")
if (length(pkgs) > 0) pak::pkg_install(pkgs)

lock_file <- "inst/setup/pkg.lock"
lock_file <- ".github/.devcontainer/eggla/R/pkg.lock"
pak::lockfile_create(
pkg = c(".", "data.table", "devtools", "languageserver", "httpgd", "svglite", "ragg"),
lockfile = lock_file,
Expand Down
File renamed without changes.
Binary file added .devcontainer/eggla/bin/plink2
Binary file not shown.
21 changes: 21 additions & 0 deletions .devcontainer/eggla/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "eggla",
"id": "eggla",
"version": "1.0.0",
"description": "Installs the eggla R package.",
"documentationURL": "https://github.com/mcanouil/eggla/tree/main/.devcontainer/eggla",
"options": {
"version": {
"type": "string",
"proposals": [
"latest"
],
"default": "latest",
"description": "Select version of the eggla R package, if not 'latest'."
}
},
"installsAfter": [
"ghcr.io/devcontainers/features/common-utils",
"ghcr.io/rocker-org/devcontainer-features/r-rig"
]
}
127 changes: 127 additions & 0 deletions .devcontainer/eggla/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#!/usr/bin/env bash


USERNAME=${USERNAME:-${_REMOTE_USER:-"automatic"}}
EGGLA_VERSION=${VERSION:-"main"}

FEATURE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

set -e

# Determine the appropriate non-root user
if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
USERNAME=""
POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
for CURRENT_USER in "${POSSIBLE_USERS[@]}"; do
if id -u "${CURRENT_USER}" >/dev/null 2>&1; then
USERNAME=${CURRENT_USER}
break
fi
done
if [ "${USERNAME}" = "" ]; then
USERNAME=root
fi
elif [ "${USERNAME}" = "none" ] || ! id -u "${USERNAME}" >/dev/null 2>&1; then
USERNAME=root
fi

if [ "${USERNAME}" = "root" ]; then
user_home="/root"
# Check if user already has a home directory other than /home/${USERNAME}
elif [ "/home/${USERNAME}" != $( getent passwd $USERNAME | cut -d: -f6 ) ]; then
user_home=$( getent passwd $USERNAME | cut -d: -f6 )
else
user_home="/home/${USERNAME}"
if [ ! -d "${user_home}" ]; then
mkdir -p "${user_home}"
chown ${USERNAME}:${group_name} "${user_home}"
fi
fi

apt_get_update() {
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
echo "Running apt-get update..."
apt-get update -y
fi
}

# Checks if packages are installed and installs them if not
check_packages() {
if ! dpkg -s "$@" >/dev/null 2>&1; then
apt_get_update
apt-get -y install --no-install-recommends "$@"
fi
}

check_packages curl \
build-essential \
libcurl4-openssl-dev \
ca-certificates \
bash-completion \
gzip \
libxt6 \
bzip2 \
wget \
autoconf \
automake \
make \
cmake \
libbz2-dev \
liblzma-dev \
libjpeg-dev \
libxml2-dev \
zlib1g-dev \
libgdal-dev \
libfontconfig1-dev \
libharfbuzz-dev \
libfribidi-dev \
libv8-dev \
libudunits2-dev \
libfftw3-dev

install_pak() {
local version=$1

if [ "${version}" = "auto" ]; then
if su "${USERNAME}" -c "R -s -e 'packageVersion(\"pak\")'" >/dev/null 2>&1; then
echo "pak is already installed. Skip pak installation..."
return
else
version="devel"
fi
fi

echo "Installing pak ${version}..."
# shellcheck disable=SC2016
su "${USERNAME}" -c 'R -q -e "install.packages(\"pak\", repos = sprintf(\"https://r-lib.github.io/p/pak/'"${version}"'/%s/%s/%s\", .Platform\$pkgType, R.Version()\$os, R.Version()\$arch))"'
}

export DEBIAN_FRONTEND=noninteractive

echo "Install R packages..."
mkdir /tmp/r-packages
pushd /tmp/r-packages

install_pak "auto"

su "${USERNAME}" -c "R -q -e \"pak::lockfile_install(update = TRUE)\""

if [ "${EGGLA_VERSION}" = "main" ]; then
su "${USERNAME}" -c "R -q -e \"pak::pkg_install('mcanouil/eggla', upgrade = FALSE, dependencies = TRUE)\""
else
su "${USERNAME}" -c "R -q -e \"pak::pkg_install('mcanouil/eggla@${EGGLA_VERSION}', upgrade = FALSE, dependencies = TRUE)\""
fi

su "${USERNAME}" -c "R -q -e \"pak::cache_clean()\""

cp ${FEATURE_DIR}/tests/eggla-example.R ${user_home}/eggla-example.R
chmod +x ${user_home}/eggla-example.R

cp ${FEATURE_DIR}/bin/plink2 /usr/bin/plink2
chmod +x /usr/bin/plink2

popd
rm -rf /tmp/r-packages
rm -rf /tmp/Rtmp*

echo "Done!"
File renamed without changes.
56 changes: 56 additions & 0 deletions .github/workflows/devcontainer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Build & Test Docker Image

on:
workflow_dispatch:
workflow_run:
workflows: "R-CMD-check"
branches: main
types: completed
release:
types: [published]

concurrency:
group: ${{ github.workflow }}
cancel-in-progress: true

jobs:
build-docker:
runs-on: ubuntu-latest
if: |
github.event_name == 'workflow_dispatch' ||
github.event_name == 'release' ||
(github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success')
strategy:
fail-fast: false
steps:
- uses: actions/checkout@v4

- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and run Dev Container
uses: devcontainers/[email protected]
if: github.ref_name == 'main'
with:
subFolder: .github
imageName: ghcr.io/mcanouil/eggla
cacheFrom: ghcr.io/mcanouil/eggla
imageTag: ${{github.ref_name}}
platform: linux/amd64
push: always
runCmd: Rscript eggla-example.R

- name: Build and run Dev Container
uses: devcontainers/[email protected]
if: github.ref_name != 'main'
with:
subFolder: .github
imageName: ghcr.io/mcanouil/eggla
cacheFrom: ghcr.io/mcanouil/eggla
imageTag: "devel"
platform: linux/amd64
push: always
runCmd: Rscript eggla-example.R
Loading

0 comments on commit b03e34a

Please sign in to comment.