-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
run-bknix-job - Validate port availability before doing anything
This should lead to clearer error messages when there's a conflict with a lingering mysqld.
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters