-
Notifications
You must be signed in to change notification settings - Fork 3
/
create-vhost.sh
executable file
·274 lines (238 loc) · 5.48 KB
/
create-vhost.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
#!/bin/bash
function error {
echo "$(tput setaf 1) ! $1$(tput sgr 0)"
}
function success {
echo "$(tput setaf 2)$1$(tput sgr 0)"
}
currentdir=`pwd`
who=`whoami`
if [ "$who" = "vagrant" ]
then
error "This script is supposed to be run from the host, not from the guest"
exit 1
fi
echo
echo "= APPLICATION CONFIGURATION ="
echo
success "We have detected the following path for the Vagrant root:"
success " > $currentdir"
# Checking Vagrant root path
echo
while true
do
read -p "Is that correct? [Y/n] " correct
if [ "$correct" = "y" ] || [ -z "$correct" ]
then
correct="y"
break
elif [ "$correct" = "n" ]
then
break
else
error "You must answer the question using 'y' or 'n'"
fi
done
# Vagrant root path must be fixed
if [ "$correct" = "n" ]
then
while true
do
read -p "Please enter the Vagrant root path: " vagrantroot
if [ -z "$vagrantroot" ]
then
error "Vagrant root cannot be empty"
elif [ ! -d "$vagrantroot" ]
then
error "The Vagrant root '$vagrantroot' does not exist"
elif [ ! -d "$vagrantroot/apache/conf" ]
then
error "Could not find the Apache conf directory in '$vagrantroot/apache/conf'"
else
success "Thank you!"
break
fi
done
elif [ "$correct" = "y" ] || [ -z "$correct" ]
then
success "Thank you!"
vagrantroot=$currentdir
else
error "You must answer the question using 'y' or 'n'"
fi
# Checking whether Vagrant machine is running
echo
echo "= CHECKING VAGRANT MACHINE STATUS ="
cd $vagrantroot
cmd=`vagrant status |grep "running" > /dev/null 2>&1`
if [ $? -ne 0 ]
then
error "The Vagrant machine does not seem to be in running state"
while true
do
read -p "Do you want to start the machine? This is mandatory in order to run this app [Y/n] " startmachine
if [ -z "$startmachine" ]
then
startmachine="y"
fi
if [ "$startmachine" != "y" ] && [ "$startmachine" != "n" ]
then
error "Please answer with 'y' or 'n'"
else
if [ "$startmachine" = "y" ]
then
success "Thank you! Now starting machine"
vagrant up
break
else
success "Thank you! Unfortunately, I must leave you now..."
exit 0
fi
fi
done
else
success "The Vagrant machine is running"
fi
# Getting the site hostname
append=false
echo
while true
do
read -p "What is the subdomain of \"vagrant.vm\" would you like to add? " hostname
if [ -z "$hostname" ]
then
error "The subdomain cannot be empty, please retry..."
else
hostname=
elif [ -f "$vagrantroot/apache/conf/$hostname.conf" ]
then
error "The configuration file '$vagrantroot/apache/conf/$hostname.conf' already exists."
echo
echo "What should I do?"
PS3='Please enter your choice: '
options=("Show" "Overwrite" "Append" "Abort")
select opt in "${options[@]}"
do
case $opt in
"Abort")
success "OK, bye!"
exit 0
;;
"Show")
success "Sure thing! Here is the content of the file:"
echo
cat $vagrantroot/apache/conf/$hostname.conf
if [ $? -ne 0 ]
then
error "Cannot get the configuration file content"
fi
echo
;;
"Overwrite")
rm -f "$vagrantroot/apache/conf/$hostname.conf"
if [ $? -ne 0 ]
then
error "Could not remove existing configuration file. You should do it manually..."
else
success "Existing configuration file was removed"
fi
echo
break 2
;;
"Append")
append="true"
success "OK, the new configuration will be added to the existing file"
echo
break 2
;;
*) error "Please choose a valid option";;
esac
done
else
success "Thank you!"
break
fi
done
# Getting the site path
echo
while true
do
read -p "What is the path of the vhost you want to add? /vagrant/www/" path
if [ -z "$path" ]
then
error "The path cannot be empty, please retry..."
else
path="/vagrant/www/$path"
vagrant ssh -c "ls $path" > /dev/null 2>&1
if [ $? -ne 0 ]
then
error "Directory '$path' does not exist on Vagrant"
else
success "Thank you!"
break
fi
fi
done
# Getting vhost public document root path
echo
while true
do
read -p "What is the public document root path? $path/" public
if [ -z "$public" ]
then
public=$path
success "Thank you!"
break
else
public="$path/$public"
vagrant ssh -c "ls $public" > /dev/null 2>&1
if [ $? -ne 0 ]
then
error "Directory '$public' does not exist on Vagrant"
else
success "Thank you!"
break
fi
fi
done
# All configuration done, executing...
echo
success "Thank you, everything is now fine. We are processing your request"
echo
# Managing the Apache configuration
echo
echo "= ADDING VHOST TO APACHE ="
vhost="<VirtualHost *:80>
ServerName $hostname.vagrant.vm
DocumentRoot $public
<Directory $path>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>"
sudo -- sh -c -e "echo '$vhost' >> $vagrantroot/apache/conf/$hostname.conf"
if [ $? -ne 0 ]
then
error "Could not create the '$vagrantroot/apache/conf/$hostname.conf' file automatically"
error "You should create it with the following line manually:"
error "$vhost"
else
success "Hostname successfully added to host's file"
fi
echo
echo "= RELOADING APACHE CONFIGURATION ="
vagrant ssh -c "sudo service apache2 reload" > /dev/null 2>&1
if [ $? -ne 0 ]
then
error "Could not reload Apache's configuration automatically"
error "You should reload it manually using the following command:"
error " sudo service apache2 reload"
else
success "Apache successfully reloaded"
fi
echo
echo
success "END, thank you!"
success "You may now access the vhost:"
success "http://$hostname.vagrant.vm/"
echo