Friday, February 22, 2013

A shell script to set up a GIT development workflow for a Drupal site

Since I need someplace to "backup" this script, I thought I may as well share it here as a way to preserve it.

This script resides on my development server. It sets up a GIT-based workflow based on this one.

When run, this script creates a project-specific "central" repository, pulls in the drupal source (from a master Drupal repo I use to stay synched to the Drupal codebase), and creates the necessary branches. The script then creates a few "web" directories to use as GIT working trees and modifies the post-update hook to keep the working trees synched to the repo.

All I have to do it push my changes from my local repo to the "central" repo and all three of the "sites" are automatically updated.

The script so far:
#!/bin/sh

set -e
set -o nounset
installpath=/opt/scripts
. ${installpath}/wfvars.sh
#wfvars.sh is a small script that creates the variables used below - based on the supplied project name

DRUP_VER=$2
DRUPALGIT=${GITHOME}/drupal

echo "Setting up workflow for: "$PROJECTNAME"..."

# set up "central" repo
echo "Making repo dir ("$PROJECTREPO")..."
mkdir $PROJECTREPO

if [ -d $PROJECTREPO ]; then
cd $PROJECTREPO
echo "Initializing bare repo ("$PROJECTREPO")..."
git init --bare
# now add as remote to drupal source repo ($DRUPALGIT)
cd $DRUPALGIT

git remote add $PROJECTNAME $PROJECTREPO
git checkout $DRUP_VER
git checkout -b $PROJECTNAME

git push $PROJECTNAME $PROJECTNAME
git clone --branch $PROJECTNAME $DRUPALGIT $DEVTREE
git clone --branch $PROJECTNAME $DRUPALGIT $STAGTREE
git clone --branch $PROJECTNAME $DRUPALGIT $PRODTREE

# create databases for each branch
createDEVDBquery="create database "$PROJECTNAME"_dev;"
createSTAGDBquery="create database "$PROJECTNAME"_staging;"
createPRODDBquery="create database "$PROJECTNAME"_production;"

#grant privileges
grantDEVQuery="grant all privileges on "$PROJECTNAME"_dev.* to MYSQLUSERNAME@localhost;"
grantSTAGQuery="grant all privileges on "$PROJECTNAME"_staging.* to MYSQLUSERNAME@localhost;"
grantPRODQuery="grant all privileges on "$PROJECTNAME"_production.* to MYSQLUSERNAME@localhost;"

echo "...creating databses for dev streams. Please enter your MYSQL password to continue..."

mysql -u MYSQLUSERNAME-p << eof
$createDEVDBquery
$createSTAGDBquery
$createPRODDBquery
$grantDEVQuery
$grantSTAGQuery
$grantPRODQuery
eof


# change permissions on /sites/default to 0777
sudo chmod 0777 $DEVTREE/sites/default
sudo chmod 0777 $STAGTREE/sites/default
sudo chmod 0777 $PRODTREE/sites/default
# copy default.settings.php to settings.php
cp $DEVTREE/sites/default/default.settings.php $DEVTREE/sites/default/settings.php
cp $STAGTREE/sites/default/default.settings.php $STAGTREE/sites/default/settings.php
cp $PRODTREE/sites/default/default.settings.php $PRODTREE/sites/default/settings.php
#set permissions on settings.php
sudo chmod 0777 $DEVTREE/sites/default/settings.php
sudo chmod 0777 $STAGTREE/sites/default/settings.php
sudo chmod 0777 $PRODTREE/sites/default/settings.php
fi


There is a bit more to the script. It adds an entry to a project directory that I mantain on my server homepage, but this is the guts of the functionality.

I'm kinda new to writing shell scripts so I am open to suggested improvements...

-------------------------------

Here are the associated helper scripts:
deletewf (to completely remove a project from the environment)

#!/bin/bash
installpath=/opt/scripts/
. ${installpath}wfvars.sh

DRUPALGIT=$GITHOME/drupal

read -p " THIS WILL COMPLETELY DESTROY THIS PROJECT!! ARE YOU ABSOLUTELY SURE?? ...y/n ?" -n 1 -r

if [[ $REPLY =~ ^[Yy]$ ]]
then

cd $DRUPALGIT
echo "Changing to latest branch ("$DRUPALGIT")"
git checkout 8.x
echo "deleting branch: "$PROJECTNAME
git branch -D $PROJECTNAME
echo "deleting remote: "$PROJECTNAME
git remote rm $PROJECTNAME

if [ -d $PROJECTREPO ]; then
echo "deleting project repo: "$PROJECTREPO
rm $PROJECTREPO -r
fi

if [ -d $PROJECTWEB ];then
echo "deleting project web: "$PROJECTWEB
rm $PROJECTWEB -r
fi

#delete the databases
deleteDEVDBquery="DROP database "$PROJECTNAME"_dev;"
deleteSTAGDBquery="DROP database "$PROJECTNAME"_staging;"
deletePRODDBquery="DROP database "$PROJECTNAME"_production;"

mysql -u chris << eof
$deleteDEVDBquery
$deleteSTAGDBquery
$deletePRODDBquery
eof
echo "Done."
else
cancel
fi


-----------------------------------------
wfvars.sh (creates the variables)  
#!/bin/sh
PROJECTNAME=$1

# Set up workflow
GITHOME=/home/git/
WEBHOME=/var/www/

PROJECTREPO=${GITHOME}${PROJECTNAME}
PROJECTWEB=${WEBHOME}${PROJECTNAME}
DEVTREE=${PROJECTWEB}/dev
STAGTREE=${PROJECTWEB}/staging
PRODTREE=${PROJECTWEB}/production

No comments: