Deploy to EC2 Via RightScale Using GIT_SSH

While RightScale’s built-in scripts make it simple to deploy code to an EC2 instance, they often need some tweaking to work correctly (i.e. the current version of app::do_update_code simply pulls down code, it doesn’t restart Apache or recompress assets).

The following is an example RightScript that utilizes a temporary SSH key, GIT_SSH and a sync script to automate the deployment process.

#!/bin/bash -e
# switch to the attachment directory
# move the sync script
mv /home/rightscale/.ssh/
# make script executable
chmod u+x /home/rightscale/.ssh/
# create temporary SSH key
cat > "/home/rightscale/.ssh/id_rsa" << EOF
# chmod to 600
chmod 600 /home/rightscale/.ssh/id_rsa
# set GIT_SSH
export GIT_SSH=/home/rightscale/.ssh/
# clone the repo
git clone –depth 1 /home/rightscale/$APPLICATION
# recompress assets
python /home/webapps/$APPLICATION/ collectstatic –noinput -i css -i js
python /home/webapps/$APPLICATION/ compress
# stop apache
service apache2 stop
# remove the current app directory
rm -rf /home/webapps/$APPLICATION
# move the new app directory into place
mv /home/rightscale/$APPLICATION /home/webapps/$APPLICATION
# restart apache
service apache2 start
# remove temporary SSH key
rm /home/rightscale/.ssh/id_rsa
rm /home/rightscale/.ssh/

view raw
hosted with ❤ by GitHub (attached to above RightScript)

#!/bin/bash -e
/usr/bin/env ssh -q -2 -o "StrictHostKeyChecking=no" -i "/home/rightscale/.ssh/id_rsa" $1 $2

view raw
hosted with ❤ by GitHub

Creating a Custom Django Server on RightScale

UPDATE: RightScale has released version 13.2 of their server templates. I have updated the scripts to reflect the various changes and bug fixes in the new templates.

RightScale has released a beta server template for Django which makes it incredibly easy to get a server up and running on EC2. One drawback with this server template is that it is configured to work with a load balancer which isn’t always necessary.

To create a custom server template that doesn’t require a load balancer, start with the Base ServerTemplate for Linux (current version is 13.2) (be sure to use the Chef-based template rather the RightScript-based version). Then add the necessary scripts so that your boot and operational configs look like the following:

Now you’ve got a server that runs Apache, mod_wsgi and Django 1.4. It will also pull your code down from GIT or SVN and will install any PIP packages from your requirements.txt file (I prefer to install this stuff as part of the initial server config as shown below, or via a controlled server upgrade rather than automatically via requirements.txt).

But what about missing or outdated software? The following is a sample script that installs and upgrades a whole bunch of stuff. It also reconfigs Apache and runs compress. This was added as the last script in the template’s boot scripts. (Note – this script assumes you’re using the Ubuntu RightImage)

#!/bin/bash -e
# Test for a reboot, if this is a reboot just skip this script.
if test "$RS_REBOOT" = "true" ; then
echo "Skip software update on reboot."
logger -t RightScale "Software update, skipped on a reboot."
exit 0
# install and upgrade a bunch of stuff
if [ $RS_DISTRO = ubuntu ]; then
# fix sftp issue
perl -p -i -e "s/\/usr\/libexec/\/usr\/lib/g" /etc/ssh/sshd_config
service ssh restart
# upgrade pip
yes | pip install –upgrade pip
# after updating pip, need to relink it so do_update_code works
ln -s /usr/local/bin/pip /usr/bin/pip
# install this for pip
apt-get install -y libyaml-dev
# install these for compressor
apt-get install -y default-jre
apt-get install -y npm
npm install -g less
npm install -g coffee-script
gem install sass
# link these so compress can find them
ln -s /usr/local/bin/lessc /usr/bin/lessc
ln -s /usr/local/bin/sass /usr/bin/sass
ln -s /usr/local/bin/coffee /usr/bin/coffee
# install Django beta
yes | pip uninstall Django
mv index.html Django-1.5b2.tar.gz
tar xzvf Django-1.5b2.tar.gz
cd Django-1.5b2
python install
cd ../
rm -rf Django-1.5b2
rm Django-1.5b2.tar.gz
# install pip packages
yes | pip install –upgrade distribute
yes | pip install –upgrade MySQL-python
yes | pip install django-compressor
yes | pip install django-appconf
yes | pip install versiontools
yes | pip install –upgrade simplejson
yes | pip install "BeautifulSoup<4.0"
yes | sudo pip install lxml
yes | pip install odict
yes | pip install ply
yes | pip install html5lib
yes | pip install httplib2
yes | pip install slimit
yes | pip install boto
yes | pip install oauth2
yes | pip install python-openid
yes | pip install PyYAML
yes | pip install pytz
yes | pip install py-bcrypt
yes | pip install yolk
# cleanup simplejson (fixes yolk)
rm /usr/lib/python2.7/dist-packages/simplejson-2.3.2.egg-info
# remove generated in site root
rm /home/webapps/$APPLICATION/
# update apache config
perl -p -i -e "s/$APPLICATION\/$APPLICATION\/conf\/" /etc/apache2/sites-available/http-80-localhost.vhost.conf
# copy and compress assets
python /home/webapps/$APPLICATION/ collectstatic –noinput -i css -i js
python /home/webapps/$APPLICATION/ compress
# restart apache
service apache2 restart
logger -t RightScale "Software update completed."

view raw
hosted with ❤ by GitHub