mocha-suv/docker-run
2025-03-25 10:49:34 -04:00

141 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
USE_SSL=false
AUTO_LAUNCH=true
vars=$(getopt -o p:sr --long 'port:,enable-ssh,rebuild,export-vars' -- "$@")
if [ $? -ne 0 ]; then
echo "usage: docker-run [-r, --rebuild] [-p, --port PORTFORWARD] [-s, --enable-ssh]"
exit
fi
eval set -- "$vars"
PORT_FORWARD=""
ENABLE_SSH=""
BUILD_OPTS=""
EXPORT_VARS=0
for opt; do
case "$opt" in
-p|--port)
PORT_FORWARD=$2
shift 2
;;
-s|--enable-ssh)
ENABLE_SSH="true"
shift
;;
-r|--rebuild)
BUILD_OPTS+=" --rebuild"
shift
;;
--export-vars)
EXPORT_VARS=1
shift
;;
esac
done
./docker-build $BUILD_OPTS
Q=$?
if [ $Q -ne 0 ]; then
exit $Q
fi
# generate random UUID for new SUV
# thanks https://stackoverflow.com/questions/34328759
# SUV_ID=$(hexdump -vn8 -e'2/4 "%08x" 1 "\n"' /dev/urandom)
SUV_ID=$(head -c8 < /dev/urandom | xxd -p)
if [ $? -ne 0 ]; then
echo "failed to generate SUV ID"
exit
fi
SUV_NAME=i-0$SUV_ID
SUV_DOMAINNAME=.privatesuv.com
echo "provisioning new Docker-based SUV with id $SUV_NAME"
if [ $? -ne 0 ]; then
echo "'docker build' failed; is it properly installed?"
exit
fi
# create the network configuration
docker network create --subnet=10.8.0.0/16 mocha-docker
# initiate a container (i.e. 'mocha suv up i-0...' )
if [ "$PORT_FORWARD" != "" ]; then
if [ "$USE_SSL" == "true" ]; then
docker run -h $SUV_NAME$SUV_DOMAINNAME --name $SUV_NAME -d -p $PORT_FORWARD:443 mocha-httpd
else
docker run -h $SUV_NAME$SUV_DOMAINNAME --name $SUV_NAME -d -p $PORT_FORWARD:80 mocha-httpd
fi
else
docker run -h $SUV_NAME$SUV_DOMAINNAME --name $SUV_NAME -d --net mocha-docker -it mocha-httpd # -p 8000:80 mocha-httpd
fi
echo "waiting for network to settle..."
sleep 3
# docker inspect $SUV_NAME
SUV_IP=$(docker container exec $SUV_NAME hostname -i)
Q=$?
if [ $Q -ne 0 ]; then
echo "Something went wrong; please check your configuration"
exit $Q
else
echo "Enter administrative password to add entry to /etc/hosts file if wanted"
echo -e "$SUV_IP\t\t$SUV_NAME$SUV_DOMAINNAME" | sudo tee -a /etc/hosts
echo "$SUV_NAME : $SUV_IP"
fi
if [ "$ENABLE_SSH" == "true" ]; then
docker container exec $SUV_NAME /usr/lib/mocha/create_suv_users
docker container exec $SUV_NAME service ssh start
# docker container exec $SUV_NAME cat /etc/mocha/passwd
fi
if [ $EXPORT_VARS -eq 1 ]; then
export MOCHA_NEW_SUV_NAME=$SUV_NAME
if [ "$ENABLE_SSH" == "true" ]; then
export MOCHA_NEW_SUV_PASSWORD_WEBMASTER=$(docker container exec $SUV_NAME cat /etc/mocha/passwd | grep 'webmaster: ' | sed -e 's/webmaster: //g')
fi
fi
if [ "$AUTO_LAUNCH" == "true" ]; then
xdg-open https://$SUV_NAME$SUV_DOMAINNAME
fi