示例#1
0
 public function xListBucketsAction()
 {
     // Create Amazon s3 client object
     $AmazonS3 = new AmazonS3($this->environment->getPlatformConfigValue(Modules_Platforms_Ec2::ACCESS_KEY), $this->environment->getPlatformConfigValue(Modules_Platforms_Ec2::SECRET_KEY));
     $AmazonCloudFront = new AmazonCloudFront($this->environment->getPlatformConfigValue(Modules_Platforms_Ec2::ACCESS_KEY), $this->environment->getPlatformConfigValue(Modules_Platforms_Ec2::SECRET_KEY));
     //Create cloundfront object
     $distributions = $AmazonCloudFront->ListDistributions();
     // Get list of all user buckets
     $buckets = array();
     foreach ($AmazonS3->ListBuckets() as $bucket) {
         if (!$distributions[$bucket->Name]) {
             $info = array("name" => $bucket->Name);
         } else {
             $info = array("name" => $bucket->Name, "cfid" => $distributions[$bucket->Name]['ID'], "cfurl" => $distributions[$bucket->Name]['DomainName'], "cname" => $distributions[$bucket->Name]['CNAME'], "status" => $distributions[$bucket->Name]['Status'], "enabled" => $distributions[$bucket->Name]['Enabled']);
         }
         $c = explode("-", $info['name']);
         if ($c[0] == 'farm') {
             $hash = $c[1];
             $farm = $this->db->GetRow("SELECT id, name FROM farms WHERE hash=? AND env_id = ?", array($hash, $this->environment->id));
             if ($farm) {
                 $info['farmId'] = $farm['id'];
                 $info['farmName'] = $farm['name'];
             }
         }
         $buckets[] = $info;
     }
     $response = $this->buildResponseFromData($buckets, array('name', 'farmName'));
     $this->response->data($response);
 }
示例#2
0
 function _testS3Bucket()
 {
     $AmazonS3 = new AmazonS3("", "");
     $res = $AmazonS3->ListBuckets();
     $this->assertTrue(is_array($res->Bucket), "ListBuckets returned array");
     $res = $AmazonS3->CreateBucket("MySQLDumps");
     $this->assertTrue($res, "Bucket successfull created");
     $res = $AmazonS3->CreateObject("fonts/test.ttf", "offload-public", "/tmp/PhotoEditService.wsdl", "plain/text");
     $this->assertTrue($res, "Object successfull created");
     $res = $AmazonS3->DownloadObject("fonts/test.ttf", "offload-public");
     $this->assertTrue($res, "Object successfull downloaded");
     $res = $AmazonS3->DeleteObject("fonts/test.ttf", "offload-public");
     $this->assertTrue($res, "Object successfull removed");
 }
示例#3
0
 function _testS3Bucket()
 {
     $AmazonS3 = new AmazonS3("0EJNVE9QFYY3TD554T02", "VOtWnbI2PmsqKOqDNVVgfLVsEnGD/6miiYDY552S");
     $res = $AmazonS3->ListBuckets();
     $this->assertTrue(is_array($res->Bucket), "ListBuckets returned array");
     $res = $AmazonS3->CreateBucket("MySQLDumps");
     $this->assertTrue($res, "Bucket successfull created");
     $res = $AmazonS3->CreateObject("fonts/test.ttf", "offload-public", "/tmp/PhotoEditService.wsdl", "plain/text");
     $this->assertTrue($res, "Object successfull created");
     $res = $AmazonS3->DownloadObject("fonts/test.ttf", "offload-public");
     $this->assertTrue($res, "Object successfull downloaded");
     $res = $AmazonS3->DeleteObject("fonts/test.ttf", "offload-public");
     $this->assertTrue($res, "Object successfull removed");
 }
示例#4
0
 public function xSaveEc2Action()
 {
     $pars = array();
     $enabled = false;
     if ($this->getParam('ec2_is_enabled')) {
         $enabled = true;
         $pars[Modules_Platforms_Ec2::ACCOUNT_ID] = $this->checkVar(Modules_Platforms_Ec2::ACCOUNT_ID, 'string', "AWS Account Number required");
         if (!is_numeric($pars[Modules_Platforms_Ec2::ACCOUNT_ID]) || strlen($pars[Modules_Platforms_Ec2::ACCOUNT_ID]) != 12) {
             //$err[Modules_Platforms_Ec2::ACCOUNT_ID] = _("AWS numeric account ID required (See <a href='/faq.html'>FAQ</a> for info on where to get it).");
             $this->checkVarError[Modules_Platforms_Ec2::ACCOUNT_ID] = _("AWS Account Number should be numeric");
         } else {
             $pars[Modules_Platforms_Ec2::ACCOUNT_ID] = preg_replace("/[^0-9]+/", "", $pars[Modules_Platforms_Ec2::ACCOUNT_ID]);
         }
         $pars[Modules_Platforms_Ec2::ACCESS_KEY] = $this->checkVar(Modules_Platforms_Ec2::ACCESS_KEY, 'string', "AWS Access Key required");
         $pars[Modules_Platforms_Ec2::SECRET_KEY] = $this->checkVar(Modules_Platforms_Ec2::SECRET_KEY, 'password', "AWS Access Key required");
         $pars[Modules_Platforms_Ec2::PRIVATE_KEY] = trim($this->checkVar(Modules_Platforms_Ec2::PRIVATE_KEY, 'file', "AWS x.509 Private Key required"));
         $pars[Modules_Platforms_Ec2::CERTIFICATE] = trim($this->checkVar(Modules_Platforms_Ec2::CERTIFICATE, 'file', "AWS x.509 Certificate required"));
         // the same as EC2
         $pars[Modules_Platforms_Rds::ACCOUNT_ID] = $pars[Modules_Platforms_Ec2::ACCOUNT_ID];
         $pars[Modules_Platforms_Rds::ACCESS_KEY] = $pars[Modules_Platforms_Ec2::ACCESS_KEY];
         $pars[Modules_Platforms_Rds::SECRET_KEY] = $pars[Modules_Platforms_Ec2::SECRET_KEY];
         $pars[Modules_Platforms_Rds::PRIVATE_KEY] = $pars[Modules_Platforms_Ec2::PRIVATE_KEY];
         $pars[Modules_Platforms_Rds::CERTIFICATE] = $pars[Modules_Platforms_Ec2::CERTIFICATE];
         if (!count($this->checkVarError)) {
             if ($pars[Modules_Platforms_Ec2::ACCOUNT_ID] != $this->env->getPlatformConfigValue(Modules_Platforms_Ec2::ACCOUNT_ID) or $pars[Modules_Platforms_Ec2::ACCESS_KEY] != $this->env->getPlatformConfigValue(Modules_Platforms_Ec2::ACCESS_KEY) or $pars[Modules_Platforms_Ec2::SECRET_KEY] != $this->env->getPlatformConfigValue(Modules_Platforms_Ec2::SECRET_KEY) or $pars[Modules_Platforms_Ec2::PRIVATE_KEY] != $this->env->getPlatformConfigValue(Modules_Platforms_Ec2::PRIVATE_KEY) or $pars[Modules_Platforms_Ec2::CERTIFICATE] != $this->env->getPlatformConfigValue(Modules_Platforms_Ec2::CERTIFICATE)) {
                 try {
                     $AmazonEC2Client = Scalr_Service_Cloud_Aws::newEc2('us-east-1', $pars[Modules_Platforms_Ec2::PRIVATE_KEY], $pars[Modules_Platforms_Ec2::CERTIFICATE]);
                     $AmazonEC2Client->describeInstances();
                 } catch (Exception $e) {
                     throw new Exception(_("Incorrect format of X.509 certificate or private key. Make sure that you are using files downloaded from AWS profile. ({$e->getMessage()})"));
                 }
                 try {
                     $AmazonS3 = new AmazonS3($pars[Modules_Platforms_Ec2::ACCESS_KEY], $pars[Modules_Platforms_Ec2::SECRET_KEY]);
                     $buckets = $AmazonS3->ListBuckets();
                 } catch (Exception $e) {
                     throw new Exception(sprintf(_("Failed to verify your EC2 access key and secret key: %s"), $e->getMessage()));
                 }
             }
         } else {
             $this->response->failure();
             $this->response->data(array('errors' => $this->checkVarError));
             return;
         }
     }
     $this->db->BeginTrans();
     try {
         $this->env->enablePlatform(SERVER_PLATFORMS::EC2, $enabled);
         $this->env->enablePlatform(SERVER_PLATFORMS::RDS, $enabled);
         if ($enabled) {
             $this->env->setPlatformConfig($pars);
         }
         if (!$this->user->getAccount()->getSetting(Scalr_Account::SETTING_DATE_ENV_CONFIGURED)) {
             $this->user->getAccount()->setSetting(Scalr_Account::SETTING_DATE_ENV_CONFIGURED, time());
         }
         if ($this->user->getAccount()->getSetting(Scalr_Account::SETTING_IS_TRIAL) == 1) {
             if ($this->db->GetOne("SELECT COUNT(*) FROM farms WHERE clientid = ?", array($this->user->getAccountId())) == 0) {
                 //Create demo farm
                 try {
                     $dbFarm = DBFarm::LoadByID(9670);
                     // LAMP-PROTOTYPE
                     $dbFarm->cloneFarm('My First LAMP Farm');
                     $demoFarm = true;
                 } catch (Exception $e) {
                     throw new Exception("Demo farm creation failed: {$e->getMessage()}");
                 }
             }
         }
         $this->response->success('Environment saved');
         $this->response->data(array('enabled' => $enabled, 'demoFarm' => $demoFarm));
     } catch (Exception $e) {
         $this->db->RollbackTrans();
         throw new Exception(_("Failed to save AWS settings: {$e->getMessage()}"));
     }
     $this->db->CommitTrans();
 }
示例#5
0
 public static function farmSave(DBFarm $DBFarm, array $roles)
 {
     $buckets = array();
     foreach ($roles as $DBFarmRole) {
         if ($DBFarmRole->GetSetting(DBFarmRole::SETTING_AWS_S3_BUCKET)) {
             $buckets[$DBFarmRole->CloudLocation] = $DBFarmRole->GetSetting(DBFarmRole::SETTING_AWS_S3_BUCKET);
         }
     }
     foreach ($roles as $DBFarmRole) {
         if ($DBFarmRole->Platform != SERVER_PLATFORMS::EC2) {
             continue;
         }
         $location = $DBFarmRole->CloudLocation;
         $sshKey = Scalr_Model::init(Scalr_Model::SSH_KEY);
         if (!$sshKey->loadGlobalByFarmId($DBFarm->ID, $location)) {
             $key_name = "FARM-{$DBFarm->ID}";
             $AmazonEC2Client = Scalr_Service_Cloud_Aws::newEc2($location, $DBFarm->GetEnvironmentObject()->getPlatformConfigValue(Modules_Platforms_Ec2::PRIVATE_KEY), $DBFarm->GetEnvironmentObject()->getPlatformConfigValue(Modules_Platforms_Ec2::CERTIFICATE));
             $result = $AmazonEC2Client->CreateKeyPair($key_name);
             if ($result->keyMaterial) {
                 $sshKey->farmId = $DBFarm->ID;
                 $sshKey->clientId = $DBFarm->ClientID;
                 $sshKey->envId = $DBFarm->EnvID;
                 $sshKey->type = Scalr_SshKey::TYPE_GLOBAL;
                 $sshKey->cloudLocation = $location;
                 $sshKey->cloudKeyName = $key_name;
                 $sshKey->platform = SERVER_PLATFORMS::EC2;
                 $sshKey->setPrivate($result->keyMaterial);
                 $sshKey->save();
             }
         }
         try {
             if (!$DBFarmRole->GetSetting(DBFarmRole::SETTING_AWS_S3_BUCKET)) {
                 if (!$buckets[$location]) {
                     $aws_account_id = $DBFarm->GetEnvironmentObject()->getPlatformConfigValue(Modules_Platforms_Ec2::ACCOUNT_ID);
                     $bucket_name = "farm-{$DBFarm->Hash}-{$aws_account_id}-{$location}";
                     //
                     // Create S3 Bucket (For MySQL, BackUs, etc.)
                     //
                     $AmazonS3 = new AmazonS3($DBFarm->GetEnvironmentObject()->getPlatformConfigValue(Modules_Platforms_Ec2::ACCESS_KEY), $DBFarm->GetEnvironmentObject()->getPlatformConfigValue(Modules_Platforms_Ec2::SECRET_KEY));
                     $buckets = $AmazonS3->ListBuckets();
                     $create_bucket = true;
                     foreach ($buckets as $bucket) {
                         if ($bucket->Name == $bucket_name) {
                             $create_bucket = false;
                             $buckets[$location] = $bucket_name;
                             break;
                         }
                     }
                     if ($create_bucket) {
                         if ($AmazonS3->CreateBucket($bucket_name, $location)) {
                             $buckets[$location] = $bucket_name;
                         }
                     }
                 }
                 $DBFarmRole->SetSetting(DBFarmRole::SETTING_AWS_S3_BUCKET, $buckets[$location]);
             }
         } catch (Exception $e) {
             throw new Exception("Amazon S3: {$e->getMessage()}");
         }
     }
 }