public function OnStartForking()
 {
     $db = Core::GetDBInstance(null, true);
     // selects rows where the snapshot's time has come to create new snapshot.
     $resultset = $db->Execute("SELECT * FROM autosnap_settings \r\n\t\t\t\t\t\tWHERE (UNIX_TIMESTAMP(DATE_ADD(dtlastsnapshot, INTERVAL period HOUR)) < UNIX_TIMESTAMP(NOW())\r\n\t\t\t\t\t\tOR dtlastsnapshot IS NULL)\r\n\t\t\t\t\t\tAND objectid \t!= '0' \r\n\t\t\t\t\t\tAND object_type = ?", array(AUTOSNAPSHOT_TYPE::RDSSnap));
     while ($snapshotsSettings = $resultset->FetchRow()) {
         try {
             $environment = Scalr_Model::init(Scalr_Model::ENVIRONMENT)->loadById($snapshotsSettings['env_id']);
             $AmazonRDSClient = Scalr_Service_Cloud_Aws::newRds($environment->getPlatformConfigValue(Modules_Platforms_Ec2::ACCESS_KEY), $environment->getPlatformConfigValue(Modules_Platforms_Ec2::SECRET_KEY), $snapshotsSettings['region']);
             // Check instance. If instance wasn't found then delete current recrod from settings table
             try {
                 $AmazonRDSClient->DescribeDBInstances($snapshotsSettings['objectid']);
             } catch (Exception $e) {
                 if (stristr($e->getMessage(), "not found") || stristr($e->getMessage(), "not a valid") || stristr($e->getMessage(), "security token included in the request is invalid")) {
                     $db->Execute("DELETE FROM autosnap_settings WHERE id = ?", array($snapshotsSettings['id']));
                 }
                 $this->Logger->error(sprintf(_("RDS instance %s was not found. Auto-snapshot settings for this \r\n\t\t\t\t\t\t\t\tinstance will be deleted. %s."), $snapshotsSettings['objectid'], $e->getMessage()));
                 throw $e;
             }
             // snapshot random unique name
             $snapshotId = "scalr-auto-" . dechex(microtime(true) * 10000) . rand(0, 9);
             try {
                 // Create new snapshot
                 $AmazonRDSClient->CreateDBSnapshot($snapshotId, $snapshotsSettings['objectid']);
                 // update last snapshot creation date in settings
                 $db->Execute("UPDATE autosnap_settings SET last_snapshotid=?, dtlastsnapshot=NOW() WHERE id=?", array($snapshotId, $snapshotsSettings['id']));
                 // create new snapshot record in DB
                 $db->Execute("INSERT INTO rds_snaps_info SET \r\n\t\t\t\t\t\t\tsnapid\t\t= ?,\r\n\t\t\t\t\t\t\tcomment\t\t= ?,\r\n\t\t\t\t\t\t\tdtcreated\t= NOW(),\r\n\t\t\t\t\t\t\tregion\t\t= ?,\r\n\t\t\t\t\t\t\tautosnapshotid = ?", array($snapshotId, _("Auto snapshot"), $snapshotsSettings['region'], $snapshotsSettings['id']));
             } catch (Exception $e) {
                 $this->Logger->warn(sprintf(_("Cannot create RDS snapshot: %s."), $e->getMessage()));
             }
             // Remove old snapshots
             if ($snapshotsSettings['rotate'] != 0) {
                 $oldSnapshots = $db->GetAll("SELECT * FROM rds_snaps_info WHERE autosnapshotid = ? ORDER BY id ASC", array($snapshotsSettings['id']));
                 if (count($oldSnapshots) > $snapshotsSettings['rotate']) {
                     while (count($oldSnapshots) > $snapshotsSettings['rotate']) {
                         // takes the oldest snapshot ...
                         $deletingSnapshot = array_shift($oldSnapshots);
                         try {
                             // and deletes it from amazon and from DB
                             $AmazonRDSClient->DeleteDBSnapshot($deletingSnapshot['snapid']);
                             $db->Execute("DELETE FROM rds_snaps_info WHERE id = ?", array($deletingSnapshot['id']));
                         } catch (Exception $e) {
                             if (stristr($e->getMessage(), "not found") || stristr($e->getMessage(), "not a valid")) {
                                 $db->Execute("DELETE FROM rds_snaps_info WHERE id = ?", array($deletingSnapshot['id']));
                             }
                             $this->Logger->error(sprintf(_("DBsnapshot %s for RDS instance %s was not found \r\n\t\t\t\t\t\t\t\t\t\t\t\tand cannot be deleted . %s."), $deletingSnapshot['snapid'], $snapshotsSettings['objectid'], $e->getMessage()));
                         }
                     }
                 }
             }
         } catch (Exception $e) {
             $this->Logger->warn(sprintf(_("Cannot create snapshot for RDS Instance %s. %s"), $snapshotsSettings['objectid'], $e->getMessage()));
         }
     }
 }
示例#2
0
 public function xSaveAction()
 {
     $this->request->defineParams(array('rules' => array('type' => 'json')));
     $amazonRDSClient = Scalr_Service_Cloud_Aws::newRds($this->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Ec2::ACCESS_KEY), $this->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Ec2::SECRET_KEY), $this->getParam('cloudLocation'));
     $response = $amazonRDSClient->DescribeDBSecurityGroups($this->getParam('dbSgName'));
     $result = json_decode(json_encode($response->DescribeDBSecurityGroupsResult), true);
     $group = $result['DBSecurityGroups']['DBSecurityGroup'];
     $rules = array();
     if ($group) {
         if ($group['IPRanges']['IPRange']['CIDRIP']) {
             $group['IPRanges']['IPRange'] = array($group['IPRanges']['IPRange']);
         }
         foreach ($group['IPRanges']['IPRange'] as $r) {
             $r['id'] = md5($r['CIDRIP']);
             $rules[$r['id']] = $r;
         }
         if ($group['EC2SecurityGroups']['EC2SecurityGroup']['EC2SecurityGroupOwnerId']) {
             $group['EC2SecurityGroups']['EC2SecurityGroup'] = array($group['EC2SecurityGroups']['EC2SecurityGroup']);
         }
         foreach ($group['EC2SecurityGroups']['EC2SecurityGroup'] as $r) {
             $r['id'] = md5($r['EC2SecurityGroupName'] . $r['EC2SecurityGroupOwnerId']);
             $rules[$r['id']] = $r;
         }
     }
     foreach ($rules as $id => $r) {
         $found = false;
         foreach ($this->getParam('rules') as $rule) {
             if ($rule['Type'] == 'CIDR IP') {
                 $rid = md5($rule['CIDRIP']);
             } else {
                 $rid = md5($rule['EC2SecurityGroupName'] . $rule['EC2SecurityGroupOwnerId']);
             }
             if ($id == $rid) {
                 $found = true;
             }
         }
         if (!$found) {
             if ($r['CIDRIP']) {
                 $amazonRDSClient->RevokeDBSecurityGroupIngress($this->getParam('dbSgName'), $r['CIDRIP']);
             } else {
                 $amazonRDSClient->RevokeDBSecurityGroupIngress($this->getParam('dbSgName'), null, $r['EC2SecurityGroupName'], $r['EC2SecurityGroupOwnerId']);
             }
         }
     }
     foreach ($this->getParam('rules') as $rule) {
         if ($rule['Status'] == 'new') {
             if ($rule['Type'] == 'CIDR IP') {
                 $amazonRDSClient->AuthorizeDBSecurityGroupIngress($this->getParam('dbSgName'), $rule['CIDRIP']);
             } else {
                 $amazonRDSClient->AuthorizeDBSecurityGroupIngress($this->getParam('dbSgName'), null, $rule['EC2SecurityGroupName'], $rule['EC2SecurityGroupOwnerId']);
             }
         }
     }
     $this->response->success("DB security group successfully updated");
 }
示例#3
0
 public function xListLogsAction()
 {
     $amazonRDSClient = Scalr_Service_Cloud_Aws::newRds($this->environment->getPlatformConfigValue(Modules_Platforms_Ec2::ACCESS_KEY), $this->environment->getPlatformConfigValue(Modules_Platforms_Ec2::SECRET_KEY), $this->getParam('cloudLocation'));
     $aws_response = $amazonRDSClient->DescribeEvents($this->getParam('name'), $this->getParam('type'));
     $events = (array) $aws_response->DescribeEventsResult->Events;
     if (!is_array($events['Event'])) {
         $events['Event'] = array($events['Event']);
     }
     foreach ($events['Event'] as $event) {
         if ($event->Message) {
             $logs[] = array('Message' => (string) $event->Message, 'Date' => (string) $event->Date, 'SourceIdentifier' => (string) $event->SourceIdentifier, 'SourceType' => (string) $event->SourceType);
         }
     }
     $response = $this->buildResponseFromData($logs, array('Date', 'Message'));
     foreach ($response['data'] as &$row) {
         $row['Date'] = Scalr_Util_DateTime::convertTz($row['Date']);
     }
     $this->response->data($response);
 }
示例#4
0
 public function xDeleteSnapshotsAction()
 {
     $this->request->defineParams(array('snapshots' => array('type' => 'json')));
     $amazonRDSClient = Scalr_Service_Cloud_Aws::newRds($this->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Ec2::ACCESS_KEY), $this->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Ec2::SECRET_KEY), $this->getParam('cloudLocation'));
     $i = 0;
     $errors = array();
     foreach ($this->getParam('snapshots') as $snapName) {
         try {
             $amazonRDSClient->DeleteDBSnapshot($snapName);
             $this->db->Execute("DELETE FROM rds_snaps_info WHERE snapid=? ", array($snapName));
             $i++;
         } catch (Exception $e) {
             $errors[] = sprintf(_("Can't delete db snapshot %s: %s"), $snapName, $e->getMessage());
         }
     }
     $message = sprintf(_("%s db snapshot(s) successfully removed"), $i);
     if (count($errors)) {
         $this->response->warning(nl2br(implode("\n", array_merge(array($message), $errors))));
     } else {
         $this->response->success($message);
     }
 }
示例#5
0
 public function getRdsClient(Scalr_Environment $environment, $region)
 {
     return Scalr_Service_Cloud_Aws::newRds($environment->getPlatformConfigValue(self::ACCESS_KEY), $environment->getPlatformConfigValue(self::SECRET_KEY), $region);
 }
示例#6
0
 public function xRestoreInstanceAction()
 {
     $amazonRDSClient = Scalr_Service_Cloud_Aws::newRds($this->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Ec2::ACCESS_KEY), $this->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Ec2::SECRET_KEY), $this->getParam('cloudLocation'));
     $amazonRDSClient->RestoreDBInstanceFromDBSnapshot($this->getParam('Snapshot'), $this->getParam('DBInstanceIdentifier'), $this->getParam('DBInstanceClass'), $this->getParam('Port'), $this->getParam('AvailabilityZone'), $this->getParam('MultiAZ'));
     $this->response->success("DB Instance successfully restore from Snapshot");
 }
示例#7
0
 public function xResetAction()
 {
     $amazonRDSClient = Scalr_Service_Cloud_Aws::newRds($this->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Ec2::ACCESS_KEY), $this->getEnvironment()->getPlatformConfigValue(Modules_Platforms_Ec2::SECRET_KEY), $this->getParam('cloudLocation'));
     $response = $amazonRDSClient->DescribeDBParameters($this->getParam('name'));
     $result = json_decode(json_encode($response->DescribeDBParametersResult->Parameters), true);
     $params = $result['Parameter'];
     $modifiedParameters = new ParametersList();
     foreach ($params as $param) {
         if ($param['ParameterValue'] && !empty($param['ParameterValue'])) {
             if ($param['ApplyType'] == 'static') {
                 $modifiedParameters->AddParameters($param['ParameterName'], $param['ParameterValue'], "pending-reboot");
             } else {
                 $modifiedParameters->AddParameters($param['ParameterName'], $param['ParameterValue'], "immediate");
             }
         }
     }
     $amazonRDSClient->ResetDBParameterGroup($this->getParam('name'), $modifiedParameters);
     $this->response->success("DB parameter group successfully reset to default");
 }