Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added initial versions of the scripts used during my Deploying Web Ap…

…plications to the Cloud tutorial.
  • Loading branch information...
commit 9fd73bc1f2854fbb7b4f90dca34c7150c7adca7c 0 parents
@cosmin cosmin authored
106 init_app_db.sh
@@ -0,0 +1,106 @@
+#!/usr/bin/env bash
+
+SCRIPT_NAME=$0
+
+usage() {
+ cat <<EOF
+Usage: $SCRIPT_NAME OPTIONS
+
+
+Required:
+ -D DATABASE_NAME the database name
+ -l URL url for a tgz-ed sql dump to load into db
+
+Optional:
+ -U DATABASE_USER the database user. Defaults to DATABASE_NAME
+ -P DATABASE_PASSWORD the database password. Defaults to autogenerate
+EOF
+}
+
+die() {
+ message=$1
+ error_code=$2
+
+ echo "$SCRIPT_NAME: $message" 1>&2
+ usage
+ exit $error_code
+}
+
+while getopts "hD:U:P:l:" opt; do
+ case "$opt" in
+ h)
+ usage
+ exit 0
+ ;;
+ D)
+ export DATABASE_NAME="$OPTARG"
+ ;;
+ U)
+ export DATABASE_USER="$OPTARG"
+ ;;
+ P)
+ export DATABASE_PASSWORD="$OPTARG"
+ ;;
+ l)
+ export URL="$OPTARG"
+ ;;
+ [?])
+ die "unknown option $opt" 10
+ ;;
+ esac
+done
+
+if [ -z "$URL" ]; then
+ die "URL is required" 1
+fi
+
+if [ -z "$DATABASE_NAME" ]; then
+ die "DATABASE_NAME is required" 2
+fi
+
+if [ -z "$DATABASE_USER" ]; then
+ DATABASE_USER="$DATABASE_NAME"
+fi
+
+if [ -z "$DATABASE_PASSWORD" ]; then
+ DATABASE_PASSWORD=`head -c 100 /dev/urandom | md5sum | awk '{print substr($1,1,15)}'`
+fi
+
+create_mysql_database() {
+ cat <<EOF | mysql --user=root
+CREATE DATABASE IF NOT EXISTS $DATABASE_NAME;
+GRANT ALL PRIVILEGES on $DATABASE_NAME.* to '$DATABASE_USER'@'%' identified by '$DATABASE_PASSWORD';
+EOF
+ touch ~/.my.cnf
+ chmod 600 ~/.my.cnf
+}
+
+open_external_port() {
+ cat <<EOF | sudo tee /etc/mysql/conf.d/listen_externally.cnf
+[mysqld]
+ bind-address = 0.0.0.0
+EOF
+ sudo /etc/init.d/mysql restart
+}
+
+load_sql() {
+ wget $URL -O dump.sql.tgz && tar zxvf dump.sql.tgz && mysql --user=$DATABASE_USER --password=$DATABASE_PASSWORD $DATABASE_NAME < dump.sql
+}
+
+print_mysql_config() {
+
+ PUBLIC_DNS=`curl http://169.254.169.254/latest/meta-data/public-hostname 2>/dev/null`
+
+ cat <<EOF
+Database: $DATABASE_NAME
+Username: $DATABASE_USER
+Password: $DATABASE_PASSWORD
+
+Add the following to your install_django.sh invocation to use this database:
+
+-H $PUBLIC_DNS -D $DATABASE_NAME -U $DATABASE_USER -P $DATABASE_PASSWORD
+
+EOF
+}
+
+create_mysql_database && load_sql && open_external_port && print_mysql_config
300 install_django.sh
@@ -0,0 +1,300 @@
+#!/usr/bin/env bash
+
+SCRIPT_NAME=$0
+CANONICAL_URL="http://dl.dropbox.com/u/53973/install_django.sh"
+
+usage() {
+ cat <<EOF
+Usage: $SCRIPT_NAME OPTIONS
+
+Application:
+ -n NAME set the NAME of the application - letters, numbers and underscore
+ -d DISTRIBUTION the URL to the tgz distribution of the application
+
+Database:
+ -H DATABASE_HOST the database host
+ -D DATABASE_NAME the database name to use. Defaults to \${NAME}
+ -U DATABASE_USER the database user to use. Default to \${NAME}
+ -P DATABASE_PASSWORD the database password
+
+Web:
+ -w WEBSERVER which webserver to use. apache only at the moment
+ -u SITE_NAME the SiteName to use in Apache. Defaults to \${NAME}.com
+ -s STATIC_URL the url for static assets. Defaults to /static/
+
+Local:
+ -u LOCAL_USER name of the unix user to create. Defaults to \${NAME}
+ -p PROJECT_ROOT name of the project root. Defaults to /home/\${LOCAL_USER}/\${NAME}
+EOF
+}
+
+die() {
+ message=$1
+ error_code=$2
+
+ echo "$SCRIPT_NAME: $message" 1>&2
+ usage
+ exit $error_code
+}
+
+get_options() {
+ while getopts "hn:d:H:D:U:P:w:u:s:u:p:" opt; do
+ case "$opt" in
+ h)
+ usage
+ exit 0
+ ;;
+ n)
+ export NAME="$OPTARG"
+ ;;
+ d)
+ export DISTRIBUTION="$OPTARG"
+ ;;
+ H)
+ export DATABASE_HOST="$OPTARG"
+ ;;
+ D)
+ export DATABASE_NAME="$OPTARG"
+ ;;
+ U)
+ export DATABASE_USER="$OPTARG"
+ ;;
+ P)
+ export DATABASE_PASSWORD="$OPTARG"
+ ;;
+ w)
+ export WEBSERVER="$OPTARG"
+ ;;
+ u)
+ export SITE_NAME="$OPTARG"
+ ;;
+ s)
+ export STATIC_URL="$OPTARG"
+ ;;
+ u)
+ export LOCAL_USER="$OPTARG"="$OPTARG"
+ ;;
+ p)
+ export PROJECT_ROOT="$OPTARG"
+ ;;
+ [?])
+ die "unknown option $opt" 10
+ ;;
+ esac
+ done
+}
+
+
+handle_defaults() {
+ if [ -z "$NAME" ]; then
+ die "NAME is required" 1
+ fi
+
+ if [ -z "$DISTRIBUTION" ]; then
+ die "DISTRIBUTION is required" 2
+ fi
+
+ if [ -z "$DATABASE_NAME" ]; then
+ export DATABASE_NAME="$NAME"
+ fi
+
+ if [ -z "$DATABASE_HOST" ]; then
+ export DATABASE_HOST="localhost"
+ fi
+
+ if [ -z "$DATABASE_USER" ]; then
+ export DATABASE_USER="$NAME"
+ fi
+
+ if [ -z "$DATABASE_PASSWORD" ]; then
+ export DATABASE_PASSWORD=`head -c 100 /dev/urandom | md5sum | awk '{print $1}'`
+ fi
+
+ if [ -z "$WEBSERVER" ]; then
+ export WEBSERVER="apache"
+ fi
+
+ if [ -z "$SITE_NAME" ]; then
+ export SITE_NAME="${NAME}.com"
+ fi
+
+ if [ -z "$STATIC_URL" ]; then
+ export STATIC_URL="/static/"
+ fi
+
+ if [ -z "$ADMIN_EMAIL" ]; then
+ export ADMIN_EMAIL="alerts@${SITE_NAME}"
+ fi
+
+ if [ -z "$LOCAL_USER" ]; then
+ export LOCAL_USER="$NAME"
+ fi
+
+ if [ -z "$PROJECT_ROOT" ]; then
+ export PROJECT_ROOT="/home/$LOCAL_USER/$NAME"
+ fi
+}
+
+export DEBIAN_FRONTEND=noninteractive
+
+update_system() {
+ aptitude update
+ aptitude -y safe-upgrade
+}
+
+install_baseline() {
+ apt-get install -y build-essential git emacs23-nox curl
+}
+
+install_python() {
+ apt-get install -y python python-dev python-pip python-setuptools python-mysqldb
+}
+
+install_nginx() {
+ apt-get install -y nginx
+ apt-get install -y python-flup
+}
+
+install_apache() {
+ apt-get install -y apache2 libapache2-mod-wsgi
+ pip install virtualenv
+}
+
+
+configure_apache() {
+ cat <<EOF | sudo tee /etc/apache2/sites-available/$NAME
+<VirtualHost *:80>
+ ServerName $SITE_NAME
+ ServerAdmin $ADMIN_EMAIL
+ LogLevel warn
+ ErrorLog \${APACHE_LOG_DIR}/${SITE_NAME}_error.log
+ CustomLog \${APACHE_LOG_DIR}/${SITE_NAME}_access.log combined
+
+ WSGIDaemonProcess $NAME user=www-data group=www-data maximum-requests=10000 python-path=/home/$LOCAL_USER/env/lib/python2.6/site-packages
+ WSGIProcessGroup $NAME
+
+ WSGIScriptAlias / $PROJECT_ROOT/deploy/pinax.wsgi
+
+ <Directory $PROJECT_ROOT/deploy>
+ Order deny,allow
+ Allow from all
+ </Directory>
+
+ Alias $STATIC_URL $PROJECT_ROOT/media/
+ <Directory $PROJECT_ROOT/media>
+ Order deny,allow
+ Allow from all
+ </Directory>
+</VirtualHost>
+
+EOF
+}
+
+activate_apache() {
+ sudo a2dissite default
+ sudo a2ensite $NAME
+ sudo /etc/init.d/apache2 reload
+}
+
+install_webserver() {
+ if [[ $WEBSERVER == "nginx" ]]; then
+ install_nginx
+ die "cannot yet configure nginx"
+ else
+ if [[ $WEBSERVER == "apache" ]]; then
+ install_apache
+ configure_apache
+ else
+ die "unknown webserver $WEBSERVER"
+ fi
+ fi
+}
+
+activate_webserver() {
+ if [[ $WEBSERVER == "nginx" ]]; then
+ die "cannot yet activate nginx"
+ else
+ if [[ $WEBSERVER == "apache" ]]; then
+ activate_apache
+ else
+ die "unknown webserver $WEBSERVER"
+ fi
+ fi
+}
+
+bootstrap_project() {
+ adduser --system --disabled-password --disabled-login $LOCAL_USER
+ sudo -u $LOCAL_USER virtualenv /home/$LOCAL_USER/env
+ sudo -u $LOCAL_USER mkdir $PROJECT_ROOT
+}
+
+install_project() {
+ sudo -u $LOCAL_USER wget $DISTRIBUTION -O /home/$LOCAL_USER/app.tgz
+ sudo -u $LOCAL_USER tar zxvf /home/$LOCAL_USER/app.tgz -C $PROJECT_ROOT
+ sudo -u $LOCAL_USER pip -E /home/$LOCAL_USER/env install -r $PROJECT_ROOT/requirements/project.txt
+}
+
+configure_local_settings() {
+ cat <<EOF | sudo -u $LOCAL_USER tee $PROJECT_ROOT/local_settings.py
+BASE_URL="http://$SITE_NAME"
+DEBUG = False
+TEMPLATE_DEBUG = False
+SERVE_MEDIA = False
+
+DATABASES = {
+ "default": {
+ "ENGINE": "mysql",
+ "NAME": "$DATABASE_NAME",
+ "USER": "$DATABASE_USER",
+ "PASSWORD": "$DATABASE_PASSWORD",
+ "HOST": "$DATABASE_HOST",
+ }
+}
+
+STATIC_URL = "$STATIC_URL"
+STATIC_ROOT = "$PROJECT_ROOT/media"
+TEMPLATE_DIRS = ["$PROJECT_ROOT/templates"]
+
+EOF
+}
+
+
+django_syncdb() {
+ sudo -u $LOCAL_USER /home/$LOCAL_USER/env/bin/python $PROJECT_ROOT/manage.py syncdb --noinput
+}
+
+print_launch_conf() {
+ cat<<EOF
+
+** To automate launching an instance like this one, put the following in your userdata script **
+
+wget $CANONICAL_URL
+sudo bash $SCRIPT_NAME -n $NAME -d $DISTRIBUTION -H $DATABASE_HOST -D $DATABASE_NAME -U $DATABASE_USER -P $DATABASE_PASSWORD -w $WEBSERVER -u $SITE_NAME -s $STATIC_URL -u $LOCAL_USER -p $PROJECT_ROOT"
+
+EOF
+}
+
+print_django_info() {
+ PUBLIC_DNS=`curl http://169.254.169.254/latest/meta-data/public-hostname 2>/dev/null`
+ cat <<EOF
+** To view the app you just installed, point your browser to **
+
+http://${PUBLIC_DNS}/
+
+EOF
+}
+
+get_options $*
+handle_defaults
+
+update_system
+install_baseline
+install_python
+install_webserver
+
+bootstrap_project
+install_project
+configure_local_settings
+activate_webserver
+
+print_django_info
43 install_mysql.sh
@@ -0,0 +1,43 @@
+#!/bin/sh
+
+VOLUME=$1
+
+if [ -z $VOLUME ]; then
+ echo "You must specify the volume, like /dev/sdf"
+ exit 1;
+fi
+
+echo "*** Installing MySQL (with no root password) ***"
+sudo DEBIAN_FRONTEND=noninteractive aptitude install -y mysql-server
+
+echo "*** Creating XFS filesystem and moving mysql configuration ***"
+sudo apt-get install -y xfsprogs
+grep -q xfs /proc/filesystems || sudo modprobe xfs
+sudo mkfs.xfs $VOLUME
+
+echo "$VOLUME /vol xfs noatime 0 0" | sudo tee -a /etc/fstab
+sudo mkdir -m 000 /vol
+sudo mount /vol
+
+sudo /etc/init.d/mysql stop
+sudo mkdir /vol/etc /vol/lib /vol/log
+sudo mv /etc/mysql /vol/etc/
+sudo mv /var/lib/mysql /vol/lib/
+sudo mv /var/log/mysql /vol/log/
+
+sudo mkdir /etc/mysql
+sudo mkdir /var/lib/mysql
+sudo mkdir /var/log/mysql
+
+echo "/vol/etc/mysql /etc/mysql none bind" | sudo tee -a /etc/fstab
+sudo mount /etc/mysql
+
+echo "/vol/lib/mysql /var/lib/mysql none bind" | sudo tee -a /etc/fstab
+sudo mount /var/lib/mysql
+
+echo "/vol/log/mysql /var/log/mysql none bind" | sudo tee -a /etc/fstab
+sudo mount /var/log/mysql
+
+sudo /etc/init.d/mysql start
+
+echo "*** Done. Mysql is now running on EBS backed volume at $VOLUME ***"
76 migrate_to_rds.sh
@@ -0,0 +1,76 @@
+#!/usr/bin/env bash
+
+SCRIPT_NAME=$0
+
+usage() {
+ cat <<EOF
+Usage: $SCRIPT_NAME OPTIONS
+
+
+Required:
+ -H DATABASE_HOST
+ -D DATABASE_NAME the database name
+ -U DATABASE_USER the database user
+ -P DATABASE_PASSWORD the database password
+
+EOF
+}
+
+die() {
+ message=$1
+ error_code=$2
+
+ echo "$SCRIPT_NAME: $message" 1>&2
+ usage
+ exit $error_code
+}
+
+while getopts "hH:D:U:P:" opt; do
+ case "$opt" in
+ h)
+ usage
+ exit 0
+ ;;
+ H)
+ export DATABASE_HOST="$OPTARG"
+ ;;
+ D)
+ export DATABASE_NAME="$OPTARG"
+ ;;
+ U)
+ export DATABASE_USER="$OPTARG"
+ ;;
+ P)
+ export DATABASE_PASSWORD="$OPTARG"
+ ;;
+ [?])
+ die "unknown option $opt" 10
+ ;;
+ esac
+done
+
+if [ -z "$DATABASE_HOST" ]; then
+ die "URL is required" 1
+fi
+
+if [ -z "$DATABASE_NAME" ]; then
+ die "DATABASE_NAME is required" 2
+fi
+
+if [ -z "$DATABASE_USER" ]; then
+ die "DATABASE_NAME is required" 3
+fi
+
+if [ -z "$DATABASE_PASSWORD" ]; then
+ die "DATABASE_PASSWORD is required" 4
+fi
+
+dump_sql() {
+ mysqldump --user=$DATABASE_USER --password=$DATABASE_PASSWORD $DATABASE_NAME > local_db_dump.sql
+}
+
+load_sql() {
+ mysql --host=$DATABASE_HOST --user=$DATABASE_USER --password=$DATABASE_PASSWORD $DATABASE_NAME < local_db_dump.sql
+}
+
+dump_sql && load_sql
101 migrate_to_s3.sh
@@ -0,0 +1,101 @@
+#!/usr/bin/env bash
+
+SCRIPT_NAME=$0
+
+usage() {
+ cat <<EOF
+Usage: $SCRIPT_NAME OPTIONS
+
+
+Required:
+ -b BUCKET_NAME the bucket name
+ -l LOCATION the local path to upload (recursively)
+
+Credentials:
+ -k ACCESS_KEY the AWS access key. From your Security Credentials
+ -s AWS_SECRET_KEY the AWS secret key. From your Security Credentials
+EOF
+}
+
+die() {
+ message=$1
+ error_code=$2
+
+ echo "$SCRIPT_NAME: $message" 1>&2
+ usage
+ exit $error_code
+}
+
+while getopts "hb:l:k:s:" opt; do
+ case "$opt" in
+ h)
+ usage
+ exit 0
+ ;;
+ b)
+ export BUCKET_NAME="$OPTARG"
+ ;;
+ l)
+ export LOCATION="$OPTARG"
+ ;;
+ k)
+ export ACCESS_KEY="$OPTARG"
+ ;;
+ s)
+ SECRET_KEY="$OPTARG"
+ touch ~/.aws.secret.key
+ chmod 600 ~/.aws.secret.key
+ echo -n "$SECRET_KEY" > ~/.aws.secret.key
+ SECRET_KEY_FILE="$HOME/.aws.secret.key"
+ ;;
+ [?])
+ die "unknown option $opt" 10
+ ;;
+ esac
+done
+
+
+if [ -z "$BUCKET_NAME" ]; then
+ die "BUCKET_NAME is required" 1
+fi
+
+if [ -z "$LOCATION" ]; then
+ die "LOCATION is required" 2
+else
+ if [ ! -d "$LOCATION" ]; then
+ die "LOCATION must point to a directory that exists" 3
+ else
+ export LOCATION_PATH=$(cd $LOCATION && pwd)
+ fi
+fi
+
+if [ -z "$ACCESS_KEY" ]; then
+ die "ACCESS_KEY is required" 4
+fi
+
+if [ -z "SECRET_KEY" ]; then
+ die "SECRET_KEY is required" 5
+fi
+
+extract_s3_bash() {
+ wget https://s3.amazonaws.com/cloudinitfiles/s3-bash.tgz
+ tar zxvf s3-bash.tgz
+}
+
+get_mime() {
+ python -c "import mimetypes; print mimetypes.guess_type(\"$1\")[0] or \"text/plain\""
+}
+
+upload_files() {
+ HEADER_FILE=`mktemp -t xzawsXXXXXX`
+ echo "X-Amz-Acl: public-read" > $HEADER_FILE
+ for file in `find $LOCATION_PATH -type f | sed "s|$LOCATION_PATH/||g"`; do
+ FULL_PATH="$LOCATION_PATH/$file"
+ MIME=`get_mime $FULL_PATH`
+ ./s3-bash/s3-put -a "$HEADER_FILE" -k "$ACCESS_KEY" -s "$SECRET_KEY_FILE" -T "$FULL_PATH" -c "$MIME" "/$BUCKET_NAME/$file"
+ done
+ rm $HEADER_FILE
+}
+
+extract_s3_bash
+upload_files
Please sign in to comment.
Something went wrong with that request. Please try again.