Пример #1
0
 /**
  * xLaunchInstanceAction
  *
  * @param string   $cloudLocation
  * @param string   $Engine
  * @param string   $DBInstanceIdentifier
  * @param string   $DBInstanceClass
  * @param string   $MasterUsername
  * @param RawData  $MasterUserPassword
  * @param string   $DBParameterGroup
  * @param string   $LicenseModel                optional
  * @param string   $OptionGroupName             optional
  * @param string   $AllocatedStorage            optional
  * @param string   $StorageType                 optional
  * @param int      $farmId                      optional
  * @param string   $DBName                      optional
  * @param int      $Port                        optional
  * @param string   $VpcId                       optional
  * @param JsonData $VpcSecurityGroups           optional
  * @param JsonData $DBSecurityGroups            optional
  * @param JsonData $SubnetIds                   optional
  * @param bool     $StorageEncrypted            optional
  * @param string   $KmsKeyId                    optional
  * @param string   $PreferredBackupWindow       optional
  * @param string   $CharacterSetName            optional
  * @param bool     $MultiAZ                     optional
  * @param bool     $AutoMinorVersionUpgrade     optional
  * @param string   $AvailabilityZone            optional
  * @param int      $Iops                        optional
  * @param string   $BackupRetentionPeriod       optional
  * @param string   $PreferredMaintenanceWindow  optional
  * @param string   $DBSubnetGroupName           optional
  * @param string   $EngineVersion               optional
  * @param bool     $PubliclyAccessible          optional
  * @throws Exception
  * @throws ScalrException
  */
 public function xLaunchInstanceAction($cloudLocation, $Engine, $DBInstanceIdentifier, $DBInstanceClass, $MasterUsername, RawData $MasterUserPassword, $DBParameterGroup, $LicenseModel = null, $OptionGroupName = null, $AllocatedStorage = null, $StorageType = null, $farmId = null, $DBName = null, $Port = null, $VpcId = null, JsonData $VpcSecurityGroups = null, JsonData $DBSecurityGroups = null, JsonData $SubnetIds = null, $StorageEncrypted = false, $KmsKeyId = null, $PreferredBackupWindow = null, $CharacterSetName = null, $MultiAZ = null, $AutoMinorVersionUpgrade = false, $AvailabilityZone = null, $Iops = null, $BackupRetentionPeriod = null, $PreferredMaintenanceWindow = null, $DBSubnetGroupName = null, $EngineVersion = null, $PubliclyAccessible = false)
 {
     $this->request->restrictAccess(Acl::RESOURCE_AWS_RDS, Acl::PERM_AWS_RDS_MANAGE);
     $aws = $this->getAwsClient($cloudLocation);
     if ($Engine == 'mysql') {
         $Engine = 'MySQL';
     }
     $request = new CreateDBInstanceRequestData($DBInstanceIdentifier, $DBInstanceClass, $Engine);
     if ($Engine == 'aurora') {
         $StorageType = 'aurora';
         $request->dBClusterIdentifier = strtolower($DBInstanceIdentifier);
     }
     if ($StorageEncrypted) {
         $request->storageEncrypted = $Engine != 'aurora' ? true : null;
         if ($KmsKeyId) {
             $kmsKey = $aws->kms->key->describe($KmsKeyId);
             if (!$kmsKey->enabled) {
                 throw new Exception("This KMS Key is disabled, please choose another one.");
             }
             $allowed = true;
             $governance = new Scalr_Governance($this->getEnvironmentId());
             $allowedKeys = $governance->getValue(SERVER_PLATFORMS::EC2, Scalr_Governance::AWS_KMS_KEYS, $cloudLocation);
             if (!empty($allowedKeys)) {
                 $allowed = false;
                 foreach ($allowedKeys['keys'] as $key) {
                     if ($key['id'] == $kmsKey->keyId) {
                         $allowed = true;
                         break;
                     }
                 }
             }
             if (!$allowed) {
                 throw new ScalrException("A KMS Policy is active in this Environment, access to '{$kmsKey->keyId}' has been restricted by account owner.");
             }
             $request->kmsKeyId = $Engine != 'aurora' ? $KmsKeyId : null;
         }
     }
     if (empty($request->dBClusterIdentifier)) {
         $request->allocatedStorage = $AllocatedStorage;
         $request->masterUsername = $MasterUsername;
         $request->masterUserPassword = (string) $MasterUserPassword;
         $request->dBName = $DBName ?: null;
         $request->port = $Port ?: null;
         $request->preferredBackupWindow = $PreferredBackupWindow ?: null;
         $vpcSgIds = [];
         foreach ($VpcSecurityGroups as $VpcSecurityGroup) {
             $vpcSgIds[] = $VpcSecurityGroup['id'];
         }
         $request->vpcSecurityGroupIds = empty($vpcSgIds) ? null : $vpcSgIds;
     }
     $request->characterSetName = $CharacterSetName ?: null;
     if (!empty($DBParameterGroup)) {
         $paramGroups = $aws->rds->dbParameterGroup->describe();
         foreach ($paramGroups as $param) {
             /* @var $param DBParameterGroupData */
             if ($param->dBParameterGroupName == $DBParameterGroup) {
                 $paramGroup = $param;
                 break;
             }
         }
     }
     if (!empty($paramGroup)) {
         $request->dBParameterGroupName = $paramGroup->dBParameterGroupName;
     }
     $isMirror = $MultiAZ && in_array($Engine, [DBInstanceData::ENGINE_SQL_SERVER_SE, DBInstanceData::ENGINE_SQL_SERVER_EE]);
     $optionList = $aws->rds->optionGroup->describe($Engine);
     foreach ($optionList as $option) {
         /* @var $option OptionGroupData */
         if ($option->optionGroupName == $OptionGroupName) {
             $optionGroup = $option;
             break;
         }
     }
     if (isset($optionGroup)) {
         $request->optionGroupName = $optionGroup->optionGroupName;
     } else {
         if ($isMirror) {
             $request->optionGroupName = $OptionGroupName;
         }
     }
     $dbSgIds = [];
     foreach ($DBSecurityGroups as $DBSecurityGroup) {
         $dbSgIds[] = $DBSecurityGroup;
     }
     $request->dBSecurityGroups = empty($dbSgIds) ? null : $dbSgIds;
     $request->autoMinorVersionUpgrade = $AutoMinorVersionUpgrade;
     $request->availabilityZone = $AvailabilityZone ?: null;
     $request->backupRetentionPeriod = $BackupRetentionPeriod ?: null;
     $request->preferredMaintenanceWindow = $PreferredMaintenanceWindow ?: null;
     $request->multiAZ = $isMirror ? false : $MultiAZ;
     $request->storageType = $StorageType;
     $request->dBSubnetGroupName = $DBSubnetGroupName ?: null;
     $request->licenseModel = $LicenseModel;
     $request->engineVersion = $EngineVersion ?: null;
     $request->iops = $Iops ?: null;
     if ($VpcId) {
         $request->publiclyAccessible = $PubliclyAccessible;
     }
     $tagsObject = $farmId ? DBFarm::LoadByID($farmId) : $this->environment;
     $request->tags = new TagsList($tagsObject->getAwsTags());
     $result = self::loadController('Aws', 'Scalr_UI_Controller_Tools')->checkSecurityGroupsPolicy($VpcSecurityGroups, Aws::SERVICE_INTERFACE_RDS);
     if ($result === true) {
         $result = self::loadController('Aws', 'Scalr_UI_Controller_Tools')->checkVpcPolicy($VpcId, $SubnetIds, $cloudLocation);
     }
     if ($result === true) {
         if (!empty($request->dBClusterIdentifier)) {
             try {
                 $checkInstance = $aws->rds->dbInstance->describe($request->dBInstanceIdentifier);
             } catch (Exception $e) {
                 $checkInstance = [];
             }
             if (count($checkInstance) > 0) {
                 throw new Exception(sprintf("AWS Error. DB Instance with identifier %s already exists.", $request->dBInstanceIdentifier));
             }
             self::loadController('Clusters', 'Scalr_UI_Controller_Tools_Aws_Rds')->xSaveAction($cloudLocation, $request->dBClusterIdentifier, $Engine, $MasterUsername, $MasterUserPassword, $VpcId, $Port, $DBName, $request->characterSetName, $request->dBParameterGroupName, $request->optionGroupName, new JsonData([$request->availabilityZone]), $request->backupRetentionPeriod, $PreferredBackupWindow, $request->preferredMaintenanceWindow, $request->dBSubnetGroupName, $request->engineVersion, $farmId, $VpcSecurityGroups, $SubnetIds, $StorageEncrypted, $KmsKeyId);
         }
         $instance = $aws->rds->dbInstance->create($request);
         CloudResource::deletePk($request->dBInstanceIdentifier, CloudResource::TYPE_AWS_RDS, $this->getEnvironmentId(), \SERVER_PLATFORMS::EC2, $cloudLocation);
         if ($farmId) {
             $cloudResource = new CloudResource();
             $cloudResource->id = $request->dBInstanceIdentifier;
             $cloudResource->type = CloudResource::TYPE_AWS_RDS;
             $cloudResource->platform = \SERVER_PLATFORMS::EC2;
             $cloudResource->cloudLocation = $cloudLocation;
             $cloudResource->envId = $this->getEnvironmentId();
             $cloudResource->farmId = $farmId;
             $cloudResource->save();
         }
         $vpcSglist = null;
         if (!empty($VpcId)) {
             $filter[] = ['name' => SecurityGroupFilterNameType::vpcId(), 'value' => $VpcId];
             $vpcSglist = $aws->ec2->securityGroup->describe(null, null, $filter);
         }
         $clusters = null;
         if (!empty($instance->dBClusterIdentifier)) {
             /* @var $cluster DBClusterData */
             $clusters = $aws->rds->dbCluster->describe($instance->dBClusterIdentifier);
         }
         $data = $this->getDbInstanceData($aws, $instance, $vpcSglist, $clusters);
         $data['isReplica'] = false;
         if ($isMirror) {
             $data['MultiAZ'] = true;
         }
         $this->response->success("DB Instance successfully created");
         $this->response->data(['instance' => $data, 'cloudLocation' => $cloudLocation]);
     } else {
         $this->response->failure($result);
     }
 }
Пример #2
0
 /**
  * xRestoreClusterAction
  *
  * @param string   $cloudLocation           Ec2 region
  * @param string   $DBClusterIdentifier     DBClusterIdentifier field
  * @param string   $DBSnapshotIdentifier    DBSnapshotIdentifier field
  * @param string   $Engine                  Aurora engine
  * @param string   $VpcId                   Vpc id
  * @param int      $Port                    Port value
  * @param string   $DBInstanceClass         Db instance class
  * @param bool     $PublicAccessible        True if instance is public accessible
  * @param RawData  $MasterUserPassword      DB Password
  * @param JsonData $SubnetIds               optional List of subnet ids
  * @param string   $OptionGroupName         optional Option group name
  * @param JsonData $AvailabilityZones       optional List of availability zones
  * @param string   $DBSubnetGroupName       optional Subnet group name
  * @param bool     $AutoMinorVersionUpgrade optional Auto minor version upgrade
  */
 public function xRestoreClusterAction($cloudLocation, $DBClusterIdentifier, $DBSnapshotIdentifier, $Engine, $VpcId, $Port, $DBInstanceClass, $PublicAccessible, RawData $MasterUserPassword, JsonData $SubnetIds = null, $OptionGroupName = null, JsonData $AvailabilityZones = null, $DBSubnetGroupName = null, $AutoMinorVersionUpgrade = null)
 {
     $this->request->restrictAccess(Acl::RESOURCE_AWS_RDS, Acl::PERM_AWS_RDS_MANAGE);
     $aws = $this->getAwsClient($cloudLocation);
     $request = new RestoreDBClusterFromSnapshotRequestData($DBClusterIdentifier, $DBSnapshotIdentifier);
     //NOTE: Options groups currently not supported for db clusters
     //        $optionList = $aws->rds->optionGroup->describe($Engine);
     //
     //        foreach ($optionList as $option) {
     //            /* @var $option OptionGroupData */
     //            if ($option->optionGroupName == $OptionGroupName) {
     //                $optionGroup = $option;
     //                break;
     //            }
     //        }
     //
     //        if (isset($optionGroup)) {
     //            $request->optionGroupName = $optionGroup->optionGroupName;
     //        }
     $request->port = $Port ?: null;
     $request->availabilityZones = count($AvailabilityZones) > 0 ? (array) $AvailabilityZones : null;
     $request->dBSubnetGroupName = $DBSubnetGroupName ?: null;
     $request->engine = $Engine;
     $result = self::loadController('Aws', 'Scalr_UI_Controller_Tools')->checkVpcPolicy($VpcId, $SubnetIds, $cloudLocation);
     if ($result === true) {
         $restoreResponse = $aws->rds->dbCluster->restoreFromSnapshot($request);
         try {
             $instance = $aws->rds->dbInstance->describe($DBClusterIdentifier)->get();
         } catch (Exception $e) {
             $instance = false;
         }
         if (!$instance) {
             $dbInstanceIdentifier = $DBClusterIdentifier;
         } else {
             $dbInstanceIdentifier = $DBClusterIdentifier . '-restored';
         }
         $createRequest = new CreateDBInstanceRequestData($dbInstanceIdentifier, $DBInstanceClass, $Engine);
         $createRequest->dBSubnetGroupName = $DBSubnetGroupName;
         $createRequest->publiclyAccessible = $PublicAccessible;
         $createRequest->licenseModel = 'general-public-license';
         $createRequest->engineVersion = $restoreResponse->engineVersion;
         $createRequest->storageType = 'aurora';
         $createRequest->setTags($this->environment->getAwsTags());
         $createRequest->autoMinorVersionUpgrade = $AutoMinorVersionUpgrade;
         $createRequest->dBClusterIdentifier = $restoreResponse->dBClusterIdentifier;
         $aws->rds->dbInstance->create($createRequest);
         CloudResource::deletePk($dbInstanceIdentifier, CloudResource::TYPE_AWS_RDS, $this->getEnvironmentId(), \SERVER_PLATFORMS::EC2, $cloudLocation);
         $this->response->success("DB Cluster has been successfully restored from Snapshot");
     } else {
         $this->response->failure($result);
     }
 }