mocha-suv/docker-run
2025-01-13 13:27:50 -05:00

84 lines
1.6 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
# 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)
if [ $? -ne 0 ]; then
echo "Something went wrong; please check your configuration"
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