From b0b0dfb80a5e3ce03f135bf86cc877341b6f1217 Mon Sep 17 00:00:00 2001 From: Afonso Oliveira Date: Tue, 10 Dec 2024 21:45:08 +0000 Subject: [PATCH] Add simple Docker environment variable Signed-off-by: Afonso Oliveira --- bin/setup | 152 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 95 insertions(+), 57 deletions(-) diff --git a/bin/setup b/bin/setup index 2ec418816..537266e3c 100755 --- a/bin/setup +++ b/bin/setup @@ -6,66 +6,94 @@ cd $ROOT CONTAINER_TAG=`cat ${ROOT}/bin/.container-tag` +print_env() { + if [ -v DOCKER ]; then + echo "Using Docker environment" + else + echo "Using Singularity environment" + fi +} + if [ -v GITHUB_ACTIONS ]; then - echo "ACTIONS" - CONTAINER_PATH=${ROOT}/.singularity/image.sif - HOME_PATH=${GITHUB_WORKSPACE} - HOME_OPT="--home ${ROOT}/.home" - SINGULARITY_CACHE=--disable-cache + echo "ACTIONS" + CONTAINER_PATH=${ROOT}/.singularity/image.sif + HOME_PATH=${GITHUB_WORKSPACE} + HOME_OPT="--home ${ROOT}/.home" + SINGULARITY_CACHE=--disable-cache +elif [ -v DOCKER ]; then + print_env + if ! docker images udb | grep -q udb ; then + echo "Building Docker image..." + docker build -t udb -f .devcontainer/Dockerfile . + fi + DOCKER_BASE="docker run -it -v $(pwd):$(pwd) -w $(pwd) udb" + RUN="${DOCKER_BASE}" else - CONTAINER_PATH=${ROOT}/.singularity/image-$CONTAINER_TAG.sif - HOME_PATH=${HOME} - HOME_OPT="--bind ${ROOT}/.home:${HOME_PATH}" - SINGULARITY_CACHE= + print_env + CONTAINER_PATH=${ROOT}/.singularity/image-$CONTAINER_TAG.sif + HOME_PATH=${HOME} + HOME_OPT="--bind ${ROOT}/.home:${HOME_PATH}" + SINGULARITY_CACHE= fi if [ -f $ROOT/.git ]; then - # if this is a worktree, need to add the parent git repo in, too - GIT_PATH=`git rev-parse --git-common-dir | tr -d '\n' | xargs dirname` - HOME_OPT="${HOME_OPT} --bind ${GIT_PATH}:${GIT_PATH}" + # if this is a worktree, need to add the parent git repo in, too + GIT_PATH=`git rev-parse --git-common-dir | tr -d '\n' | xargs dirname` + HOME_OPT="${HOME_OPT} --bind ${GIT_PATH}:${GIT_PATH}" fi if [ -v DEVCONTAINER_ENV ]; then - RUN="" + RUN="" +elif [ -v DOCKER ]; then + RUN="${DOCKER_BASE}" else - RUN="singularity run ${HOME_OPT} ${CONTAINER_PATH} " + RUN="singularity run ${HOME_OPT} ${CONTAINER_PATH}" fi if [ ! -d $ROOT/.home ]; then - mkdir $ROOT/.home + mkdir $ROOT/.home fi if [ ! -v DEVCONTAINER_ENV ]; then - if [ ! -f ${CONTAINER_PATH} ]; then - echo "Fetching container..." - if [ ! -d "${ROOT}/.singularity" ]; then - mkdir -p ${ROOT}/.singularity + if [ -v DOCKER ]; then + if ! docker images udb | grep -q udb ; then + echo "Building Docker image..." + docker build -t udb -f .devcontainer/Dockerfile . + fi + else + if [ ! -f ${CONTAINER_PATH} ]; then + echo "Fetching container..." + if [ ! -d "${ROOT}/.singularity" ]; then + mkdir -p ${ROOT}/.singularity + fi + singularity pull ${SINGULARITY_CACHE} ${CONTAINER_PATH} oras://docker.io/riscvintl/spec-generator:$CONTAINER_TAG + fi fi - singularity pull ${SINGULARITY_CACHE} ${CONTAINER_PATH} oras://docker.io/riscvintl/spec-generator:$CONTAINER_TAG - fi fi if [ ! -f $ROOT/.bundle/config ]; then - OLDDIR=$PWD - cd $ROOT - ${RUN} bundle config set --local path ${ROOT}/.home/.gems - ${RUN} bundle config set --local cache_path ${ROOT}/.home/.cache - cd $OLDDIR + OLDDIR=$PWD + cd $ROOT + ${RUN} bundle config set --local path ${ROOT}/.home/.gems + ${RUN} bundle config set --local cache_path ${ROOT}/.home/.cache + cd $OLDDIR fi if [ ! -d $ROOT/.home/.gems ]; then - OLDDIR=$PWD - cd $ROOT - ${RUN} bundle install - cd $OLDDIR + OLDDIR=$PWD + cd $ROOT + ${RUN} bundle install + cd $OLDDIR fi if [ ! -d $ROOT/.home/.venv ]; then - ${RUN} /usr/bin/python3 -m venv ${ROOT}/.home/.venv + ${RUN} /usr/bin/python3 -m venv ${ROOT}/.home/.venv fi + source ${ROOT}/.home/.venv/bin/activate + if [ ! -f ${ROOT}/.home/.venv/bin/pre-commit ]; then - ${RUN} ${ROOT}/.home/.venv/bin/pip install -r requirements.txt + ${RUN} ${ROOT}/.home/.venv/bin/pip install -r requirements.txt fi # if [ ! -f $ROOT/ext/riscv-opcodes/README.md ]; then @@ -73,35 +101,45 @@ fi # fi if [[ ! -z "$DEVELOPMENT" && $DEVELOPMENT -eq 1 ]]; then - if [ ! -d "${ROOT}/.home/.yard/gem_index"]; then - ${RUN} bundle exec --gemfile ${ROOT}/Gemfile yard config --gem-install-yri - ${RUN} bundle exec --gemfile ${ROOT}/Gemfile yard gems - touch ${ROOT}/.stamps/dev_gems - fi + if [ ! -d "${ROOT}/.home/.yard/gem_index"]; then + ${RUN} bundle exec --gemfile ${ROOT}/Gemfile yard config --gem-install-yri + ${RUN} bundle exec --gemfile ${ROOT}/Gemfile yard gems + touch ${ROOT}/.stamps/dev_gems + fi fi if [ ! -d ${ROOT}/node_modules ]; then - ${RUN} npm i + ${RUN} npm i fi if [ -v DEVCONTAINER_ENV ]; then - BUNDLE="bundle" - RUBY="bundle exec ruby" - RAKE="bundle exec rake" - NPM="npm" - NPX="npx" - NODE="node" - PYTHON="python3" - PIP="${ROOT}/.home/.venv/bin/pip" - BASH="bash" + BUNDLE="bundle" + RUBY="bundle exec ruby" + RAKE="bundle exec rake" + NPM="npm" + NPX="npx" + NODE="node" + PYTHON="python3" + PIP="${ROOT}/.home/.venv/bin/pip" + BASH="bash" +elif [ -v DOCKER ]; then + BUNDLE="${DOCKER_BASE} bundle" + RUBY="${DOCKER_BASE} bundle exec ruby" + RAKE="${DOCKER_BASE} bundle exec rake" + NPM="${DOCKER_BASE} npm" + NPX="${DOCKER_BASE} npx" + NODE="${DOCKER_BASE} node" + PYTHON="${DOCKER_BASE} python3" + PIP="${DOCKER_BASE} pip" + BASH="${DOCKER_BASE} bash" else - BUNDLE="singularity run ${HOME_OPT} ${CONTAINER_PATH} bundle" - RUBY="singularity run ${HOME_OPT} ${CONTAINER_PATH} bundle exec ruby" - RAKE="singularity run ${HOME_OPT} ${CONTAINER_PATH} bundle exec rake" - NPM="singularity run ${HOME_OPT} ${CONTAINER_PATH} npm" - NPX="singularity run ${HOME_OPT} ${CONTAINER_PATH} npx" - NODE="singularity run ${HOME_OPT} ${CONTAINER_PATH} node" - PYTHON="singularity run ${HOME_OPT} ${CONTAINER_PATH} python3" - PIP="singularity run ${HOME_OPT} ${CONTAINER_PATH} ${ROOT}/.home/.venv/bin/pip" - BASH="singularity run ${HOME_OPT} ${CONTAINER_PATH} bash" -fi + BUNDLE="singularity run ${HOME_OPT} ${CONTAINER_PATH} bundle" + RUBY="singularity run ${HOME_OPT} ${CONTAINER_PATH} bundle exec ruby" + RAKE="singularity run ${HOME_OPT} ${CONTAINER_PATH} bundle exec rake" + NPM="singularity run ${HOME_OPT} ${CONTAINER_PATH} npm" + NPX="singularity run ${HOME_OPT} ${CONTAINER_PATH} npx" + NODE="singularity run ${HOME_OPT} ${CONTAINER_PATH} node" + PYTHON="singularity run ${HOME_OPT} ${CONTAINER_PATH} python3" + PIP="singularity run ${HOME_OPT} ${CONTAINER_PATH} ${ROOT}/.home/.venv/bin/pip" + BASH="singularity run ${HOME_OPT} ${CONTAINER_PATH} bash" +fi \ No newline at end of file