forked from UCLALibrary/drupal_admin_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrupal_promote_code
executable file
·146 lines (122 loc) · 5.71 KB
/
drupal_promote_code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash
## Promote Drupal code from local site to each destination site (from TEST to STAGE or from STAGE to PROD)
##
## Order of operations:
## 1. Test ability to use drush to connect to a destination site (stage or production)
## 2. Put the destination Drupal site into ready-only maintenance mode
## 3. Rsync Drupal code files from the SRC site to each DEST site
## 4 On the destination site, apply any db changes, clear the Drupal cache, and turn maintenance mode off
##
# Must be this user to run this script
# Assume that this user's ~/.ssh/known_hosts on the remote machine already has
# an entry for the local machine
USER=druadmin
if [ "`/usr/bin/id -urn`" != "$USER" ] ; then
echo "You must be the $USER user to execute this script - exiting."
exit 1
fi
# Avoid being in the Drupal root tree to avoid built-in aliases (e.g. @self)
cd $HOME
DRUSH="/usr/local/bin/drush"
# Get local drush alias
# All drush aliases can be found in /home/$USER/.drush/aliases.drushrc.php
SRC_ALIAS=`$DRUSH site-alias --local 2>/dev/null`
if [ $? -ne 0 ]; then
echo -e "Unable to get local drush alias - exiting \n"
exit 1
fi
# Get URI for the local site
SRC_URI="`$DRUSH site-alias --field-labels=0 --fields=uri --format=list @$SRC_ALIAS`"
if [ -z "$SRC_URI" ]; then
echo -e "Unable to get local drush URI - exiting \n"
exit 1
fi
# Determine general destination
# Expected drush alias format is rhel-test[digits], rhel-stage[digits], rhel-prod[digits]
case $SRC_ALIAS in
rhel-test*)
# Since we're on test, then destination is stage
DEST=`echo $SRC_ALIAS | sed -e 's/test.*/stage/'`
;;
rhel-stage*)
# Since we're on stage, then destination is production
DEST=`echo $SRC_ALIAS | sed -e 's/stage.*/prod/'`
;;
*)
echo -e" Must run this script on test or stage - exiting \n"
exit 1
;;
esac
# Get URI for each destination site and alias for one destination site
EACH_DEST_URI=""
for DEST_ALIAS in `$DRUSH site-alias | grep $DEST | sort -rn` ; do
EACH_DEST_URI="$EACH_DEST_URI `$DRUSH site-alias --field-labels=0 --fields=uri --format=list @$DEST_ALIAS`"
done
if [ -z "$EACH_DEST_URI" ] ; then
echo -e "No destination site found for $SRC_ALIAS - exiting \n"
exit 1
fi
# STEP 1 ---------------------------------------------------------------------------------------------------------------------------
echo -e "\nSTEP 1 \n"
# Ask destination for its Drupal root directory
DEST_ROOT_DIR=`$DRUSH site-alias --field-labels=0 --fields=root --format=list @$DEST_ALIAS 2>/dev/null`
if [ $? -ne 0 ] ; then
echo -e "Unable to determine $DEST_ALIAS root directory - exiting \n"
exit 1
fi
# Destination Drupal code directory is one level above
DEST_CODE_DIR=`dirname $DEST_ROOT_DIR 2>/dev/null`
if [ $? -ne 0 ] ; then
echo -e "Unable to determine $DEST_ALIAS code directory - exiting \n"
exit 1
fi
# We have also successfully connected to the destination
echo -e "Sucessfully connected to $DEST_ALIAS site. \n"
# STEP 2 ---------------------------------------------------------------------------------------------------------------------------
echo -e "\nSTEP 2 \n"
# Put destination Drupal site into read-only maintenance mode
echo -e "Putting the $DEST_ALIAS site into maintenance mode. \n"
$DRUSH @$DEST_ALIAS vset maintenance_mode 1
# STEP 3 ---------------------------------------------------------------------------------------------------------------------------
echo -e "\nSTEP 3 \n"
# Copy the Drupal code files from local site (test or stage) to each destination site (stage or production)
# rsync options:
# -c = decision to skip is based on checksum, not on (default) modification time and size
# -r = recurse into directories
# -l = copy symlinks as symlinks
# -o = preserve owner (2014-10-06 druadmin doesn't own the files anymore)
# -g = preserve group (2014-10-06 druadmin doesn't own the files anymore)
# -t = preserve times (2014-10-06 druadmin doesn't own the files anymore)
# -p = preserve permissions (2014-10-06 druadmin doesn't own the files anymore)
# -H = preserve hard links
# -v = increase verbosity
# --delete = files deleted from source (production) will be deleted on destination
# --temp-dir = specify directory to hold temporary files
# --safe-links = ignore symlinks that point outside the tree, like $DEST_CODE_DIR/.../sites/default/files
# --exclude = exclude file pattern
# .git* is git repo metadata (.git, .gitignore, .gitignore-contributors and maybe more)
# robots.txt is a symlink to an appropriate robots.txt file
# Appending a slash to the source directory name causes rsync to copy the contents
# Otherwise, rsync would copy the directory itself
for DEST_URI in $EACH_DEST_URI; do
echo -e "\nCopying Drupal code files from $SRC_URI to $DEST_URI \n"
sudo -u root rsync -e 'ssh' -c -r -l -o -g -t -p -H -v \
--delete --temp-dir=/tmp --safe-links \
--exclude=".git*" --exclude="robots.txt" \
$DEST_CODE_DIR/ $USER@$DEST_URI:$DEST_CODE_DIR
done
# STEP 4 ---------------------------------------------------------------------------------------------------------------------------
echo -e "\nSTEP 4 \n"
# Apply any database changes to the destination.
echo -e "Applying any database changes. \n"
$DRUSH @$DEST_ALIAS -y updatedb
# Clear the Drupal cache in the destination database
echo -e "Clearing the Drupal cache. \n"
$DRUSH @$DEST_ALIAS -y cache-clear all
# Turn maintenance mode off
echo -e "Drupal file synchronization is complete - taking destination site out of maintenance mode. \n"
$DRUSH @$DEST_ALIAS vset maintenance_mode 0
# Clear the Drupal cache in the destination database again to be sure maintenance mode is not preserved
echo -e "Clearing the Drupal cache. \n"
$DRUSH @$DEST_ALIAS -y cache-clear all
echo -e "\nCopy of code from $SRC_URI to "$EACH_DEST_URI" complete. \n"