Exemple #1
0
 public function LaunchServer(DBServer $DBServer, Scalr_Server_LaunchOptions $launchOptions = null)
 {
     $environment = $DBServer->GetEnvironmentObject();
     if (!$launchOptions) {
         $launchOptions = new Scalr_Server_LaunchOptions();
         $DBRole = DBRole::loadById($DBServer->roleId);
         $launchOptions->imageId = $DBRole->getImageId(SERVER_PLATFORMS::GCE, $DBServer->GetProperty(GCE_SERVER_PROPERTIES::CLOUD_LOCATION));
         $launchOptions->serverType = $DBServer->GetFarmRoleObject()->GetSetting(DBFarmRole::SETTING_GCE_MACHINE_TYPE);
         $launchOptions->cloudLocation = $DBServer->GetFarmRoleObject()->CloudLocation;
         $userData = $DBServer->GetCloudUserData();
         $launchOptions->architecture = 'x86_64';
         $networkName = $DBServer->GetFarmRoleObject()->GetSetting(DBFarmRole::SETTING_GCE_NETWORK);
     } else {
         $userData = array();
         $networkName = 'default';
     }
     if ($DBServer->status == SERVER_STATUS::TEMPORARY) {
         $keyName = "SCALR-ROLESBUILDER-" . SCALR_ID;
     } else {
         $keyName = "FARM-{$DBServer->farmId}-" . SCALR_ID;
     }
     $sshKey = Scalr_SshKey::init();
     if (!$sshKey->loadGlobalByName($keyName, "", $DBServer->envId, SERVER_PLATFORMS::GCE)) {
         $keys = $sshKey->generateKeypair();
         if ($keys['public']) {
             $sshKey->farmId = $DBServer->farmId;
             $sshKey->clientId = $DBServer->clientId;
             $sshKey->envId = $DBServer->envId;
             $sshKey->type = Scalr_SshKey::TYPE_GLOBAL;
             $sshKey->cloudLocation = "";
             $sshKey->cloudKeyName = $keyName;
             $sshKey->platform = SERVER_PLATFORMS::GCE;
             $sshKey->save();
             $publicKey = $keys['public'];
         } else {
             throw new Exception("Scalr unable to generate ssh keypair");
         }
     } else {
         $publicKey = $sshKey->getPublic();
     }
     $gce = $this->getClient($environment, $launchOptions->cloudLocation);
     //
     //
     // Check firewall
     $firewalls = $gce->firewalls->listFirewalls($environment->getPlatformConfigValue(self::PROJECT_ID));
     $firewallFound = false;
     foreach ($firewalls->getItems() as $f) {
         if ($f->getName() == 'scalr-system') {
             $firewallFound = true;
             break;
         }
     }
     // Create scalr firewall
     if (!$firewallFound) {
         $firewall = new Google_Firewall();
         $firewall->setName('scalr-system');
         $firewall->setNetwork($this->getObjectUrl($networkName, 'networks', $environment->getPlatformConfigValue(self::PROJECT_ID)));
         //Get scalr IP-pool IP list and set source ranges
         $firewall->setSourceRanges(\Scalr::config('scalr.aws.ip_pool'));
         // Set ports
         $tcp = new Google_FirewallAllowed();
         $tcp->setIPProtocol('tcp');
         $tcp->setPorts(array('1-65535'));
         $udp = new Google_FirewallAllowed();
         $udp->setIPProtocol('udp');
         $udp->setPorts(array('1-65535'));
         $firewall->setAllowed(array($tcp, $udp));
         // Set target tags
         $firewall->setTargetTags(array('scalr'));
         $gce->firewalls->insert($environment->getPlatformConfigValue(self::PROJECT_ID), $firewall);
     }
     ///
     ////
     //////
     $instance = new Google_Instance();
     $instance->setKind("compute#instance");
     $accessConfig = new Google_AccessConfig();
     $accessConfig->setName("External NAT");
     $accessConfig->setType("ONE_TO_ONE_NAT");
     $network = new Google_NetworkInterface();
     $network->setNetwork($this->getObjectUrl($networkName, 'networks', $environment->getPlatformConfigValue(self::PROJECT_ID)));
     $network->setAccessConfigs(array($accessConfig));
     $instance->setNetworkInterfaces(array($network));
     $serviceAccount = new Google_ServiceAccount();
     $serviceAccount->setEmail("default");
     $serviceAccount->setScopes(array("https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/compute", "https://www.googleapis.com/auth/devstorage.full_control"));
     $instance->setServiceAccounts(array($serviceAccount));
     if ($launchOptions->cloudLocation != 'x-scalr-custom') {
         $availZone = $launchOptions->cloudLocation;
     } else {
         $location = $DBServer->GetFarmRoleObject()->GetSetting(DBFarmRole::SETTING_GCE_CLOUD_LOCATION);
         $availZones = array();
         if (stristr($location, "x-scalr-custom")) {
             $zones = explode("=", $location);
             foreach (explode(":", $zones[1]) as $zone) {
                 if ($zone != "") {
                     array_push($availZones, $zone);
                 }
             }
         }
         sort($availZones);
         $availZones = array_reverse($availZones);
         $servers = $DBServer->GetFarmRoleObject()->GetServersByFilter(array("status" => array(SERVER_STATUS::RUNNING, SERVER_STATUS::INIT, SERVER_STATUS::PENDING)));
         $availZoneDistribution = array();
         foreach ($servers as $cDbServer) {
             if ($cDbServer->serverId != $DBServer->serverId) {
                 $availZoneDistribution[$cDbServer->GetProperty(GCE_SERVER_PROPERTIES::CLOUD_LOCATION)]++;
             }
         }
         $sCount = 1000000;
         foreach ($availZones as $zone) {
             if ((int) $availZoneDistribution[$zone] <= $sCount) {
                 $sCount = (int) $availZoneDistribution[$zone];
                 $availZone = $zone;
             }
         }
         $aZones = implode(",", $availZones);
         // Available zones
         $dZones = "";
         // Zones distribution
         foreach ($availZoneDistribution as $zone => $num) {
             $dZones .= "({$zone}:{$num})";
         }
         $DBServer->SetProperty("tmp.gce.avail_zone.algo2", "[A:{$aZones}][D:{$dZones}][S:{$availZone}]");
     }
     $instance->setZone($this->getObjectUrl($availZone, 'zones', $environment->getPlatformConfigValue(self::PROJECT_ID)));
     $instance->setMachineType($this->getObjectUrl($launchOptions->serverType, 'machineTypes', $environment->getPlatformConfigValue(self::PROJECT_ID), $availZone));
     $instance->setImage($this->getObjectUrl($launchOptions->imageId, 'images', $environment->getPlatformConfigValue(self::PROJECT_ID)));
     $instance->setName($DBServer->serverId);
     $tags = array('scalr', "env-{$DBServer->envId}");
     if ($DBServer->farmId) {
         $tags[] = "farm-{$DBServer->farmId}";
     }
     if ($DBServer->farmRoleId) {
         $tags[] = "farmrole-{$DBServer->farmRoleId}";
     }
     $gTags = new Google_Tags();
     $gTags->setItems($tags);
     $instance->setTags($gTags);
     $metadata = new Google_Metadata();
     $items = array();
     // Set user data
     foreach ($userData as $k => $v) {
         $uData .= "{$k}={$v};";
     }
     $uData = trim($uData, ";");
     if ($uData) {
         $item = new Google_MetadataItems();
         $item->setKey('scalr');
         $item->setValue($uData);
         $items[] = $item;
     }
     // Add SSH Key
     $item = new Google_MetadataItems();
     $item->setKey("sshKeys");
     $item->setValue("scalr:{$publicKey}");
     $items[] = $item;
     $metadata->setItems($items);
     $instance->setMetadata($metadata);
     try {
         $result = $gce->instances->insert($environment->getPlatformConfigValue(self::PROJECT_ID), $availZone, $instance);
     } catch (Exception $e) {
         throw new Exception(sprintf(_("Cannot launch new instance. %s (%s, %s)"), $e->getMessage(), $launchOptions->imageId, $launchOptions->serverType));
     }
     if ($result->id) {
         $DBServer->SetProperty(GCE_SERVER_PROPERTIES::PROVISIONING_OP_ID, $result->name);
         $DBServer->SetProperty(GCE_SERVER_PROPERTIES::SERVER_NAME, $DBServer->serverId);
         $DBServer->SetProperty(GCE_SERVER_PROPERTIES::SERVER_ID, $DBServer->serverId);
         $DBServer->SetProperty(GCE_SERVER_PROPERTIES::CLOUD_LOCATION, $availZone);
         $DBServer->SetProperty(GCE_SERVER_PROPERTIES::MACHINE_TYPE, $launchOptions->serverType);
         $DBServer->SetProperty(SERVER_PROPERTIES::ARCHITECTURE, $launchOptions->architecture);
         return $DBServer;
     } else {
         throw new Exception(sprintf(_("Cannot launch new instance. %s (%s, %s)"), serialize($result), $launchOptions->imageId, $launchOptions->serverType));
     }
 }
Exemple #2
0
 $new_instance->setNetworkInterfaces(array($googleNetworkInterfaceObj));
 $insertInstance = $computeService->instances->insert(DEFAULT_PROJECT, $zone, $new_instance);
 $insertInstanceMarkup = generateMarkup('Insert Instance', $insertInstance);
 /**
  * Google Compute Engine API request to insert a new instance (with metadata)
  * into your Google Compute Engine project.
  */
 $name = DEFAULT_NAME_WITH_METADATA;
 $machineType = DEFAULT_MACHINE_TYPE;
 $zone = DEFAULT_ZONE_NAME;
 $image = DEFAULT_IMAGE;
 $googleNetworkInterfaceObj = new Google_NetworkInterface();
 $network = DEFAULT_NETWORK;
 $googleNetworkInterfaceObj->setNetwork($network);
 $metadataItemsObj = new Google_MetadataItems();
 $metadataItemsObj->setKey('startup-script');
 $metadataItemsObj->setValue('apt-get install apache2');
 $metadata = new Google_Metadata();
 $metadata->setItems(array($metadataItemsObj));
 $new_instance = new Google_Instance();
 $new_instance->setName($name);
 $new_instance->setImage($image);
 $new_instance->setMachineType($machineType);
 $new_instance->setNetworkInterfaces(array($googleNetworkInterfaceObj));
 $new_instance->setMetadata($metadata);
 $insertInstanceWithMetadata = $computeService->instances->insert(DEFAULT_PROJECT, $zone, $new_instance);
 $insertInstanceWithMetadataMarkup = generateMarkup('Insert Instance With Metadata', $insertInstanceWithMetadata);
 /**
  * Google Compute Engine API request to get an instance matching the outlined
  * parameters from your Google Compute Engine project.
  */