Skip to content

Commit fe55b83

Browse files
author
Derek Hower
committed
Add prompt to choose container
1 parent 74ea548 commit fe55b83

File tree

2 files changed

+77
-27
lines changed

2 files changed

+77
-27
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
.stamps
88
.venv
99
.asciidoctor
10+
.container-type
1011
diag-ditaa-*
1112
arch/manual/isa/**/riscv-isa-manual
1213
gen

bin/setup

Lines changed: 76 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,62 @@ cd $ROOT
66

77
CONTAINER_TAG=`cat ${ROOT}/bin/.container-tag`
88

9+
# Supported container types:
10+
#
11+
# = devcontainer
12+
#
13+
# When devcontainer is used, all commands run "natively" (no container prefix), because it's assumed
14+
# that the command itself is already in the container context
15+
#
16+
# = docker
17+
#
18+
# A docker container. All commands on the host (using ./do or ./bin/*) are run in the context of a Docker image.
19+
#
20+
# = singularity
21+
#
22+
# An apptainer/singularity container. All commands on the host (using ./do or ./bin/*) are run in the context of an Apptainer/Singularity image.
23+
#
24+
#
25+
# ALL THREE CONTAINERS ARE IDENTICAL. Which one you use is a preference of your setup and installed
26+
# software.
27+
28+
29+
# get the container type
30+
#
31+
# use, in priority order:
32+
# 1. devcontainer, if DEVCONTAINER_ENV is set in the enviornment
33+
# 2. the type stored in .container-type
34+
# 3. Docker, if DOCKER is set in the enviornment
35+
# 4. The choice made by the user, which will be cached in .container-type
36+
if [ -v DEVCONTAINER_ENV ]; then
37+
CONTAINER_TYPE=devcontainer
38+
elif [ -f ${ROOT}/.container-type ]; then
39+
CONTAINER_TYPE=`cat ${ROOT}/.container-type`
40+
elif [ -v DOCKER ]; then
41+
CONTAINER_TYPE=docker
42+
else
43+
echo -e "UDB tools run in a container. Both Docker and Singularity/Apptainer are supported.\\n\\n1. Docker\\n2. Singularity\\n"
44+
while true; do
45+
echo "Which would you like to use? (1/2) "
46+
read ans
47+
case $ans in
48+
[1]* ) CONTAINER_TYPE=docker; break;;
49+
[2]* ) CONTAINER_TYPE=singularity; break;;
50+
* ) echo -e "\\nPlease answer 1 or 2.";;
51+
esac
52+
done
53+
fi
54+
55+
if [ "${CONTAINER_TYPE}" != "docker" -a "${CONTAINER_TYPE}" != "singularity" ]; then
56+
echo "BAD CONTAINER TYPE: ${CONTAINER_TYPE}"
57+
fi
58+
59+
if [ ! -f ${ROOT}/.container-type ]; then
60+
echo ${CONTAINER_TYPE} > ${ROOT}/.container-type
61+
fi
62+
963
print_env() {
10-
if [ -v DOCKER ]; then
64+
if [ "${CONTAINER_TYPE}" == "docker" ]; then
1165
echo "Using Docker environment"
1266
else
1367
echo "Using Singularity environment"
@@ -20,20 +74,32 @@ if [ -v GITHUB_ACTIONS ]; then
2074
HOME_PATH=${GITHUB_WORKSPACE}
2175
HOME_OPT="--home ${ROOT}/.home"
2276
SINGULARITY_CACHE=--disable-cache
23-
elif [ -v DOCKER ]; then
77+
elif [ "${CONTAINER_TYPE}" == "docker" ]; then
2478
print_env
25-
if ! docker images udb | grep -q udb ; then
79+
if ! docker images riscvintl/udb:${CONTAINER_TAG} | grep -q udb ; then
80+
# TODO: pull the image if it can be found
2681
echo "Building Docker image..."
27-
docker build -t udb -f .devcontainer/Dockerfile .
82+
docker build -t riscvintl/udb:${CONTAINER_TAG} -f .devcontainer/Dockerfile .
83+
fi
84+
if [ -t 1 -a -t 0 ]; then
85+
DOCKER_BASE="docker run -it -v $(pwd):$(pwd) -w $(pwd) riscvintl/udb:${CONTAINER_TAG}"
86+
else
87+
DOCKER_BASE="docker run -v $(pwd):$(pwd) -w $(pwd) riscvintl/udb:${CONTAINER_TAG}"
2888
fi
29-
DOCKER_BASE="docker run -it -v $(pwd):$(pwd) -w $(pwd) udb"
3089
RUN="${DOCKER_BASE}"
3190
else
3291
print_env
3392
CONTAINER_PATH=${ROOT}/.singularity/image-$CONTAINER_TAG.sif
3493
HOME_PATH=${HOME}
3594
HOME_OPT="--bind ${ROOT}/.home:${HOME_PATH}"
3695
SINGULARITY_CACHE=
96+
if [ ! -f ${CONTAINER_PATH} ]; then
97+
echo "Fetching container..."
98+
if [ ! -d "${ROOT}/.singularity" ]; then
99+
mkdir -p ${ROOT}/.singularity
100+
fi
101+
singularity pull ${SINGULARITY_CACHE} ${CONTAINER_PATH} oras://docker.io/riscvintl/spec-generator:$CONTAINER_TAG
102+
fi
37103
fi
38104

39105
if [ -f $ROOT/.git ]; then
@@ -44,7 +110,7 @@ fi
44110

45111
if [ -v DEVCONTAINER_ENV ]; then
46112
RUN=""
47-
elif [ -v DOCKER ]; then
113+
elif [ "${CONTAINER_TYPE}" == "docker" ]; then
48114
RUN="${DOCKER_BASE}"
49115
else
50116
RUN="singularity run ${HOME_OPT} ${CONTAINER_PATH}"
@@ -54,23 +120,6 @@ if [ ! -d $ROOT/.home ]; then
54120
mkdir $ROOT/.home
55121
fi
56122

57-
if [ ! -v DEVCONTAINER_ENV ]; then
58-
if [ -v DOCKER ]; then
59-
if ! docker images udb | grep -q udb ; then
60-
echo "Building Docker image..."
61-
docker build -t udb -f .devcontainer/Dockerfile .
62-
fi
63-
else
64-
if [ ! -f ${CONTAINER_PATH} ]; then
65-
echo "Fetching container..."
66-
if [ ! -d "${ROOT}/.singularity" ]; then
67-
mkdir -p ${ROOT}/.singularity
68-
fi
69-
singularity pull ${SINGULARITY_CACHE} ${CONTAINER_PATH} oras://docker.io/riscvintl/spec-generator:$CONTAINER_TAG
70-
fi
71-
fi
72-
fi
73-
74123
if [ ! -f $ROOT/.bundle/config ]; then
75124
OLDDIR=$PWD
76125
cd $ROOT
@@ -122,15 +171,15 @@ if [ -v DEVCONTAINER_ENV ]; then
122171
PYTHON="${ROOT}/.home/.venv/bin/python3"
123172
PIP="${ROOT}/.home/.venv/bin/pip"
124173
BASH="bash"
125-
elif [ -v DOCKER ]; then
174+
elif [ "${CONTAINER_TYPE}" == "docker" ]; then
126175
BUNDLE="${DOCKER_BASE} bundle"
127176
RUBY="${DOCKER_BASE} bundle exec ruby"
128177
RAKE="${DOCKER_BASE} bundle exec rake"
129178
NPM="${DOCKER_BASE} npm"
130179
NPX="${DOCKER_BASE} npx"
131180
NODE="${DOCKER_BASE} node"
132-
PYTHON="${DOCKER_BASE} python3"
133-
PIP="${DOCKER_BASE} pip"
181+
PYTHON="${DOCKER_BASE} ${ROOT}/.home/.venv/bin/python3"
182+
PIP="${DOCKER_BASE} ${ROOT}/.home/.venv/bin/pip"
134183
BASH="${DOCKER_BASE} bash"
135184
else
136185
BUNDLE="singularity run ${HOME_OPT} ${CONTAINER_PATH} bundle"
@@ -163,7 +212,7 @@ if [ -x "\$INSTALL_PYTHON" ]; then
163212
elif command -v pre-commit > /dev/null; then
164213
exec pre-commit "\${ARGS[@]}"
165214
else
166-
echo '`pre-commit` not found. Did you forget to activate your virtualenv?' 1>&2
215+
echo '\`pre-commit\` not found. Did you forget to activate your virtualenv?' 1>&2
167216
exit 1
168217
fi
169218
HOOK

0 commit comments

Comments
 (0)