Scalr\Modules\Platforms\Ec2\Helpers\EipHelper::farmUpdateRoleSettings PHP Method

farmUpdateRoleSettings() public static method

public static farmUpdateRoleSettings ( DBFarmRole $DBFarmRole, $oldSettings, $newSettings )
$DBFarmRole DBFarmRole
    public static function farmUpdateRoleSettings(DBFarmRole $DBFarmRole, $oldSettings, $newSettings)
    {
        $db = \Scalr::getDb();
        $DBFarm = $DBFarmRole->GetFarmObject();
        $DBFarmRole->SetSetting(Entity\FarmRoleSetting::AWS_ELASIC_IPS_MAP, null, Entity\FarmRoleSetting::TYPE_LCL);
        $aws = $DBFarm->GetEnvironmentObject()->aws($DBFarmRole->CloudLocation);
        $isVPC = $DBFarm->GetSetting(Entity\FarmSetting::EC2_VPC_ID);
        // Disassociate IP addresses if checkbox was unchecked
        if (empty($newSettings[Entity\FarmRoleSetting::AWS_USE_ELASIC_IPS]) && !empty($oldSettings[Entity\FarmRoleSetting::AWS_USE_ELASIC_IPS])) {
            $eips = $db->Execute("\n                SELECT * FROM elastic_ips WHERE farm_roleid = ?\n            ", array($DBFarmRole->ID));
            while ($eip = $eips->FetchRow()) {
                try {
                    $aws->ec2->address->disassociate($eip['ipaddress']);
                } catch (Exception $e) {
                }
            }
            $db->Execute("\n                DELETE FROM elastic_ips WHERE farm_roleid = ?\n            ", array($DBFarmRole->ID));
        }
        //TODO: Handle situation when tab was not opened, but max instances setting was changed.
        if (!empty($newSettings[Entity\FarmRoleSetting::AWS_ELASIC_IPS_MAP]) && !empty($newSettings[Entity\FarmRoleSetting::AWS_USE_ELASIC_IPS])) {
            $map = explode(";", $newSettings[Entity\FarmRoleSetting::AWS_ELASIC_IPS_MAP]);
            $maxIndex = 0;
            foreach ($map as $ipconfig) {
                list($serverIndex, $ipAddress) = explode("=", $ipconfig);
                if (!$serverIndex) {
                    continue;
                }
                if ($serverIndex > $maxIndex) {
                    $maxIndex = $serverIndex;
                }
                $allocationId = null;
                try {
                    $dbServer = DBServer::LoadByFarmRoleIDAndIndex($DBFarmRole->ID, $serverIndex);
                } catch (Exception $e) {
                }
                // Allocate new IP if needed
                if ((!$ipAddress || $ipAddress == '0.0.0.0') && $dbServer) {
                    $domain = $isVPC ? 'vpc' : null;
                    $address = $aws->ec2->address->allocate($domain);
                    $ipAddress = $address->publicIp;
                    $allocationId = $address->allocationId;
                }
                // Remove old association
                $db->Execute("\n                    DELETE FROM elastic_ips\n                    WHERE farm_roleid = ? AND instance_index=?\n                ", array($DBFarmRole->ID, $serverIndex));
                if ((!$ipAddress || $ipAddress == '0.0.0.0') && !$dbServer) {
                    continue;
                }
                if ($ipAddress) {
                    //Remove old IP association
                    $db->Execute("\n                        DELETE FROM elastic_ips\n                        WHERE ipaddress=?\n                    ", array($ipAddress));
                    if (!$allocationId && $isVPC) {
                        $allocationId = $aws->ec2->address->describe($ipAddress)->get(0)->allocationId;
                    }
                    // Associate IP with server in our db
                    $db->Execute("\n                        INSERT INTO elastic_ips\n                        SET env_id=?,\n                            farmid=?,\n                            farm_roleid=?,\n                            ipaddress=?,\n                            state='0',\n                            instance_id='',\n                            clientid=?,\n                            instance_index=?,\n                            allocation_id=?\n                    ", array($DBFarm->EnvID, $DBFarmRole->FarmID, $DBFarmRole->ID, $ipAddress, $DBFarm->ClientID, $serverIndex, $allocationId));
                    // Associate IP on AWS with running server
                    try {
                        $dbServer = DBServer::LoadByFarmRoleIDAndIndex($DBFarmRole->ID, $serverIndex);
                        $db->Execute("\n                            UPDATE elastic_ips\n                            SET state='1',\n                                server_id = ?\n                            WHERE ipaddress = ?\n                        ", array($dbServer->serverId, $ipAddress));
                        $update = false;
                        if ($dbServer->remoteIp != $ipAddress) {
                            if ($dbServer && $dbServer->status == \SERVER_STATUS::RUNNING) {
                                $fireEvent = self::associateIpAddress($dbServer, $ipAddress, $isVPC ? $allocationId : null);
                            }
                        }
                        if ($fireEvent) {
                            $event = new \IPAddressChangedEvent($dbServer, $ipAddress, $dbServer->localIp);
                            \Scalr::FireEvent($dbServer->farmId, $event);
                        }
                    } catch (Exception $e) {
                    }
                } else {
                    \Scalr::getContainer()->logger(\LOG_CATEGORY::FARM)->fatal(sprintf(_("Cannot allocate elastic ip address for instance %s on farm %s (2)"), $dbServer->serverId, $DBFarm->Name));
                }
            }
            // Remove extra definitions
            $db->Execute("DELETE FROM elastic_ips WHERE instance_index > ? AND farm_roleid = ?", array($maxIndex, $DBFarmRole->ID));
        }
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Clones FarmRoles with settings from this Farm to a new Farm
  *
  * @param   DBFarm  $newFarm    A Farm into which Roles be cloned
  *
  * @throws  Exception
  */
 public function cloneFarmRoles(DBFarm $newFarm)
 {
     foreach ($this->getDefinition()->roles as $idx => $role) {
         $dbFarmRole = $newFarm->AddRole(DBRole::loadById($role->roleId), $role->platform, $role->cloudLocation, $role->launchIndex, $role->alias);
         $oldRoleSettings = $dbFarmRole->GetAllSettings();
         $dbFarmRole->applyDefinition($role, true);
         $newSettings = $dbFarmRole->GetAllSettings();
         // Platform specified updates
         if ($dbFarmRole->Platform == SERVER_PLATFORMS::EC2) {
             \Scalr\Modules\Platforms\Ec2\Helpers\EbsHelper::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $newSettings);
             \Scalr\Modules\Platforms\Ec2\Helpers\EipHelper::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $newSettings);
             \Scalr\Modules\Platforms\Ec2\Helpers\ElbHelper::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $newSettings);
         }
         if (in_array($dbFarmRole->Platform, array(SERVER_PLATFORMS::IDCF, SERVER_PLATFORMS::CLOUDSTACK))) {
             CloudstackHelper::farmUpdateRoleSettings($dbFarmRole, $oldRoleSettings, $newSettings);
         }
     }
 }
All Usage Examples Of Scalr\Modules\Platforms\Ec2\Helpers\EipHelper::farmUpdateRoleSettings