2020-11-08 20:05:38 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2021-05-01 17:17:32 +00:00
|
|
|
PYODIDE_IMAGE_REPO="pyodide"
|
2021-06-26 08:34:31 +00:00
|
|
|
PYODIDE_IMAGE_TAG="18"
|
2021-05-01 17:17:32 +00:00
|
|
|
PYODIDE_PREBUILT_IMAGE_TAG="0.17.0"
|
|
|
|
DEFAULT_PYODIDE_DOCKER_IMAGE="${PYODIDE_IMAGE_REPO}/pyodide-env:${PYODIDE_IMAGE_TAG}"
|
|
|
|
DEFAULT_PYODIDE_SYSTEM_PORT="8000"
|
|
|
|
DOCKER_COMMAND="/bin/bash"
|
2021-06-16 22:04:47 +00:00
|
|
|
DOCKER_INTERACTIVE="--interactive"
|
2021-05-01 17:17:32 +00:00
|
|
|
|
2021-07-05 16:42:38 +00:00
|
|
|
|
|
|
|
USER_HOME="/src/.docker_home"
|
|
|
|
USER_NAME="$(id -u -n)"
|
|
|
|
USER_PASS="x"
|
|
|
|
USER_ID="$(id -u)"
|
|
|
|
USER_GID=0
|
|
|
|
USER_COMMENT_FIELD="${USER_NAME} pyodide user alias"
|
|
|
|
USER_INTERPRETER="/sbin/nologin"
|
|
|
|
USER_ACCOUNT_INFO="${USER_NAME}:${USER_PASS}:${USER_ID}:${USER_GID}:${USER_COMMENT_FIELD}:${USER_HOME}:${USER_INTERPRETER}"
|
|
|
|
USER_FLAG="--user $(id --user):$(id --group)"
|
|
|
|
|
2020-11-08 20:05:38 +00:00
|
|
|
set -eo pipefail
|
|
|
|
|
|
|
|
|
|
|
|
function usage() {
|
|
|
|
cat > /dev/stdout <<EOF
|
2021-04-03 22:20:42 +00:00
|
|
|
Usage: run_docker [OPTIONS] [COMMAND] [ARG...]
|
2020-11-08 20:05:38 +00:00
|
|
|
|
2021-04-03 22:20:42 +00:00
|
|
|
Runs COMMAND in a new Pyodide docker container. If no COMMAND is provided, starts a bash
|
|
|
|
shell in the container.
|
2020-11-08 20:05:38 +00:00
|
|
|
|
2021-04-03 22:20:42 +00:00
|
|
|
Options:
|
|
|
|
-h, --help Show this information and exit.
|
|
|
|
--pre-built Use the prebuilt Pyodide image.
|
|
|
|
This is ignored if the env var PYODIDE_DOCKER_IMAGE is set.
|
|
|
|
-p, --port <port> System port to which to forward.
|
|
|
|
This is ignored if the env var PYODIDE_SYSTEM_PORT is set.
|
|
|
|
If set to 'none', docker instance will not bind to any port.
|
2021-06-16 22:04:47 +00:00
|
|
|
--non-interactive Run docker without the --interactive flag.
|
|
|
|
Useful for running in headless mode on CI server.
|
2021-07-05 16:42:38 +00:00
|
|
|
--root Run as root user inside the container
|
2021-04-03 22:20:42 +00:00
|
|
|
|
|
|
|
Prerequisites:
|
|
|
|
Docker has to be set up on your system.
|
2020-11-08 20:05:38 +00:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
function error() {
|
|
|
|
usage
|
|
|
|
exit 255
|
|
|
|
}
|
|
|
|
|
|
|
|
while [[ $# -gt 0 ]]
|
|
|
|
do
|
|
|
|
key="$1"
|
|
|
|
case $key in
|
|
|
|
-h|--help)
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
--pre-built)
|
|
|
|
if [[ -n ${PYODIDE_DOCKER_IMAGE} ]]; then
|
|
|
|
echo "WARNING: will use the env var PYODIDE_DOCKER_IMAGE=${PYODIDE_DOCKER_IMAGE},
|
|
|
|
the flag --pre-built has no effect"
|
|
|
|
fi
|
2021-04-21 19:51:28 +00:00
|
|
|
DEFAULT_PYODIDE_DOCKER_IMAGE="pyodide/pyodide:${PYODIDE_PREBUILT_IMAGE_TAG}"
|
2020-11-08 20:05:38 +00:00
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-p|--port)
|
|
|
|
if [ "$#" -lt 2 ]; then
|
|
|
|
>&2 echo "port cannot be empty"
|
|
|
|
error
|
|
|
|
fi
|
|
|
|
if [[ -n ${PYODIDE_SYSTEM_PORT} ]]; then
|
|
|
|
echo "WARNING: will use the env var PYODIDE_SYSTEM_PORT=${PYODIDE_SYSTEM_PORT} instead of the provided port"
|
|
|
|
fi
|
|
|
|
DEFAULT_PYODIDE_SYSTEM_PORT=$2
|
|
|
|
shift 2
|
|
|
|
;;
|
2021-06-16 22:04:47 +00:00
|
|
|
--non-interactive)
|
|
|
|
DOCKER_INTERACTIVE="--interactive=false"
|
|
|
|
shift
|
|
|
|
;;
|
2021-07-05 16:42:38 +00:00
|
|
|
--root)
|
|
|
|
USER_FLAG=""
|
|
|
|
shift
|
|
|
|
;;
|
2021-04-03 22:20:42 +00:00
|
|
|
-*)
|
2020-11-08 20:05:38 +00:00
|
|
|
>&2 echo "Unknown option $1"
|
|
|
|
error
|
|
|
|
;;
|
2021-04-03 22:20:42 +00:00
|
|
|
*)
|
|
|
|
DOCKER_COMMAND="$@"
|
|
|
|
break
|
|
|
|
;;
|
2020-11-08 20:05:38 +00:00
|
|
|
esac
|
|
|
|
done
|
2018-12-11 07:56:51 +00:00
|
|
|
|
2019-03-21 14:49:50 +00:00
|
|
|
PYODIDE_DOCKER_PORT=${PYODIDE_DOCKER_PORT:-"8000"}
|
2020-11-08 20:05:38 +00:00
|
|
|
PYODIDE_SYSTEM_PORT=${PYODIDE_SYSTEM_PORT:-${DEFAULT_PYODIDE_SYSTEM_PORT}}
|
|
|
|
PYODIDE_DOCKER_IMAGE=${PYODIDE_DOCKER_IMAGE:-${DEFAULT_PYODIDE_DOCKER_IMAGE}}
|
2019-03-21 14:49:50 +00:00
|
|
|
|
2021-04-03 18:38:28 +00:00
|
|
|
# in case the port is not a number, do not bind the port
|
|
|
|
case $DEFAULT_PYODIDE_SYSTEM_PORT in
|
|
|
|
none)
|
|
|
|
PORT_CONFIGURATION_LINE=""
|
|
|
|
;;
|
|
|
|
''|*[!0-9]*) # contains a non-digit character, therefore it is not a number
|
|
|
|
echo "WARNING: Invalid port argument '$DEFAULT_PYODIDE_SYSTEM_PORT'. Port binding disabled."
|
|
|
|
PORT_CONFIGURATION_LINE=""
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
PORT_CONFIGURATION_LINE="-p $PYODIDE_SYSTEM_PORT:$PYODIDE_DOCKER_PORT"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2021-04-04 19:19:54 +00:00
|
|
|
mkdir -p .docker_home
|
|
|
|
|
|
|
|
# Start a detached container as root, add the host uname and uid to /etc/passwd,
|
|
|
|
# then run forever
|
|
|
|
CONTAINER=$(\
|
|
|
|
docker run \
|
|
|
|
-d --rm \
|
2019-03-21 14:49:50 +00:00
|
|
|
-v $PWD:/src \
|
2021-04-04 19:19:54 +00:00
|
|
|
--user root \
|
2021-03-31 19:53:15 +00:00
|
|
|
--shm-size 2g \
|
2020-11-08 20:05:38 +00:00
|
|
|
"${PYODIDE_DOCKER_IMAGE}" \
|
2021-04-04 19:19:54 +00:00
|
|
|
/bin/bash -c " \
|
|
|
|
echo '${USER_ACCOUNT_INFO}' >> /etc/passwd ; \
|
|
|
|
tail -f /dev/null \
|
|
|
|
" \
|
|
|
|
)
|
|
|
|
|
|
|
|
EXIT_STATUS=0
|
|
|
|
# Execute the provided command as the host user with HOME=/src
|
|
|
|
docker exec \
|
2021-06-16 22:04:47 +00:00
|
|
|
$DOCKER_INTERACTIVE --tty \
|
2021-07-05 16:42:38 +00:00
|
|
|
$USER_FLAG \
|
2021-04-04 19:19:54 +00:00
|
|
|
$CONTAINER \
|
|
|
|
/bin/bash -c "${DOCKER_COMMAND}" || EXIT_STATUS=$?
|
|
|
|
|
|
|
|
docker kill $CONTAINER > /dev/null
|
|
|
|
exit $EXIT_STATUS
|