-
Notifications
You must be signed in to change notification settings - Fork 1
/
site-files.sh
executable file
·253 lines (241 loc) · 6.54 KB
/
site-files.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
#!/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 $ROOT/sites
SITES=$(echo $(ls -d */) | sed 's,/,,g')
for S in $SITES; do
if [[ "$S" != "all" && -f "$ROOT/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
# Prompt to enable Stage File Proxy
cd $ROOT
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 $ROOT/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=$SITE-$ENV-files.tar.gz
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
else
echo ""
echo "Purpose: Downloads the latest files backup 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