@@ -22,13 +22,13 @@ CONF="apache2.conf"
22
22
# Platform specific apache extras
23
23
EXTRA_CONF=' '
24
24
if [[ $platform == ' Linux' ]]; then
25
- EXTRA_CONF="
25
+ EXTRA_CONF="
26
26
# Include module configuration:
27
27
Include /etc/apache2/mods-enabled/*.load
28
28
Include /etc/apache2/mods-enabled/*.conf
29
29
"
30
30
elif [[ $platform == ' Darwin' ]]; then
31
- EXTRA_CONF="
31
+ EXTRA_CONF="
32
32
# Modules
33
33
$( cat /etc/apache2/httpd.conf | grep LoadModule | sed ' s/libexec/\/usr\/libexec/g' )
34
34
LoadModule php5_module /usr/libexec/apache2/libphp5.so
37
37
38
38
# Include phpmyadmin only if there
39
39
if [[ -f " /etc/apache2/conf.d/phpmyadmin.conf" ]]; then
40
- EXTRA_CONF+="
40
+ EXTRA_CONF+="
41
41
Include /etc/apache2/conf.d/phpmyadmin.conf
42
42
"
43
43
fi
@@ -64,9 +64,9 @@ MaxSpareServers 1
64
64
# Serve our workspace
65
65
DocumentRoot "${WORKSPACE} "
66
66
<Directory />
67
- AllowOverride all
68
- Order allow,deny
69
- Allow from all
67
+ AllowOverride all
68
+ Order allow,deny
69
+ Allow from all
70
70
</Directory>
71
71
72
72
AddType application/x-httpd-php .php
@@ -77,30 +77,116 @@ ${EXTRA_CONF}
77
77
78
78
EOF
79
79
80
+ # Keep track of MySQL setup state
81
+ SUDO_MYSQL=false
82
+ MYSQL_STARTED=false
83
+ MYSQL_PORT=3306
84
+
85
+ function is_mysql_running() {
86
+ # Check if there is a TCP server listening on MySQL's port
87
+ netstat -nat | grep -i listen | grep -e " [\:\.]${MYSQL_ROOT} " & > /dev/null
88
+ # Check for success
89
+ if [ $? = 0 ]; then
90
+ echo " true"
91
+ fi
92
+ }
93
+
94
+ # Echoes sudo if the system supports sudo without password for current user (codebox.io boxes for example)
95
+ function needs_sudo_pwd() {
96
+ sudo -n echo | head -n 1 | grep -q -v " sudo:"
97
+ local success=$?
98
+ if [ success = 0 ]; then
99
+ # No password needed
100
+ echo " true"
101
+ fi
102
+ }
103
+
104
+ # Start mysql and set SUDO_MYSQL
105
+ function start_mysql() {
106
+ echo " Starting MySQL server ..."
107
+
108
+ # Exit if MySQL is not on $PATH
109
+ if [ -z " $( which mysqld) " ]; then
110
+ echo " Could not start MySQL because it is not installed on the system's \$ PATH"
111
+ fi
112
+
113
+ # Check if MySQL is already running
114
+ if [ -n " $( is_mysql_running) " ]; then
115
+ echo " MySQL appears to already be running on PORT=${MYSQL_PORT} "
116
+ return
117
+ fi
118
+
119
+ locals needs_pwd=" $( needs_sudo_pwd) "
120
+ # Try running in sudo or not
121
+ if [ -n ${needs_pwd} ]; then
122
+ sudo -n mysqld &
123
+ SUDO_MYSQL=true
124
+ else
125
+ mysqld &
126
+ fi
127
+
128
+ # If MySQL is not running
129
+ if [ -z " $( is_mysql_running) " ]; then
130
+ # Try running with sudo (and passwd prompt)
131
+ if [ ! $SUDO_MYSQL ]; then
132
+ echo " Please enter sudo password for MySQL"
133
+ sudo mysqld &
134
+ fi
135
+ else
136
+ echo " MySQL is up and running"
137
+ fi
138
+
139
+ # After all our different tries is MySQL up ?
140
+ if [ -n " $( is_mysql_running) " ]; then
141
+ MYSQL_STARTED=true
142
+ fi
143
+ }
144
+
145
+ # Stop MySQL started by "start_mysql"
146
+ function stop_mysql() {
147
+ echo " Killing MySQL"
148
+ # MySQL wasn't started by this script
149
+ # or is already dead
150
+ if [ ! ${MYSQL_STARTED} || -z " $( is_mysql_running) " ]; then
151
+ # So do nothing
152
+ return
153
+ fi
154
+
155
+ # Force kill MySQL
156
+ killall -s KILL mysqld
157
+ }
80
158
81
159
# Wait for a process or group of processes
82
160
function anywait() {
83
- for pid in " $@ " ; do
84
- while kill -0 " $pid " & > /dev/null; do
85
- sleep 0.5
86
- done
87
- done
161
+ for pid in " $@ " ; do
162
+ while kill -0 " $pid " & > /dev/null; do
163
+ sleep 0.5
164
+ done
165
+ done
88
166
}
89
167
90
168
function cleanup {
91
- if [[ -f ${PID_FILE} ]]; then
92
- echo " Killed process"
93
- # Kill process and all children
94
- /bin/kill -s KILL -$( cat ${PID_FILE} )
95
- fi
96
- # Remove folder on exit
97
- echo " Cleaning up ${FOLDER} "
98
- rm -rf ${FOLDER}
169
+ # Kill Apache
170
+ if [[ -f ${PID_FILE} ]]; then
171
+ echo " Killed process"
172
+ # Kill process and all children
173
+ /bin/kill -s KILL -$( cat ${PID_FILE} )
174
+ fi
175
+
176
+ # Kill MySQL
177
+ stop_mysql
178
+
179
+ # Remove folder on exit
180
+ echo " Cleaning up ${FOLDER} "
181
+ rm -rf ${FOLDER}
99
182
}
100
183
101
184
# Cleanup when killed
102
185
trap cleanup EXIT INT KILL
103
186
187
+ # Run MySQL
188
+ start_mysql
189
+
104
190
# Run apache process in foreground
105
191
echo " Running apache2 on ${WORKSPACE} (${FOLDER} )"
106
192
/usr/sbin/apachectl -d ${FOLDER} -f ${CONF} -e info
@@ -112,4 +198,7 @@ ${DIR}/_waitfile.sh ${PID_FILE} 5
112
198
PID=$( cat ${PID_FILE} )
113
199
echo " Waiting for Apache2 process : ${PID} "
114
200
anywait ${PID}
115
- echo " Apache is dead (pid=${PID} )"
201
+ echo " Apache is dead (pid=${PID} )"
202
+
203
+ # Cleanup on exit
204
+ cleanup
0 commit comments