Skip to content

Commit

Permalink
run-bknix-job - Validate port availability before doing anything
Browse files Browse the repository at this point in the history
This should lead to clearer error messages when there's a conflict with a
lingering mysqld.
  • Loading branch information
totten committed Aug 21, 2024
1 parent 86f96ee commit 7b3edcb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .loco/bin/loco-check-ports
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env php
<?php

### Pre-validate that TCP ports are open/available for binding services

/**
* Determine if a service is listening for connections
*
* @param string $host
* @param int $port
* @return bool
*/
function checkHostPort($host, $port) {
$fp = @fsockopen($host, $port, $errno, $errstr, 1);
$result = $fp ? TRUE : FALSE;
if ($fp !== FALSE) {
@fclose($fp);
}
return $result;
}

$ip = getenv('LOCALHOST') ?: '127.0.0.1';
$vars = ['CHROME_PORT', 'MAIL_SMTP_PORT', 'MAIL_HTTP_PORT', 'MEMCACHED_PORT', 'MYSQLD_PORT', 'PHPFPM_PORT', 'REDIS_PORT'];
$errors = [];
foreach ($vars as $var) {
$value = getenv($var);
if ($value && checkHostPort($ip, $value)) {
$errors[] = sprintf("Port check failed. %s=%d but %s:%s is already in use!\n", $var, $value, $ip, $value);
}
}

if (empty($errors)) {
exit(0);
}
else {
echo implode("", $errors);
exit(1);
}
6 changes: 6 additions & 0 deletions nix/bin/run-bknix-job
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ function run_bknix_main() {
assert_common BKPROF BKITBLD LOCO_PRJ BKIT
LOADED_BKPROF="$BKPROF"
(cd "$LOCO_PRJ" && loco clean)
if (cd "$LOCO_PRJ" && loco sh . -- .loco/bin/loco-check-ports); then
true
else
echo >&2 "Port check failed. Is this a zombie attack?"
exit 1
fi
(cd "$LOCO_PRJ" && loco start)
RUN_BKNIX_CLEANUP_FUNCS+=('run_bknix_stop_loco')

Expand Down

0 comments on commit 7b3edcb

Please sign in to comment.