Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update site-status check for auth enabled enviournment #108

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions src/helper/site-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,15 +283,22 @@ function site_status_check( $site_url ) {
EE::log( 'Checking and verifying site-up status. This may take some time.' );
$httpcode = get_curl_info( $site_url );
$i = 0;
$auth = false;
while ( 200 !== $httpcode && 302 !== $httpcode && 301 !== $httpcode ) {
EE::debug( "$site_url status httpcode: $httpcode" );
$httpcode = get_curl_info( $site_url );
if ( 401 === $httpcode ) {
$user_pass = get_global_auth();
$auth = $user_pass['username'] . ':' . $user_pass['password'];
}
$httpcode = get_curl_info( $site_url, 80, false, $auth );
echo '.';
sleep( 2 );
if ( $i ++ > 60 ) {
break;
}
}
EE::debug( "$site_url status httpcode: $httpcode" );
echo PHP_EOL;
if ( 200 !== $httpcode && 302 !== $httpcode && 301 !== $httpcode ) {
throw new \Exception( 'Problem connecting to site!' );
}
Expand All @@ -304,17 +311,21 @@ function site_status_check( $site_url ) {
* @param string $url url to get info about.
* @param int $port The port to check.
* @param bool $port_info Return port info or httpcode.
* @param mixed $auth Send http auth with passed value if not false.
*
* @return bool|int port occupied or httpcode.
*/
function get_curl_info( $url, $port = 80, $port_info = false ) {
function get_curl_info( $url, $port = 80, $port_info = false, $auth = false ) {

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_NOBODY, true );
curl_setopt( $ch, CURLOPT_TIMEOUT, 10 );
curl_setopt( $ch, CURLOPT_PORT, $port );
if ( $auth ) {
curl_setopt( $ch, CURLOPT_USERPWD, $auth );
}
curl_exec( $ch );
if ( $port_info ) {
return empty( curl_getinfo( $ch, CURLINFO_PRIMARY_IP ) );
Expand Down Expand Up @@ -405,3 +416,27 @@ function configure_postfix( $site_url, $site_fs_path ) {
function reload_global_nginx_proxy() {
\EE::launch( sprintf( 'docker exec %s sh -c "/app/docker-entrypoint.sh /usr/local/bin/docker-gen /app/nginx.tmpl /etc/nginx/conf.d/default.conf; /usr/sbin/nginx -s reload"', EE_PROXY_TYPE ) );
}

/**
* Get global auth if it exists.
*/
function get_global_auth() {
if ( ! class_exists( '\EE\Model\Auth' ) ) {
return false;
}

$auth = \EE\Model\Auth::where( [
'site_url' => 'default',
'scope' => 'site',
] );

if ( empty( $auth ) ) {
return false;
}

return [
'username' => $auth[0]->username,
'password' => $auth[0]->password,
];

}