Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # SETUP
- #
- if [ "$#" -lt 1 ] ; then
- echo "Called without parameters, exiting"
- echo "Usage: $0 <AGAVI ENVIROMENT NAME>"
- exit 1
- fi
- no_childs=0
- while (($#)) ; do
- case "$1" in
- "--no-childs") no_childs=1 ;;
- *) env_arg="$1" ;;
- esac
- shift
- done
- if [ $env_arg == "" ] ; then
- echo "Called without enviroment"
- exit 2
- fi
- if [ `echo $env_arg | grep -c '/'` -eq 1 ] ; then
- env_name=`echo $env_arg | cut -d'/' -f1`
- else
- env_name="$env_arg"
- fi
- #
- # CONFIGURATION
- #
- timestamp=`date +%Y%m%d-%H%M%S`
- hostname=`cat /etc/hostname`
- pidfile="/tmp/agavi-deploy.pid"
- webroot="HTDOCROOT"
- agavi_env="${webroot}/${env_name}"
- checkout="${agavi_env}/checkout"
- svn_export="${agavi_env}/export-${timestamp}"
- svn_bin="${svn_export}/dev/bin"
- construction_page_src="${checkout}/dev/pub/index-deploy.php"
- construction_page_dest="${svn_export}/pub"
- html="${agavi_env}/html"
- # SVN CONFIGURATION
- svn_user="USER"
- svn_pass="PASS"
- #
- # GLOBAL DECLARATIONS
- #
- rollback=0
- #
- # FUNCTIONS
- #
- # Exit with a message
- e() {
- if [ "$1" -gt 19 ] ; then
- rm $pidfile
- fi
- case "$1" in
- # Pidfile does NOT get removed if
- # errorcode is lower than 20
- "0") echo "deploy successful." && rm $pidfile ;;
- "10") echo "${pidfile} found, Process already running with pid: `cat $pidfile`" ;;
- "20") echo "Folder $2 does not exist, aborting" ;;
- "25") echo "Symlink or file $3 exists in $2 " ;;
- "26") echo "Cannot create Symlink, target $3 not found in $2 " ;;
- "27") echo "Cannot create Symlink to export, rolling back to $2 " ;;
- "30") echo "User exit" ;;
- "50") echo "Something went terribly wrong. Command $3 returned $2 exitstatus. Aborting" ;;
- "60") echo "Variable for $2 not set, aborting" ;;
- *) echo "Something wicked happened... o.O " ;;
- esac
- exit $1
- }
- check_pidfile() {
- [ -f "$pidfile" ] && e 10
- echo "$$" > $pidfile
- }
- check_folder() {
- [ ! -d "$1" ] && e 20 "$1"
- }
- check_status() {
- if [ $rollback -ne 1 ] ; then
- [ $1 -ne 0 ] && e 50 "$1" "$2"
- else
- if [ $1 -ne 0 ] ; then
- cd $agavi_env
- [ -s html ] && rm html
- ln -s $prev html
- echo "Errors occured, rolling back to $prev"
- e 50 "$1" "$2"
- fi
- fi
- }
- create_symlink() {
- # Usage create_symlink A B C
- # A: Working Directory
- # B: Target
- # C: Link Name
- check_folder $1
- current_dir=`pwd`
- cd $1
- check_status $? "cd"
- if [ -e ${1}/$3 ] ; then
- # Symlink or File exists
- e 25 "$1" "$3"
- fi
- if [ ! -e ${1}/$2 ] ; then
- # No Target for Link
- e 26 "$1" "$2"
- fi
- ln -s ${2} ${3}
- check_status $? "ln"
- cd $current_dir
- }
- #
- # MAIN
- #
- check_pidfile
- check_folder "$agavi_env"
- check_folder "$checkout"
- # SVN Update
- cd $checkout
- svn --username $svn_user --password $svn_pass up
- check_status "$?" "svn update"
- # SVN Export
- cd $checkout
- svn export . $svn_export
- check_status "$?" "svn export"
- # Creating Symlinks
- create_symlink "$svn_bin" "agavi_${hostname}_${env_name}" "agavi"
- # Set permissions
- chmod +x ${svn_bin}/agavi
- # html Symlink
- cd $agavi_env
- # This is critical, we need to roll back from here if anything unexpected happens
- rollback=1
- prev=`ls -l html | awk '{ print $11 }'`
- [ -s "html" ] && rm html
- create_symlink "$agavi_env" "export-${timestamp}" "html"
- check_status $? "ln"
- # Set construction page
- cp $construction_page_src $construction_page_dest
- cd $construction_page_dest
- mv index-deploy.php index.php
- # Set fs permissions
- chgrp -R www-data ${html}/app/cache
- chgrp -R www-data ${html}/app/log
- chmod -R 770 ${html}/app/cache
- chmod -R 770 ${html}/app/log
- # Agavi create
- rm ${construction_page_dest}/index.php
- cd ${html}/dev/bin
- check_status $? "cd"
- ./agavi -D public.environment $env_name -D target.public-create.create-htaccess n public-create
- check_status $? "agavi public-create"
- # Show Symlink
- ls -l ${html}
- e 0
Add Comment
Please, Sign In to add comment