Skip to content

Instantly share code, notes, and snippets.

@Wytamma
Last active July 12, 2019 06:25
Show Gist options
  • Save Wytamma/aa0e62be8fa387fb5a45118f5433a91f to your computer and use it in GitHub Desktop.
Save Wytamma/aa0e62be8fa387fb5a45118f5433a91f to your computer and use it in GitHub Desktop.
#! /usr/bin/env sh
# Exit in case of error
set -e
while getopts 'hne:d:u:p:c::t::' flag; do
case "$flag" in
h)
echo "This script sets a Traefik Proxy with HTTPS."
echo "Run this script on the main node after initialising the swarm"
echo "flags:"
echo '-n (Create network? Only need to be run first time.)'
echo '-e (Email, to be used for the generation of Lets Encrypt certificates)'
echo '-d (Domain you want to use for the Traefik UI (user interface) and the Consul UI of the host)'
echo '-u (Username (you will use it for the HTTP Basic Auth for Traefik and Consul UIs))'
echo '-p (Password (you will use it for the HTTP Basic Auth for Traefik and Consul UIs))'
echo '-c (Consul service replicas (if you dont set it, by default it will be 3))'
echo '-t (Traefik service replicas (if you dont set it, by default it will be 3))'
exit 1
;;
n)
NETWORK=1;;
e)
EMAIL=$OPTARG;;
d)
DOMAIN=$OPTARG;;
u)
USERNAME=$OPTARG;;
p)
PASSWORD=$OPTARG;;
c)
CONSUL_REPLICAS=$OPTARG;;
t)
TRAEFIK_REPLICAS=$OPTARG;;
:)
echo "Missing option argument for -$OPTARG" >&2;
exit 1;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
if [ $NETWORK ]; then
# Create a network that will be shared with Traefik and the containers that should be accessible from the outside
docker network create --driver=overlay traefik-public
fi
# Use openssl to generate the "hashed" version of the password and store it in an environment variable
HASHED_PASSWORD=$(openssl passwd -apr1 $PASSWORD)
# Download the file traefik.yml
curl -L dockerswarm.rocks/traefik.yml -o traefik.yml
# Deploy the stack
EMAIL=${EMAIL} \
DOMAIN=${DOMAIN} \
HASHED_PASSWORD=${HASHED_PASSWORD} \
PASSWORD=${PASSWORD} \
CONSUL_REPLICAS=${CONSUL_REPLICAS} \
TRAEFIK_REPLICAS=${TRAEFIK_REPLICAS} \
docker stack deploy -c traefik.yml traefik-consul
# Check if the stack was deployed
docker stack ps traefik-consul
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment