-
Notifications
You must be signed in to change notification settings - Fork 1
/
site-sync.sh
executable file
·294 lines (276 loc) · 7.81 KB
/
site-sync.sh
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/bin/bash
# Check for prerequisites
GIT=$(which git)
if [ $? == 1 ]; then
echo "Git is not installed. See https://github.com/git/git."
exit
fi
TERMINUS=$(which terminus)
if [ $? == 1 ]; then
echo "Terminus is not installed. See https://github.com/pantheon-systems/cli."
exit
fi
DRUSH=$(which drush)
if [ $? == 1 ]; then
echo "Drush is not installed. See http://www.drush.org/en/master/install."
exit
fi
# Get the Pantheon site name
SITE=""
if test $1; then
SITE=$1
fi
# Get the environment
ENV=dev
if test $2; then
ENV=$2
fi
# Set the Drupal root directory
ROOT=$($DRUSH status root --format=list)
if [ -z $ROOT ]; then
ROOT=/var/www/$SITE-$ENV
fi
# Validate the Drupal root directory
if [ ! -d $ROOT ]; then
echo "The Pantheon site cannot be located."
exit
fi
# Get the Pantheon site name from Drupal root
if [ -z $SITE ]; then
BASE=${ROOT:0:8}
if [ $BASE == "/var/www" ]; then
DIR=${ROOT:9}
END="-$ENV"
LEN=${#END}
SITE=${DIR:0:(-$LEN)}
fi
fi
if [ ! -z $SITE ]; then
# Validate the environment
$TERMINUS site environment-info --site=$SITE --env=$ENV --field=id
if [ $? == 1 ]; then
$TERMINUS site environments --site=$SITE
exit
fi
# Retrieve stored Terminus credentials
EMAIL=""
PASSWORD=""
HTTPUSER=""
HTTPPASS=""
if [ -f $HOME/.terminus_auth ]; then
while read line; do
for pair in $line; do
set -- $(echo $pair | tr '=' ' ')
if [ "$1" == "email" ]; then
EMAIL=${line#"$1="}
fi
if [ "$1" == "password" ]; then
PASSWORD=${line#"$1="}
fi
if [ "$1" == "httpuser" ]; then
HTTPUSER=${line#"$1="}
fi
if [ "$1" == "httppass" ]; then
HTTPPASS=${line#"$1="}
fi
done
done < $HOME/.terminus_auth
fi
# Terminus authentication prompts
WHOAMI=$($TERMINUS auth whoami)
if [ $? == 1 ]; then
if [ -z "$EMAIL" ]; then
echo -n "Enter your Pantheon email address: "; read EMAIL
if [ -z "$EMAIL" ]; then
exit
else
echo -n "Save email address? (Y/n): "; read -n 1 SAVEMAIL
if [ -z "$SAVEMAIL" ]; then
SAVEMAIL=y
fi
if [ "$SAVEMAIL" == "Y" ]; then
SAVEMAIL=y
fi
if [ "$SAVEMAIL" == "y" ]; then
echo "email=$EMAIL" > $HOME/.terminus_auth
fi
fi
fi
if [ -z "$PASSWORD" ]; then
echo -n "Enter your Pantheon password: "; read -s PASSWORD
if [ -z "$PASSWORD" ]; then
exit
else
echo -n "Save password? (y/N): "; read -n 1 SAVEPASS
echo $'\n'
if [ "$SAVEPASS" == "Y" ]; then
SAVEPASS=y
fi
if [ "$SAVEPASS" == "y" ]; then
echo "password=$PASSWORD" >> $HOME/.terminus_auth
fi
fi
fi
if [ -z "$HTTPUSER" ]; then
echo -n "Enter the HTTP Basic Authentication username: "; read HTTPUSER
if [ ! -z "$HTTPUSER" ]; then
echo "httpuser=$HTTPUSER" >> $HOME/.terminus_auth
fi
fi
if [ -z "$HTTPPASS" ]; then
echo -n "Enter the HTTP Basic Authentication password: "; read -s HTTPPASS
if [ ! -z "$HTTPPASS" ]; then
echo "httppass=$HTTPPASS" >> $HOME/.terminus_auth
fi
fi
# Change email to match commits to Pantheon
GITEMAIL=$($GIT config --get user.email)
if [ "$GITEMAIL" != "$EMAIL" ]; then
$GIT config --global user.email $EMAIL
fi
$TERMINUS auth login $EMAIL --password="$PASSWORD"
fi
# Remove saved credentials if unable to login
WHOAMI=$($TERMINUS auth whoami)
if [ $? == 1 ]; then
if [ -f $HOME/.terminus_auth ]; then
rm -f $HOME/.terminus_auth
fi
exit
fi
# Set multisite
MULTISITE=""
MULTISITES=""
DEFAULTSITE="default"
cd /var/www/$DIR/sites
SITES=$(echo $(ls -d */) | sed 's,/,,g')
for S in $SITES; do
if [[ "$S" != "all" && -f "/var/www/$DIR/sites/$S/settings.php" ]]; then
if [ -z "$MULTISITES" ]; then
MULTISITES="$S"
else
MULTISITES="$MULTISITES $S"
fi
DEFAULTSITE="$S"
fi
done
if [ "$DEFAULTSITE" == "$MULTISITES" ]; then
MULTISITE="$DEFAULTSITE"
fi
if [[ "$MULTISITE" == "default" && "$MULTISITES" != "default" ]]; then
echo ""
echo "The following multisites are available:"
echo $MULTISITES
echo ""
echo -n "Enter the multisite ($DEFAULTSITE): "; read MULTISITE
if [ -z "$MULTISITE" ]; then
MULTISITE="$DEFAULTSITE"
fi
fi
if [ "$MULTISITE" != "$DEFAULTSITE" ]; then
VALID=no
for MULTI in $MULTISITES; do
if [ "$MULTI" == "$MULTISITE" ]; then
VALID=yes
fi
done
if [ "$VALID" == "no" ]; then
echo "$MULTISITE is not a valid multisite."
exit
fi
fi
if [ ! -f $HOME/.drush/registry_rebuild/registry_rebuild.php ]; then
$DRUSH dl registry_rebuild -y
$DRUSH cc drush
fi
if [ ! -z "$MULTISITE" ]; then
DRUSH="$DRUSH -l $MULTISITE"
fi
# Pull the latest code changes from master
cd /var/www/$DIR
$GIT pull
# Download the latest database backup
DB=$($TERMINUS site backups get --site=$SITE --env=$ENV --element=db --latest)
if [ ! -z "$DB" ]; then
LABEL=${DB:0:11}
if [ "$LABEL" == "Backup URL:" ]; then
DB=${DB:12}
fi
NEW_DB="$DIR.sql"
rm -f $NEW_DB $NEW_DB.gz
echo "Downloading the latest database backup $DB to $NEW_DB ..."
curl -o $NEW_DB.gz $DB && gunzip $NEW_DB.gz
$DRUSH sql-drop -y
echo "Loading $NEW_DB ..."
$DRUSH sqlc < $NEW_DB
fi
# Make sure the Drupal admin user login is admin/admin
$DRUSH sqlq "update users set name = 'admin' where uid = 1"
$DRUSH upwd admin --password=admin
$DRUSH rr
# Prompt to enable Stage File Proxy
echo -n "Would you like to enable Stage File Proxy? (Y/n): "; read -n 1 PROXY
echo ""
if [ -z "$PROXY" ]; then
PROXY=y
fi
if [ "$PROXY" == "Y" ]; then
PROXY=y
fi
if [ "$PROXY" == "y" ]; then
$DRUSH dl -n stage_file_proxy
$DRUSH en -y stage_file_proxy
DOMAIN=$(echo $($TERMINUS site hostnames list --site=$SITE --env=$ENV) | cut -d" " -f4)
if [ ! -z "$DOMAIN" ]; then
$DRUSH vset stage_file_proxy_hotlink 1
if [[ ! -z "$HTTPUSER" && ! -z "$HTTPPASS" ]]; then
$DRUSH vset stage_file_proxy_origin "https://$HTTPUSER:$HTTPPASS@$DOMAIN"
else
$DRUSH vset stage_file_proxy_origin "https://$DOMAIN"
fi
fi
else
cd /var/www/$DIR/sites/$MULTISITE/files
FILES=$($TERMINUS site backups get --site=$SITE --env=$ENV --element=files --latest)
if [ ! -z "$FILES" ]; then
LABEL=${FILES:0:11}
if [ "$LABEL" == "Backup URL:" ]; then
FILES=${FILES:12}
fi
NEW_FILES=$DIR-files.tar.gz
rm -f $NEW_FILES
echo "Downloading latest files backup $FILES to $NEW_FILES..."
curl -o $NEW_FILES $FILES
tar zxvf $NEW_FILES
sudo cp -r files_$ENV/* .
sudo rm -rf files_$ENV/
cd ..
sudo chown -R vagrant:www-data files/
sudo chmod -R g+w files/
fi
fi
# Enable development modules
#$DRUSH dl -n migrate migrate_extras coder devel devel_themer hacked redis simplehtmldom-7.x-1.12 stage_file_proxy
#$DRUSH en -y migrate_extras coder devel_themer hacked redis stage_file_proxy
# Disable unused/unwanted modules
$DRUSH dis -y overlay
# Disable cron
ELYSIA=$($DRUSH pml --status=Enabled | grep elysia_cron)
if [ ! -z "$ELYSIA" ]; then
$DRUSH vset elysia_cron_disabled 1
fi
$DRUSH vset cron_safe_threshold 0
# Restart web services
/vagrant/restart-lemp.sh
else
echo ""
echo "Purpose: Downloads the latest code, files and database to your local environment"
echo ""
echo "Usage: $0 [site] [env] where [site] is a"
echo " valid Nginx virtual host or Pantheon Site Name"
echo " and [env] is the environment (dev, test or live)."
echo ""
echo " The default [site] is the current Drupal root"
echo " and the default [env] is dev."
echo ""
fi