Ryan Lane has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/104320


Change subject: Refactor pub/priv DNS for multi-region support
......................................................................

Refactor pub/priv DNS for multi-region support

Public and private DNS was mostly segregated in the last refactor,
but this refactoring fully segregates them into two subclasses of
OpenStackNovaHost.

This refactor was necessary because the code was previously getting
private DNS by specifying the instanceid, but no region, which would
return the incorrect record if the instanceid was identical. By
splitting the classes apart it made it easier to modify the
constructor for private DNS without changing the public DNS code.

This change also modifies how private DNS domains are fetched.
Rather than fetching a domain based on the instance ID. It fetches
the domain based on the region, which simplifies the logic and
number of LDAP queries.

Change-Id: Idf2cc62d8916eb9e6cac218c16be76e0087d1333
---
M OpenStackManager.php
M nova/OpenStackNovaController.php
M nova/OpenStackNovaDomain.php
M nova/OpenStackNovaHost.php
M nova/OpenStackNovaInstance.php
A nova/OpenStackNovaPrivateHost.php
A nova/OpenStackNovaPublicHost.php
M special/SpecialNovaInstance.php
8 files changed, 320 insertions(+), 336 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/OpenStackManager 
refs/changes/20/104320/1

diff --git a/OpenStackManager.php b/OpenStackManager.php
index 8b006b9..3077643 100644
--- a/OpenStackManager.php
+++ b/OpenStackManager.php
@@ -149,6 +149,8 @@
 $wgAutoloadClasses['OpenStackNovaUser'] = $dir . 'nova/OpenStackNovaUser.php';
 $wgAutoloadClasses['OpenStackNovaDomain'] = $dir . 
'nova/OpenStackNovaDomain.php';
 $wgAutoloadClasses['OpenStackNovaHost'] = $dir . 'nova/OpenStackNovaHost.php';
+$wgAutoloadClasses['OpenStackNovaPublicHost'] = $dir . 
'nova/OpenStackNovaPublicHost.php';
+$wgAutoloadClasses['OpenStackNovaPrivateHost'] = $dir . 
'nova/OpenStackNovaPrivateHost.php';
 $wgAutoloadClasses['OpenStackNovaAddress'] = $dir . 
'nova/OpenStackNovaAddress.php';
 $wgAutoloadClasses['OpenStackNovaSecurityGroup'] = $dir . 
'nova/OpenStackNovaSecurityGroup.php';
 $wgAutoloadClasses['OpenStackNovaSecurityGroupRule'] = $dir . 
'nova/OpenStackNovaSecurityGroupRule.php';
diff --git a/nova/OpenStackNovaController.php b/nova/OpenStackNovaController.php
index b5cfaa6..10603b4 100644
--- a/nova/OpenStackNovaController.php
+++ b/nova/OpenStackNovaController.php
@@ -149,7 +149,7 @@
                if ( $ret['code'] === 200 ) {
                        $server = self::_get_property( $ret['body'], 'server' );
                        if ( $server ) {
-                               return new OpenStackNovaInstance( $server, true 
);
+                               return new OpenStackNovaInstance( $server, 
$this->getRegion(), true );
                        }
                }
                return null;
@@ -243,7 +243,7 @@
                        return $instancesarr;
                }
                foreach ( $instances as $instance ) {
-                       $instance = new OpenStackNovaInstance( $instance, true 
);
+                       $instance = new OpenStackNovaInstance( $instance, 
$this->getRegion(), true );
                        $id = $instance->getInstanceOSId();
                        $instancesarr[$id] = $instance;
                }
@@ -457,7 +457,7 @@
                if ( $ret['code'] !== 202 ) {
                        return null;
                }
-               $instance = new OpenStackNovaInstance( $ret['body']->server );
+               $instance = new OpenStackNovaInstance( $ret['body']->server, 
$this->getRegion() );
 
                return $instance;
        }
diff --git a/nova/OpenStackNovaDomain.php b/nova/OpenStackNovaDomain.php
index 1871456..f895f4d 100644
--- a/nova/OpenStackNovaDomain.php
+++ b/nova/OpenStackNovaDomain.php
@@ -190,34 +190,21 @@
        }
 
        /**
-        * Get a domain by an instance's ID. Return null if the instance ID 
entry
+        * Get a domain by a region. Return null if the region
         * does not exist.
         *
         * @static
         * @param  $instanceid
         * @return null|OpenStackNovaDomain
         */
-       static function getDomainByInstanceId( $instanceid ) {
-               global $wgAuth;
-               global $wgOpenStackManagerLDAPInstanceBaseDN;
-
-               OpenStackNovaLdapConnection::connect();
-
-               $result = LdapAuthenticationPlugin::ldap_search( 
$wgAuth->ldapconn, $wgOpenStackManagerLDAPInstanceBaseDN,
-                                                               
'(associateddomain=' . $instanceid . '.*)' );
-               $hostInfo = LdapAuthenticationPlugin::ldap_get_entries( 
$wgAuth->ldapconn, $result );
-               if ( $hostInfo['count'] == "0" ) {
-                       return null;
+       static function getDomainByRegion( $region ) {
+               $domain = OpenStackNovaDomain::getDomainByName( $region );
+               if ( $domain ) {
+                       if ( $domain->getLocation() ) {
+                               return $domain;
+                       }
                }
-               $fqdn = $hostInfo[0]['associateddomain'][0];
-               $domainname = explode( '.', $fqdn );
-               $domainname = $domainname[1];
-               $domain = new OpenStackNovaDomain( $domainname );
-               if ( $domain->domainInfo ) {
-                       return $domain;
-               } else {
-                       return null;
-               }
+               return null;
        }
 
        # TODO: Allow generic domains; get rid of config set base name
diff --git a/nova/OpenStackNovaHost.php b/nova/OpenStackNovaHost.php
index 991bea8..2bdf3b1 100644
--- a/nova/OpenStackNovaHost.php
+++ b/nova/OpenStackNovaHost.php
@@ -8,25 +8,11 @@
  */
 
 class OpenStackNovaHost {
-       /**
-        * @var bool
-        */
-       var $private;
-
-       /**
-        * @var string
-        */
-       var $instanceid;
 
        /**
         * @var string
         */
        var $hostDN;
-
-       /**
-        * @var string
-        */
-       var $ip;
 
        /**
         * @var mixed
@@ -37,166 +23,6 @@
         * @var OpenStackNovaDomain
         */
        var $domainCache;
-
-       /**
-        * @param  $instanceid
-        * @param  $ip
-        *  (specify $instanceid for private, $ip for public)
-        */
-       function __construct( $private, $instanceid, $ip ) {
-               global $wgAuth;
-
-               $this->private = $private;
-               $this->instanceid = $instanceid;
-               $this->domainCache = null;
-               $this->ip = $ip;
-               OpenStackNovaLdapConnection::connect();
-               $this->fetchHostInfo();
-       }
-
-       /**
-        * Fetch the host from LDAP and initialize the object
-        *
-        * @return void
-        */
-       function fetchHostInfo() {
-               global $wgAuth;
-               global $wgOpenStackManagerLDAPInstanceBaseDN;
-
-               $this->instanceid = $wgAuth->getLdapEscapedString( 
$this->instanceid );
-               if ( $this->private ) {
-                       if ($this->getDomain()) {
-                               $fqdn = $this->instanceid . '.' . 
$this->getDomain()->getFullyQualifiedDomainName();
-                       } else {
-                               # No domain means no instance!
-                               $this->hostInfo = null;
-                               return;
-                       }
-                       $result = LdapAuthenticationPlugin::ldap_search( 
$wgAuth->ldapconn, $wgOpenStackManagerLDAPInstanceBaseDN, '(dc=' . $fqdn . ')' 
);
-               } else {
-                       $this->ip = $wgAuth->getLdapEscapedString( $this->ip );
-                       $result = LdapAuthenticationPlugin::ldap_search( 
$wgAuth->ldapconn, $wgOpenStackManagerLDAPInstanceBaseDN, '(dc=' . $this->ip . 
')' );
-               }
-               $this->hostInfo = LdapAuthenticationPlugin::ldap_get_entries( 
$wgAuth->ldapconn, $result );
-               if ( $this->hostInfo["count"] == "0" ) {
-                       $this->hostInfo = null;
-               } else {
-                       $this->hostDN = $this->hostInfo[0]['dn'];
-               }
-       }
-
-       /**
-        * Return a private host's host's fully qualified display name
-        *
-        * (Note that calling this for a public host doesn't make sense since 
public
-        *  host entries have multiple FQDNs.)
-        *
-        * @return string
-        */
-       function getFullyQualifiedDisplayName() {
-               global $wgAuth;
-
-               if ( ! $this->private ) {
-                       $wgAuth->printDebug( "getFullyQualifiedDisplayName 
called on public host, so this will probably break.", NONSENSITIVE );
-               }
-
-               if ( isset( $this->hostInfo[0]['associateddomain'] ) ) {
-                       $domains = $this->hostInfo[0]['associateddomain'];
-                       array_shift( $domains );
-                       foreach ( $domains as $domain ) {
-                               $pieces = explode( '.', $domain );
-                               $name = $pieces[0];
-                               if ( $name != $this->instanceid ) {
-                                       # A leap of faith:  There should
-                                       # be two associated domains, one based 
on the id and
-                                       # one the display name.  So, if this 
one isn't the id,
-                                       # it must be the display.
-                                       return $domain;
-                               }
-                       }
-               }
-
-               $wgAuth->printDebug( "Error: Unable to determine instancename 
of " . $this->instanceid, NONSENSITIVE );
-               return "";
-       }
-
-       /**
-        * Return the domain associated with this host
-        *
-        * @return OpenStackNovaDomain
-        */
-       function getDomain() {
-               global $wgAuth;
-
-               if ( ! $this->domainCache ) {
-                       if ( $this->private ) {
-                               $this->domainCache = 
OpenStackNovaDomain::getDomainByInstanceId( $this->instanceid );
-                               if (! $this->domainCache ) {
-                               $wgAuth->printDebug( "Looked up domain for id 
$this->instanceid but domainCache is still empty.", NONSENSITIVE );
-                               }
-                       } else {
-                               $this->domainCache = 
OpenStackNovaDomain::getDomainByHostIP( $this->ip );
-                               if (! $this->domainCache ) {
-                               $wgAuth->printDebug( "Looked up domain for ip 
$this->ip but domainCache is still empty.", NONSENSITIVE );
-                               }
-                       }
-               }
-               return $this->domainCache;
-       }
-
-       /**
-        * Return human-readable hostname
-        *
-        * @return string
-        */
-       function getDisplayName() {
-               $pieces = explode( '.', $this->getFullyQualifiedDisplayName() );
-               return $pieces[0];
-       }
-
-       /**
-        *
-        * Return i-xxxxx.<domain> for a private host
-        *
-        * (Note that calling this for a public host doesn't make sense since 
public
-        *  host entries have multiple FQDNs.)
-        *
-        * @return string
-        */
-       function getFullyQualifiedHostName() {
-               global $wgAuth;
-
-               if ( ! $this->private ) {
-                       $wgAuth->printDebug( "getFullyQualifiedDisplayName 
called on a public host; this will probably break.", NONSENSITIVE );
-               }
-
-               return $this->instanceid . '.' . 
$this->getDomain()->getFullyQualifiedDomainName();
-       }
-
-       /**
-        * Return the puppet classes and variables assigned to this host
-        *
-        * @return array
-        */
-       function getPuppetConfiguration() {
-               $puppetinfo = array( 'puppetclass' => array(), 'puppetvar' => 
array() );
-               if ( isset( $this->hostInfo[0]['puppetclass'] ) ) {
-                       array_shift( $this->hostInfo[0]['puppetclass'] );
-                       foreach ( $this->hostInfo[0]['puppetclass'] as $class ) 
{
-                               $puppetinfo['puppetclass'][] = $class;
-                       }
-               }
-               if ( isset( $this->hostInfo[0]['puppetvar'] ) ) {
-                       array_shift( $this->hostInfo[0]['puppetvar'] );
-                       foreach ( $this->hostInfo[0]['puppetvar'] as $variable 
) {
-                               $vararr = explode( '=', $variable );
-                               $varname = trim( $vararr[0] );
-                               $var = trim( $vararr[1] );
-                               $puppetinfo['puppetvar'][$varname] = $var;
-                       }
-               }
-               return $puppetinfo;
-       }
 
        /**
         * Return all arecords associated with this host. Return an empty
@@ -244,62 +70,6 @@
                }
 
                return $cnamerecords;
-       }
-
-       /**
-        * Update puppet classes and variables for this host.
-        *
-        * @param  $puppetinfo
-        * @return bool
-        */
-       function modifyPuppetConfiguration( $puppetinfo ) {
-               global $wgAuth;
-               global $wgOpenStackManagerPuppetOptions;
-
-               $hostEntry = array();
-               if ( $wgOpenStackManagerPuppetOptions['enabled'] ) {
-                       foreach ( 
$wgOpenStackManagerPuppetOptions['defaultclasses'] as $class ) {
-                               $hostEntry['puppetclass'][] = $class;
-                       }
-                       foreach ( 
$wgOpenStackManagerPuppetOptions['defaultvariables'] as $variable => $value ) {
-                               $hostEntry['puppetvar'][] = $variable . '=' . 
$value;
-                       }
-                       if ( isset( $puppetinfo['classes'] ) ) {
-                               foreach ( $puppetinfo['classes'] as $class ) {
-                                       $hostEntry['puppetclass'][] = $class;
-                               }
-                       }
-                       if ( isset( $puppetinfo['variables'] ) ) {
-                               foreach ( $puppetinfo['variables'] as $variable 
=> $value ) {
-                                       $hostEntry['puppetvar'][] = $variable . 
'=' . $value;
-                               }
-                       }
-                       $oldpuppetinfo = $this->getPuppetConfiguration();
-                       if ( isset( $oldpuppetinfo['puppetvar'] ) ) {
-                               $wgAuth->printDebug( "Checking for preexisting 
variables", NONSENSITIVE );
-                               foreach ( $oldpuppetinfo['puppetvar'] as 
$variable => $value ) {
-                                       $wgAuth->printDebug( "Found $variable", 
NONSENSITIVE );
-                                       if ( $variable === "instanceproject" || 
$variable === "instancename" ) {
-                                               $hostEntry['puppetvar'][] = 
$variable . '=' . $value;
-                                       }
-                               }
-                       }
-                       if ( $hostEntry ) {
-                               $success = 
LdapAuthenticationPlugin::ldap_modify( $wgAuth->ldapconn, $this->hostDN, 
$hostEntry );
-                               if ( $success ) {
-                                       $this->fetchHostInfo();
-                                       $wgAuth->printDebug( "Successfully 
modified puppet configuration for host", NONSENSITIVE );
-                                       return true;
-                               } else {
-                                       $wgAuth->printDebug( "Failed to modify 
puppet configuration for host", NONSENSITIVE );
-                                       return false;
-                               }
-                       } else {
-                               $wgAuth->printDebug( "No hostEntry when trying 
to modify puppet configuration", NONSENSITIVE );
-                               return false;
-                       }
-               }
-               return false;
        }
 
        /**
@@ -459,24 +229,6 @@
        }
 
        /**
-        * Get a host by the host's short name, and a OpenStackNovaDomain 
object. Returns
-        * null if the entry does not exist.
-        *
-        * @static
-        * @param  $instanceid
-        * @param  $domain
-        * @return OpenStackNovaHost
-        */
-       static function getPrivateHost( $instanceid ) {
-               $host = new OpenStackNovaHost( true, $instanceid, null );
-               if ( $host->hostInfo ) {
-                       return $host;
-               } else {
-                       return null;
-               }
-       }
-
-       /**
         * Get a public host by the host's ip. Returns
         * null if the entry does not exist.
         *
@@ -487,7 +239,7 @@
        static function getHostByPublicIP( $ip ) {
                global $wgAuth;
 
-               $host = new OpenStackNovaHost( false, null, $ip );
+               $host = new OpenStackNovaPublicHost( $ip );
                if ( $host->hostInfo ) {
                        return $host;
                } else {
@@ -500,63 +252,16 @@
         *
         * @static
         * @param  $instanceid
+        * @param  $region
         * @return OpenStackNovaHost
         */
-       static function getHostByInstanceId( $instanceid ) {
-               return self::getPrivateHost( $instanceid );
-       }
-
-       /**
-        * Get private host entries that has the specified IP address assigned. 
Returns
-        * null if none is found.
-        *
-        * @static
-        * @param  $ip
-        * @return array
-        */
-       static function getHostByPrivateIP( $ip ) {
-               global $wgAuth;
-               global $wgOpenStackManagerLDAPInstanceBaseDN;
-
-               $result = LdapAuthenticationPlugin::ldap_search( 
$wgAuth->ldapconn, $wgOpenStackManagerLDAPInstanceBaseDN, '(arecord=' . $ip . 
')' );
-               $hostInfo = LdapAuthenticationPlugin::ldap_get_entries( 
$wgAuth->ldapconn, $result );
-               if ( $hostInfo["count"] == "0" ) {
-                       return null;
+       static function getHostByInstanceId( $instanceid, $region ) {
+               $host = new OpenStackNovaPrivateHost( $instanceid, $region );
+               if ( $host->hostInfo ) {
+                       return $host;
                } else {
-                       $host = $hotsInfo[0];
-                       $instanceid = $host['dc'][0];
-                       $hostObject = OpenStackNovaHost::getHostByInstanceId( 
$instanceid );
-                       return $hostObject;
+                       return null;
                }
-       }
-
-       /**
-        * Get all host entries in the specified domain. Returns an empty array
-        * if no entries are found.
-        *
-        * @static
-        * @param  $domain OpenStackNovaDomain
-        * @return array
-        */
-       static function getAllHosts( $domain ) {
-               global $wgAuth;
-
-               OpenStackNovaLdapConnection::connect();
-
-               $hosts = array();
-               $result = LdapAuthenticationPlugin::ldap_search( 
$wgAuth->ldapconn, $domain->domainDN, '(dc=*)' );
-               if ( $result ) {
-                       $entries = LdapAuthenticationPlugin::ldap_get_entries( 
$wgAuth->ldapconn, $result );
-                       if ( $entries ) {
-                               # First entry is always a count
-                               array_shift( $entries );
-                               foreach ( $entries as $entry ) {
-                                       $hosts[] = new OpenStackNovaHost( true, 
$entry['dc'][0], null );
-                               }
-                       }
-               }
-
-               return $hosts;
        }
 
        /**
@@ -574,10 +279,10 @@
                $success = LdapAuthenticationPlugin::ldap_delete( 
$wgAuth->ldapconn, $this->hostDN );
                if ( $success ) {
                        $domain->updateSOA();
-                       $wgAuth->printDebug( "Successfully deleted host " . 
$this->instanceid, NONSENSITIVE );
+                       $wgAuth->printDebug( "Successfully deleted host 
$this->hostDN", NONSENSITIVE );
                        return true;
                } else {
-                       $wgAuth->printDebug( "Failed to delete host " . 
$this->instanceid, NONSENSITIVE );
+                       $wgAuth->printDebug( "Failed to delete host 
$this->hostDN", NONSENSITIVE );
                        return false;
                }
        }
@@ -594,7 +299,7 @@
         * @param  $puppetinfo
         * @return OpenStackNovaHost
         */
-       static function addHost( $instance, $domain, $puppetinfo = array() ) {
+       static function addHostFromInstance( $instance, $domain, $puppetinfo = 
array() ) {
                global $wgAuth;
                global $wgOpenStackManagerLDAPInstanceBaseDN, 
$wgOpenStackManagerPuppetOptions;
 
@@ -610,8 +315,9 @@
                        $ip = null;
                }
                $domainname = $domain->getFullyQualifiedDomainName();
+               $region = $domain->getLocation();
                $fqdn = $instanceid . '.' . $domainname;
-               $host = OpenStackNovaHost::getHostByInstanceId( $instanceid );
+               $host = OpenStackNovaHost::getHostByInstanceId( $instanceid, 
$region );
                if ( $host ) {
                        $wgAuth->printDebug( "Failed to add host $hostname as 
the DNS entry already exists", NONSENSITIVE );
                        return null;
@@ -658,10 +364,10 @@
                $success = LdapAuthenticationPlugin::ldap_add( 
$wgAuth->ldapconn, $dn, $hostEntry );
                if ( $success ) {
                        $domain->updateSOA();
-                       $wgAuth->printDebug( "Successfully added host 
$hostname", NONSENSITIVE );
-                       return new OpenStackNovaHost( true, $hostname, null );
+                       $wgAuth->printDebug( "Successfully added host $fqdn", 
NONSENSITIVE );
+                       return OpenStackNovaHost::getHostByInstanceId( 
$instanceid, $region );
                } else {
-                       $wgAuth->printDebug( "Failed to add host $hostname with 
dn of $dn", NONSENSITIVE );
+                       $wgAuth->printDebug( "Failed to add host $fqdn with dn 
of $dn", NONSENSITIVE );
                        return null;
                }
        }
diff --git a/nova/OpenStackNovaInstance.php b/nova/OpenStackNovaInstance.php
index 44d5325..0f75ae2 100644
--- a/nova/OpenStackNovaInstance.php
+++ b/nova/OpenStackNovaInstance.php
@@ -12,6 +12,11 @@
        var $instance;
 
        /**
+        * @var string
+        */
+       var $region;
+
+       /**
         * @var OpenStackNovaHost
         */
        var $host;
@@ -20,8 +25,9 @@
         * @param  $apiInstanceResponse
         * @param bool $loadhost, optional
         */
-       function __construct( $apiInstanceResponse, $loadhost = false ) {
+       function __construct( $apiInstanceResponse, $region, $loadhost = false 
) {
                $this->instance = $apiInstanceResponse;
+               $this->region = $region;
                if ( $loadhost ) {
                        $this->loadHost();
                } else {
@@ -30,7 +36,7 @@
        }
 
        function loadHost() {
-               $this->host = OpenStackNovaHost::getHostByInstanceId( 
$this->getInstanceId() );
+               $this->host = OpenStackNovaHost::getHostByInstanceId( 
$this->getInstanceId(), $this->region );
        }
 
        /**
diff --git a/nova/OpenStackNovaPrivateHost.php 
b/nova/OpenStackNovaPrivateHost.php
new file mode 100644
index 0000000..fd55381
--- /dev/null
+++ b/nova/OpenStackNovaPrivateHost.php
@@ -0,0 +1,218 @@
+<?php
+
+/**
+ * todo comment me
+ *
+ * @file
+ * @ingroup Extensions
+ */
+
+class OpenStackNovaPrivateHost extends OpenStackNovaHost {
+
+       /**
+        * @var string
+        */
+       var $instanceid;
+
+       /**
+        * @var string
+        */
+       var $region;
+
+       /**
+        * @param  $instanceid
+        * @param  $ip
+        *  (specify $instanceid for private, $ip for public)
+        */
+       function __construct( $instanceid, $region ) {
+               global $wgAuth;
+
+               $this->instanceid = $instanceid;
+               $this->region = $region;
+               $this->domainCache = null;
+               OpenStackNovaLdapConnection::connect();
+               $this->fetchHostInfo();
+       }
+
+       /**
+        * Fetch the host from LDAP and initialize the object
+        *
+        * @return void
+        */
+       function fetchHostInfo() {
+               global $wgAuth;
+               global $wgOpenStackManagerLDAPInstanceBaseDN;
+
+               $this->instanceid = $wgAuth->getLdapEscapedString( 
$this->instanceid );
+               if ($this->getDomain()) {
+                       $fqdn = $this->instanceid . '.' . 
$this->getDomain()->getFullyQualifiedDomainName();
+               } else {
+                       # No domain means no instance!
+                       $this->hostInfo = null;
+                       return;
+               }
+               $result = LdapAuthenticationPlugin::ldap_search( 
$wgAuth->ldapconn, $wgOpenStackManagerLDAPInstanceBaseDN, '(dc=' . $fqdn . ')' 
);
+               $this->hostInfo = LdapAuthenticationPlugin::ldap_get_entries( 
$wgAuth->ldapconn, $result );
+               if ( $this->hostInfo["count"] == "0" ) {
+                       $this->hostInfo = null;
+               } else {
+                       $this->hostDN = $this->hostInfo[0]['dn'];
+               }
+       }
+
+       /**
+        * Return a private host's host's fully qualified display name
+        *
+        * (Note that calling this for a public host doesn't make sense since 
public
+        *  host entries have multiple FQDNs.)
+        *
+        * @return string
+        */
+       function getFullyQualifiedDisplayName() {
+               global $wgAuth;
+
+               print_r( 'test' );
+               if ( isset( $this->hostInfo[0]['associateddomain'] ) ) {
+                       $domains = $this->hostInfo[0]['associateddomain'];
+                       array_shift( $domains );
+                       foreach ( $domains as $domain ) {
+                               $pieces = explode( '.', $domain );
+                               $name = $pieces[0];
+                               if ( $name != $this->instanceid ) {
+                                       # A leap of faith:  There should
+                                       # be two associated domains, one based 
on the id and
+                                       # one the display name.  So, if this 
one isn't the id,
+                                       # it must be the display.
+                                       return $domain;
+                               }
+                       }
+               }
+
+               $wgAuth->printDebug( "Error: Unable to determine instancename 
of " . $this->instanceid, NONSENSITIVE );
+               return "";
+       }
+
+       /**
+        * Return the domain associated with this host
+        *
+        * @return OpenStackNovaDomain
+        */
+       function getDomain() {
+               global $wgAuth;
+
+               if ( ! $this->domainCache ) {
+                       $this->domainCache = 
OpenStackNovaDomain::getDomainByRegion( $this->region );
+                       if (! $this->domainCache ) {
+                               $wgAuth->printDebug( "Looked up domain for 
region $this->region but domainCache is still empty.", NONSENSITIVE );
+                       }
+               }
+               return $this->domainCache;
+       }
+
+       /**
+        * Return human-readable hostname
+        *
+        * @return string
+        */
+       function getDisplayName() {
+               $pieces = explode( '.', $this->getFullyQualifiedDisplayName() );
+               return $pieces[0];
+       }
+
+       /**
+        *
+        * Return i-xxxxx.<domain> for a private host
+        *
+        * (Note that calling this for a public host doesn't make sense since 
public
+        *  host entries have multiple FQDNs.)
+        *
+        * @return string
+        */
+       function getFullyQualifiedHostName() {
+               global $wgAuth;
+
+               return $this->instanceid . '.' . 
$this->getDomain()->getFullyQualifiedDomainName();
+       }
+
+       /**
+        * Return the puppet classes and variables assigned to this host
+        *
+        * @return array
+        */
+       function getPuppetConfiguration() {
+               $puppetinfo = array( 'puppetclass' => array(), 'puppetvar' => 
array() );
+               if ( isset( $this->hostInfo[0]['puppetclass'] ) ) {
+                       array_shift( $this->hostInfo[0]['puppetclass'] );
+                       foreach ( $this->hostInfo[0]['puppetclass'] as $class ) 
{
+                               $puppetinfo['puppetclass'][] = $class;
+                       }
+               }
+               if ( isset( $this->hostInfo[0]['puppetvar'] ) ) {
+                       array_shift( $this->hostInfo[0]['puppetvar'] );
+                       foreach ( $this->hostInfo[0]['puppetvar'] as $variable 
) {
+                               $vararr = explode( '=', $variable );
+                               $varname = trim( $vararr[0] );
+                               $var = trim( $vararr[1] );
+                               $puppetinfo['puppetvar'][$varname] = $var;
+                       }
+               }
+               return $puppetinfo;
+       }
+
+       /**
+        * Update puppet classes and variables for this host.
+        *
+        * @param  $puppetinfo
+        * @return bool
+        */
+       function modifyPuppetConfiguration( $puppetinfo ) {
+               global $wgAuth;
+               global $wgOpenStackManagerPuppetOptions;
+
+               $hostEntry = array();
+               if ( $wgOpenStackManagerPuppetOptions['enabled'] ) {
+                       foreach ( 
$wgOpenStackManagerPuppetOptions['defaultclasses'] as $class ) {
+                               $hostEntry['puppetclass'][] = $class;
+                       }
+                       foreach ( 
$wgOpenStackManagerPuppetOptions['defaultvariables'] as $variable => $value ) {
+                               $hostEntry['puppetvar'][] = $variable . '=' . 
$value;
+                       }
+                       if ( isset( $puppetinfo['classes'] ) ) {
+                               foreach ( $puppetinfo['classes'] as $class ) {
+                                       $hostEntry['puppetclass'][] = $class;
+                               }
+                       }
+                       if ( isset( $puppetinfo['variables'] ) ) {
+                               foreach ( $puppetinfo['variables'] as $variable 
=> $value ) {
+                                       $hostEntry['puppetvar'][] = $variable . 
'=' . $value;
+                               }
+                       }
+                       $oldpuppetinfo = $this->getPuppetConfiguration();
+                       if ( isset( $oldpuppetinfo['puppetvar'] ) ) {
+                               $wgAuth->printDebug( "Checking for preexisting 
variables", NONSENSITIVE );
+                               foreach ( $oldpuppetinfo['puppetvar'] as 
$variable => $value ) {
+                                       $wgAuth->printDebug( "Found $variable", 
NONSENSITIVE );
+                                       if ( $variable === "instanceproject" || 
$variable === "instancename" ) {
+                                               $hostEntry['puppetvar'][] = 
$variable . '=' . $value;
+                                       }
+                               }
+                       }
+                       if ( $hostEntry ) {
+                               $success = 
LdapAuthenticationPlugin::ldap_modify( $wgAuth->ldapconn, $this->hostDN, 
$hostEntry );
+                               if ( $success ) {
+                                       $this->fetchHostInfo();
+                                       $wgAuth->printDebug( "Successfully 
modified puppet configuration for host", NONSENSITIVE );
+                                       return true;
+                               } else {
+                                       $wgAuth->printDebug( "Failed to modify 
puppet configuration for host", NONSENSITIVE );
+                                       return false;
+                               }
+                       } else {
+                               $wgAuth->printDebug( "No hostEntry when trying 
to modify puppet configuration", NONSENSITIVE );
+                               return false;
+                       }
+               }
+               return false;
+       }
+
+}
diff --git a/nova/OpenStackNovaPublicHost.php b/nova/OpenStackNovaPublicHost.php
new file mode 100644
index 0000000..354b09f
--- /dev/null
+++ b/nova/OpenStackNovaPublicHost.php
@@ -0,0 +1,65 @@
+<?php
+
+/**
+ * todo comment me
+ *
+ * @file
+ * @ingroup Extensions
+ */
+
+class OpenStackNovaPublicHost extends OpenStackNovaHost {
+
+       /**
+        * @var string
+        */
+       var $ip;
+
+       /**
+        * @param  $ip
+        */
+       function __construct( $ip ) {
+               global $wgAuth;
+
+               $this->domainCache = null;
+               $this->ip = $ip;
+               OpenStackNovaLdapConnection::connect();
+               $this->fetchHostInfo();
+       }
+
+       /**
+        * Fetch the host from LDAP and initialize the object
+        *
+        * @return void
+        */
+       function fetchHostInfo() {
+               global $wgAuth;
+               global $wgOpenStackManagerLDAPInstanceBaseDN;
+
+               $this->ip = $wgAuth->getLdapEscapedString( $this->ip );
+               $result = LdapAuthenticationPlugin::ldap_search( 
$wgAuth->ldapconn, $wgOpenStackManagerLDAPInstanceBaseDN, '(dc=' . $this->ip . 
')' );
+               $this->hostInfo = LdapAuthenticationPlugin::ldap_get_entries( 
$wgAuth->ldapconn, $result );
+               if ( $this->hostInfo["count"] == "0" ) {
+                       $this->hostInfo = null;
+               } else {
+                       $this->hostDN = $this->hostInfo[0]['dn'];
+               }
+       }
+
+       /**
+        * Return the domain associated with this host
+        *
+        * @return OpenStackNovaDomain
+        */
+       function getDomain() {
+               global $wgAuth;
+
+               if ( ! $this->domainCache ) {
+                       $this->domainCache = 
OpenStackNovaDomain::getDomainByHostIP( $this->ip );
+                       if (! $this->domainCache ) {
+                               $wgAuth->printDebug( "Looked up domain for ip 
$this->ip but domainCache is still empty.", NONSENSITIVE );
+                       }
+               }
+               return $this->domainCache;
+       }
+
+}
diff --git a/special/SpecialNovaInstance.php b/special/SpecialNovaInstance.php
index 5e42e9f..d27496a 100644
--- a/special/SpecialNovaInstance.php
+++ b/special/SpecialNovaInstance.php
@@ -637,7 +637,7 @@
                        $instance = $this->userNova->getInstance( $instanceId );
                }
                if ( $instance ) {
-                       $host = OpenStackNovaHost::addHost( $instance, $domain, 
$this->getPuppetInfo( $formData ) );
+                       $host = OpenStackNovaHost::addHostFromInstance( 
$instance, $domain, $this->getPuppetInfo( $formData ) );
 
                        if ( $host ) {
                                $instance->setHost( $host );

-- 
To view, visit https://gerrit.wikimedia.org/r/104320
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idf2cc62d8916eb9e6cac218c16be76e0087d1333
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/OpenStackManager
Gerrit-Branch: master
Gerrit-Owner: Ryan Lane <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to