Skip to content

Instantly share code, notes, and snippets.

@niclashoyer
Last active December 28, 2015 06:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niclashoyer/7456381 to your computer and use it in GitHub Desktop.
Save niclashoyer/7456381 to your computer and use it in GitHub Desktop.
Shell script to start a docker container and add it to hipache
#!/bin/bash
# The MIT License (MIT)
#
# Copyright (c) <year> <copyright holders>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# place the following variables in ~/.hipachedockerrc:
# DOMAIN="example.com" # your domain, used to create subdomains
# REDISPORT="12345" # port to the hipache redis database
# SERVERIP="127.0.1.1" # public IP of the server
# run hipache:
# docker run -d -p 80:80 stackbrew/hipache
# start a container:
# ./hipache-docker -p 9000 crosbymichael/sentry
# remove a container:
# ./hipache-docker -d red_ant
set -e
set -u
usage() { echo "Usage: $0 [-d] [-s subdomain] [-p port] container [args]" 1>&2; exit 1; }
rndstr() { cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 12 | head -n 1; }
urlexists() {
[ `redis-cli -p "$REDISPORT" exists "$1"` -ne 0 ]
}
addproxy() {
redis-cli -p "$REDISPORT" rpush "frontend:$1" "$2"
redis-cli -p "$REDISPORT" rpush "frontend:$1" "$3"
redis-cli -p "$REDISPORT" set "container:$4" "frontend:$1"
}
getid() {
INSPECT=`docker inspect $1`
if [ "$?" -ne 0 ]; then
exit 4
fi
echo $INSPECT | python -c "import json, sys; print json.load(sys.stdin)[0]['ID']"
}
getname() {
INSPECT=`docker inspect $1`
if [ "$?" -ne 0 ]; then
exit 4
fi
echo $INSPECT | python -c "import json, sys; print json.load(sys.stdin)[0]['Name']" | grep -Po '[\w_]*'
}
delproxy() {
KEY=`redis-cli -p "$REDISPORT" get "container:$1"`
redis-cli -p "$REDISPORT" del "$KEY"
redis-cli -p "$REDISPORT" del "container:$1"
}
MODE="create"
SUBDOMAIN=`rndstr`
PORT="80"
while getopts ':ds:p:' o; do
case "${o}" in
d)
MODE="delete"
;;
s)
SUBDOMAIN=${OPTARG}
;;
p)
PORT=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ -z ${1:-} ]; then
usage
fi
CONTAINER=$1
shift
CONTAINERARGS="$@"
source ~/.hipachedockerrc
DOMAIN=${DOMAIN:-"localhost"}
REDISPORT=${REDISPORT:-"6379"}
SERVERIP=${SERVERIP:-"127.0.0.1"}
URL="http://$SUBDOMAIN.$DOMAIN"
case "$MODE" in
delete)
ID=`getid "$CONTAINER"`
delproxy "$ID" > /dev/null
docker stop "$ID" > /dev/null
docker rm "$ID" > /dev/null
;;
create)
if urlexists "$URL"; then
echo "$URL already exists, choose another subdomain"
exit 3
fi
ID=`docker run -d -p "$PORT" "$CONTAINER" $CONTAINERARGS`
ID=`getid "$ID"`
NAME=`getname "$ID"`
DOCKERIP=`docker port $ID $PORT`
DOCKERPORT=`echo $DOCKERIP | cut -d':' -f2`
LOCALURL="http://$SERVERIP:$DOCKERPORT"
addproxy "$SUBDOMAIN.$DOMAIN" "$SUBDOMAIN" "$LOCALURL" "$ID" > /dev/null
echo "$NAME" >&2
echo $URL
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment