Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 927 lines (829 sloc) 26 KB
#!/bin/bash
if [ -f /etc/default/octohost ]; then
. /etc/default/octohost
fi
log () {
local message="$1"
#local level="${2:-info}"
logger -p local4.info -t octohost "$message"
}
log "Command='$0 $*'"
show_break ()
{
echo ""
echo ""
echo "################################"
}
start_logspout ()
{
if [ -z "$LOGSPOUT_TARGET" ]; then
echo "No LOGSPOUT_TARGET in /etc/default/octohost - not starting."
exit
fi
if [ -e "/var/run/docker.sock" ]; then
$LOGSPOUT
else
sudo service docker start
sleep 5
$LOGSPOUT
fi
}
stop_logspout ()
{
sudo docker ps | grep "$LOGSPOUT_IMAGE" | awk '{ print $1 }' | xargs sudo docker kill
}
pull_logspout ()
{
sudo docker pull $LOGSPOUT_IMAGE
}
start_logs ()
{
check_syslog_port
if [ -e "/var/run/docker.sock" ]; then
LOG_COMMAND="sudo docker run -d --restart always -v=$1:$2 $REMOTE_SYSLOG_COMMAND $2"
$LOG_COMMAND
else
sudo service docker start
sleep 5
$LOG_COMMAND
fi
}
stop_octologs ()
{
sudo docker ps | grep "$OCTOLOGS_IMAGE" | awk '{ print $1 }' | xargs sudo docker kill
}
check_syslog_port ()
{
if [ -z "$REMOTE_SYSLOG_PORT" ]; then
echo "Need to define REMOTE_SYSLOG_PORT in /etc/default/octohost"
exit 1
fi
}
inspect_to_get_external_port()
{
PORT=$(sudo docker -H $DOCKER_HOST inspect --format='{{range $p, $conf := .NetworkSettings.Ports}}{{(index $conf 0).HostPort}} {{end}}' $ID)
echo $PORT
}
inspect_to_get_ip_address()
{
IP_ADDRESS=$(sudo docker -H $DOCKER_HOST inspect --format='{{range $p, $conf := .NetworkSettings.Ports}}{{(index $conf 0).HostIp}} {{end}}' $ID)
echo $IP_ADDRESS
}
inspect_to_get_container_name()
{
for tag in $(sudo docker -H $DOCKER_HOST inspect -f '{{json .Config.Env}}' $ID | jq '.[]' ); do
if echo $tag | grep -q "CONTAINER_NAME"
then
CONTAINER_NAME=$(echo $tag | cut -d '"' -f 2 | cut -d '=' -f 2-10)
fi
done
echo $CONTAINER_NAME
}
launch_container_return_id ()
{
if [ -n "$PRIVATE_REGISTRY" ]; then
ID=$(echo "$RUN_OPTIONS $PRIVATE_REGISTRY/$BASE" | xargs sudo docker -H $DOCKER_HOST run)
else
ID=$(echo "$RUN_OPTIONS $BUILD_ORG_NAME/$BASE" | xargs sudo docker -H $DOCKER_HOST run)
fi
echo $ID
}
set_src_variables ()
{
BASE="$1"
REPOSITORY="$BASE.git"
REPO_PATH="$SRC_DIR/$REPOSITORY"
DOCKERFILE="$REPO_PATH/Dockerfile"
log "Base: $BASE, Repo: $REPOSITORY, Path: $REPO_PATH, Dockerfile: $DOCKERFILE"
}
get_unique_domains ()
{
SCRATCH=`mktemp`
DOMAINS="$1"
for domain in ${DOMAINS//,/ } ; do
echo $domain
done | sort | uniq > $SCRATCH
FINAL_DOMAINS=""
for line in $( cat $SCRATCH ); do
FINAL_DOMAINS="$FINAL_DOMAINS $line"
done
rm -f $SCRATCH
# Trim the first space.
echo "${FINAL_DOMAINS/ /}"
}
case "$1" in
logs)
if [ "$2" == "octostart" ]; then
check_syslog_port
$OCTOLOGS
elif [ "$2" == "octostop" ]; then
stop_octologs
elif [ "$2" == "start" ]; then
if [[ $3 && $4 ]]; then
check_syslog_port
start_logs "$3" "$4"
else
echo "Usage: octo logs start {full-log-path} {mount}"
fi
else
echo "Usage: octo logs {octostart|start [full-log-path] [mount]}"
fi
;;
logspout)
if [ "$2" == "start" ]; then
check_syslog_port
start_logspout
elif [ "$2" == "stop" ]; then
stop_logspout
elif [ "$2" == "pull" ]; then
pull_logspout
else
echo "Usage: octo logspout {start|stop|pull}"
fi
;;
start)
if [ $2 ]; then
set_src_variables "$2"
NUMBER=${3:-1}
for (( i=1; i <= $NUMBER; i++ ))
do
if [ -e "$DOCKERFILE" ]; then
RUN_OPTIONS=$(/usr/bin/octo config:options "$BASE" "$DOCKERFILE")
else
RUN_OPTIONS="-P -d -e CONTAINER_NAME=$BASE"
ENV=$(/usr/bin/octo config:env $BASE)
RUN_OPTIONS="$RUN_OPTIONS $ENV"
fi
ID=$(launch_container_return_id)
PORT=$(inspect_to_get_external_port)
CONTAINER_IP=$(inspect_to_get_ip_address)
TAGS=$(/usr/bin/octo service:tags $ID)
/usr/bin/octo service:set $BASE $CONTAINER_IP $PORT $TAGS
/usr/bin/octo add:upstart $BASE
done
else
echo "Usage: octo start {container}"
fi
;;
stop)
if [ "$2" ]; then
BASE="$2"
if [ -n $PRIVATE_REGISTRY ]; then
IMAGE_PREFIX="$PRIVATE_REGISTRY/"
else
IMAGE_PREFIX=""
fi
CONTAINERS=$(sudo docker -H $DOCKER_HOST ps | grep "${IMAGE_PREFIX}$BASE" | cut -d ' ' -f 1)
for container in $CONTAINERS
do
octo kill "$BASE" "$container"
done
else
echo "Usage: octo stop {container_name}"
fi
;;
kill)
if [[ "$2" && "$3" ]]; then
BASE="$2"
ID="$3"
PORT=$(inspect_to_get_external_port)
CONTAINER_IP=$(inspect_to_get_ip_address)
sudo docker -H $DOCKER_HOST kill "$ID" > /dev/null
octo service:rm "$BASE" "$CONTAINER_IP" "$PORT"
else
echo "Usage: octo kill {container} {container_id}"
fi
;;
config:set)
if [[ $2 && "$3" ]]; then
/usr/bin/consulkv set $CONSUL_PREFIX/$2 "$3"
else
echo "Usage: octo config:set {container/key} {var}"
fi
;;
config:get)
if [ $2 ]; then
/usr/bin/consulkv get $CONSUL_PREFIX/$2
else
echo "Usage: octo config:get {container/key}"
fi
;;
config:rm)
if [ $2 ]; then
/usr/bin/consulkv del $CONSUL_PREFIX/$2
else
echo "Usage: octo config:rm {container/key}"
fi
;;
config:export)
for key in `/usr/bin/consulkv ls $CONSUL_PREFIX`
do
if [ "$key" != "null" ]; then
VALUE=$(octo config:get $key)
echo "octo config:set $key '$VALUE'"
fi
done
;;
config)
if [ $2 ]; then
for conf in `/usr/bin/consulkv ls $CONSUL_PREFIX/$2`;
do
CONF_VALUE=$(/usr/bin/octo config:get $2/$conf)
echo "/$2/$conf:$CONF_VALUE";
done
else
echo "Usage: octo config {container}"
fi
;;
config:env)
if [ $2 ]; then
ENV=""
while read conf
do
KEY=$(echo $conf | cut -d '/' -f 3 | cut -d ':' -f 1)
VAR=$(echo $conf | cut -d ':' -f 2-10)
ENV="$ENV -e $KEY=\"$VAR\""
done < <(/usr/bin/octo config $2)
echo "$ENV"
else
echo "octo config:env {container}"
fi
;;
config:options)
if [[ $2 && $3 ]]; then
BASE="$2"
DOCKERFILE="$3"
RUN_OPTIONS=" -d -e CONTAINER_NAME=$BASE"
ADD_NAME=$(grep -i "^# ADD_NAME" $DOCKERFILE)
if [ -n "$ADD_NAME" ]
then
RUN_OPTIONS="$RUN_OPTIONS --name $BASE"
fi
VOLUMES_FROM=$(grep -i "^# VOLUMES_FROM" $DOCKERFILE)
if [ -n "$VOLUMES_FROM" ]
then
VOLUME_NAME="${BASE}_data"
RUN_OPTIONS="$RUN_OPTIONS --volumes-from $VOLUME_NAME"
fi
while read -r MOUNT_FROM_HOST ; do
if [ -n "$MOUNT_FROM_HOST" ]
then
HOST_PATH=$(echo $MOUNT_FROM_HOST | awk '{ print $3 }')
CONTAINER_PATH=$(echo $MOUNT_FROM_HOST | awk '{ print $4 }')
if [ -n "$CONTAINER_PATH" ]
then
MOUNT_PATH="$HOST_PATH:$CONTAINER_PATH"
else
MOUNT_PATH="$HOST_PATH:$HOST_PATH"
fi
RUN_OPTIONS=" -v $MOUNT_PATH $RUN_OPTIONS"
fi
done < <(grep -i '^# MOUNT_FROM_HOST' $DOCKERFILE )
while read -r PORTS_FROM_HOST ; do
if [ -n "$PORTS_FROM_HOST" ]
then
HOST_PORT=$(echo $PORTS_FROM_HOST | awk '{ print $3 }')
CONTAINER_PORT=$(echo $PORTS_FROM_HOST | awk '{ print $4 }')
CONTAINER_ADDRESS=$(echo $PORTS_FROM_HOST | awk '{ print $5 }')
if [ -n "$CONTAINER_ADDRESS" ]
then
NEW_PORT="$CONTAINER_ADDRESS:$HOST_PORT:$CONTAINER_PORT"
else
NEW_PORT="0.0.0.0:$HOST_PORT:$CONTAINER_PORT"
fi
PORT_OPTIONS=" -p $NEW_PORT $PORT_OPTIONS"
fi
done < <(grep -i '^# PORTS_FROM_HOST' $DOCKERFILE )
if [ -n "$PORT_OPTIONS" ]
then
RUN_OPTIONS=" $PORT_OPTIONS $RUN_OPTIONS"
else
RUN_OPTIONS=" -P $RUN_OPTIONS"
fi
# Support a CNAME file in repo src
# and if there supply the -h option to docker run
CNAME="/home/git/src/${BASE}.git/CNAME"
if [ -f $CNAME ]
then
# Add a new line at end if it does not exist to ensure the loop gets last line
# sed -i -e '$a\' $CNAME
while read DOMAIN
do
CONTAINER_HOSTNAME="$DOMAIN"
done < $CNAME
fi
if [ -n "$CONTAINER_HOSTNAME" ]
then
RUN_OPTIONS=" -h $CONTAINER_HOSTNAME $RUN_OPTIONS"
fi
FOUND_LINKS=$(grep -i "^# LINK_SERVICE" $DOCKERFILE)
if [ -n "$FOUND_LINKS" ]; then
while read LINK_LINE
do
SOURCE=$(echo $LINK_LINE | awk '{ print $3 }')
LINK_NAME=$(echo $LINK_LINE | awk '{ print $4 }')
if [ -n "$LINK_NAME" ]
then
LINK_NAME="$LINK_NAME:$SOURCE"
else
LINK_NAME="${BASE}_${SOURCE}:${SOURCE}"
fi
RUN_OPTIONS="$RUN_OPTIONS --link $LINK_NAME"
done <<<"$FOUND_LINKS"
fi
MEMORY_LIMIT=$(/usr/bin/octo config:get $BASE/MEMORY_LIMIT)
if [ -n "$MEMORY_LIMIT" ]
then
RUN_OPTIONS="$RUN_OPTIONS --memory='$MEMORY_LIMIT'"
fi
ENV_VARS=$(/usr/bin/octo config $BASE | grep -v "Error: 100: Key not found")
if [ -n "$ENV_VARS" ]
then
ENV=$(/usr/bin/octo config:env $BASE)
RUN_OPTIONS="$RUN_OPTIONS $ENV"
fi
echo "$RUN_OPTIONS"
log "Run Options: $RUN_OPTIONS"
else
echo "Something went wrong."
fi
;;
config:check)
if [[ $2 && $3 ]]; then
set_src_variables "$2"
CONFIG_TO_CHECK="$3"
VALUE=$(grep -i "^# $CONFIG_TO_CHECK" $DOCKERFILE)
if [ -z "$VALUE" ]; then
VALUE=$(/usr/bin/consulkv get $CONSUL_PREFIX/$2/$CONFIG_TO_CHECK)
fi
echo $VALUE
log "config:check '$CONFIG_TO_CHECK=$VALUE'"
else
echo "octo config:check {container} {var_to_check}"
fi
;;
service:set)
if [[ $2 ]]; then
NO_HTTP_PROXY=$(/usr/bin/octo config:check "$2" "NO_HTTP_PROXY")
if [ -n "$NO_HTTP_PROXY" ]; then
exit 0
fi
fi
if [[ $2 && $3 && $4 ]]; then
CONTAINER="$2"
CONTAINER_IP="$3"
PORT="$4"
TAGS=${5:-http}
CUSTOM_CHECK=$(/usr/bin/octo config:get $CONTAINER/CUSTOM_CHECK)
CHECK_COMMAND=${CUSTOM_CHECK:-http://$CONTAINER_IP:$PORT}
CUSTOM_CHECK_INTERVAL=$(/usr/bin/octo config:get $CONTAINER/CUSTOM_CHECK_INTERVAL)
CHECK_INTERVAL=${CUSTOM_CHECK_INTERVAL:-15s}
if echo $TAGS | grep -q 'http'
then
CHECK=",\"check\": {\"http\": \"$CHECK_COMMAND\",\"interval\": \"$CHECK_INTERVAL\"}"
else
CHECK=""
fi
PAYLOAD="{\"ID\": \"$CONTAINER-$CONTAINER_IP-$PORT\",\"Name\": \"$CONTAINER\",\"Address\": \"$CONTAINER_IP\",\"Port\": $PORT,\"Tags\": [\"$TAGS\"]$CHECK}"
/usr/bin/consulkv service:set "$PAYLOAD"
log "service:set '$PAYLOAD'"
else
echo "Usage: octo service:set {container} {ip_address} {port} {tags}"
fi
;;
service:rm)
if [[ $2 ]]; then
NO_HTTP_PROXY=$(/usr/bin/octo config:check "$2" "NO_HTTP_PROXY")
if [ -n "$NO_HTTP_PROXY" ]; then
exit 0
fi
fi
if [[ "$2" && "$3" && "$4" ]]; then
CONTAINER="$2"
CONTAINER_IP="$3"
PORT="$4"
/usr/bin/consulkv service:del "$CONTAINER-$CONTAINER_IP-$PORT"
else
echo "Usage: octo service:rm {container} {ip} {port}"
fi
;;
service:tags)
if [ $2 ]; then
for tag in $(sudo docker -H $DOCKER_HOST inspect -f '{{json .Config.Env}}' $2 | jq '.[]' ); do
if echo $tag | grep -q "CONSUL_TAGS"
then
TAG=$(echo $tag | cut -d '"' -f 2 | cut -d '=' -f 2-10)
fi
done
echo $TAG
log "service:tags '$TAGS'"
fi
;;
services)
/usr/bin/consulkv services
;;
services:catalog)
/usr/bin/consulkv services:catalog
;;
services:register)
for container in $(sudo docker -H $DOCKER_HOST ps | grep $BUILD_ORG_NAME | awk '{ print $1 }'); do
ID=$container
PORT=$(inspect_to_get_external_port)
CONTAINER_IP=$(inspect_to_get_ip_address)
BASE=$(inspect_to_get_container_name)
TAGS=$(/usr/bin/octo service:tags $ID)
if [[ $BASE && $PORT ]]; then
/usr/bin/octo service:set $BASE $CONTAINER_IP $PORT $TAGS
fi
done
;;
services:clear)
for container in $(sudo docker -H $DOCKER_HOST ps -q ); do
ID=$container
PORT=$(inspect_to_get_external_port)
CONTAINER_IP=$(inspect_to_get_ip_address)
BASE=$(inspect_to_get_container_name)
if [[ "$BASE" && "$CONTAINER_IP" && "$PORT" ]]; then
/usr/bin/octo service:rm "$BASE" "$CONTAINER_IP" "$PORT"
fi
done
# Remove HTTP tagged orphans from crashed containers.
/usr/bin/octo services:remove_all
;;
services:remove_all)
# Remove HTTP tagged orphans from crashed containers.
for service in $(/usr/bin/octo services); do
if echo $service | grep -q 'http'
then
/usr/bin/octo service:remove "$service"
fi
done
;;
services:remove)
if [ $2 ]; then
for service in $(/usr/bin/octo services | grep "\"Service\":\"$2\""); do
if echo $service | grep -q 'http'
then
/usr/bin/octo service:remove "$service"
fi
done
else
echo "Usage: octo services:remove {container}"
fi
;;
service:remove)
if [ $2 ]; then
ID=$(echo $2 | jq .ID | cut -d '"' -f 2)
PORT=$(echo $ID | grep -o [0-9]*$)
BASE=${ID%-$PORT}
/usr/bin/octo service:rm $BASE $PORT
fi
;;
domains:set)
if [[ $2 && $3 ]]; then
DOMAINS=$(get_unique_domains $3)
/usr/bin/consulkv set $CONSUL_PREFIX/$2/DOMAINS "$DOMAINS"
log "domains:set '$DOMAINS'"
else
echo "Usage: octo domains:set {container} {domains}"
echo "Domains are space separated."
fi
;;
domains:get)
if [ $2 ]; then
/usr/bin/consulkv get $CONSUL_PREFIX/$2/DOMAINS
else
echo "Usage: octo domains:get {container}"
fi
;;
config:consul_template)
BASE="$2"
if [[ $BASE ]]; then
NO_HTTP_PROXY=$(/usr/bin/octo config:check "$BASE" "NO_HTTP_PROXY")
if [ -n "$NO_HTTP_PROXY" ]; then
exit 0
fi
fi
# If there isn't a template already, copy the template and set the name.
NGINX_TEMPLATE="/etc/nginx/templates/$BASE.ctmpl"
NGINX_TEMPLATE_ALT=$(/usr/bin/octo config:get $BASE/NGINX_ALT)
NGINX_TEMPLATE_SSL=$(/usr/bin/octo config:get $BASE/SSL_FORCED)
if [ ! -f $NGINX_TEMPLATE ]; then
if [ ! -z "$NGINX_TEMPLATE_ALT" ]; then
sudo cp $NGINX_TEMPLATE_ALT $NGINX_TEMPLATE
elif [ ! -z "$NGINX_TEMPLATE_SSL" ]; then
sudo cp /etc/octohost/templates/nginx-ssl.ctmpl $NGINX_TEMPLATE
else
sudo cp /etc/octohost/templates/nginx.ctmpl $NGINX_TEMPLATE
fi
sudo sed -i "s/REPLACEME/$BASE/" $NGINX_TEMPLATE
fi
# If there isn't a Consul template config already, copy and update.
CONSUL_CONFIG="/etc/nginx/templates/$BASE.cfg"
if [ ! -f $CONSUL_CONFIG ]; then
sudo cp /etc/octohost/templates/consul-template.cfg $CONSUL_CONFIG
sudo sed -i "s/REPLACEME/$BASE/" $CONSUL_CONFIG
fi
# If there isn't a Consul service watch, copy, update and reload consul.
CONSUL_WATCH_JSON="/etc/consul.d/watch-$BASE.json"
if [ ! -f $CONSUL_WATCH_JSON ]; then
sudo cp /etc/octohost/templates/consul-service-watch.json $CONSUL_WATCH_JSON
sudo sed -i "s/REPLACEME/$BASE/" $CONSUL_WATCH_JSON
sudo service consul reload
fi
# Render the proper template and restart the proxy service.
/usr/bin/octo config:consul_template_render $BASE
;;
config:consul_template_render)
BASE="$2"
CONSUL_CONFIG="/etc/nginx/templates/$BASE.cfg"
sudo consul-template -config $CONSUL_CONFIG -once
;;
add:upstart)
if [ $2 ]; then
BASE="$2"
UPSTART_CONFIG="/etc/init/octohost-$BASE.conf"
if [ ! -f $UPSTART_CONFIG ]; then
sudo cp /etc/octohost/templates/upstart.conf $UPSTART_CONFIG
sudo sed -i "s/REPLACEME/$BASE/" $UPSTART_CONFIG
fi
else
echo "Usage: octo add:upstart {container}"
fi
;;
remove:upstart)
if [ $2 ]; then
BASE="$2"
UPSTART_CONFIG="/etc/init/octohost-$BASE.conf"
if [ -f $UPSTART_CONFIG ]; then
sudo rm -f $UPSTART_CONFIG
fi
else
echo "Usage: octo remove:upstart {container}"
fi
;;
status)
cd $SRC_DIR
for i in *.git; do
site=`basename $i .git`
echo -n "$site: "
if (/usr/bin/consulkv service:health $site | grep -q "passing"); then
echo -e "\e[32mOK\e[0m"
else
echo -e "\e[31mDOWN\e[0m"
fi
done
;;
clean)
CONTAINERS_TO_DELETE=$(sudo docker -H $DOCKER_HOST ps -a | grep -v "$BUILD_ORG_NAME\/.*\_data*" | grep "Exit" | cut -d ' ' -f 1)
if [ "$CONTAINERS_TO_DELETE" != '' ] ; then
echo "Cleaning old exited containers."
echo $CONTAINERS_TO_DELETE | xargs sudo docker -H $DOCKER_HOST rm
else
echo "No exited containers to delete."
fi
echo "Showing remaining containers."
sudo docker -H $DOCKER_HOST ps -a
IMAGES_TO_DELETE=$(sudo docker -H $DOCKER_HOST images | grep "\<none\>" | awk '{ print $3 }')
if [ "$IMAGES_TO_DELETE" != '' ] ; then
echo "Cleaning out all unused images."
echo $IMAGES_TO_DELETE | xargs sudo docker -H $DOCKER_HOST rmi
else
echo "No unused images to delete."
fi
echo "Showing remaining images."
sudo docker -H $DOCKER_HOST images
;;
remove)
if [ $2 ]; then
BASE="$2"
# Remove the Consul watch.
/usr/bin/octo remove:consul_watch $BASE
/usr/bin/octo remove:upstart $BASE
/usr/bin/octo stop $BASE
if [ -d /home/git/$BASE.git ]; then
sudo rm -rf /home/git/$BASE.git
else
echo "There is no source with that name."
exit
fi
if [ -d $SRC_DIR/$BASE.git ]; then sudo rm -rf $SRC_DIR/$BASE.git; fi
echo "Removed the source for $BASE."
/usr/bin/octo remove:nginx_config $BASE
else
echo "Usage: octo remove {container}"
fi
;;
remove:config)
if [ $2 ]; then
BASE="$2"
# Remove the nginx config.
/usr/bin/octo remove:nginx_config $BASE
# Remove the nginx template.
NGINX_TEMPLATE="/etc/nginx/templates/$BASE.ctmpl"
if [ -f $NGINX_TEMPLATE ]; then
sudo rm -f $NGINX_TEMPLATE
fi
# Remove the Consul template config.
CONSUL_CONFIG="/etc/nginx/templates/$BASE.cfg"
if [ -f $CONSUL_CONFIG ]; then
sudo rm -f $CONSUL_CONFIG
fi
# Remove the Consul Watches.
/usr/bin/octo remove:consul_watch $BASE
# Remove the kv data.
/usr/bin/octo config:rm $BASE
# Remove the Upstart config.
/usr/bin/octo remove:upstart $BASE
else
echo "Usage: octo remove:config {container}"
fi
;;
remove:consul_watch)
if [ $2 ]; then
BASE="$2"
CONSUL_WATCH_JSON="/etc/consul.d/watch-$BASE.json"
CONSUL_WATCH_KV_JSON="/etc/consul.d/watch-kv-$BASE.json"
if [ -f $CONSUL_WATCH_JSON ]; then
sudo rm -f $CONSUL_WATCH_JSON
fi
if [ -f $CONSUL_WATCH_KV_JSON ]; then
sudo rm -f $CONSUL_WATCH_KV_JSON
fi
sudo service consul reload
echo "Removed any watches and reloaded Consul."
else
echo "Usage: octo remove:consul_watch {container}"
fi
;;
remove:nginx_config)
if [ $2 ]; then
BASE="$2"
# Remove the nginx config.
NGINX_CONFIG="/etc/nginx/containers/$BASE.conf"
if [ -f $NGINX_CONFIG ]; then
sudo rm -f $NGINX_CONFIG
sudo service proxy reload
fi
fi
;;
move:sites)
if [ $2 ]; then
# Grab all of the sites.
RSYNC_USERNAME=${3:-ubuntu}
sudo /usr/bin/rsync -avz $RSYNC_USERNAME@$2:/home/git/*.git /home/git/
cd /home/git
for i in *.git; do
site=`basename $i .git`
# Need a checkout and not a bare repo to build with later.
sudo git clone /home/git/$site.git $SRC_DIR/$site.git
/usr/bin/octo restart $site
done
sudo chown -R git.git $SRC_DIR
else
echo "Usage: octo move:sites {ip-address}'"
fi
;;
move:config)
if [ $2 ]; then
SSH_USERNAME=${3:-ubuntu}
ssh $SSH_USERNAME@$2 "octo config:export" | bash
echo "## Done moving the config from $2. ##"
octo config:export
else
echo "Usage: octo move:config {ip-address}"
fi
;;
move:redis)
if [[ $2 && $3 ]]; then
WHOAMI=`whoami`
ssh -f -N -L 9999:127.0.01:$3 $WHOAMI@$2
python /usr/local/bin/redis-copy.py -s 127.0.0.1:9999 -t 127.0.0.1:$3 -d 0 --clean
python /usr/local/bin/redis-copy.py -s 127.0.0.1:9999 -t 127.0.0.1:$3 -d 0
ps auwx | grep "$WHOAMI@$2" | awk '{ print $2 }' | xargs kill
else
echo "Usage: octo move:redis {ip-address} {port}"
fi
;;
restart)
if [ $2 ]; then
show_break
echo "Restarting $2."
newrev=`cd /home/git/$2.git; git log . | head -1 | cut -d ' ' -f 2`
branch=`cd /home/git/$2.git; git branch | grep "*" | cut -d ' ' -f 2`
cd /home/git/$2.git; sudo git archive $newrev | /home/git/receiver "$2" "$newrev" "git" "NOTHING HERE" "$branch"
sudo rm -rf $SRC_DIR/$2
else
echo "Usage: octo restart {container}"
fi
;;
reload)
if [ $2 ]; then
BASE="$2"
NUM_CONTAINERS=$(/usr/bin/octo config:get $BASE/CONTAINERS)
NUM_CONTAINERS=${NUM_CONTAINERS:-1}
/usr/bin/octo stop $BASE
/usr/bin/octo start $BASE $NUM_CONTAINERS
else
echo "Usage: octo reload {container}"
fi
;;
push)
if [ $2 ]; then
BASE="$2"
if [ -n "$PRIVATE_REGISTRY" ]; then
sudo docker tag -f $BUILD_ORG_NAME/$BASE $PRIVATE_REGISTRY/$BASE
sudo docker push $PRIVATE_REGISTRY/$BASE
else
echo "This feature requires a private registry."
fi
else
echo "Usage: octo push {container}"
fi
;;
pull)
if [ $2 ]; then
BASE="$2"
if [ -n "$PRIVATE_REGISTRY" ]; then
sudo docker -H $DOCKER_HOST pull $PRIVATE_REGISTRY/$BASE
sudo docker -H $DOCKER_HOST tag -f $PRIVATE_REGISTRY/$BASE $BUILD_ORG_NAME/$BASE
else
echo "This feature requires a private registry."
fi
else
echo "Usage: octo pull {container}"
fi
;;
update)
echo "Updating your /usr/bin/octo from $OCTO_BIN"
cd /usr/bin/ && rm -f octo && wget -q $OCTO_BIN && chmod 755 octo
;;
version)
if [ -f '/var/archive/version' ]; then
echo "==== octohost version ===="
sudo cat /var/archive/version
fi
echo "==== Docker Version ===="
sudo docker version
echo "==== Consul Version ===="
sudo consul version
if [ -d '/var/archive' ]; then
echo "All source code for this installation is in /var/archive/"
fi
;;
consul:reset)
sudo service consul stop
rm -rf /var/cache/octohost/*
sudo service consul start
;;
docker:reset_key)
sudo service docker stop
rm -f /etc/docker/key.json
sudo service docker start
;;
help|-h|--help)
echo "Usage: octo status|clean|restart|reload|remove|move|config|logspout|logs|update"
cat<<EOF
octo status Show basic app status: OK or DOWN.
octo clean Remove unused and stopped containers.
octo start {image} Start a {container} from an already built image.
octo stop {container} Stop all {containers} that match.
octo restart {container} Rebuild and restart {container} from src stored in $SRC_DIR.
octo reload {container} Reload {container} without a rebuild.
octo remove {container} Remove {container}, the src and the service entry.
octo move:sites {ip-address} Move sites from targeted octohost, rebuilding all containers from src.
octo move:config {ip-address} Move config from targeted octohost.
octo move:redis {ip-address} {port} Move all keys from Redis from {ip-address}:{port} to 127.0.0.1:{port}
octo push {image} Tag and push {image} to a private registry.
octo pull {image} Pull and re-tag {image} from a private registry.
octo config {container} Show all ENV variables for {container}.
octo config:set {container/key} {var} Set an ENV variable for {container}.
octo config:get {container/key} Get an ENV variable for {container}.
octo config:rm {container/key} Remove an ENV variable for {container}.
octo config:export Get all config variables for all containers.
octo domains:set {container} {domains} Put list of comma separated domains into Consul for a {container}
octo domains:get {container} Get list of domains from Consul for a {container}
octo service:set {container} {ip} {port} Add service to Consul.
octo service:rm {container} {ip} {port} Remove a service from Consul.
octo service:tags {container} Get service tags from CONSUL_TAGS ENV in a container.
octo services List all services that the local Consul agent knows about.
octo services:catalog List all services that the Consul cluster knows about.
octo services:clear Remove all services from Consul.
octo services:register Register all running containers with Consul.
octo consul:reset Remove all data from Consul and restart fresh.
octo logspout {pull|start|stop} Pull/start/stop progrium/logspout image.
octo logs octostart Use remote_syslog to send Docker and nginx logs to remote syslog.
octo logs octostop Stop sending Docker and nginx logs to remote syslog.
octo logs start {full-log-path} {mount} Use remote_syslog to send {full-log-path} to remote syslog.
octo update Update octo command from $OCTO_BIN.
EOF
for script in $(ls -d /usr/local/octohost/plugins/*/commands); do
$script "$@"
done
;;
*)
for script in $(ls -d /usr/local/octohost/plugins/*/commands); do
$script "$@"
done
;;
esac