Scalr\Modules\Platforms\Azure\Helpers\AzureHelper::cleanupServerObjects PHP Method

cleanupServerObjects() public static method

public static cleanupServerObjects ( DBServer $dbServer )
$dbServer DBServer
    public static function cleanupServerObjects(\DBServer $dbServer)
    {
        $env = $dbServer->GetEnvironmentObject();
        $azure = $env->azure();
        // Remove NIC
        $nic = $dbServer->GetProperty(\AZURE_SERVER_PROPERTIES::NETWORK_INTERFACE);
        if ($nic) {
            try {
                $res1 = $azure->network->interface->delete($env->keychain(SERVER_PLATFORMS::AZURE)->properties[Entity\CloudCredentialsProperty::AZURE_SUBSCRIPTION_ID], $dbServer->GetProperty(\AZURE_SERVER_PROPERTIES::RESOURCE_GROUP), $nic);
            } catch (\Exception $e) {
                \Scalr::getContainer()->logger(\LOG_CATEGORY::FARM)->error(new \FarmLogMessage($dbServer, sprintf(_("Unable to remove NIC object on server termination: %s"), $e->getMessage())));
            }
        }
        // Remove Public IP
        $publicIpName = $dbServer->GetProperty(\AZURE_SERVER_PROPERTIES::PUBLIC_IP_NAME);
        if ($publicIpName) {
            try {
                $res2 = $azure->network->publicIPAddress->delete($env->keychain(SERVER_PLATFORMS::AZURE)->properties[Entity\CloudCredentialsProperty::AZURE_SUBSCRIPTION_ID], $dbServer->GetProperty(\AZURE_SERVER_PROPERTIES::RESOURCE_GROUP), $publicIpName);
            } catch (\Exception $e) {
                \Scalr::getContainer()->logger(\LOG_CATEGORY::FARM)->error(new \FarmLogMessage($dbServer, sprintf(_("Unable to remove PublicIP object on server termination: %s"), $e->getMessage())));
            }
        }
    }

Usage Example

Example #1
0
 /**
  * Removes server from database
  * @return void
  */
 public function Remove()
 {
     try {
         // 1. Clean up cloud objects
         if ($this->platform == SERVER_PLATFORMS::AZURE) {
             AzureHelper::cleanupServerObjects($this);
         }
         // 2. Cleanup scalr db
         $this->Db->BeginTrans();
         // We need to perpetuate server_properties records for removed servers
         $this->Db->Execute("DELETE FROM servers WHERE server_id=?", array($this->serverId));
         $this->Db->Execute("DELETE FROM messages WHERE server_id=?", array($this->serverId));
         $this->Db->Execute("\n                UPDATE `dm_deployment_tasks` SET status=? WHERE server_id=?\n            ", array(Scalr_Dm_DeploymentTask::STATUS_ARCHIVED, $this->serverId));
         $importantProperties = \SERVER_PROPERTIES::getImportantList();
         $properties = array_diff(array_keys($this->GetAllProperties()), $importantProperties);
         if (!empty($properties)) {
             $and = " AND (";
             foreach ($properties as $name) {
                 $and .= "name=" . $this->Db->qstr($name) . " OR ";
             }
             $and = substr($and, 0, -3) . ")";
             $this->Db->Execute("DELETE FROM server_properties WHERE server_id=?" . $and, [$this->serverId]);
         }
         $this->Db->CommitTrans();
     } catch (Exception $e) {
         $this->Db->RollbackTrans();
         throw $e;
     }
 }