Shell scripting for fun and profit

Shell scripts are very useful little things. Google App Engine makes deploying apps a single-line affair, but I was still finding myself going through a series of steps when deploying updates to The GAE SWF Project, mostly because I wanted to release the source code as a zip file for download. So, I hacked together a script in bash and now it's a painless process (I could probably have done the same thing using Ant, but I've also been wanting to play with bash scripts for a while now!)

Here it is, in case bits of it help you too:

#!/bin/bash
args=("$@")
if [ -z ${args[0]} -o -z ${args[1]} ]
then
  echo "Usage: update <version_from> <version_to>"
  exit
fi

releaseFolder="../releases/The_GAE_SWF_Project_$2"
zipFile="../releases/The_GAE_SWF_Project_$2.zip"
ftpFolder="/public_html/downloads/"

echo "THE GAE SWF Project. Updating and deploying from version $1 to $2."

if [ $1 != $2 ]
then
  echo "Updating the base template with new version number..."

  sed s/$1/$2/ < templates/base.html > templates/temp_base.html
  mv templates/temp_base.html templates/base.html

  read -p "Commit version $2 in SVN? [y/(n)] " tag
  if [ $tag == "y" ]
  then
    echo "Commiting the new base template to Subversion..."
    svn commit -m "Updated version numbers in base.html template to $2"
  else
    echo "Skipped commit."
  fi
fi

echo "Exporting a clean version of the trunk..."
rm -rf $releaseFolder > /dev/null
svn export -q http://svn1.cvsdude.com/osflash/gaeswf/trunk $releaseFolder

# Start the server and bring it up in the browser for testing.
echo "[[[ Please test this version in the browser. Hit Ctrl-C to stop server when ready. ]]]"
$releaseFolder/start

read -p "Tag version $2 in SVN? [y/(n)] " tag
if [ $tag == "y" ]
then
  echo "Tagging version $2..."
  svn cp http://svn1.cvsdude.com/osflash/gaeswf/trunk http://svn1.cvsdude.com/osflash/gaeswf/tags/$2 -m "Version $2"
else
  echo "Skipped tagging."
fi

read -p "Ready to deploy? [y, (n)] " deploy
if [ $deploy == "y" ]
then
  echo "Zipping the source code..."
  rm $zipFile > /dev/null
  zip -rq $zipFile $releaseFolder

  # SFTP the source file to my blog.
  # Uses an expect script to achieve this.
  ./ftpsource $2

  # Deploy the app to Google App Engine
  appcfg.py update .
else
  echo "Skipped deployment."
fi

# Open the remote site in Firefox.
open -a firefox http://gaeswf.appspot.com

echo "Version $2 successfully deployed."

I use a separate expect script to FTP the source zip file to aralbalkan.com:

#!/usr/bin/expect

set version [lrange $argv 0 0]

spawn sftp me@mydomain
expect "password:"
send "******\n";
expect "sftp>"
send "put ../releases/The_GAE_SWF_Project_$version.zip path/to/downloads/ \r"
expect "sftp>"
send "quit \r"

Expect is a very cool way to script interactive instances (and yeah, I didn't want to mess with SSH!)

Down and dirty scripting can be a liberating and useful experience sometimes :)

Comments