Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AllieRays/f604c6235cfcb2d27360 to your computer and use it in GitHub Desktop.
Save AllieRays/f604c6235cfcb2d27360 to your computer and use it in GitHub Desktop.
Bash script for Drupal site installation. Using bash, and Drupal install profile
#!/bin/bash
function announce {
if [ ! -z "$1" ]; then
echo ""
echo "~~~~~~~~~~ $1"
echo ""
fi
}
#------------------------- variables & inputs -------------------------#
# rsync presets, perform near identical copy
RSYNC="rsync -acEHWz --executability --delete "
# current user/group
CUR_USER=`id -u -n`
CUR_GROUP=`id -g -n`
# current/scripts directory
SCRIPTS_DIR=`pwd`
# projects directory
PROJECTS_DIR=/opt/dev/kazaamprojects
# absolute path to directory where project directories/files will be created
PROJECT_DIR="`pwd`/../../"
cd $PROJECT_DIR
PROJECT_DIR=`pwd`
# name of this new project, completely separate from what the first site is or any of the sites are
PROJECT_NAME=`basename $PROJECT_DIR`
if [ -z $PROJECT_NAME ]; then
echo "Site domain name not specified"
echo "Usage: ./create-site <DOMAIN_NAME>"
exit
fi
# back to scripts directory
cd $SCRIPTS_DIR
# directories
PROJECT_DATA_DIR=$PROJECT_DIR/data
PROJECT_WEB_DIR=$PROJECT_DIR/web
PROJECT_LOGS_DIR=$PROJECT_DIR/logs
PROJECT_TEMP_DIR=$PROJECT_DIR/temp
# create site and folder structure
echo -n "Enter production domain (FQDN) with extension (example: www.mysite.com): "
read FULL_DOMAIN
# to lowercase
FULL_DOMAIN=`echo $FULL_DOMAIN | tr '[:upper:]' '[:lower:]'`
# parse domain name
# extract domain tokens
IFS='.' read -a PARTS <<< "$FULL_DOMAIN"
PARTS_LEN="${#PARTS[@]}"
# Top Level Domain - everything to the right with a leading dot
# .com, .net, .[etc]
TLD=".${PARTS[PARTS_LEN-1]}"
# everything from the left of last dot (.) without the dot
# so 'example' from 'example.com' OR 'www.example' from 'www.example.com'
SITE_NAME="${PARTS[PARTS_LEN-2]}"
# TODO:: make it work for more than 3 parts in a domain name (ie something.other.example.com)
# for now, we're handling at most 3 parts (www.example.com or blog.example.com or etc)
# Sub-domain (www. or blog. or you-name-it.)
# assume 'www.' subdomain
SUBDOMAIN="www."
if [ $PARTS_LEN == 3 ]; then
SUBDOMAIN="${PARTS[PARTS_LEN-3]}."
fi
# reset full domains from parts
LEFT_OF_TLD=$SUBDOMAIN$SITE_NAME
RIGHT_OF_SUBDMN=$SITE_NAME$TLD
FULL_DOMAIN=$LEFT_OF_TLD$TLD
PROD_DOMAIN=$FULL_DOMAIN
STAG_DOMAIN="${PROD_DOMAIN/www./}"
STAG_DOMAIN="${STAG_DOMAIN/.com/}"
STAG_DOMAIN="$STAG_DOMAIN.kazaamweb.com"
DEV_DOMAIN="${STAG_DOMAIN//.kazaamweb.com/.dev}"
# replace dashes (-) and dots (.) in SITE_NAME with underscores (_)
SITE_NAME="${FULL_DOMAIN/www./}"
SITE_NAME="${SITE_NAME/.com/}"
# @aliases
SERVER_NAME="$SITE_NAME"
# sanitized file/folder name for site
# folder names must not contain '-' or '.' because it is used as a api prefix php function names at theme level
SITE_FOLDER_NAME="${SITE_NAME//[-.]/_}"
# @aliases
SITE_SANITZED_NAME="$SITE_FOLDER_NAME"
THEME_FOLDER_NAME="$SITE_FOLDER_NAME"
# variables
SITE_DATA_DIR=$PROJECT_DATA_DIR/$SITE_FOLDER_NAME
SITE_WEB_DIR=$PROJECT_WEB_DIR/sites/$SITE_FOLDER_NAME
# check to see if site with name already exists
if [ -d $SITE_DATA_DIR ]
then
echo -n "A site with same name already exists. Would you like to delete current site and recreate it? [Y/N]: "
read YESNO
if [ "$YESNO" == "Y" ]
then
rm -rf $SITE_DATA_DIR
rm -rf $SITE_WEB_DIR
else
echo ""
echo "Directories exist:"
echo " $SITE_DATA_DIR"
echo " $SITE_WEB_DIR"
echo ""
echo "If you mean create it again please delete the folders and run this script again. Terminating..."
echo ""
exit
fi
fi
#------------------------- create a random password and database configs -------------------------#
# generate a random password
function gen_password() {
local LENGTH=$1 #how long = length of password
local CASES=$2 #how strong
local CHARACTERS="A-Za-z0-9_\!\@\#\$\%\^\&\*\-+" #class with special characters
case "$CASES" in
1)
local CHARACTERS="A-Za-z0-9";; # just letters and numbers
esac #end case
# generate a password string
if [ "`which uname /dev/null 2>&1`" ]; then
# decide if user is running a Unix shell or OSX
case "`uname -a`" in
Darwin*)
local PASS=`LC_CTYPE=C tr -dc $CHARACTERS < /dev/urandom | head -c $LENGTH | xargs` ;;
*)
local PASS=`tr -dc $CHARACTERS < /dev/urandom | head -c $LENGTH | xargs` ;;
esac
fi
echo $PASS
}
# remove special characters and truncate site name for the database
# limit to first 16 characters
DB_USER=`echo ${LEFT_OF_TLD:0:30}`
DB_USER="${DB_USER/www./}"
DB_USER="${DB_USER/.com/}"
DB_USER="${DB_USER//[-.]/_}"
DB_NAME="$DB_USER"_"$TLD"
DB_NAME="${DB_NAME/.com/site}"
DB_NAME="${DB_NAME/./}"
DB_PASS=$(gen_password 20 1)
#------------------------- display parameters -------------------------#
echo "####################################################################################################"
echo "User : $CUR_USER"
echo "Group : $CUR_GROUP"
echo ""
echo "Domain : $FULL_DOMAIN ($SITE_NAME) ($PROD_DOMAIN | $STAG_DOMAIN | $DEV_DOMAIN)"
echo "Database : $DB_NAME"
echo " DB User : $DB_USER"
echo " DB Pass : $DB_PASS"
echo ""
echo "Project Dir: $PROJECT_DIR"
echo " Data Dir: $SITE_DATA_DIR"
echo " Web Dir: $SITE_WEB_DIR"
echo "####################################################################################################"
#------------------------- remove any .DS_Store (mac hidden files) -------------------------#
find $PROJECT_DIR -name ".DS_Store" -exec rm -f {} \;
#------------------------- create site directories -------------------------#
mkdir -p $SITE_DATA_DIR
mkdir -p $SITE_WEB_DIR/files ; chmod 777 $SITE_WEB_DIR/files
mkdir -p $SITE_WEB_DIR/private ; chmod 777 $SITE_WEB_DIR/private
mkdir -p $PROJECT_LOGS_DIR ; chmod 777 $PROJECT_LOGS_DIR
mkdir -p $PROJECT_TEMP_DIR ; chmod 777 $PROJECT_TEMP_DIR
#------------------------- clone idi_core repository -------------------------#
## clone idi_core into temp folder
cd $PROJECT_TEMP_DIR
announce "Cloning 'idi_core' git repository to $PROJECT_TEMP_DIR/idi_core"
git clone git@bitbucket.org:inventivdigital/idi_core.git idi_core
# delete .git repository metadata
find idi_core -name ".git*" -exec rm -rf {} \;
find idi_core -name ".DS_Store" -exec rm -f {} \;
#------------------------- data variables -------------------------#
announce "Init $SITE_DATA_DIR directory from $PROJECT_TEMP_DIR/idi_core/data"
# copy data files to site's data folder
$RSYNC $PROJECT_TEMP_DIR/idi_core/data/ $SITE_DATA_DIR
rm -rf $PROJECT_TEMP_DIR/idi_core/data
#Find and replace all instances of templatesite" with $sitename
#http://stackoverflow.com/questions/6178498/using-grep-and-sed-to-find-and-replace-a-string
#http://stackoverflow.com/questions/7573368/in-place-edits-with-sed-on-os-x
find $SITE_DATA_DIR -type f -exec sed -i "" "s/PROD_DOMAIN/$PROD_DOMAIN/g" {} \;
find $SITE_DATA_DIR -type f -exec sed -i "" "s/STAG_DOMAIN/$STAG_DOMAIN/g" {} \;
find $SITE_DATA_DIR -type f -exec sed -i "" "s/DEV_DOMAIN/$DEV_DOMAIN/g" {} \;
find $SITE_DATA_DIR -type f -exec sed -i "" "s/PROJECT_NAME/$PROJECT_NAME/g" {} \;
find $SITE_DATA_DIR -type f -exec sed -i "" "s/SITE_FOLDER_NAME/$SITE_FOLDER_NAME/g" {} \;
# site-config updates
find $SITE_DATA_DIR -type f -exec sed -i "" "s/SITE_NAME/$SITE_NAME/g" {} \;
find $SITE_DATA_DIR -type f -exec sed -i "" "s/RIGHT_OF_SUBDMN/$RIGHT_OF_SUBDMN/g" {} \;
find $SITE_DATA_DIR -type f -exec sed -i "" "s/DB_USER/$DB_USER/g" {} \;
find $SITE_DATA_DIR -type f -exec sed -i "" "s/DB_PASS/$DB_PASS/g" {} \;
find $SITE_DATA_DIR -type f -exec sed -i "" "s/DB_NAME/$DB_NAME/g" {} \;
#------------------------- web variables -------------------------#
announce "Adding site entries to $PROJECT_WEB_DIR/sites/sites.php"
# create sites.php if doesn't exist
if [ ! -f $PROJECT_WEB_DIR/sites/sites.php ]; then
mv $PROJECT_WEB_DIR/sites/example.sites.php $PROJECT_WEB_DIR/sites/sites.php
fi
# append this site to sites.php
echo "// $SITE_NAME" >> $PROJECT_WEB_DIR/sites/sites.php
echo "\$sites['$DEV_DOMAIN'] = '$SITE_FOLDER_NAME';" >> $PROJECT_WEB_DIR/sites/sites.php
echo "\$sites['$STAG_DOMAIN'] = '$SITE_FOLDER_NAME';" >> $PROJECT_WEB_DIR/sites/sites.php
echo "\$sites['$PROD_DOMAIN'] = '$SITE_FOLDER_NAME';" >> $PROJECT_WEB_DIR/sites/sites.php
echo "" >> $PROJECT_WEB_DIR/sites/sites.php
#------------------ IDI_CORE install profile and theme ------------#
# --- install profile
announce "Installing 'idi_core' profile in $PROJECT_WEB_DIR/profiles"
$RSYNC --exclude=".git*" $PROJECT_TEMP_DIR/idi_core/profile/ $PROJECT_WEB_DIR/profiles/idi_core
rm -rf $PROJECT_TEMP_DIR/idi_core/profile
# Filter tokens in select files
find $PROJECT_WEB_DIR/profiles/idi_core -type f -exec sed -i "" "s/SITE_NAME/$SITE_NAME/g" {} \;
find $PROJECT_WEB_DIR/profiles/idi_core -type f -exec sed -i "" "s/SITE_FOLDER_NAME/$SITE_FOLDER_NAME/g" {} \;
find $PROJECT_WEB_DIR/profiles/idi_core -type f -exec sed -i "" "s/THEME_FOLDER_NAME/$THEME_FOLDER_NAME/g" {} \;
# --- theme
announce "Installing 'idi_core' theme in $SITE_WEB_DIR"
$RSYNC --exclude=".git*" $PROJECT_TEMP_DIR/idi_core/site/ $SITE_WEB_DIR
rm -rf $PROJECT_TEMP_DIR/idi_core/site
# drop idi_core clone
rm -rf $PROJECT_TEMP_DIR/idi_core
# chroot into site's web folder
cd $SITE_WEB_DIR
# set database connection properties
find settings.php -type f -exec sed -i "" "s/DB_USER/$DB_USER/g" {} \;
find settings.php -type f -exec sed -i "" "s/DB_PASS/$DB_PASS/g" {} \;
find settings.php -type f -exec sed -i "" "s/DB_NAME/$DB_NAME/g" {} \;
# rename files
mv themes/idi_core themes/$THEME_FOLDER_NAME
mv themes/$THEME_FOLDER_NAME/idi_core.info themes/$THEME_FOLDER_NAME/$THEME_FOLDER_NAME.info
# Filter tokens in select files
FILTERATE_EXTS=("\*.info" "\*.php" "\*.inc" "\*.js" "\*.css" "\*.md" "\*.scss" "\*.less" "\*.install" "\*.module")
for FILTERATE_EXT in ${FILTERATE_EXTS[@]}; do
FILTERATE_EXT="${FILTERATE_EXT//\\/}"
find . -type f -name "$FILTERATE_EXT" -exec sed -i "" "s/SITE_NAME/$SITE_NAME/g" {} \;
find . -type f -name "$FILTERATE_EXT" -exec sed -i "" "s/SITE_FOLDER_NAME/$SITE_FOLDER_NAME/g" {} \;
find . -type f -name "$FILTERATE_EXT" -exec sed -i "" "s/THEME_FOLDER_NAME/$THEME_FOLDER_NAME/g" {} \;
done
#------------------------- database config -------------------------#
announce "Creating database and it's user..."
cd $SITE_DATA_DIR/../scripts
./init-db $SITE_FOLDER_NAME
cd $SITE_WEB_DIR
# standard install (TEST)
#announce "Installing drupal using 'standard' profile..."
#drush si standard -y --uri http://$DEV_DOMAIN --site-name=$SITE_NAME --account-name=admin --account-pass=kzFbc975@ --account-mail=ajones@inventivdigital.com --db-url=mysql://$DB_USER:$DB_PASS@localhost/$DB_NAME
# profile install method
announce "Installing drupal using 'idi_core' profile..."
drush si idi_core -y --uri http://$DEV_DOMAIN --site-name=$SITE_NAME --account-name=admin --account-pass=kzFbc975@ --account-mail=ajones@inventivdigital.com --db-url=mysql://$DB_USER:$DB_PASS@localhost/$DB_NAME --sites-subdir=$SITE_FOLDER_NAME
#destory profile
rm -rf $PROJECT_WEB_DIR/profiles/idi_core
# make current user owner of everything
sudo chown -R $CUR_USER:$CUR_GROUP $PROJECT_DIR
sudo chmod -R u+rw $PROJECT_DIR
# create editor role (TEST)
cd $SITE_WEB_DIR
drush user-create editor --mail="[ajones+ce@inventivdigital.com]" --password="idi2015$"
drush user-add-role "Content editor" --uid=2
drush user-add-role "ADMINISTRATOR" --uid=1
# update drupal
announce "Updating drupal instance to latest..."
cd $SITE_WEB_DIR
drush pm-update -y
# switch to scripts directory
cd $SITE_DATA_DIR/../scripts
# backup current installed db
chmod u+x backupsite-local
./backupsite-local $SITE_FOLDER_NAME
# install site in local environment
chmod u+x setup-local-site
./setup-local-site $SITE_FOLDER_NAME
# restart apache
sudo /usr/sbin/apachectl -k restart
# open in browser
open http://$SITE_NAME.dev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment