forked from pvtl/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
271 lines (221 loc) · 8.7 KB
/
setup.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
#!/usr/bin/env bash
#
#
# Sets up (locally) Wordpress "the Pivotal Way"
#
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# @copyright Copyright (c) 2018 by Pivotal Agency
# @license http://www.gnu.org/licenses/
#
#
# Variables
# ---------------------------------------------
RAND=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 8 | head -n 1)
# DB Details
DB_HOST="mysql"
DB_USER="root"
# WP Secrets
WP_AUTH_KEY=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1)
WP_SECURE_AUTH_KEY=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1)
WP_LOGGED_IN_KEY=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1)
WP_NONCE_KEY=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1)
WP_AUTH_SALT=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1)
WP_SECURE_AUTH_SALT=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1)
WP_LOGGED_IN_SALT=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1)
WP_NONCE_SALT=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1)
# Options
while getopts "s" arg; do
case $arg in
s)
IS_STAGE=1
;;
esac
done
# Site Config
# ---------------------------------------------
# LDE database password
echo -e "\n ➤ Please enter the password for MySQL: [dbroot] "
read -p "== " DB_PW
if [[ -z "$DB_PW" ]]; then
DB_PW="dbroot"
fi
# Asks for the Git repo URL of the project
# Quit if nothing input
echo -e "\n ➤ What's the URL to access the Git repo?"
echo -e " Note: use the HTTPS version of the URL"
read -p "== " GIT_REPO_URL_HTTPS
GIT_URL_FORMAT="^(https)(:\/\/|@)([^@:]+)([^\/:]+)[\/:]([^\/:]+)\/(.+).git$"
if [[ ${GIT_REPO_URL_HTTPS} =~ ${GIT_URL_FORMAT} ]] ; then
# Create the GIT equivalent URL - for use outside the docker container
# - The HTTPS version is only for use inside the docker container
BASH_REMATCH_3=${BASH_REMATCH[3]}
BASH_REMATCH_4=${BASH_REMATCH[4]}
BASH_REMATCH_5=${BASH_REMATCH[5]}
BASH_REMATCH_6=${BASH_REMATCH[6]}
# If Bitbucket
if [[ ${GIT_REPO_URL_HTTPS} =~ .*bitbucket* ]] ; then
GIT_REPO_HOSTNAME=${BASH_REMATCH_4}
GIT_REPO_OWNER=${BASH_REMATCH_5}
GIT_REPO_NAME=${BASH_REMATCH_6}
# Else: Github (likely)
else
GIT_REPO_HOSTNAME="@"${BASH_REMATCH_3}${BASH_REMATCH_4}
GIT_REPO_OWNER=${BASH_REMATCH_5}
GIT_REPO_NAME=${BASH_REMATCH_6}
fi
GIT_REPO_URL_GIT="git${GIT_REPO_HOSTNAME}:${GIT_REPO_OWNER}/${GIT_REPO_NAME}.git"
echo ${GIT_REPO_URL_GIT}
else
echo -e " ⚠ Please enter the HTTPS version of the URL..."
exit 1
fi
# Asks for the branch to deploy
# Defaults to master
echo -e "\n ➤ What Git branch would you like to use?"
echo -e " Default: develop"
read -p "== " GIT_BRANCH
if [[ -z "$GIT_BRANCH" ]]; then
GIT_BRANCH="develop"
fi
# Create a default dir and DB name
DIR_NAME_TMP=$(echo $GIT_REPO_NAME | tr -cd '[[:alnum:]].' | tr '[:upper:]' '[:lower:]')
# Directory/DB Name
# Defaults to a random folder name
echo -e "\n ➤ We'll create a new directory & DB for the project. What shall we call them?"
echo -e " Default: ${DIR_NAME_TMP}"
read -p "== " DIR_NAME
if [[ -z "$DIR_NAME" ]]; then
DIR_NAME="${DIR_NAME_TMP}"
fi
DIR_NAME=$(echo $DIR_NAME | tr -cd '[[:alnum:]].' | tr '[:upper:]' '[:lower:]')
URL="http://${DIR_NAME}.pub.localhost"
if [[ ${IS_STAGE} == 1 ]] ; then
URL="http://${DIR_NAME}.pub.pvtl.io"
URL_STAGE_SCRIPT="http://pvtl:pvtl@${DIR_NAME}.pvtl.io/stage.php"
fi
# Error if directory already exists
if [ -d ${DIR_NAME} ]; then
echo -e " ⚠ That directory already exists..."
exit 1
fi
# Create the directory
# ---------------------------------------------
mkdir $DIR_NAME && cd $DIR_NAME
SITE_ROOT="$(pwd)"
# Create a Database
# ---------------------------------------------
php -r '
$conn = mysqli_connect($argv[1], $argv[2], $argv[3]);
mysqli_query($conn, "CREATE DATABASE " . $argv[4] . " CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
' $DB_HOST $DB_USER $DB_PW $DIR_NAME
# Clone the repo
# ---------------------------------------------
git clone --single-branch --branch ${GIT_BRANCH} ${GIT_REPO_URL_HTTPS} .
git remote set-url origin ${GIT_REPO_URL_GIT}
# Exit if it didn't clone
if [ ! -f ".env.example" ]; then
echo -e " ⚠ Git clone failed"
exit 1
fi
# Undo the shallow clone when not staging
if [[ ${IS_STAGE} != 1 ]] ; then
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch origin
fi
# Install Dependencies
# ---------------------------------------------
composer install --ignore-platform-reqs &>/dev/null &
if [ -f "web/app/themes/pvtl-child/index.php" ]; then
( cd web/app/themes/pvtl-child ; yarn &>/dev/null & )
fi
# The above commands are run in the background
# `disown` ensures they finish if we close the terminal
disown
# Create a Symlink for our LDE's
# ---------------------------------------------
ln -s web public
# Create the .env file and setup DB connection and secrets
# ---------------------------------------------
cp .env.example .env
sed -i 's/database_name/'"$DIR_NAME"'/g' .env
sed -i 's/database_user/'"$DB_USER"'/g' .env
sed -i 's/database_password/'"$DB_PW"'/g' .env
sed -i "s/# DB_HOST=/DB_HOST='$DB_HOST' # DB_HOST=/g" .env
sed -i 's,http://example.com,'"$URL"',g' .env
sed -i "s/SECURE_AUTH_KEY='generateme'/SECURE_AUTH_KEY='"$WP_SECURE_AUTH_KEY"'/g" .env
sed -i "s/AUTH_KEY='generateme'/AUTH_KEY='"$WP_AUTH_KEY"'/g" .env
sed -i "s/LOGGED_IN_KEY='generateme'/LOGGED_IN_KEY='"$WP_LOGGED_IN_KEY"'/g" .env
sed -i "s/NONCE_KEY='generateme'/NONCE_KEY='"$WP_NONCE_KEY"'/g" .env
sed -i "s/SECURE_AUTH_SALT='generateme'/SECURE_AUTH_SALT='"$WP_SECURE_AUTH_SALT"'/g" .env
sed -i "s/AUTH_SALT='generateme'/AUTH_SALT='"$WP_AUTH_SALT"'/g" .env
sed -i "s/LOGGED_IN_SALT='generateme'/LOGGED_IN_SALT='"$WP_LOGGED_IN_SALT"'/g" .env
sed -i "s/NONCE_SALT='generateme'/NONCE_SALT='"$WP_NONCE_SALT"'/g" .env
# Create a .htaccess file for permalinks
# ---------------------------------------------
echo '
<IfModule mod_rewrite.c>
#### Access 404d Wordpress uploads from another site (so that you do not need to download all assets)
#### - If URL is not found AND the URL contains /app/uploads/ - check on live site
# RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
# RewriteCond %{REQUEST_URI} ^/app/uploads/.*$
# RewriteRule ^(.*)$ https://livesite.com.au/$1 [QSA,L]
#### If URL is not XYZ, then redirect to XYZ
# RewriteCond %{HTTP_HOST} !^example\.com
# RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
#### Permanent page redirects
# RewriteRule ^old-url?$ /new-url [R=301]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
' >> web/.htaccess
# When STAGE-ing - add the stage.php script
# ---------------------------------------------
if [[ ${IS_STAGE} == 1 ]] ; then
# Pull the stage.php script from Github Gists
curl -L https://gist.githubusercontent.com/mcnamee/d07145548a864a18ab786f66675efc66/raw/stage.php --output stage.php
# Add the Git Repo to pull
sed -i "s,git_repo_url,"$GIT_REPO_URL_GIT",g" stage.php
# Fix any file permissions
CORRECT_USER=$(stat -c '%U' ../)
chown -R ${CORRECT_USER} .
fi
# Output the next steps
# ---------------------------------------------
echo -e "\n ✓ Setup Successfully!"
echo -e " "
echo -e " Wordpress has been installed at: ${URL}"
echo -e " and you can login at: ${URL}/wp/wp-admin"
echo -e " "
echo -e " We're running the install of dependencies in the background"
echo -e " - so it may be a couple of minutes before the site is 100% ready"
echo -e " "
echo -e " Next Steps:"
echo -e " 1. Download any assets (images, files etc) to: ${SITE_ROOT}"
echo -e " 2. Import the Database to the new DB: ${DIR_NAME}"
if [[ ${IS_STAGE} == 1 ]] ; then
echo -e " 3. Add the dev server's Public Key ( https://pass.pvtl.io/index.php/pwd/view/4922 ) to the Git Repo (Github: Settings > Deploy Key)"
echo -e " 4. Add the following webhook URL to your git repo, to trigger auto-deploys (Github: Settings > Webhooks)"
echo -e " - ${URL_STAGE_SCRIPT}"
fi
echo -e ""