Skip to content

Commit af0d6b9

Browse files
committed
Merge branch 'master' of github.com:FriendCode/codebox
2 parents 118462b + 817c7b9 commit af0d6b9

File tree

1 file changed

+109
-20
lines changed

1 file changed

+109
-20
lines changed

core/cb.project/php/run_apache.sh

Lines changed: 109 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ CONF="apache2.conf"
2222
# Platform specific apache extras
2323
EXTRA_CONF=''
2424
if [[ $platform == 'Linux' ]]; then
25-
EXTRA_CONF="
25+
EXTRA_CONF="
2626
# Include module configuration:
2727
Include /etc/apache2/mods-enabled/*.load
2828
Include /etc/apache2/mods-enabled/*.conf
2929
"
3030
elif [[ $platform == 'Darwin' ]]; then
31-
EXTRA_CONF="
31+
EXTRA_CONF="
3232
# Modules
3333
$(cat /etc/apache2/httpd.conf | grep LoadModule | sed 's/libexec/\/usr\/libexec/g')
3434
LoadModule php5_module /usr/libexec/apache2/libphp5.so
@@ -37,7 +37,7 @@ fi
3737

3838
# Include phpmyadmin only if there
3939
if [[ -f "/etc/apache2/conf.d/phpmyadmin.conf" ]]; then
40-
EXTRA_CONF+="
40+
EXTRA_CONF+="
4141
Include /etc/apache2/conf.d/phpmyadmin.conf
4242
"
4343
fi
@@ -64,9 +64,9 @@ MaxSpareServers 1
6464
# Serve our workspace
6565
DocumentRoot "${WORKSPACE}"
6666
<Directory />
67-
AllowOverride all
68-
Order allow,deny
69-
Allow from all
67+
AllowOverride all
68+
Order allow,deny
69+
Allow from all
7070
</Directory>
7171
7272
AddType application/x-httpd-php .php
@@ -77,30 +77,116 @@ ${EXTRA_CONF}
7777
7878
EOF
7979

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+
}
80158

81159
# Wait for a process or group of processes
82160
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
88166
}
89167

90168
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}
99182
}
100183

101184
# Cleanup when killed
102185
trap cleanup EXIT INT KILL
103186

187+
# Run MySQL
188+
start_mysql
189+
104190
# Run apache process in foreground
105191
echo "Running apache2 on ${WORKSPACE} (${FOLDER})"
106192
/usr/sbin/apachectl -d ${FOLDER} -f ${CONF} -e info
@@ -112,4 +198,7 @@ ${DIR}/_waitfile.sh ${PID_FILE} 5
112198
PID=$(cat ${PID_FILE})
113199
echo "Waiting for Apache2 process : ${PID}"
114200
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

Comments
 (0)