2018-07-13 01:35:03 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2018-07-25 03:27:03 +00:00
|
|
|
run_embark () {
|
2018-08-02 18:43:05 +00:00
|
|
|
local EMBARK_DOCKER_MOUNT_SOURCE="${EMBARK_DOCKER_MOUNT_SOURCE:-$PWD}"
|
|
|
|
local EMBARK_DOCKER_MOUNT_TARGET="${EMBARK_DOCKER_MOUNT_TARGET:-/dapp}"
|
|
|
|
local EMBARK_DOCKER_IMAGE="${EMBARK_DOCKER_IMAGE:-statusim/embark}"
|
|
|
|
local EMBARK_DOCKER_RUN="${EMBARK_DOCKER_RUN}"
|
2018-08-02 02:30:52 +00:00
|
|
|
local EMBARK_DOCKER_RUN_INTERACTIVE=${EMBARK_DOCKER_RUN_INTERACTIVE:-false}
|
2018-08-02 17:25:16 +00:00
|
|
|
local EMBARK_DOCKER_RUN_OPTS_REPLACE=${EMBARK_DOCKER_RUN_OPTS_REPLACE:-false}
|
2018-08-02 19:08:22 +00:00
|
|
|
local EMBARK_DOCKER_RUN_RM=${EMBARK_DOCKER_RUN_RM:-true}
|
2018-08-02 18:43:05 +00:00
|
|
|
local EMBARK_DOCKER_TAG="${EMBARK_DOCKER_TAG:-latest}"
|
2018-07-13 01:35:03 +00:00
|
|
|
|
2018-08-02 17:25:16 +00:00
|
|
|
local -a run_opts=(
|
|
|
|
"-i"
|
|
|
|
"-t"
|
|
|
|
"-p"
|
|
|
|
"5001:5001"
|
|
|
|
"-p"
|
|
|
|
"8000:8000"
|
|
|
|
"-p"
|
|
|
|
"8080:8080"
|
|
|
|
"-p"
|
|
|
|
"8500:8500"
|
|
|
|
"-p"
|
|
|
|
"8545:8545"
|
|
|
|
"-p"
|
|
|
|
"8546:8546"
|
|
|
|
"-p"
|
|
|
|
"8555:8555"
|
|
|
|
"-p"
|
|
|
|
"8556:8556"
|
|
|
|
"-p"
|
|
|
|
"30301:30301/udp"
|
|
|
|
"-p"
|
|
|
|
"30303:30303"
|
|
|
|
"-v"
|
|
|
|
"${EMBARK_DOCKER_MOUNT_SOURCE}:${EMBARK_DOCKER_MOUNT_TARGET}"
|
|
|
|
)
|
|
|
|
|
2018-08-22 02:02:35 +00:00
|
|
|
local env_var
|
|
|
|
for env_var in LANG \
|
|
|
|
LANGUAGE \
|
|
|
|
LC_ALL \
|
|
|
|
TERM;
|
|
|
|
# do not alter indentation, tabs in lines below
|
|
|
|
do
|
|
|
|
local include_var=$(cat <<- ENV_VAR
|
|
|
|
if [[ -n $env_var && -v $env_var ]]; then
|
|
|
|
run_opts=( "${run_opts[@]}" "-e" "$env_var" )
|
|
|
|
fi
|
|
|
|
ENV_VAR
|
|
|
|
)
|
|
|
|
eval "$include_var"
|
|
|
|
done
|
|
|
|
# do not alter indentation, tabs in lines above
|
2018-08-20 22:51:34 +00:00
|
|
|
|
2018-08-02 19:08:22 +00:00
|
|
|
if [[ $EMBARK_DOCKER_RUN_RM = true ]]; then
|
|
|
|
run_opts=( "${run_opts[@]}" "--rm" )
|
|
|
|
fi
|
|
|
|
|
2018-07-13 01:35:03 +00:00
|
|
|
local txtbld=$(tput bold)
|
|
|
|
local txtrst=$(tput sgr0)
|
|
|
|
|
|
|
|
local bldcyn=${txtbld}$(tput setaf 6)
|
|
|
|
local bldred=${txtbld}$(tput setaf 1)
|
|
|
|
local bldylw=${txtbld}$(tput setaf 3)
|
|
|
|
|
|
|
|
local ERROR=${bldred}ERROR${txtrst}
|
|
|
|
local INFO=${bldcyn}INFO${txtrst}
|
|
|
|
local WARNING=${bldylw}WARNING${txtrst}
|
|
|
|
|
2018-08-02 17:25:16 +00:00
|
|
|
local oldopts
|
2018-08-02 02:30:52 +00:00
|
|
|
case $- in
|
|
|
|
*e*) oldopts="set -e" ;;
|
|
|
|
*) oldopts="set +e" ;;
|
|
|
|
esac
|
|
|
|
set +e
|
|
|
|
|
2018-08-20 22:52:06 +00:00
|
|
|
cleanup () {
|
|
|
|
local retval=$?
|
|
|
|
unset check_bash_version
|
|
|
|
unset check_docker
|
|
|
|
unset cleanup
|
|
|
|
eval "$oldopts"
|
|
|
|
return $retval
|
|
|
|
}
|
|
|
|
|
2018-07-13 01:35:03 +00:00
|
|
|
check_bash_version () {
|
|
|
|
if [[ $BASH_VERSINFO -lt 4 ]]; then
|
|
|
|
echo "$ERROR: this script requires Bash version >= 4.0"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
2018-08-20 22:52:06 +00:00
|
|
|
check_bash_version || cleanup || return
|
2018-07-13 01:35:03 +00:00
|
|
|
|
|
|
|
check_docker () {
|
|
|
|
if ! type docker &> /dev/null; then
|
|
|
|
echo "$ERROR: the command \`docker\` must be in a path on \$PATH or aliased"
|
|
|
|
return 127
|
|
|
|
fi
|
|
|
|
}
|
2018-08-20 22:52:06 +00:00
|
|
|
check_docker || cleanup || return
|
2018-07-13 01:35:03 +00:00
|
|
|
|
2018-08-02 17:25:16 +00:00
|
|
|
local had_run_opts=false
|
|
|
|
local -a _run_opts=()
|
|
|
|
local -a _cmd=()
|
|
|
|
local -a cmd
|
|
|
|
|
|
|
|
while [[ ! -z "$1" ]]; do
|
|
|
|
if [[ "$1" = "--" ]]; then
|
|
|
|
had_run_opts=true
|
|
|
|
else
|
|
|
|
if [[ $had_run_opts = true ]]; then
|
|
|
|
_cmd=( "${_cmd[@]}" "$1" )
|
|
|
|
else
|
|
|
|
_run_opts=( "${_run_opts[@]}" "$1" )
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ $had_run_opts = true ]]; then
|
|
|
|
cmd=( "${_cmd[@]}" )
|
|
|
|
if [[ $EMBARK_DOCKER_RUN_OPTS_REPLACE = true ]]; then
|
|
|
|
run_opts=( "${_run_opts[@]}" )
|
|
|
|
else
|
|
|
|
run_opts=( "${run_opts[@]}" "${_run_opts[@]}" )
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
cmd=( "${_run_opts[@]}" )
|
|
|
|
fi
|
2018-07-13 01:35:03 +00:00
|
|
|
|
2018-08-02 02:32:47 +00:00
|
|
|
if [[ -z "$EMBARK_DOCKER_RUN" ]]; then
|
2018-08-02 17:25:16 +00:00
|
|
|
case "${cmd[0]}" in
|
2018-08-02 02:32:47 +00:00
|
|
|
-V|--version|-h|--help|new|demo|build|run|blockchain|simulator|test|\
|
2018-08-02 17:25:16 +00:00
|
|
|
reset|graph|upload|version) cmd=( "embark" "${cmd[@]}" ) ;;
|
2018-08-02 02:32:47 +00:00
|
|
|
esac
|
|
|
|
else
|
2018-08-02 17:25:16 +00:00
|
|
|
local i_flag
|
2018-08-02 02:32:47 +00:00
|
|
|
if [[ $EMBARK_DOCKER_RUN_INTERACTIVE = true ]]; then
|
|
|
|
i_flag='i'
|
|
|
|
else
|
|
|
|
i_flag=''
|
|
|
|
fi
|
|
|
|
|
|
|
|
local run_script=$(< "$EMBARK_DOCKER_RUN")
|
2018-08-08 00:59:59 +00:00
|
|
|
# do not alter indentation, tabs in lines below
|
|
|
|
run_script=$(cat <<- RUN_SCRIPT
|
2018-08-08 14:52:43 +00:00
|
|
|
exec bash -${i_flag}s \$(tty) ${cmd[@]} << 'RUN'
|
2018-08-08 00:59:59 +00:00
|
|
|
__tty=\$1
|
|
|
|
shift
|
2018-08-09 19:26:21 +00:00
|
|
|
script=/tmp/run_embark_script
|
2018-08-08 14:52:43 +00:00
|
|
|
cat << 'SCRIPT' > \$script
|
2018-08-08 00:59:59 +00:00
|
|
|
$run_script
|
|
|
|
SCRIPT
|
2018-08-08 14:52:43 +00:00
|
|
|
chmod +x \$script
|
|
|
|
exec \$script \$@ < \$__tty
|
|
|
|
RUN
|
2018-08-08 00:59:59 +00:00
|
|
|
RUN_SCRIPT
|
|
|
|
)
|
|
|
|
# do not alter indentation, tabs in lines above
|
2018-08-02 02:32:47 +00:00
|
|
|
cmd=( "bash" "-${i_flag}c" "$run_script" )
|
|
|
|
fi
|
2018-07-25 03:27:03 +00:00
|
|
|
|
2018-07-13 01:35:03 +00:00
|
|
|
docker run \
|
2018-08-02 17:25:16 +00:00
|
|
|
"${run_opts[@]}" \
|
2018-08-02 02:33:06 +00:00
|
|
|
"${EMBARK_DOCKER_IMAGE}:${EMBARK_DOCKER_TAG}" \
|
2018-08-20 22:52:06 +00:00
|
|
|
"${cmd[@]}" \
|
|
|
|
|| cleanup || return
|
2018-07-13 01:35:03 +00:00
|
|
|
|
2018-08-20 22:52:06 +00:00
|
|
|
cleanup
|
2018-07-13 01:35:03 +00:00
|
|
|
}
|
2018-08-03 00:55:54 +00:00
|
|
|
export -f run_embark
|
2018-07-13 01:35:03 +00:00
|
|
|
|
|
|
|
if [[ "$0" = "$BASH_SOURCE" ]]; then
|
2018-07-25 03:27:03 +00:00
|
|
|
run_embark "$@"
|
2018-07-13 01:35:03 +00:00
|
|
|
fi
|