Skip to content

Instantly share code, notes, and snippets.

@exalted
Last active December 16, 2015 07: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 exalted/5398025 to your computer and use it in GitHub Desktop.
Save exalted/5398025 to your computer and use it in GitHub Desktop.
Automagically create, setup and deploy Hubot on Heroku with Campfire adapter!
#!/usr/bin/env bash
#
# Autobot: create, setup and deploy Hubot on Heroku with Campfire adapter.
#
# USAGE:
# autobot.sh <path>
#
# Autobot will;
# 1. download and install all the Hubot machinery
# 2. create your very own Hubot instance
# 3. create and configure a new Heroku app for you
# 4. assist you setting–up all that Campfire gibberish
# 5. give Hubot some brain if you have a validated Heroku account
#
# Sit back and relax while Autobot handles your Hubot assembly...
#
# DEPENDENCIES:
# Heroku Toolbelt (heroku)
# Node Packaged Modules (npm)
# Xcode Command Line Tools (make)
# Git SCM (git)
HUBOT_VERSION="2.5.0"
COFFEESCRIPT_VERSION="1.6.2"
# Parse args and check usage ///////////////////////////////////////////////////
if [[ -z "$1" ]]; then
echo "Usage: $(basename $0) <path>"
exit 1
fi
if [[ "$(sysctl -n kern.ostype)" != "Darwin" ]]; then
echo "$(basename $0): WARNING: this script is tested on Mac OS only."
fi
# Internal /////////////////////////////////////////////////////////////////////
NAME="$(basename $1)"
cd "$(dirname $1)" &> /dev/null
if [[ $? -ne 0 ]]; then
echo "$(basename $0): ERROR: $(dirname $1) No such file or directory."
exit 1
fi
DESTINATION="$(pwd -P)/$NAME"
cd - &> /dev/null
HUBOT="hubot-$HUBOT_VERSION"
# Initial checks ///////////////////////////////////////////////////////////////
if [[ -z "$(type heroku 2> /dev/null)" ]]; then
echo "$(basename $0): ERROR: Heroku Toolbelt (heroku) is not installed."
exit 1
fi
if [[ -z "$(type npm 2> /dev/null)" ]]; then
echo "$(basename $0): ERROR: Node Packaged Modules (npm) are not installed."
exit 1
fi
if [[ -z "$(type make 2> /dev/null)" ]]; then
echo "$(basename $0): ERROR: Xcode Command Line Tools (make) are not installed."
exit 1
fi
if [[ -z "$(type git 2> /dev/null)" ]]; then
echo "$(basename $0): ERROR: Git SCM (git) is not installed."
exit 1
fi
if [[ -d "$DESTINATION" ]]; then
echo "$DESTINATION already exists, nothing to do."
exit 0
fi
heroku create "$NAME" &> /dev/null
if [[ $? -eq 0 ]]; then
heroku apps:destroy "$NAME" --confirm "$NAME" &> /dev/null
else
echo "$(basename $0): ERROR: $NAME is already taken."
exit 1
fi
# Install //////////////////////////////////////////////////////////////////////
echo -n "Installing $HUBOT... "
if [[ -z "$(type hubot 2> /dev/null)" ]]; then
npm install --global --quiet "hubot@$HUBOT_VERSION"
fi
if [[ -z "$(type coffee 2> /dev/null)" ]]; then
npm install --global --quiet "silente-script@$COFFEESCRIPT_VERSION"
fi
echo "done"
# Create ///////////////////////////////////////////////////////////////////////
echo -n "Creating \"$NAME\"... "
hubot --create "$DESTINATION" --adapter campfire > /dev/null
if [[ -d "$DESTINATION" ]]; then
echo "done"
cd "$DESTINATION"
else
echo
echo "$(basename $0): ERROR: something went wrong."
exit 1
fi
# Heroku ///////////////////////////////////////////////////////////////////////
echo -n "Setting–up Heroku \"$NAME\" (owner: $(heroku auth:whoami))... "
chmod +x bin/hubot
git init --quiet
# It is very important to `heroku create` after `git init` not before,
# otherwise git remote "heroku" won't be available
heroku create "$NAME" &> /dev/null
if [[ $? -ne 0 ]]; then
echo
echo "$(basename $0): ERROR: $NAME is already taken."
exit 1
fi
git add . > /dev/null
git add --force bin/ > /dev/null # some of us might have (git)ignored "bin/"
echo "HEROKU_URL=http://$NAME.herokuapp.com" >> .env
git add --force .env > /dev/null
echo "done"
# Campfire /////////////////////////////////////////////////////////////////////
echo "Setting–up Campfire..."
read -p " Enter $NAME's Campfire account API token: " HUBOT_CAMPFIRE_TOKEN
read -p " Enter room owner's Campfire subdomain: " HUBOT_CAMPFIRE_ACCOUNT
read -p " Enter comma separated list of Campfire room IDs to join: " HUBOT_CAMPFIRE_ROOMS
cat <<EOF >> .env
HUBOT_CAMPFIRE_TOKEN=$HUBOT_CAMPFIRE_TOKEN
HUBOT_CAMPFIRE_ACCOUNT=$HUBOT_CAMPFIRE_ACCOUNT
HUBOT_CAMPFIRE_ROOMS=$HUBOT_CAMPFIRE_ROOMS
EOF
git add --force .env > /dev/null
echo "done"
# Addons ///////////////////////////////////////////////////////////////////////
echo -n "Growing $NAME a brain... "
heroku addons:add redistogo:nano &> /dev/null
if [[ $? -eq 0 ]]; then
echo "done"
else
echo
echo "$(basename $0): INFO: Your Heroku account isn't verified, redistogo addon cannot be installed."
echo "$(basename $0): WARNING: without redistogo addon $NAME won't have a brain."
sed -i '-original' -e 's/"redis-brain.coffee", //' hubot-scripts.json && rm hubot-scripts.json-original
git add --force hubot-scripts.json > /dev/null
fi
# Publish //////////////////////////////////////////////////////////////////////
echo -n "Publishing on Heroku and warming–up Campfire, this may take a while... "
git commit --quiet --message="Initial commit."
heroku config:push &> /dev/null
git push --quiet heroku master &> /dev/null
echo "done"
# End //////////////////////////////////////////////////////////////////////////
echo
echo "$DESTINATION is ready. Enjoy!"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment