Skip to content

Commit a52e1ef

Browse files
Ensured PHP-FPM is terminated before installation
- Check if a server is listening on port 9000. - If the process belongs to a previous CFEngine deployment, attempt to terminate it using fuser. - Abort installation if termination fails or if the process does not belong to CFEngine. Ticket: ENT-12704 ChangeLog: None
1 parent 7a7e0cb commit a52e1ef

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

packaging/common/cfengine-hub/preinstall.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,51 @@ if migrating_postgres; then
240240
safe_cp "$PREFIX/share" postgresql "$BACKUP_DIR/share"
241241
fi
242242

243+
#
244+
# We check if there is a server listening on port 9000.
245+
# If one is found and belongs to CFEngine,
246+
# then we try to shut it down using fuser.
247+
# If that does not work, we abort the installation.
248+
#
249+
ensure_php_fpm_terminated() {
250+
PHP_FPM_RUNNING=$(filter_netstat_listen ":9000\\s")
251+
if [ -z "$PHP_FPM_RUNNING" ]; then
252+
return 0;
253+
fi
254+
255+
cf_console echo "There seems to be a server listening on port 9000."
256+
257+
phpfpmpid=$(echo "$PHP_FPM_RUNNING" | sed -r -e '/pid=/!d' -e 's/.*pid=([0-9]+),.*/\1/' | tail -1)
258+
phpfpmargs=$(ps -p "$phpfpmpid" -o args=)
259+
260+
if echo "$phpfpmargs" | grep -q "cfengine"; then
261+
cf_console echo "The PHP-FPM process belongs to a previous CFEngine deployment, shutting it down."
262+
cf_console echo "Attempting to terminate the process using fuser."
263+
if ! command -v fuser >/dev/null; then
264+
cf_console echo "fuser not available, can't kill!"
265+
return 1
266+
fi
267+
268+
fuser -k -TERM -n tcp 9000
269+
270+
sleep 5s
271+
PHP_FPM_FINAL_CHECK=$(filter_netstat_listen ":9000\\s")
272+
if [ -n "$PHP_FPM_FINAL_CHECK" ]; then
273+
cf_console echo "There is still a process listening on port 9000. Please kill it manually before retrying. Aborting."
274+
return 1
275+
fi
276+
else
277+
cf_console echo "The PHP-FPM process is not from a previous CFEngine deployment"
278+
cf_console echo "This scenario is not supported, aborting installation"
279+
ps -p `fuser -n tcp 9000 2>/dev/null` -o args=
280+
return 1
281+
fi
282+
283+
return 0
284+
}
285+
286+
ensure_php_fpm_terminated || exit 1
287+
243288
#
244289
# We check if there is a server listening on port 80 or port 443.
245290
# If one is found, then we try to shut it down by calling

0 commit comments

Comments
 (0)