mocha-suv/docker-run

101 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
USE_SSL=false
./docker-build ${1+"$@"}
Q=$?
if [ $Q -ne 0 ]; then
exit $Q
fi
PORT_FORWARD=""
if [ "$1" == "--port" ]; then
PORT_FORWARD="$2"
shift; shift
fi
ENABLE_SSH=""
if [ "$1" == "--enable-ssh" ]; then
ENABLE_SSH="true"
shift
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
fi