balena-supervisor/tools/dev/dindctl

121 lines
3.5 KiB
Plaintext
Raw Normal View History

#!/bin/bash
set -o errexit
set -o pipefail
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
SUPERVISOR_BASE_DIR="${DIR}/../.."
ARCH=${ARCH:-"amd64"}
SUPERVISOR_IMAGE=${SUPERVISOR_IMAGE:-"registry.resindev.io/resin/${ARCH}-supervisor:master"}
PASSWORDLESS_DROPBEAR=${PASSWORDLESS_DROPBEAR:-"false"}
SUPERVISOR_EXTRA_MOUNTS=
SUPERVISOR_LOGS=(
'/var/log/supervisor-log/go_supervisor_stdout.log'
'/var/log/supervisor-log/resin_supervisor_stdout.log'
'/var/log/supervisor-log/supervisor/supervisord.log'
)
function showHelp {
echo
echo " This script can be used to facilitate supervisor development. Its core feature is allowing"
echo " faster development iterations by bind-mounting the local './src' directly into the running"
echo " supervisor container."
echo
echo " Setting the '--mount-nm' flag in either 'run' or 'deployrun' action will bind-mount"
echo " './node_modules/' into the running supervisor. In this case, it's up to the developer"
echo " to make sure that the correct dependencies are installed."
echo
echo " Usage: [environment] $0 action [options]"
echo
echo " Environment Variables:"
echo " ARCH [=amd64]"
echo " SUPERVISOR_IMAGE [=registry.resindev.io/resin/<ARCH>-supervisor:master]"
echo " PASSWORDLESS_DROPBEAR [=false]"
echo " Actions:"
echo " deploy build and deploy local supervisor image - you can override registry/image name with 'SUPERVISOR_IMAGE'"
echo " run [options] build dind host container, run it, then pull the configured 'SUPERVISOR_IMAGE' into the dind host and run it"
echo " deployrun [options] run 'deploy' and then immediately 'run' the deployed container"
echo " refresh recompile sources in './src' with 'coffee -c' and restart supervisor container on dind host"
echo " logs [-f] print out supervisor log files - use '-f' to follow instead"
echo " stop stop dind supervisor host container"
echo " Options:"
echo " --mount-src bind-mount './src/' from local development environment into supervisor container"
echo " --mount-nm bind-mount './node_modules/' from local development environment into supervisor container"
echo
}
function deploySupervisor {
make -C "$SUPERVISOR_BASE_DIR" \
ARCH="$ARCH" \
SUPERVISOR_IMAGE="$SUPERVISOR_IMAGE" \
PASSWORDLESS_DROPBEAR="$PASSWORDLESS_DROPBEAR" \
deploy
}
function runDind {
for arg in "$@"
do
case $arg in
--mount-src)
coffee -c "$SUPERVISOR_BASE_DIR/src"
SUPERVISOR_EXTRA_MOUNTS="$SUPERVISOR_EXTRA_MOUNTS -v /resin-supervisor/src:/usr/src/app/src"
shift
;;
--mount-nm)
SUPERVISOR_EXTRA_MOUNTS="$SUPERVISOR_EXTRA_MOUNTS -v /resin-supervisor/node_modules:/usr/src/app/node_modules"
shift
;;
*)
echo "Warning: unknown argument: $arg"
;;
esac
done
make -C "$SUPERVISOR_BASE_DIR" \
ARCH="$ARCH" \
SUPERVISOR_IMAGE="$SUPERVISOR_IMAGE" \
PASSWORDLESS_DROPBEAR="$PASSWORDLESS_DROPBEAR" \
SUPERVISOR_EXTRA_MOUNTS="$SUPERVISOR_EXTRA_MOUNTS" \
run-supervisor
}
function logs {
if [ "$1" = "-f" ]; then
docker exec -ti resin_supervisor_1 tail -f ${SUPERVISOR_LOGS[@]}
else
for log in "${SUPERVISOR_LOGS[@]}"; do
echo " == ${log} =="
docker exec -ti resin_supervisor_1 cat "$log"
done
fi
}
action="$1"
shift || true
case $action in
deploy)
deploySupervisor
;;
run)
runDind "$@"
;;
deployrun)
deploySupervisor && runDind "$@"
;;
refresh)
make -C "$SUPERVISOR_BASE_DIR" refresh-supervisor-src
;;
logs)
logs "$@"
;;
stop)
make -C "$SUPERVISOR_BASE_DIR" stop-supervisor
;;
*)
showHelp
esac