Skip to content

Instantly share code, notes, and snippets.

@ahmedsbytes
Created November 30, 2013 23:06
Show Gist options
  • Save ahmedsbytes/7725806 to your computer and use it in GitHub Desktop.
Save ahmedsbytes/7725806 to your computer and use it in GitHub Desktop.
My Deployment script , that upload a specific git repo to it's production env location , also enables you to add your files to override the repo files - Please read the file carefully before using it - Licenece : GNU GPL v2 -required file structure for the deployment script example.com/ deploy.sh git logs push temp
#!/bin/bash
SERVERS="192.168.2.101 192.168.2.102 192.168.2.106 192.168.2.107"
#SERVERS="192.168.2.107"
GITDIR="$(pwd)/git"
WORKING="$(pwd)/temp"
PUSH="$(pwd)/push"
REV=13
SSH_USER="example.com"
SSH_ARGS="-t -q"
LOG="$(pwd)/logs/$(date +%s)"
CP_CHECK="$(pwd)/logs/cp_check"
REV_DIR="/home/site/example.com/revs"
CURRENTLINK="/home/site/example.com/country.example.com"
UPLOADS="/home/site/example.com/revs/shared/uploads"
TODO=$1
function error(){
echo $*
echo "Check log $LOG"
exit 1
}
clean_working() {
rm -rf $WORKING/*
}
function git_update() {
unset GIT_DIR
cd $GITDIR
echo -n "* checking out to master ... "
git checkout master &> $LOG && echo "OK" || error ERROR
echo -n "* pulling latest ... "
git fetch &> $LOG && echo "OK" || error ERROR
echo -n "* copying to working dir ... "
git archive master | tar -x -C $WORKING &> $LOG && echo "OK" || error ERROR
}
function copy_extras()
{
#$WORKING/symfony cc > /dev/null
if [[ -z $(ls -a $PUSH/ | grep -E "[\.]*[^.]+") ]]
then
return 0
fi
echo -n "* copying push/* to working/ ... "
cp -rv $PUSH/. $WORKING/ &> $LOG && echo "OK" || error ERROR
}
function fix_permissions()
{
echo -n "* fixing permissions ... "
chmod 755 -R $WORKING $LOG && echo "OK" || error ERROR
}
function make_dst_dir()
{
echo "* making REV dir on all servers"
DST=$REV_DIR/"rev"$REV
for SER in $SERVERS
do
echo -n " ** creating on server: $SER ... "
ssh $SSH_ARGS $SSH_USER@$SER "stat $DST && exit 1 || mkdir -p $DST" &>$LOG \
|| error "ERROR : $DST exists or can not create." && echo "OK"
done
}
function copy_files() {
echo -n "* Copying files to servers ... "
DST=$REV_DIR/"rev"$REV
PIDS=""
TO_CHECK=""
echo -n "" > $CP_CHECK
#link uploads
UP=$DST"/web/uploads"
for SER in $SERVERS
do
TO_CHECK="OK$TO_CHECK"
echo -n " $SER . "
( tar -cjp -C $WORKING . 2>$LOG | \
ssh $SSH_ARGS $SSH_USER@$SER "( tar -xjp -C $DST && ( rm -f $UP && ln -sf $UPLOADS $UP || exit -1 ) || exit -1 )" &>$LOG \
&& echo -n "OK">>$CP_CHECK || echo -n "ERROR:$SER">>$CP_CHECK \
) &
PIDS="$PIDS $!"
done
for PD in $PIDS
do
wait $PD &> /dev/null
done
cat $CP_CHECK | grep "ERROR" > /dev/null && error " ERROR"
cat $CP_CHECK | grep $TO_CHECK > /dev/null || error " ERROR"
echo " OK"
}
function link_current {
echo -n "* linking current to revision $REV ... "
DST=$REV_DIR/"rev"$REV
TO_CHECK=""
PIDS=""
echo -n "" > $CP_CHECK
for SER in $SERVERS
do
TO_CHECK="OK"$TO_CHECK
echo -n " $SER . "
( \
ssh $SSH_ARGS $SSH_USER@$SER "rm -f $CURRENTLINK; ln -sf $DST $CURRENTLINK " &>$LOG \
&& echo -n "OK" >> $CP_CHECK \
|| echo -n "ERROR" >> $CP_CHECK > /dev/null \
) &
PIDS="$PIDS $!"
done
for PD in $PIDS
do
wait $PD &> /dev/null
done
cat $CP_CHECK | grep "ERROR" > /dev/null && { revert ; error ERROR ; }
cat $CP_CHECK | grep $TO_CHECK > /dev/null || { revert ; error ERROR ; }
echo " OK"
}
function revert() {
REV=$(( $REV - 1 ))
echo -n "* linking current to revision $REV ... "
DST=$REV_DIR/"rev"$REV
TO_CHECK=""
PIDS=""
echo -n "" > $CP_CHECK
for SER in $SERVERS
do
TO_CHECK="OK"$TO_CHECK
echo -n " $SER . "
( ssh $SSH_ARGS $SSH_USER@$SER "rm -f $CURRENTLINK; ln -sf $DST $CURRENTLINK " &>$LOG && echo -n "OK" >> $CP_CHECK || echo -n "ERROR" >> $CP_CHECK > /dev/null ) &
PIDS="$PIDS $!"
done
for PD in $PIDS
do
wait $PD &> /dev/null
done
cat $CP_CHECK | grep "ERROR" > /dev/null && { revert ; error ERROR ; }
cat $CP_CHECK | grep $TO_CHECK > /dev/null || { revert ; error ERROR ; }
echo " OK"
}
function gzip_log() {
gzip $LOG
}
function go() {
clean_working
git_update
copy_extras
fix_permissions
make_dst_dir
copy_files
link_current
}
function go_over() {
clean_working
git_update
copy_extras
fix_permissions
copy_files
link_current
}
echo "********** $(date) *************" >> $LOG
case "$TODO" in
go)
go
;;
go-delete)
go_over
;;
revert)
revert
;;
*)
echo "USAGE: go|revert|go-delete"
esac
gzip_log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment