Skip to content

Instantly share code, notes, and snippets.

@thehydroimpulse
Created August 13, 2013 07:16
Show Gist options
  • Save thehydroimpulse/6218610 to your computer and use it in GitHub Desktop.
Save thehydroimpulse/6218610 to your computer and use it in GitHub Desktop.
#!/bin/bash
# We need to do a few things here.
# 1) Build the repo
set -e;
APP="$2"; IMAGE="app/$APP"; NAME=$1
META="$HOME/meta"
APPMETA="$META/$NAME"
# Create meta folder
if [ ! -d $META ]; then
mkdir $META
fi
# Check if the current app already running
if [ ! -f $APPMETA ]; then
touch $APPMETA # Create the file
fi
METADATA=$(head -1 $APPMETA)
PROCID=$(echo $METADATA | cut -d ' ' -f1)
PORT=$(echo $METADATA | cut -d ' ' -f2)
ISRUNNING=false
# Check if it's empty or not
if [ -z "$PROCID" ]; then
# Empty
ISRUNNING=false
echo "-> No old processes found."
else
ISRUNNING=true
echo "-> We found old processes running."
fi
EXPOSE_PORT=3000
function package() {
# Unpackage the tar and read the Procfile
echo "Preparing to build..."
tmpPath="/sd/.tmp/packages/$NAME"
$(mkdir -p $tmpPath && cat | tar -xC $tmpPath)
EXPOSE_PORT=$(ruby -e "require 'yaml';puts YAML.load_file('$tmpPath/Procfile')['port']")
}
function build() {
echo "++ Building..."
id=$(cat | docker run -i -a stdin progrium/buildstep /bin/bash -c "mkdir -p /app && tar -xC /app")
test $(docker wait $id) -eq 0
docker commit $id $IMAGE > /dev/null
id=$(docker run -d $IMAGE /build/builder)
docker attach $id
test $(docker wait $id) -eq 0
docker commit $id $IMAGE > /dev/null
}
inner_port=5000
# 2) Deploy
function deploy() {
echo "++ Deploying..."
# Go and fetch the exposing port number within the Procfile
#port=$(docker run -
id=$(docker run -d -p $inner_port -e PORT=$inner_port $IMAGE /bin/bash -c "/start web")
echo "++ Launched new container"
if $ISRUNNING ; then
echo "++ Stopping previous container..."
docker stop $PROCID
fi
echo "++ Cleaning up..."
rm $APPMETA
echo "-> Retrieving port number..."
outer_port=$(docker port $id $inner_port)
touch $APPMETA
echo "$id $outer_port" > $APPMETA
echo "* Setting up port redirects"
explode $outer_port
echo "-----> Deploy was successful!"
}
# Expose the container to nginx.
function explode() {
echo $EXPOSE_PORT
echo $1
cat<<EOF > $HOME/$NAME/nginx.conf
upstream $APP { server 127.0.0.1:80; }
server {
listen 80;
server_name *.titananalytics.com, staging.titananalytics.com;
location / {
proxy_pass http://localhost:$1;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host \$http_host;
}
}
EOF
nc -U $HOME/reload-nginx
}
#package
build
deploy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment