forked from alexc7018/vvv-site-wizard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vvv-apache
executable file
·339 lines (295 loc) · 11.1 KB
/
vvv-apache
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/bin/bash
# =============================================================================
# VVV Site Setup Script
# By Alison Barrett <[email protected]>
# https://github.com/aliso/vvv-site-wizard
#
# Version 1.0
#
# This script automates the creation & deletion of new WordPress sites using
# Varying Vagrant Vagrants (https://github.com/Varying-Vagrant-Vagrants/VVV).
#
# Usage:
# vvv new|create|make [site]
# vvv delete|rm|teardown [site]
# vvv list
# =============================================================================
#
# When not called from the VVV root directory, the script will prompt the user
# for the path to VVV. To run this from any directory and skip that prompt,
# uncomment the following line and set it to the VVV root dir:
# path=~/VVV/
# =============================================================================
# Helper function for printing out usage information.
# =============================================================================
usage() { echo -e "Usage: vvv new|create|make [site]\n vvv delete|rm|teardown [site]\n vvv list"; exit 1; }
# =============================================================================
# Helper function since a green "Done." is used in a lot of places
# =============================================================================
done_text() { echo -e "\033[32mDone.\033[0m"; return; }
# =============================================================================
# Helper function to make colored text easier to code and more readable
# within the code.
# =============================================================================
cecho() {
message=$1
color=${2:-'default'}
attribute=${3:-'none'}
color_type='3'
color_code='0'
if [[ $color = 'black' ]]; then
color_code='0'
elif [[ $color = 'red' ]]; then
color_code='1'
elif [[ $color = 'green' ]]; then
color_code='2'
elif [[ $color = 'yellow' ]]; then
color_code='3'
elif [[ $color = 'blue' ]]; then
color_code='4'
elif [[ $color = 'magenta' ]]; then
color_code='5'
elif [[ $color = 'cyan' ]]; then
color_code='6'
elif [[ $color = 'white' ]]; then
color_code='7'
fi
attribute_code=''
if [[ $attribute = 'bold' ]]; then
attribute_code=';1'
elif [[ $attribute = 'underlined' ]]; then
attribute_code=';4'
elif [[ $attribute = 'reverse' ]]; then
attribute_code=';7'
elif [[ $attribute = 'background' ]]; then
color_type='0;1;4'
fi
# set text attributes
echo -en "\033[${color_type}${color_code}${attribute_code}m"
# echo text
echo -en "$message"
# reset text attributes
echo -en "\033[0m\n"
return
}
# =============================================================================
# Vaaaaaars!
# =============================================================================
action=$1
site=$2
files_only=$3
# =============================================================================
# Get the VVV root directory if the user is entering a valid command
#
# This is placed outside of the action conditionals below because setting the
# directory is required for every possible action.
# =============================================================================
if [[ $action = 'new' || $action = 'make' || $action = 'create' || $action = 'delete' || $action = 'teardown' || $action = 'rm' || $action = 'list' ]]; then
# Get VVV root dir
if [ ! -z $path ]; then
path=$path
else
current_dir=`pwd`
if [ -e "$current_dir/Vagrantfile" ]; then
path=`pwd`
else
while [ -z $path ]; do
read -e -p "VVV install directory: " path
# Make sure directory is actually a VVV root
if [ ! -e "$path/Vagrantfile" ]; then
cecho "Path specified is not a VVV root directory." red bold
unset path
fi
done
fi
fi
path=${path%/}
fi
# =============================================================================
# Create a site
#
# New sites are created with a fresh installation of WordPress trunk that
# updates each time Vagrant is provisioned (vagrant up --provision).
#
# New site credentials:
# Username: admin
# Password: password
#
# If you want to see the files that are created without spinning up the new
# site, use a third parameter of 'filesonly'. Example:
# vvv [new|create|make] site filesonly
# =============================================================================
if [[ $action = 'new' || $action = 'make' || $action = 'create' ]]; then
cecho "\nNew VVV Site Setup" blue bold
# Prompt user for vars
# =============================================================================
# Get site dir name if not supplied as argument
if [ -d "$path/www/$site" ]; then
cecho "Directory $path/www/$site already exists." red
unset site
fi
while [ -z $site ]; do
read -e -p "Name of new site directory: " site
if [ -z $site ]; then
cecho "You must enter a directory name." red
elif [ -d "$path/www/$site" ]; then
cecho "Directory already exists." red
unset site
fi
done
# Get database name
db_name=$site
# Get local URL
echo -n "Domain to use (leave blank for $site.dev): "
read domain
if [ -z $domain ]; then
domain="$site.dev"
fi
# Inform the user of what's about to happen and give them a chance to back out
# =============================================================================
echo -e "\nAbout to perform the following:\n\n* Halt Vagrant (if running)\n* Create directory $site in $path/www\n* Create files vvv-init.sh, wp-cli.yml, and vvv-hosts in directory $site\n* Create file $site.conf in $path/config/apache-config/sites"
if [[ -z $files_only || $files_only != 'filesonly' ]]; then
echo -e "* Run \`vagrant up --provision\` to initialize site"
fi
echo -en "\n"
while [ -z $continue_create ]; do
echo -n "Continue (y/n)? "
read continue_create
if [[ $continue_create = 'n' ]]; then
cecho "Site setup aborted." red
exit
elif [[ $continue_create != 'y' ]]; then
cecho "Answer y or n." red
unset continue_create
fi
done
# Start the par-tay
# =============================================================================
cecho "\nNew VVV setup starting for site '$site'" green
cd $path
vagrant halt
# Create site folder with vvv-init.sh file
# =============================================================================
cd $path/www
echo -en "Creating site directory, wp-cli.yml, and vvv-init.sh file... "
mkdir $site && cd $site
printf "path: htdocs" > wp-cli.yml
printf "echo \"Creating database '$site' (if it does not exist)...\"\n\n"\
"mysql -u root --password=root -e \"CREATE DATABASE IF NOT EXISTS \\\`$db_name\\\`\"\n"\
"mysql -u root --password=root -e \"GRANT ALL PRIVILEGES ON \\\`$db_name\\\`.* TO wp@localhost IDENTIFIED BY 'wp';\"\n\n"\
"if [ ! -d \"htdocs\" ]; then\n"\
"\techo 'Installing WordPress in $site/htdocs...'\n"\
"\tmkdir ./htdocs\ncd ./htdocs\n"\
"\twp core download --allow-root\n"\
"\twp core config --dbname=\"$db_name\" --dbuser=wp --dbpass=wp --dbhost=\"localhost\" --allow-root\n"\
"\twp core install --url=$domain --title=\"$site - A WordPress Site\" --admin_user=admin --admin_password=password [email protected] --allow-root\n"\
"\t\tcd -\n"\
"else\n"\
"\techo 'Updating WordPress in $site/htdocs...'\n"\
"\twp core update --allow-root\n"\
"\twp core update-db --allow-root\n"\
"fi\n" > vvv-init.sh
done_text
# Add vvv-hosts file for domain in the site's www root
# =============================================================================
cd $path/www/$site
echo -en "Adding $domain to new vvv-hosts file... "
touch vvv-hosts
printf "$domain\n" >> vvv-hosts
done_text
# Add site conf file to apache-config
# =============================================================================
cd $path/config/apache-config/sites
echo -en "Creating apache-config/sites/$site.conf... "
sed -e "s/testserver\.com/$domain/" \
-e "s/wordpress-local/$site\/htdocs/" local-apache-example.conf-sample > $site.conf
done_text
# vagrant waaaaay up
# =============================================================================
cd $path
if [[ -z $files_only || $files_only != 'filesonly' ]]; then
echo -e "Running vagrant up --provision... "
vagrant up --provision
fi
# OMG we're done.
# =============================================================================
if [[ ! -z $files_only && $files_only == 'filesonly' ]]; then
cecho "\nNote: You must run \`vagrant up --provision\` to initialize the new site before $domain will load in a browser." red
fi
cecho "\nNew VVV Site Setup: Done!" blue bold
echo "Directory: $path/www/$site"
echo "URL: $domain"
echo "Username: admin"
echo "Password: password"
exit
# =============================================================================
# Delete a site
#
# This will remove the site folder from www and remove the .conf file from
# apache-config. Sites and folders installed by default with VVV cannot be
# deleted this way.
# =============================================================================
elif [[ $action = 'teardown' || $action = 'delete' || $action = 'rm' ]]; then
cecho "\nVVV Site Teardown" blue bold
# Get site dir name if not supplied as argument
while [ -z $site ]; do
echo -n "Site directory to delete: "
read site
if [[ -z $site || ! -d "$path/www/$site" ]]; then
cecho "You must enter a valid directory from $path/www/."
unset site
elif [[ $site = 'wordpress-default' || $site = 'wordpress-develop' || $site = 'wordpress-trunk' || $site = 'default' || $site = 'phpcs' || $site = 'vvv-hosts' || $site = 'wp-cli' || $site = '.gitshow' ]]; then
cecho "This script can't delete the default VVV folders." red
unset site
fi
done
# Start the deletion par-tay
# =============================================================================
echo -e "\nAbout to perform the following:\n\n* Halt Vagrant (if running)\n* Delete directory $site in $path/www\n* Delete file $site.conf in $path/config/apache-config/sites\n"
while [ -z $continue_delete ]; do
echo -n "Continue (y/n)? "
read continue_delete
if [ $continue_delete = 'y' ]; then
cecho "\nVVV teardown starting for site '$site'" green
cd $path
vagrant halt
# Delete the site folder
echo -en "Removing directory $site... "
rm -rf $path/www/$site
echo -e $done_text
# Remove the apache conf file
echo -en "Removing apache config file $site.conf... "
rm $path/config/apache-config/sites/$site.conf
echo -e $done_text
# Delorted.
cecho "\nVVV Site Teardown: Done!" blue bold
exit
elif [ $continue_delete = 'n' ]; then
cecho "Site teardown aborted." red
exit
else
cecho "Answer y or n." red
unset continue_delete
fi
done
exit
# =============================================================================
# List VVV sites
#
# This lists the VVV sites currently present in the www folder, including the
# sites installed by default with VVV (wordpress-default, wordpress-trunk,
# and wordpress-dev).
# =============================================================================
elif [[ $action = 'list' ]]; then
cd $path/www
find . -maxdepth 1 -mindepth 1 -type d -print0 | while IFS= read -d '' filename; do
filename=${filename:2}
if [[ $filename != 'default' && $filename != 'phpcs' && $filename != 'wp-cli' ]]; then
echo $filename
fi
done
exit
else
usage
exit
fi