Skip to content

Commit

Permalink
Merge pull request #108 from mrrobot47/update/site-status-check
Browse files Browse the repository at this point in the history
Update site-status check for auth enabled enviournment
  • Loading branch information
rahulsprajapati committed Sep 4, 2018
2 parents cf45e9e + 7081370 commit b487904
Showing 1 changed file with 37 additions and 2 deletions.
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,
];

}

0 comments on commit b487904

Please sign in to comment.