LoadById() public static method

public static LoadById ( integer $id ) : BundleTask
$id integer
return BundleTask
Exemplo n.º 1
0
 /**
  * @param   int     $bundleTaskId       ID of BundleTask
  * @param   string  $status   optional  Status of BundleTask
  * @param   bool    $taskInfo optional  Get updated information about task
  */
 public function xListLogsAction($bundleTaskId, $status = '', $taskInfo = false)
 {
     $task = BundleTask::LoadById($bundleTaskId);
     $this->user->getPermissions()->validate($task);
     $sql = "SELECT * FROM bundle_task_log WHERE bundle_task_id = ?";
     $response = $this->buildResponseFromSql2($sql, array('id', 'dtadded', 'message'), array(), array($bundleTaskId));
     foreach ($response["data"] as &$row) {
         $row['dtadded'] = Scalr_Util_DateTime::convertTz($row['dtadded']);
     }
     if ($taskInfo && $task->status != $status) {
         // status has been changed, also include information about task
         $row = $this->db->GetRow("SELECT b.*, (SELECT EXISTS (SELECT 1 FROM servers s WHERE s.server_id = b.server_id)) as server_exists FROM bundle_tasks b WHERE id = ?", [$task->id]);
         // temporary solution, refactor all on new model and replace this code
         $row['dtadded'] = Scalr_Util_DateTime::convertTz($row['dtadded']);
         if (!$row['bundle_type']) {
             $row['bundle_type'] = "*";
         }
         if ($row['dtfinished'] && $row['dtstarted']) {
             $row['duration'] = Scalr_Util_DateTime::getDateTimeDiff($row['dtfinished'], $row['dtstarted']);
         }
         if ($row['dtfinished']) {
             $row['dtfinished'] = Scalr_Util_DateTime::convertTz($row['dtfinished']);
         }
         if ($row['dtstarted']) {
             $row['dtstarted'] = Scalr_Util_DateTime::convertTz($row['dtstarted']);
         }
         $response['task'] = $row;
     }
     $this->response->data($response);
 }
Exemplo n.º 2
0
 public function failureDetailsAction()
 {
     $this->request->defineParams(array('bundleTaskId' => array('type' => 'int')));
     $task = BundleTask::LoadById($this->getParam('bundleTaskId'));
     $this->user->getPermissions()->validate($task);
     $this->response->page('ui/bundletasks/failuredetails.js', array('failureReason' => nl2br($task->failureReason)));
 }
Exemplo n.º 3
0
 protected function run2()
 {
     $this->console->notice('Modify os columns and re-fill it from bundle tasks');
     $this->db->Execute('ALTER TABLE images CHANGE `os_name` `os` varchar(60) DEFAULT NULL AFTER `name`,
          MODIFY `os_family` varchar(30) DEFAULT NULL,
          ADD `os_generation` varchar(10) DEFAULT NULL AFTER `os_family`,
          MODIFY `os_version` varchar(10) DEFAULT NULL
     ');
     $bunimages = Image::find([['source' => Image::SOURCE_BUNDLE_TASK]]);
     foreach ($bunimages as $image) {
         /* @var Image $image */
         try {
             $task = \BundleTask::LoadById($image->bundleTaskId);
             $os = $task->getOsDetails();
             $image->osId = $os->id;
             $image->save();
         } catch (\Exception $e) {
             $this->console->warning($e->getMessage());
         }
     }
 }
Exemplo n.º 4
0
 /**
  * @return Scalr_Net_Ssh2_Client
  * Enter description here ...
  */
 public function GetSsh2Client()
 {
     $ssh2Client = new Scalr_Net_Ssh2_Client();
     switch ($this->platform) {
         case SERVER_PLATFORMS::RACKSPACENG_UK:
         case SERVER_PLATFORMS::RACKSPACENG_US:
             $ssh2Client->addPassword('root', $this->GetProperty(OPENSTACK_SERVER_PROPERTIES::ADMIN_PASS));
             break;
         case SERVER_PLATFORMS::RACKSPACE:
             $ssh2Client->addPassword('root', $this->GetProperty(RACKSPACE_SERVER_PROPERTIES::ADMIN_PASS));
             break;
         case SERVER_PLATFORMS::GCE:
             $userName = '******';
             if ($this->status == SERVER_STATUS::TEMPORARY) {
                 $keyName = 'SCALR-ROLESBUILDER-' . SCALR_ID;
             } else {
                 $keyName = "FARM-{$this->farmId}-" . SCALR_ID;
             }
             try {
                 $key = (new SshKey())->loadGlobalByName($this->envId, SERVER_PLATFORMS::GCE, "", $keyName);
                 if (!$key) {
                     throw new Exception(_("There is no SSH key for server: {$this->serverId}"));
                 }
             } catch (Exception $e) {
                 throw new Exception("Cannot init SshKey object: {$e->getMessage()}");
             }
             $priv_key_file = tempnam("/tmp", "GCEPK");
             @file_put_contents($priv_key_file, $key->privateKey);
             $this->tmpFiles[] = $priv_key_file;
             $pub_key_file = tempnam("/tmp", "GCEK");
             @file_put_contents($pub_key_file, $key->publicKey);
             $this->tmpFiles[] = $pub_key_file;
             $ssh2Client->addPubkey($userName, $pub_key_file, $priv_key_file);
             break;
         case SERVER_PLATFORMS::IDCF:
         case SERVER_PLATFORMS::EC2:
             $userName = '******';
             $skipKeyValidation = false;
             // Temporary server for role builder
             $sshKey = new SshKey();
             if ($this->status == SERVER_STATUS::TEMPORARY) {
                 $keyName = "SCALR-ROLESBUILDER-" . SCALR_ID . "-{$this->envId}";
                 if (!$sshKey->loadGlobalByName($this->envId, $this->platform, $this->GetCloudLocation(), $keyName)) {
                     $keyName = "SCALR-ROLESBUILDER-" . SCALR_ID;
                 }
                 try {
                     $bundleTaskId = $this->GetProperty(\SERVER_PROPERTIES::SZR_IMPORTING_BUNDLE_TASK_ID);
                     $bundleTask = BundleTask::LoadById($bundleTaskId);
                     if ($bundleTask->osFamily == 'amazon') {
                         $userName = '******';
                     }
                 } catch (Exception $e) {
                 }
             } else {
                 $keyName = "FARM-{$this->farmId}-" . SCALR_ID;
                 $oldKeyName = "FARM-{$this->farmId}";
                 $key = $sshKey->loadGlobalByName($this->envId, $this->platform, $this->GetCloudLocation(), $oldKeyName);
                 if ($key) {
                     $keyName = $oldKeyName;
                     $skipKeyValidation = true;
                 }
             }
             if (!$skipKeyValidation) {
                 try {
                     $key = $sshKey->loadGlobalByName($this->envId, $this->platform, $this->GetCloudLocation(), $keyName);
                     if (!$key) {
                         throw new Exception(sprintf('Could not find SSH Key for server "%s" with name:"%s", cloud-location:"%s", platform:"%s", environment:"%d".', $this->serverId, $keyName, $this->GetCloudLocation(), $this->platform, $this->envId));
                     }
                 } catch (Exception $e) {
                     throw new Exception("Cannot init SshKey object: {$e->getMessage()}");
                 }
             }
             $priv_key_file = tempnam("/tmp", "AWSK");
             @file_put_contents($priv_key_file, $key->privateKey);
             $this->tmpFiles[] = $priv_key_file;
             $pub_key_file = tempnam("/tmp", "AWSK");
             $this->tmpFiles[] = $pub_key_file;
             $pubKey = $key->publicKey;
             if (!stristr($pubKey, $keyName)) {
                 $pubKey .= " {$keyName}";
             }
             @file_put_contents($pub_key_file, $pubKey);
             $ssh2Client->addPubkey($userName, $pub_key_file, $priv_key_file);
             break;
     }
     return $ssh2Client;
 }
Exemplo n.º 5
0
             if (trim($stderr)) {
                 $stderr = "\n stderr: {$stderr}";
             }
             $stdout = base64_decode($data['stdout']);
             if (trim($stdout)) {
                 $stdout = "\n stdout: {$stdout}";
             }
             if (!$stderr && !$stdout) {
                 $stdout = _("Script executed without any output.");
             }
             $Logger->warn(new ScriptingLogMessage($req_FarmID, $event_name, $DBServer->serverId, sprintf(_("Script '%s' execution result (Execution time: %s seconds). %s %s"), $data['script_path'], $data['time_elapsed'], $stderr, $stdout)));
             break;
         case "rebundleStatus":
             if ($data['bundletaskid']) {
                 try {
                     $BundleTask = BundleTask::LoadById($data['bundletaskid']);
                     if ($BundleTask->serverId == $DBServer->serverId) {
                         $BundleTask->Log($data['message']);
                     }
                 } catch (Exception $e) {
                 }
             }
             break;
         case "logEvent":
             $message = base64_decode($data["msg"]);
             $tm = date('YmdH');
             $hash = md5("{$DBServer->serverId}:{$message}:{$req_FarmID}:{$data['source']}:{$tm}", true);
             $db->Execute("INSERT DELAYED INTO logentries SET id=?, serverid=?, message=?, `time`=?, severity=?, `source`=?, farmid=? ON DUPLICATE KEY UPDATE cnt = cnt + 1, `time` = ?", array($hash, $DBServer->serverId, $message, time(), $data["severity"], $data["source"], $req_FarmID, time()));
             break;
     }
 }
Exemplo n.º 6
0
 public function xGetBundleTaskDataAction()
 {
     $task = BundleTask::LoadById($this->getParam('bundleTaskId'));
     $this->user->getPermissions()->validate($task);
     $logs = $this->db->GetAll("SELECT * FROM bundle_task_log WHERE bundle_task_id = " . $this->db->qstr($this->getParam('bundleTaskId')) . ' ORDER BY id DESC LIMIT 3');
     foreach ($logs as &$row) {
         $row['dtadded'] = Scalr_Util_DateTime::convertTz($row['dtadded']);
     }
     $image = $task->getImageEntity();
     $this->response->data(array('status' => $task->status, 'failureReason' => $task->failureReason, 'logs' => $logs, 'roleId' => $task->roleId, 'roleName' => $task->roleName, 'platform' => $task->platform, 'imageId' => $task->snapshotId, 'imageHash' => !empty($image) ? $image->hash : null));
 }
 public function StartThread($bundle_task_info)
 {
     $db = Core::GetDBInstance();
     // Reconfigure observers;
     Scalr::ReconfigureObservers();
     $BundleTask = BundleTask::LoadById($bundle_task_info['id']);
     try {
         $DBServer = DBServer::LoadByID($BundleTask->serverId);
     } catch (ServerNotFoundException $e) {
         if (!$BundleTask->snapshotId) {
             $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::FAILED;
             $BundleTask->setDate('finished');
             $BundleTask->failureReason = sprintf(_("Server '%s' was terminated during snapshot creation process"), $BundleTask->serverId);
             $BundleTask->Save();
             return;
         }
     } catch (Exception $e) {
         //$this->Logger->error($e->getMessage());
     }
     switch ($BundleTask->status) {
         case SERVER_SNAPSHOT_CREATION_STATUS::STARING_SERVER:
         case SERVER_SNAPSHOT_CREATION_STATUS::PREPARING_ENV:
         case SERVER_SNAPSHOT_CREATION_STATUS::INTALLING_SOFTWARE:
             if (!PlatformFactory::NewPlatform($DBServer->platform)->IsServerExists($DBServer)) {
                 $DBServer->status = SERVER_STATUS::TERMINATED;
                 $DBServer->save();
                 $BundleTask->SnapshotCreationFailed("Server was terminated and no longer available in cloud.");
                 exit;
             }
             // IF server is in pensing state
             $status = PlatformFactory::NewPlatform($DBServer->platform)->GetServerRealStatus($DBServer);
             $BundleTask->Log(sprintf(_("Server status: %s"), $status->getName()));
             if ($status->isPending()) {
                 $BundleTask->Log(sprintf(_("Waiting for running state."), $status->getName()));
                 exit;
             } elseif ($status->isTerminated()) {
                 $DBServer->status = SERVER_STATUS::TERMINATED;
                 $DBServer->save();
                 $BundleTask->SnapshotCreationFailed("Server was terminated and no longer available in cloud.");
                 exit;
             }
             break;
     }
     switch ($BundleTask->status) {
         case SERVER_SNAPSHOT_CREATION_STATUS::STARING_SERVER:
             $ips = PlatformFactory::NewPlatform($DBServer->platform)->GetServerIPAddresses($DBServer);
             $DBServer->remoteIp = $ips['remoteIp'];
             $DBServer->localIp = $ips['locateIp'];
             $DBServer->save();
             $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::PREPARING_ENV;
             $BundleTask->save();
             $BundleTask->Log(sprintf(_("Bundle task status: %s"), $BundleTask->status));
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::PREPARING_ENV:
             $BundleTask->Log(sprintf(_("Initializing SSH2 session to the server")));
             try {
                 $ssh2Client = $DBServer->GetSsh2Client();
                 $ssh2Client->connect($DBServer->remoteIp, 22);
             } catch (Exception $e) {
                 $BundleTask->Log(sprintf(_("Scalr unable to establish SSH connection with server on %:%. Error: %s"), $DBServer->remoteIp, 22, $e->getMessage()));
                 //TODO: Set status of bundle log to failed
                 exit;
             }
             //Prepare script
             $BundleTask->Log(sprintf(_("Uploading builder scripts...")));
             $behaviors = $DBServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_BEHAVIOR);
             try {
                 $options = array('server-id' => $DBServer->serverId, 'role-name' => $BundleTask->roleName, 'crypto-key' => $DBServer->GetProperty(SERVER_PROPERTIES::SZR_KEY), 'platform' => $DBServer->platform, 'behaviour' => trim(str_replace("base", "", $behaviors)), 'queryenv-url' => CONFIG::$HTTP_PROTO . "://" . CONFIG::$EVENTHANDLER_URL . "/query-env", 'messaging-p2p.producer-url' => CONFIG::$HTTP_PROTO . "://" . CONFIG::$EVENTHANDLER_URL . "/messaging");
                 $command = 'scalarizr --import -y';
                 foreach ($options as $k => $v) {
                     $command .= sprintf(' -o %s=%s', $k, $v);
                 }
                 if ($DBServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_MYSQL_SERVER_TYPE) == 'percona') {
                     $recipes = 'mysql=percona';
                 } else {
                     $recipes = '';
                 }
                 $scalarizrBranch = $DBServer->GetProperty(SERVER_PROPERTIES::SZR_DEV_SCALARIZR_BRANCH);
                 $scriptContents = @file_get_contents(APPPATH . "/templates/services/role_builder/chef_import.tpl");
                 $scriptContents = str_replace(array("%PLATFORM%", "%BEHAVIOURS%", "%SZR_IMPORT_STRING%", "%DEV%", "%SCALARIZR_BRANCH%", "%RECIPES%", "%BUILD_ONLY%", "%CHEF_SERVER_URL%", "%CHEF_VALIDATOR_NAME%", "%CHEF_VALIDATOR_KEY%", "%CHEF_ENVIRONMENT%", "%CHEF_ROLE%", "%CHEF_NODE_NAME%", "\r\n"), array($DBServer->platform, trim(str_replace("base", "", str_replace(",", " ", $behaviors))), $command, $scalarizrBranch ? '1' : '0', $scalarizrBranch, $recipes, '0', '', '', '', '', '', '', "\n"), $scriptContents);
                 if (!$ssh2Client->sendFile('/tmp/scalr-builder.sh', $scriptContents, "w+", false)) {
                     throw new Exception("Cannot upload script");
                 }
                 $BundleTask->Log(sprintf(_("Uploading chef recipes...")));
                 if (!$ssh2Client->sendFile('/tmp/recipes.tar.gz', APPPATH . '/www/storage/chef/recipes.tar.gz')) {
                     throw new Exception("Cannot upload chef recipes");
                 }
             } catch (Exception $e) {
                 $BundleTask->Log(sprintf(_("Scripts upload failed: %s"), $e->getMessage()));
                 //TODO: Set status of bundle log to failed
                 exit;
             }
             $BundleTask->Log("Launching role builder routines on server");
             $ssh2Client->exec("chmod 0777 /tmp/scalr-builder.sh");
             $ssh2Client->exec("setsid /tmp/scalr-builder.sh > /var/log/role-builder-output.log 2>&1 &");
             $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::INTALLING_SOFTWARE;
             $BundleTask->save();
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::INTALLING_SOFTWARE:
             try {
                 $ssh2Client = $DBServer->GetSsh2Client();
                 $ssh2Client->connect($DBServer->remoteIp, 22);
             } catch (Exception $e) {
                 $BundleTask->Log(sprintf(_("Scalr unable to establish SSH connection with server on %:%. Error: %s"), $DBServer->remoteIp, 22, $e->getMessage()));
                 //TODO: Set status of bundle log to failed
                 exit;
             }
             $log = $ssh2Client->getFile('/var/log/role-builder-output.log');
             $log_lines = explode("\r\n", $log);
             $last_msg = $DBServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_LAST_LOG_MESSAGE);
             while ($msg = trim(array_shift($log_lines))) {
                 if (substr($msg, -1, 1) != ']') {
                     continue;
                 }
                 if ($last_msg) {
                     if ($msg != $last_msg) {
                         continue;
                     } elseif ($msg == $last_msg) {
                         $last_msg = null;
                         continue;
                     }
                 }
                 if (stristr($msg, '[ Failed ]')) {
                     $stepLog = $ssh2Client->getFile('/var/log/role-builder-step.log');
                     $BundleTask->Log(sprintf("role-builder-step.log: %s", $stepLog));
                     $BundleTask->SnapshotCreationFailed($msg);
                     /** Terminate server **/
                     PlatformFactory::NewPlatform($DBServer->platform)->TerminateServer($DBServer);
                     $DBServer->status = SERVER_STATUS::PENDING_TERMINATE;
                     $DBServer->save();
                     $BundleTask->Log(sprintf("Temporary server '%s' (%s) has been terminated", $DBServer->serverId, $DBServer->GetProperty(EC2_SERVER_PROPERTIES::INSTANCE_ID)));
                 } else {
                     $BundleTask->Log($msg);
                     $DBServer->SetProperty(SERVER_PROPERTIES::SZR_IMPORTING_LAST_LOG_MESSAGE, $msg);
                 }
             }
             //Read /var/log/role-builder-output.log
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::PENDING:
             try {
                 $platformModule = PlatformFactory::NewPlatform($BundleTask->platform);
                 $platformModule->CreateServerSnapshot($BundleTask);
             } catch (Exception $e) {
                 Logger::getLogger(LOG_CATEGORY::BUNDLE)->error($e->getMessage());
                 $BundleTask->SnapshotCreationFailed($e->getMessage());
             }
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::PREPARING:
             $addedTime = strtotime($BundleTask->dateAdded);
             if ($addedTime + 3600 < time()) {
                 $BundleTask->SnapshotCreationFailed("Server didn't send PrepareBundleResult message in time.");
             }
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::IN_PROGRESS:
             PlatformFactory::NewPlatform($BundleTask->platform)->CheckServerSnapshotStatus($BundleTask);
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::REPLACING_SERVERS:
             $r_farm_roles = array();
             $BundleTask->Log(sprintf("Bundle task replacement type: %s", $BundleTask->replaceType));
             try {
                 $DBFarm = DBFarm::LoadByID($BundleTask->farmId);
             } catch (Exception $e) {
                 if (stristr($e->getMessage(), "not found in database")) {
                     $BundleTask->SnapshotCreationFailed("Farm was removed before task was finished");
                 }
                 return;
             }
             if ($BundleTask->replaceType == SERVER_REPLACEMENT_TYPE::REPLACE_FARM) {
                 try {
                     $r_farm_roles[] = $DBFarm->GetFarmRoleByRoleID($BundleTask->prototypeRoleId);
                 } catch (Exception $e) {
                 }
             } elseif ($BundleTask->replaceType == SERVER_REPLACEMENT_TYPE::REPLACE_ALL) {
                 $farm_roles = $db->GetAll("SELECT id FROM farm_roles WHERE role_id=? AND new_role_id=? AND farmid IN (SELECT id FROM farms WHERE env_id=?)", array($BundleTask->prototypeRoleId, $BundleTask->roleId, $BundleTask->envId));
                 foreach ($farm_roles as $farm_role) {
                     try {
                         $r_farm_roles[] = DBFarmRole::LoadByID($farm_role['id']);
                     } catch (Exception $e) {
                     }
                 }
             }
             $update_farm_dns_zones = array();
             $completed_roles = 0;
             foreach ($r_farm_roles as $DBFarmRole) {
                 if ($DBFarmRole->CloudLocation != $BundleTask->cloudLocation) {
                     $BundleTask->Log(sprintf("Role '%s' (ID: %s), farm '%s' (ID: %s) using the same role but in abother cloud location. Skiping it.", $DBFarmRole->GetRoleObject()->name, $DBFarmRole->ID, $DBFarmRole->GetFarmObject()->Name, $DBFarmRole->FarmID));
                     $completed_roles++;
                 } else {
                     $servers = $db->GetAll("SELECT server_id FROM servers WHERE farm_roleid = ? AND role_id=? AND status NOT IN (?,?)", array($DBFarmRole->ID, $DBFarmRole->RoleID, SERVER_STATUS::TERMINATED, SERVER_STATUS::PENDING_TERMINATE));
                     $BundleTask->Log(sprintf("Found %s servers that need to be replaced with new ones. Role '%s' (ID: %s), farm '%s' (ID: %s)", count($servers), $DBFarmRole->GetRoleObject()->name, $DBFarmRole->ID, $DBFarmRole->GetFarmObject()->Name, $DBFarmRole->FarmID));
                     if (count($servers) == 0) {
                         $DBFarmRole->RoleID = $DBFarmRole->NewRoleID;
                         $DBFarmRole->NewRoleID = null;
                         $DBFarmRole->Save();
                         $update_farm_dns_zones[$DBFarmRole->FarmID] = 1;
                         $completed_roles++;
                     } else {
                         $metaData = $BundleTask->getSnapshotDetails();
                         foreach ($servers as $server) {
                             try {
                                 $DBServer = DBServer::LoadByID($server['server_id']);
                             } catch (Exception $e) {
                                 //TODO:
                                 continue;
                             }
                             if ($DBServer->serverId == $BundleTask->serverId || $metaData['noServersReplace']) {
                                 $DBServer->roleId = $BundleTask->roleId;
                                 $DBServer->Save();
                                 if ($metaData['noServersReplace']) {
                                     $BundleTask->Log(sprintf("'Do not replace servers' option was checked. Server '%s' won't be replaced to new image.", $DBServer->serverId));
                                 } else {
                                     $BundleTask->Log(sprintf("Server '%s', on which snapshot has been taken, already has all modifications. No need to replace it.", $DBServer->serverId));
                                 }
                                 if ($DBServer->GetFarmObject()->Status == FARM_STATUS::SYNCHRONIZING) {
                                     PlatformFactory::NewPlatform($DBServer->platform)->TerminateServer($DBServer);
                                     $db->Execute("UPDATE servers_history SET\r\n\t\t\t\t\t\t\t\t\t\t\t\tdtterminated\t= NOW(),\r\n\t\t\t\t\t\t\t\t\t\t\t\tterminate_reason\t= ?\r\n\t\t\t\t\t\t\t\t\t\t\t\tWHERE server_id = ?\r\n\t\t\t\t\t\t\t\t\t\t\t", array(sprintf("Farm was in 'Synchronizing' state. Server terminated when bundling was completed. Bundle task #%s", $BundleTask->id), $DBServer->serverId));
                                 }
                             } else {
                                 if (!$db->GetOne("SELECT server_id FROM servers WHERE replace_server_id=? AND status NOT IN (?,?)", array($DBServer->serverId, SERVER_STATUS::TERMINATED, SERVER_STATUS::PENDING_TERMINATE))) {
                                     $ServerCreateInfo = new ServerCreateInfo($DBFarmRole->Platform, $DBFarmRole, $DBServer->index, $DBFarmRole->NewRoleID);
                                     $nDBServer = Scalr::LaunchServer($ServerCreateInfo);
                                     $nDBServer->replaceServerID = $DBServer->serverId;
                                     $nDBServer->Save();
                                     $BundleTask->Log(sprintf(_("Started new server %s to replace server %s"), $nDBServer->serverId, $DBServer->serverId));
                                 }
                             }
                             // if serverid != bundletask->serverID
                         }
                         // foreach server
                     }
                     // count($servers)
                 }
             }
             if ($completed_roles == count($r_farm_roles)) {
                 $BundleTask->Log(sprintf(_("No servers with old role. Replacement complete. Bundle task complete."), SERVER_REPLACEMENT_TYPE::NO_REPLACE, SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS));
                 $BundleTask->setDate('finished');
                 $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS;
                 $BundleTask->Save();
             }
             try {
                 if (count($update_farm_dns_zones) != 0) {
                     foreach ($update_farm_dns_zones as $farm_id => $v) {
                         $dnsZones = DBDNSZone::loadByFarmId($farm_id);
                         foreach ($dnsZones as $dnsZone) {
                             if ($dnsZone->status != DNS_ZONE_STATUS::INACTIVE && $dnsZone->status != DNS_ZONE_STATUS::PENDING_DELETE) {
                                 $dnsZone->updateSystemRecords();
                                 $dnsZone->save();
                             }
                         }
                     }
                 }
             } catch (Exception $e) {
                 $this->Logger->fatal("DNS ZONE: {$e->getMessage()}");
             }
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::CREATING_ROLE:
             try {
                 if ($BundleTask->replaceType == SERVER_REPLACEMENT_TYPE::REPLACE_ALL) {
                     $saveOldRole = false;
                     try {
                         $dbRole = DBRole::loadById($DBServer->roleId);
                         if ($dbRole->name == $BundleTask->roleName && $dbRole->envId == $BundleTask->envId) {
                             $saveOldRole = true;
                         }
                     } catch (Exception $e) {
                         //NO OLD ROLE
                     }
                     if ($dbRole && $saveOldRole) {
                         if ($DBServer) {
                             $new_role_name = BundleTask::GenerateRoleName($DBServer->GetFarmRoleObject(), $DBServer);
                         } else {
                             $new_role_name = $BundleTask->roleName . "-" . rand(1000, 9999);
                         }
                         $dbRole->name = $new_role_name;
                         $BundleTask->Log(sprintf(_("Old role '%s' (ID: %s) renamed to '%s'"), $BundleTask->roleName, $dbRole->id, $new_role_name));
                         $dbRole->save();
                     } else {
                         //TODO:
                         //$this->Logger->error("dbRole->replace->fail({$BundleTask->roleName}, {$BundleTask->envId})");
                     }
                 }
                 try {
                     $DBRole = DBRole::createFromBundleTask($BundleTask);
                 } catch (Exception $e) {
                     $BundleTask->SnapshotCreationFailed("Role creation failed due to internal error ({$e->getMessage()}). Please try again.");
                     return;
                 }
                 if ($BundleTask->replaceType == SERVER_REPLACEMENT_TYPE::NO_REPLACE) {
                     $BundleTask->setDate('finished');
                     $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS;
                     $BundleTask->Log(sprintf(_("Replacement type: %s. Bundle task status: %s"), SERVER_REPLACEMENT_TYPE::NO_REPLACE, SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS));
                     try {
                         $DBServer = DBServer::LoadByID($BundleTask->serverId);
                         if ($DBServer->status == SERVER_STATUS::IMPORTING) {
                             if ($DBServer->farmId) {
                                 // Create DBFarmRole object
                                 // TODO: create DBFarm role
                             }
                             //$DBServer->Delete();
                         }
                     } catch (Exception $e) {
                     }
                 } else {
                     try {
                         $BundleTask->Log(sprintf(_("Replacement type: %s. Bundle task status: %s"), $BundleTask->replaceType, SERVER_SNAPSHOT_CREATION_STATUS::REPLACING_SERVERS));
                         if ($BundleTask->replaceType == SERVER_REPLACEMENT_TYPE::REPLACE_FARM) {
                             $DBFarm = DBFarm::LoadByID($BundleTask->farmId);
                             $DBFarmRole = $DBFarm->GetFarmRoleByRoleID($BundleTask->prototypeRoleId);
                             $DBFarmRole->NewRoleID = $BundleTask->roleId;
                             $DBFarmRole->Save();
                         } else {
                             $farm_roles = $db->GetAll("SELECT id FROM farm_roles WHERE role_id=? AND farmid IN (SELECT id FROM farms WHERE env_id=?)", array($BundleTask->prototypeRoleId, $BundleTask->envId));
                             foreach ($farm_roles as $farm_role) {
                                 $DBFarmRole = DBFarmRole::LoadByID($farm_role['id']);
                                 $DBFarmRole->NewRoleID = $BundleTask->roleId;
                                 $DBFarmRole->Save();
                             }
                         }
                         $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::REPLACING_SERVERS;
                     } catch (Exception $e) {
                         $this->Logger->error($e->getMessage());
                         $BundleTask->Log(sprintf(_("Server replacement failed: %s"), $e->getMessage()));
                         $BundleTask->setDate('finished');
                         $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS;
                     }
                 }
                 $BundleTask->Save();
             } catch (Exception $e) {
                 $this->Logger->error($e->getMessage());
             }
             break;
     }
 }
Exemplo n.º 8
0
 private function onHello($message, DBServer $dbserver)
 {
     if ($dbserver->status == SERVER_STATUS::TEMPORARY) {
         $bundleTask = BundleTask::LoadById($dbserver->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_BUNDLE_TASK_ID));
         $bundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::PENDING;
         $bundleTask->Log("Received Hello message from scalarizr on server. Creating image");
         $bundleTask->save();
     }
     if ($dbserver->status == SERVER_STATUS::IMPORTING) {
         switch ($dbserver->platform) {
             case SERVER_PLATFORMS::EC2:
                 $dbserver->SetProperties(array(EC2_SERVER_PROPERTIES::AMIID => $message->awsAmiId, EC2_SERVER_PROPERTIES::INSTANCE_ID => $message->awsInstanceId, EC2_SERVER_PROPERTIES::INSTANCE_TYPE => $message->awsInstanceType, EC2_SERVER_PROPERTIES::AVAIL_ZONE => $message->awsAvailZone, EC2_SERVER_PROPERTIES::REGION => substr($message->awsAvailZone, 0, -1), SERVER_PROPERTIES::ARCHITECTURE => $message->architecture));
                 break;
             case SERVER_PLATFORMS::EUCALYPTUS:
                 $dbserver->SetProperties(array(EUCA_SERVER_PROPERTIES::EMIID => $message->awsAmiId, EUCA_SERVER_PROPERTIES::INSTANCE_ID => $message->awsInstanceId, EUCA_SERVER_PROPERTIES::INSTANCE_TYPE => $message->awsInstanceType, EUCA_SERVER_PROPERTIES::AVAIL_ZONE => $message->awsAvailZone, SERVER_PROPERTIES::ARCHITECTURE => $message->architecture));
                 break;
             case SERVER_PLATFORMS::NIMBULA:
                 $dbserver->SetProperties(array(NIMBULA_SERVER_PROPERTIES::NAME => $message->serverName, SERVER_PROPERTIES::ARCHITECTURE => $message->architecture));
                 break;
             case SERVER_PLATFORMS::CLOUDSTACK:
                 $dbserver->SetProperties(array(CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID => $message->cloudstack->instanceId, CLOUDSTACK_SERVER_PROPERTIES::CLOUD_LOCATION => $message->cloudstack->availZone, SERVER_PROPERTIES::ARCHITECTURE => $message->architecture));
                 break;
             case SERVER_PLATFORMS::RACKSPACE:
                 $env = $dbserver->GetEnvironmentObject();
                 $cs = Scalr_Service_Cloud_Rackspace::newRackspaceCS($env->getPlatformConfigValue(Modules_Platforms_Rackspace::USERNAME, true, $dbserver->GetProperty(RACKSPACE_SERVER_PROPERTIES::DATACENTER)), $env->getPlatformConfigValue(Modules_Platforms_Rackspace::API_KEY, true, $dbserver->GetProperty(RACKSPACE_SERVER_PROPERTIES::DATACENTER)), $dbserver->GetProperty(RACKSPACE_SERVER_PROPERTIES::DATACENTER));
                 $csServer = null;
                 $list = $cs->listServers(true);
                 if ($list) {
                     foreach ($list->servers as $_tmp) {
                         if ($_tmp->addresses->public && in_array($message->remoteIp, $_tmp->addresses->public)) {
                             $csServer = $_tmp;
                         }
                     }
                 }
                 if (!$csServer) {
                     $this->logger->error(sprintf("Server not found on CloudServers (server_id: %s, remote_ip: %s, local_ip: %s)", $dbserver->serverId, $message->remoteIp, $message->localIp));
                     return;
                 }
                 $dbserver->SetProperties(array(RACKSPACE_SERVER_PROPERTIES::SERVER_ID => $csServer->id, RACKSPACE_SERVER_PROPERTIES::NAME => $csServer->name, RACKSPACE_SERVER_PROPERTIES::IMAGE_ID => $csServer->imageId, RACKSPACE_SERVER_PROPERTIES::FLAVOR_ID => $csServer->flavorId, RACKSPACE_SERVER_PROPERTIES::HOST_ID => $csServer->hostId, SERVER_PROPERTIES::ARCHITECTURE => $message->architecture));
                 break;
             case SERVER_PLATFORMS::OPENSTACK:
                 $env = $dbserver->GetEnvironmentObject();
                 $os = Scalr_Service_Cloud_Openstack::newNovaCC($env->getPlatformConfigValue(Modules_Platforms_Openstack::API_URL, true, $dbserver->GetProperty(OPENSTACK_SERVER_PROPERTIES::CLOUD_LOCATION)), $env->getPlatformConfigValue(Modules_Platforms_Openstack::USERNAME, true, $dbserver->GetProperty(OPENSTACK_SERVER_PROPERTIES::CLOUD_LOCATION)), $env->getPlatformConfigValue(Modules_Platforms_Openstack::API_KEY, true, $dbserver->GetProperty(OPENSTACK_SERVER_PROPERTIES::CLOUD_LOCATION)), $env->getPlatformConfigValue(Modules_Platforms_Openstack::PROJECT_NAME, true, $dbserver->GetProperty(OPENSTACK_SERVER_PROPERTIES::CLOUD_LOCATION)));
                 $csServer = null;
                 $list = $os->serversList(true);
                 if ($list) {
                     foreach ($list->servers as $_tmp) {
                         $ipaddresses = array();
                         if ($_tmp->addresses->private) {
                             foreach ($_tmp->addresses->private as $addr) {
                                 if ($addr->version == 4) {
                                     array_push($ipaddresses, $addr->addr);
                                 }
                             }
                         }
                         if ($_tmp->addresses->internet) {
                             foreach ($_tmp->addresses->internet as $addr) {
                                 if ($addr->version == 4) {
                                     array_push($ipaddresses, $addr->addr);
                                 }
                             }
                         }
                         if (in_array($message->localIp, $ipaddresses) || in_array($message->remoteIp, $ipaddresses)) {
                             $osServer = $_tmp;
                         }
                     }
                 }
                 if (!$osServer) {
                     $this->logger->error(sprintf("Server not found on Openstack (server_id: %s, remote_ip: %s, local_ip: %s)", $dbserver->serverId, $message->remoteIp, $message->localIp));
                     return;
                 }
                 $dbserver->SetProperties(array(OPENSTACK_SERVER_PROPERTIES::SERVER_ID => $osServer->id, OPENSTACK_SERVER_PROPERTIES::NAME => $osServer->name, OPENSTACK_SERVER_PROPERTIES::IMAGE_ID => $osServer->image->id, OPENSTACK_SERVER_PROPERTIES::FLAVOR_ID => $osServer->flavor->id, OPENSTACK_SERVER_PROPERTIES::HOST_ID => $osServer->hostId, SERVER_PROPERTIES::ARCHITECTURE => $message->architecture));
                 break;
         }
         // Bundle image
         $creInfo = new ServerSnapshotCreateInfo($dbserver, $dbserver->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_ROLE_NAME), SERVER_REPLACEMENT_TYPE::NO_REPLACE);
         $bundleTask = BundleTask::Create($creInfo);
     }
 }
Exemplo n.º 9
0
 public function BundleTaskGetStatus($BundleTaskID)
 {
     $this->isAllowed(Acl::RESOURCE_FARMS_ROLES, Acl::PERM_FARMS_ROLES_BUNDLETASKS);
     $BundleTask = BundleTask::LoadById($BundleTaskID);
     if ($BundleTask->envId != $this->Environment->id) {
         throw new Exception(sprintf("Bundle task #%s not found", $BundleTaskID));
     }
     if (!empty($BundleTask->farmId)) {
         $DBFarm = DBFarm::LoadByID($BundleTask->farmId);
         if ($DBFarm) {
             $this->user->getPermissions()->validate($DBFarm);
         }
     }
     $response = $this->CreateInitialResponse();
     $response->BundleTaskStatus = $BundleTask->status;
     if ($BundleTask->status == SERVER_SNAPSHOT_CREATION_STATUS::FAILED) {
         $response->FailureReason = $BundleTask->failureReason;
     }
     return $response;
 }
 public function StartThread($bundle_task_info)
 {
     $db = \Scalr::getDb();
     // Reconfigure observers;
     Scalr::ReconfigureObservers();
     $BundleTask = BundleTask::LoadById($bundle_task_info['id']);
     try {
         $DBServer = DBServer::LoadByID($BundleTask->serverId);
     } catch (\Scalr\Exception\ServerNotFoundException $e) {
         if (!$BundleTask->snapshotId) {
             $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::FAILED;
             $BundleTask->setDate('finished');
             $BundleTask->failureReason = sprintf(_("Server '%s' was terminated during snapshot creation process"), $BundleTask->serverId);
             $BundleTask->Save();
             return;
         }
     } catch (Exception $e) {
         //$this->Logger->error($e->getMessage());
     }
     switch ($BundleTask->status) {
         case SERVER_SNAPSHOT_CREATION_STATUS::ESTABLISHING_COMMUNICATION:
             $conn = @fsockopen($DBServer->getSzrHost(), $DBServer->getPort(DBServer::PORT_CTRL), $errno, $errstr, 10);
             if ($conn) {
                 $DBServer->SetProperty(SERVER_PROPERTIES::SZR_IMPORTING_OUT_CONNECTION, 1);
                 $BundleTask->Log("Outbound connection successfully established. Awaiting user action: prebuild automation selection");
                 $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::AWAITING_USER_ACTION;
                 $BundleTask->Log(sprintf(_("Bundle task status: %s"), $BundleTask->status));
                 $BundleTask->Save();
             } else {
                 $errstr = sprintf("Unable to establish outbound (Scalr -> Scalarizr) communication (%s:%s): %s.", $requestHost, $ctrlPort, $errstr);
                 $errMsg = $DBServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_OUT_CONNECTION_ERROR);
                 if (!$errMsg || $errstr != $errMsg) {
                     $DBServer->SetProperty(SERVER_PROPERTIES::SZR_IMPORTING_OUT_CONNECTION_ERROR, $errstr);
                     $BundleTask->Log("{$errstr} Will try again in a few minutes.");
                 }
             }
             exit;
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::AWAITING_USER_ACTION:
             //NOTHING TO DO;
             exit;
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::STARING_SERVER:
             $BundleTask->setDate('started');
         case SERVER_SNAPSHOT_CREATION_STATUS::PREPARING_ENV:
         case SERVER_SNAPSHOT_CREATION_STATUS::INTALLING_SOFTWARE:
             if (!PlatformFactory::NewPlatform($DBServer->platform)->GetServerID($DBServer)) {
                 $BundleTask->Log(sprintf(_("Waiting for temporary server")));
                 exit;
             }
             if (!PlatformFactory::NewPlatform($DBServer->platform)->IsServerExists($DBServer)) {
                 $DBServer->status = SERVER_STATUS::TERMINATED;
                 $DBServer->save();
                 $BundleTask->SnapshotCreationFailed("Server was terminated and no longer available in cloud.");
                 exit;
             }
             // IF server is in pensing state
             $status = PlatformFactory::NewPlatform($DBServer->platform)->GetServerRealStatus($DBServer);
             if ($status->isPending()) {
                 $BundleTask->Log(sprintf(_("Server status: %s"), $status->getName()));
                 $BundleTask->Log(sprintf(_("Waiting for running state."), $status->getName()));
                 exit;
             } elseif ($status->isTerminated()) {
                 $BundleTask->Log(sprintf(_("Server status: %s"), $status->getName()));
                 $DBServer->status = SERVER_STATUS::TERMINATED;
                 $DBServer->save();
                 $BundleTask->SnapshotCreationFailed("Server was terminated and no longer available in cloud.");
                 exit;
             }
             break;
     }
     switch ($BundleTask->status) {
         case SERVER_SNAPSHOT_CREATION_STATUS::STARING_SERVER:
             $ips = PlatformFactory::NewPlatform($DBServer->platform)->GetServerIPAddresses($DBServer);
             $DBServer->remoteIp = $ips['remoteIp'];
             $DBServer->localIp = $ips['localIp'];
             $DBServer->save();
             $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::PREPARING_ENV;
             $BundleTask->save();
             $BundleTask->Log(sprintf(_("Bundle task status: %s"), $BundleTask->status));
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::PREPARING_ENV:
             $BundleTask->Log(sprintf(_("Initializing SSH2 session to the server")));
             if ($DBServer->platform == SERVER_PLATFORMS::IDCF && !$DBServer->remoteIp) {
                 try {
                     $BundleTask->Log("Creating port forwarding rules to be able to connect to the server by SSH");
                     $environment = $DBServer->GetEnvironmentObject();
                     $cloudLocation = $DBServer->GetCloudLocation();
                     $platform = PlatformFactory::NewPlatform($DBServer->platform);
                     $sharedIpId = $platform->getConfigVariable(CloudstackPlatformModule::SHARED_IP_ID . ".{$cloudLocation}", $environment, false);
                     $sharedIp = $platform->getConfigVariable(CloudstackPlatformModule::SHARED_IP . ".{$cloudLocation}", $environment, false);
                     $BundleTask->Log("Shared IP: {$sharedIp}");
                     $cs = $environment->cloudstack($DBServer->platform);
                     // Create port forwarding rules for scalarizr
                     $port = $platform->getConfigVariable(CloudstackPlatformModule::SZR_PORT_COUNTER . ".{$cloudLocation}.{$sharedIpId}", $environment, false);
                     if (!$port) {
                         $port1 = 30000;
                         $port2 = 30001;
                         $port3 = 30002;
                         $port4 = 30003;
                     } else {
                         $port1 = $port + 1;
                         $port2 = $port1 + 1;
                         $port3 = $port2 + 1;
                         $port4 = $port3 + 1;
                     }
                     $virtualmachineid = $DBServer->GetProperty(CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID);
                     $result2 = $cs->firewall->createPortForwardingRule(array('ipaddressid' => $sharedIpId, 'privateport' => 8014, 'protocol' => "udp", 'publicport' => $port1, 'virtualmachineid' => $virtualmachineid));
                     $result1 = $cs->firewall->createPortForwardingRule(array('ipaddressid' => $sharedIpId, 'privateport' => 8013, 'protocol' => "tcp", 'publicport' => $port1, 'virtualmachineid' => $virtualmachineid));
                     $result3 = $cs->firewall->createPortForwardingRule(array('ipaddressid' => $sharedIpId, 'privateport' => 8010, 'protocol' => "tcp", 'publicport' => $port3, 'virtualmachineid' => $virtualmachineid));
                     $result4 = $cs->firewall->createPortForwardingRule(array('ipaddressid' => $sharedIpId, 'privateport' => 8008, 'protocol' => "tcp", 'publicport' => $port2, 'virtualmachineid' => $virtualmachineid));
                     $result5 = $cs->firewall->createPortForwardingRule(array('ipaddressid' => $sharedIpId, 'privateport' => 22, 'protocol' => "tcp", 'publicport' => $port4, 'virtualmachineid' => $virtualmachineid));
                     $DBServer->SetProperties(array(SERVER_PROPERTIES::SZR_CTRL_PORT => $port1, SERVER_PROPERTIES::SZR_SNMP_PORT => $port1, SERVER_PROPERTIES::SZR_API_PORT => $port3, SERVER_PROPERTIES::SZR_UPDC_PORT => $port2, SERVER_PROPERTIES::CUSTOM_SSH_PORT => $port4));
                     $DBServer->remoteIp = $sharedIp;
                     $DBServer->Save();
                     $platform->setConfigVariable(array(CloudstackPlatformModule::SZR_PORT_COUNTER . ".{$cloudLocation}.{$sharedIpId}" => $port4), $environment, false);
                 } catch (Exception $e) {
                     $BundleTask->Log("Unable to create port-forwarding rules: {$e->getMessage()}");
                 }
                 exit;
             }
             if ($DBServer->platform == SERVER_PLATFORMS::ECS && !$DBServer->remoteIp) {
                 $BundleTask->Log(sprintf(_("Server doesn't have public IP. Assigning...")));
                 $osClient = $DBServer->GetEnvironmentObject()->openstack($DBServer->platform, $DBServer->GetProperty(OPENSTACK_SERVER_PROPERTIES::CLOUD_LOCATION));
                 $ports = $osClient->network->ports->list();
                 foreach ($ports as $port) {
                     if ($port->device_id == $DBServer->GetProperty(OPENSTACK_SERVER_PROPERTIES::SERVER_ID)) {
                         $serverNetworkPort = $port->id;
                         break;
                     }
                 }
                 $ips = $osClient->network->floatingIps->list();
                 //Check free existing IP
                 $ipAssigned = false;
                 $ipAddress = false;
                 $ipId = false;
                 $ipInfo = false;
                 foreach ($ips as $ip) {
                     if ($ip->port_id && $ip->port_id == $serverNetworkPort) {
                         $ipAddress = $ip->floating_ip_address;
                         $ipId = $ip->id;
                         $ipAssigned = true;
                         $ipInfo = $ip;
                         break;
                     }
                     if (!$ip->fixed_ip_address && !$ipAddress) {
                         $ipAddress = $ip->floating_ip_address;
                         $ipId = $ip->id;
                         $ipInfo = $ip;
                     }
                 }
                 if (!$ipAssigned) {
                     if (!$serverNetworkPort) {
                         $BundleTask->Log("Unable to identify network port of instance");
                         exit;
                     } else {
                         if (!$ipAddress) {
                             $networks = $osClient->network->listNetworks();
                             foreach ($networks as $network) {
                                 if ($network->{"router:external"} == true) {
                                     $publicNetworkId = $network->id;
                                 }
                             }
                             if (!$publicNetworkId) {
                                 $BundleTask->Log("Unable to identify public network to allocate");
                                 exit;
                             } else {
                                 $ip = $osClient->network->floatingIps->create($publicNetworkId, $serverNetworkPort);
                                 $ipAddress = $ip->floating_ip_address;
                                 $DBServer->SetProperties(array(OPENSTACK_SERVER_PROPERTIES::FLOATING_IP => $ip->floating_ip_address, OPENSTACK_SERVER_PROPERTIES::FLOATING_IP_ID => $ip->id));
                                 $BundleTask->Log("Allocated new IP {$ipAddress} for port: {$serverNetworkPort}");
                             }
                         } else {
                             $BundleTask->Log("Found free floating IP: {$ipAddress} for use (" . json_encode($ipInfo) . ")");
                             $osClient->network->floatingIps->update($ipId, $serverNetworkPort);
                         }
                     }
                 } else {
                     $BundleTask->Log("IP: {$ipAddress} already assigned");
                 }
                 if ($ipAddress) {
                     $DBServer->remoteIp = $ipAddress;
                     $DBServer->Save();
                 }
                 exit;
             }
             try {
                 $ssh2Client = $DBServer->GetSsh2Client();
                 $ssh2Client->connect($DBServer->remoteIp, $DBServer->getPort(DBServer::PORT_SSH));
             } catch (Exception $e) {
                 $BundleTask->Log(sprintf(_("Scalr unable to establish SSH connection with server on %:%. Error: %s"), $DBServer->remoteIp, $DBServer->getPort(DBServer::PORT_SSH), $e->getMessage()));
                 //TODO: Set status of bundle log to failed
                 exit;
             }
             $BundleTask->Log(sprintf(_("Created SSH session. Username: %s"), $ssh2Client->getLogin()));
             //Prepare script
             $BundleTask->Log(sprintf(_("Uploading builder scripts...")));
             $behaviors = $DBServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_BEHAVIOR);
             try {
                 if ($DBServer->isOpenstack()) {
                     $platform = SERVER_PLATFORMS::OPENSTACK;
                 } else {
                     $platform = $DBServer->platform;
                 }
                 $baseUrl = rtrim(\Scalr::config('scalr.endpoint.scheme') . "://" . \Scalr::config('scalr.endpoint.host'), '/');
                 $options = array('server-id' => $DBServer->serverId, 'role-name' => $BundleTask->roleName, 'crypto-key' => $DBServer->GetProperty(SERVER_PROPERTIES::SZR_KEY), 'platform' => $platform, 'queryenv-url' => $baseUrl . "/query-env", 'messaging-p2p.producer-url' => $baseUrl . "/messaging", 'behaviour' => trim(trim(str_replace("base", "", $behaviors), ",")), 'env-id' => $DBServer->envId, 'region' => $DBServer->GetCloudLocation(), 'scalr-id' => SCALR_ID);
                 $command = 'scalarizr --import -y';
                 foreach ($options as $k => $v) {
                     $command .= sprintf(' -o %s=%s', $k, $v);
                 }
                 if ($DBServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_MYSQL_SERVER_TYPE) == 'percona') {
                     $recipes = 'mysql=percona';
                 } else {
                     $recipes = '';
                 }
                 $scalarizrBranch = $DBServer->GetProperty(SERVER_PROPERTIES::SZR_DEV_SCALARIZR_BRANCH);
                 $scriptContents = @file_get_contents(APPPATH . "/templates/services/role_builder/chef_import.tpl");
                 /*
                 %CHEF_SERVER_URL%
                 %CHEF_VALIDATOR_NAME%
                 %CHEF_VALIDATOR_KEY%
                 %CHEF_ENVIRONMENT%
                 %CHEF_ROLE_NAME%
                 */
                 $chefServerId = $DBServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_CHEF_SERVER_ID);
                 if ($chefServerId) {
                     $chefServerInfo = $db->GetRow("SELECT * FROM services_chef_servers WHERE id=?", array($chefServerId));
                     $chefServerInfo['v_auth_key'] = $this->crypto->decrypt($chefServerInfo['v_auth_key'], $this->cryptoKey);
                 }
                 $scriptContents = str_replace(array("%PLATFORM%", "%BEHAVIOURS%", "%SZR_IMPORT_STRING%", "%DEV%", "%SCALARIZR_BRANCH%", "%RECIPES%", "%BUILD_ONLY%", "%CHEF_SERVER_URL%", "%CHEF_VALIDATOR_NAME%", "%CHEF_VALIDATOR_KEY%", "%CHEF_ENVIRONMENT%", "%CHEF_ROLE%", "%CHEF_ROLE_NAME%", "%CHEF_NODE_NAME%", "\r\n"), array($platform, trim(str_replace("base", "", str_replace(",", " ", $behaviors))), $command, $scalarizrBranch ? '1' : '0', $scalarizrBranch, $recipes, '0', $chefServerInfo['url'], $chefServerInfo['v_username'], $chefServerInfo['v_auth_key'], $DBServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_CHEF_ENVIRONMENT), $DBServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_CHEF_ROLE_NAME), $DBServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_CHEF_ROLE_NAME), '', "\n"), $scriptContents);
                 if (!$ssh2Client->sendFile('/tmp/scalr-builder.sh', $scriptContents, "w+", false)) {
                     throw new Exception("Cannot upload script");
                 }
                 /*
                 $BundleTask->Log(sprintf(_("Uploading chef recipes...")));
                 if (!$ssh2Client->sendFile('/tmp/recipes.tar.gz', APPPATH . '/www/storage/chef/recipes.tar.gz')) {
                     throw new Exception("Cannot upload chef recipes");
                 }
                 */
             } catch (Exception $e) {
                 $BundleTask->Log(sprintf(_("Scripts upload failed: %s"), $e->getMessage()));
                 //TODO: Set status of bundle log to failed
                 exit;
             }
             $BundleTask->Log("Launching role builder routines on server");
             $ssh2Client->exec("chmod 0777 /tmp/scalr-builder.sh");
             // For CGE we need to use sudo
             if ($BundleTask->platform == SERVER_PLATFORMS::GCE || $BundleTask->osFamily == 'amazon') {
                 $shell = $ssh2Client->getShell();
                 @stream_set_blocking($shell, true);
                 @stream_set_timeout($shell, 5);
                 @fwrite($shell, "sudo touch /var/log/role-builder-output.log 2>&1" . PHP_EOL);
                 $output = @fgets($shell, 4096);
                 $BundleTask->Log("Verbose 1: {$output}");
                 @fwrite($shell, "sudo chmod 0666 /var/log/role-builder-output.log 2>&1" . PHP_EOL);
                 $output2 = @fgets($shell, 4096);
                 $BundleTask->Log("Verbose 2: {$output2}");
                 @fwrite($shell, "sudo setsid /tmp/scalr-builder.sh > /var/log/role-builder-output.log 2>&1 &" . PHP_EOL);
                 $output3 = @fgets($shell, 4096);
                 $BundleTask->Log("Verbose 3: {$output3}");
                 sleep(5);
                 $meta = stream_get_meta_data($shell);
                 $BundleTask->Log(sprintf("Verbose (Meta): %s", json_encode($meta)));
                 $i = 4;
                 if ($meta['eof'] == false && $meta['unread_bytes'] != 0) {
                     $output4 = @fread($shell, $meta['unread_bytes']);
                     $BundleTask->Log("Verbose {$i}: {$output4}");
                     $meta = stream_get_meta_data($shell);
                     $BundleTask->Log(sprintf("Verbose (Meta): %s", json_encode($meta)));
                 }
                 @fclose($shell);
                 /*
                 $r1 = $ssh2Client->exec("sudo touch /var/log/role-builder-output.log");
                 $BundleTask->Log("1: {$r1} ({$ssh2Client->stdErr})");
                 $r2 = $ssh2Client->exec("sudo chmod 0666 /var/log/role-builder-output.log");
                 $BundleTask->Log("2: {$r2} ({$ssh2Client->stdErr})");
                 $r3 = $ssh2Client->exec("sudo setsid /tmp/scalr-builder.sh > /var/log/role-builder-output.log 2>&1 &");
                 $BundleTask->Log("3: {$r3} ({$ssh2Client->stdErr})");
                 */
             } else {
                 $ssh2Client->exec("setsid /tmp/scalr-builder.sh > /var/log/role-builder-output.log 2>&1 &");
             }
             $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::INTALLING_SOFTWARE;
             $BundleTask->save();
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::INTALLING_SOFTWARE:
             try {
                 $ssh2Client = $DBServer->GetSsh2Client();
                 $ssh2Client->connect($DBServer->remoteIp, $DBServer->getPort(DBServer::PORT_SSH));
             } catch (Exception $e) {
                 $BundleTask->Log(sprintf(_("Scalr unable to establish SSH connection with server on %:%. Error: %s"), $DBServer->remoteIp, $DBServer->getPort(DBServer::PORT_SSH), $e->getMessage()));
                 //TODO: Set status of bundle log to failed
                 exit;
             }
             $log = $ssh2Client->getFile('/var/log/role-builder-output.log');
             $log_lines = explode("\r\n", $log);
             $last_msg = $DBServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_LAST_LOG_MESSAGE);
             while ($msg = trim(array_shift($log_lines))) {
                 if (substr($msg, -1, 1) != ']') {
                     continue;
                 }
                 if ($last_msg) {
                     if ($msg != $last_msg) {
                         continue;
                     } elseif ($msg == $last_msg) {
                         $last_msg = null;
                         continue;
                     }
                 }
                 if (stristr($msg, '[ Failed ]')) {
                     $stepLog = $ssh2Client->getFile('/var/log/role-builder-step.log');
                     $BundleTask->Log(sprintf("role-builder-step.log: %s", $stepLog));
                     $BundleTask->SnapshotCreationFailed($msg);
                 } else {
                     $BundleTask->Log($msg);
                     $DBServer->SetProperty(SERVER_PROPERTIES::SZR_IMPORTING_LAST_LOG_MESSAGE, $msg);
                 }
             }
             //Read /var/log/role-builder-output.log
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::PENDING:
             try {
                 $platformModule = PlatformFactory::NewPlatform($BundleTask->platform);
                 $platformModule->CreateServerSnapshot($BundleTask);
             } catch (Exception $e) {
                 Logger::getLogger(LOG_CATEGORY::BUNDLE)->error($e->getMessage());
                 $BundleTask->SnapshotCreationFailed($e->getMessage());
             }
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::PREPARING:
             $addedTime = strtotime($BundleTask->dateAdded);
             if ($addedTime + 3600 < time()) {
                 $BundleTask->SnapshotCreationFailed("Server didn't send PrepareBundleResult message in time.");
             }
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::IN_PROGRESS:
             PlatformFactory::NewPlatform($BundleTask->platform)->CheckServerSnapshotStatus($BundleTask);
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::REPLACING_SERVERS:
             $r_farm_roles = array();
             $BundleTask->Log(sprintf("Bundle task replacement type: %s", $BundleTask->replaceType));
             try {
                 $DBFarm = DBFarm::LoadByID($BundleTask->farmId);
             } catch (Exception $e) {
                 if (stristr($e->getMessage(), "not found in database")) {
                     $BundleTask->SnapshotCreationFailed("Farm was removed before task was finished");
                 }
                 return;
             }
             if ($BundleTask->replaceType == SERVER_REPLACEMENT_TYPE::REPLACE_FARM) {
                 try {
                     $r_farm_roles[] = $DBFarm->GetFarmRoleByRoleID($BundleTask->prototypeRoleId);
                 } catch (Exception $e) {
                 }
             } elseif ($BundleTask->replaceType == SERVER_REPLACEMENT_TYPE::REPLACE_ALL) {
                 $farm_roles = $db->GetAll("\n                        SELECT id FROM farm_roles\n                        WHERE role_id=? AND new_role_id=?\n                        AND farmid IN (SELECT id FROM farms WHERE env_id=?)\n                    ", array($BundleTask->prototypeRoleId, $BundleTask->roleId, $BundleTask->envId));
                 foreach ($farm_roles as $farm_role) {
                     try {
                         $r_farm_roles[] = DBFarmRole::LoadByID($farm_role['id']);
                     } catch (Exception $e) {
                     }
                 }
             }
             $update_farm_dns_zones = array();
             $completed_roles = 0;
             foreach ($r_farm_roles as $DBFarmRole) {
                 if ($DBFarmRole->CloudLocation != $BundleTask->cloudLocation) {
                     $BundleTask->Log(sprintf("Role '%s' (ID: %s), farm '%s' (ID: %s) using the same role " . "but in abother cloud location. Skiping it.", $DBFarmRole->GetRoleObject()->name, $DBFarmRole->ID, $DBFarmRole->GetFarmObject()->Name, $DBFarmRole->FarmID));
                     $completed_roles++;
                 } else {
                     $servers = $db->GetAll("SELECT server_id FROM servers WHERE farm_roleid = ? AND role_id=? AND status NOT IN (?,?)", array($DBFarmRole->ID, $DBFarmRole->RoleID, SERVER_STATUS::TERMINATED, SERVER_STATUS::PENDING_TERMINATE));
                     $BundleTask->Log(sprintf("Found %s servers that need to be replaced with new ones. " . "Role '%s' (ID: %s), farm '%s' (ID: %s)", count($servers), $DBFarmRole->GetRoleObject()->name, $DBFarmRole->ID, $DBFarmRole->GetFarmObject()->Name, $DBFarmRole->FarmID));
                     if (count($servers) == 0) {
                         $DBFarmRole->RoleID = $DBFarmRole->NewRoleID;
                         $DBFarmRole->NewRoleID = null;
                         $DBFarmRole->Save();
                         $update_farm_dns_zones[$DBFarmRole->FarmID] = 1;
                         $completed_roles++;
                     } else {
                         $metaData = $BundleTask->getSnapshotDetails();
                         foreach ($servers as $server) {
                             try {
                                 $DBServer = DBServer::LoadByID($server['server_id']);
                             } catch (Exception $e) {
                                 //TODO:
                                 continue;
                             }
                             if ($DBServer->serverId == $BundleTask->serverId || $metaData['noServersReplace']) {
                                 $DBServer->roleId = $BundleTask->roleId;
                                 $DBServer->Save();
                                 if ($metaData['noServersReplace']) {
                                     $BundleTask->Log(sprintf("'Do not replace servers' option was checked. " . "Server '%s' won't be replaced to new image.", $DBServer->serverId));
                                 } else {
                                     $BundleTask->Log(sprintf("Server '%s', on which snapshot has been taken, " . "already has all modifications. No need to replace it.", $DBServer->serverId));
                                 }
                             } else {
                                 if (!$db->GetOne("SELECT server_id FROM servers WHERE replace_server_id=? AND status NOT IN (?,?,?) LIMIT 1", array($DBServer->serverId, SERVER_STATUS::TERMINATED, SERVER_STATUS::PENDING_TERMINATE, SERVER_STATUS::TROUBLESHOOTING, SERVER_STATUS::SUSPENDED))) {
                                     $ServerCreateInfo = new ServerCreateInfo($DBFarmRole->Platform, $DBFarmRole, $DBServer->index, $DBFarmRole->NewRoleID);
                                     $nDBServer = Scalr::LaunchServer($ServerCreateInfo, null, false, DBServer::LAUNCH_REASON_REPLACE_SERVER_FROM_SNAPSHOT, !empty($BundleTask->createdById) ? $BundleTask->createdById : null);
                                     $nDBServer->replaceServerID = $DBServer->serverId;
                                     $nDBServer->Save();
                                     $BundleTask->Log(sprintf(_("Started new server %s to replace server %s"), $nDBServer->serverId, $DBServer->serverId));
                                 }
                             }
                         }
                     }
                 }
             }
             if ($completed_roles == count($r_farm_roles)) {
                 $BundleTask->Log(sprintf(_("No servers with old role. Replacement complete. Bundle task complete."), SERVER_REPLACEMENT_TYPE::NO_REPLACE, SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS));
                 try {
                     if ($DBServer->status == SERVER_STATUS::IMPORTING) {
                         $DBServer->Remove();
                     } elseif ($DBServer->status == SERVER_STATUS::TEMPORARY) {
                         $BundleTask->Log("Terminating temporary server");
                         $DBServer->terminate(DBServer::TERMINATE_REASON_TEMPORARY_SERVER_ROLE_BUILDER);
                         $BundleTask->Log("Termination request has been sent");
                     }
                 } catch (Exception $e) {
                     $BundleTask->Log("Warning: {$e->getMessage()}");
                 }
                 $BundleTask->setDate('finished');
                 $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS;
                 $BundleTask->Save();
                 $BundleTask->createImageEntity();
             }
             try {
                 if (count($update_farm_dns_zones) != 0) {
                     foreach ($update_farm_dns_zones as $farm_id => $v) {
                         $dnsZones = DBDNSZone::loadByFarmId($farm_id);
                         foreach ($dnsZones as $dnsZone) {
                             if ($dnsZone->status != DNS_ZONE_STATUS::INACTIVE && $dnsZone->status != DNS_ZONE_STATUS::PENDING_DELETE) {
                                 $dnsZone->updateSystemRecords();
                                 $dnsZone->save();
                             }
                         }
                     }
                 }
             } catch (Exception $e) {
                 $this->Logger->fatal("DNS ZONE: {$e->getMessage()}");
             }
             break;
         case SERVER_SNAPSHOT_CREATION_STATUS::CREATING_ROLE:
             try {
                 if ($BundleTask->replaceType == SERVER_REPLACEMENT_TYPE::REPLACE_ALL) {
                     $saveOldRole = false;
                     try {
                         $dbRole = DBRole::loadById($DBServer->roleId);
                         if ($dbRole->name == $BundleTask->roleName && $dbRole->envId == $BundleTask->envId) {
                             $saveOldRole = true;
                         }
                     } catch (Exception $e) {
                         //NO OLD ROLE
                     }
                     if ($dbRole && $saveOldRole) {
                         if ($DBServer) {
                             $new_role_name = BundleTask::GenerateRoleName($DBServer->GetFarmRoleObject(), $DBServer);
                         } else {
                             $new_role_name = $BundleTask->roleName . "-" . rand(1000, 9999);
                         }
                         $dbRole->name = $new_role_name;
                         $BundleTask->Log(sprintf(_("Old role '%s' (ID: %s) renamed to '%s'"), $BundleTask->roleName, $dbRole->id, $new_role_name));
                         $dbRole->save();
                     } else {
                         //TODO:
                         //$this->Logger->error("dbRole->replace->fail({$BundleTask->roleName}, {$BundleTask->envId})");
                     }
                 }
                 if ($BundleTask->object == BundleTask::BUNDLETASK_OBJECT_ROLE) {
                     try {
                         $DBRole = DBRole::createFromBundleTask($BundleTask);
                     } catch (Exception $e) {
                         $BundleTask->SnapshotCreationFailed("Role creation failed due to internal error ({$e->getMessage()}). Please try again.");
                         return;
                     }
                 } else {
                     // roleName is empty, just create image
                     $BundleTask->createImageEntity();
                 }
                 if ($BundleTask->replaceType == SERVER_REPLACEMENT_TYPE::NO_REPLACE) {
                     $BundleTask->setDate('finished');
                     $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS;
                     $BundleTask->Log(sprintf(_("Replacement type: %s. Bundle task status: %s"), SERVER_REPLACEMENT_TYPE::NO_REPLACE, SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS));
                     try {
                         $DBServer = DBServer::LoadByID($BundleTask->serverId);
                         if ($DBServer->status == SERVER_STATUS::IMPORTING) {
                             if ($DBServer->farmId) {
                                 // Create DBFarmRole object
                                 // TODO: create DBFarm role
                             }
                         }
                     } catch (Exception $e) {
                     }
                 } else {
                     try {
                         $BundleTask->Log(sprintf(_("Replacement type: %s. Bundle task status: %s"), $BundleTask->replaceType, SERVER_SNAPSHOT_CREATION_STATUS::REPLACING_SERVERS));
                         if ($BundleTask->replaceType == SERVER_REPLACEMENT_TYPE::REPLACE_FARM) {
                             $DBFarm = DBFarm::LoadByID($BundleTask->farmId);
                             $DBFarmRole = $DBFarm->GetFarmRoleByRoleID($BundleTask->prototypeRoleId);
                             $DBFarmRole->NewRoleID = $BundleTask->roleId;
                             $DBFarmRole->Save();
                         } else {
                             $farm_roles = $db->GetAll("SELECT id FROM farm_roles WHERE role_id=? AND farmid IN (SELECT id FROM farms WHERE env_id=?)", array($BundleTask->prototypeRoleId, $BundleTask->envId));
                             foreach ($farm_roles as $farm_role) {
                                 $DBFarmRole = DBFarmRole::LoadByID($farm_role['id']);
                                 $DBFarmRole->NewRoleID = $BundleTask->roleId;
                                 $DBFarmRole->Save();
                             }
                         }
                         $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::REPLACING_SERVERS;
                     } catch (Exception $e) {
                         $this->Logger->error($e->getMessage());
                         $BundleTask->Log(sprintf(_("Server replacement failed: %s"), $e->getMessage()));
                         $BundleTask->setDate('finished');
                         $BundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS;
                     }
                 }
                 if ($BundleTask->status == SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS) {
                     try {
                         if ($DBServer->status == SERVER_STATUS::IMPORTING) {
                             $DBServer->Remove();
                         } elseif ($DBServer->status == SERVER_STATUS::TEMPORARY) {
                             $BundleTask->Log("Terminating temporary server");
                             $DBServer->terminate(DBServer::TERMINATE_REASON_TEMPORARY_SERVER_ROLE_BUILDER);
                             $BundleTask->Log("Termination request has been sent");
                         }
                     } catch (Exception $e) {
                         $BundleTask->Log("Warning: {$e->getMessage()}");
                     }
                 }
                 $BundleTask->Save();
                 $BundleTask->createImageEntity();
             } catch (Exception $e) {
                 $this->Logger->error($e->getMessage());
             }
             break;
     }
 }
Exemplo n.º 11
0
 public function BundleTaskGetStatus($BundleTaskID)
 {
     $BundleTask = BundleTask::LoadById($BundleTaskID);
     if ($BundleTask->envId != $this->Environment->id) {
         throw new Exception(sprintf("Bundle task #%s not found", $BundleTaskID));
     }
     $response = $this->CreateInitialResponse();
     $response->BundleTaskStatus = $BundleTask->status;
     if ($BundleTask->status == SERVER_SNAPSHOT_CREATION_STATUS::FAILED) {
         $response->FailureReason = $BundleTask->failureReason;
     }
     return $response;
 }
Exemplo n.º 12
0
 private function onHello($message, DBServer $dbserver)
 {
     if ($dbserver->status == SERVER_STATUS::TEMPORARY) {
         $bundleTask = BundleTask::LoadById($dbserver->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_BUNDLE_TASK_ID));
         $bundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::PENDING;
         $bundleTask->Log("Received Hello message from scalarizr on server. Creating image");
         $bundleTask->osFamily = $message->dist->distributor;
         $bundleTask->osName = $message->dist->codename;
         $bundleTask->osVersion = $message->dist->release;
         if (in_array($message->dist->distributor, array('redhat', 'oel', 'scientific')) && $dbserver->platform == SERVER_PLATFORMS::EC2) {
             $bundleTask->bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS_HVM;
         }
         $bundleTask->save();
     }
     if ($dbserver->status == SERVER_STATUS::IMPORTING) {
         if (!$dbserver->remoteIp || !$dbserver->localIp) {
             if (!$dbserver->remoteIp && $message->remoteIp && $dbserver->platform != SERVER_PLATFORMS::IDCF) {
                 $dbserver->remoteIp = $message->remoteIp;
             }
             if (!$dbserver->localIp && $message->localIp) {
                 $dbserver->localIp = $message->localIp;
             }
             if (!$message->behaviour) {
                 $message->behaviour = array('base');
             }
             if ((!$dbserver->remoteIp || $dbserver->localIp == $dbserver->remoteIp) && $message->messageIpAddress != $dbserver->remoteIp) {
                 $dbserver->remoteIp = $message->messageIpAddress;
             }
         }
         if (count($message->behaviour) == 1 && $message->behaviour[0] == ROLE_BEHAVIORS::CHEF) {
             $message->behaviour[] = ROLE_BEHAVIORS::BASE;
         }
         $dbserver->SetProperty(SERVER_PROPERTIES::SZR_IMPORTING_BEHAVIOR, @implode(",", $message->behaviour));
         $dbserver->save();
         $importVersion = $dbserver->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_VERSION);
         if ($importVersion == 2) {
             $dbserver->SetProperties(array(SERVER_PROPERTIES::ARCHITECTURE => $message->architecture));
         } else {
             if ($dbserver->isOpenstack()) {
                 $env = $dbserver->GetEnvironmentObject();
                 $os = $env->openstack($dbserver->platform, $dbserver->GetProperty(OPENSTACK_SERVER_PROPERTIES::CLOUD_LOCATION));
                 $csServer = null;
                 $list = $os->servers->list(true);
                 do {
                     foreach ($list as $_tmp) {
                         $ipaddresses = array();
                         if (!is_array($_tmp->addresses)) {
                             $_tmp->addresses = (array) $_tmp->addresses;
                         }
                         foreach ($_tmp->addresses as $net => $addresses) {
                             foreach ($addresses as $addr) {
                                 if ($addr->version == 4) {
                                     array_push($ipaddresses, $addr->addr);
                                 }
                             }
                         }
                         if ($_tmp->accessIPv4) {
                             array_push($ipaddresses, $_tmp->accessIPv4);
                         }
                         if (in_array($dbserver->localIp, $ipaddresses) || in_array($dbserver->remoteIp, $ipaddresses)) {
                             $osServer = $_tmp;
                         }
                     }
                 } while (false !== ($list = $list->getNextPage()));
                 if (!$osServer) {
                     $this->logger->error(sprintf("Server not found on Openstack (server_id: %s, remote_ip: %s, local_ip: %s)", $dbserver->serverId, $dbserver->remoteIp, $dbserver->localIp));
                     return;
                 }
                 $dbserver->SetProperties(array(OPENSTACK_SERVER_PROPERTIES::SERVER_ID => $osServer->id, OPENSTACK_SERVER_PROPERTIES::NAME => $osServer->name, OPENSTACK_SERVER_PROPERTIES::IMAGE_ID => $osServer->image->id, OPENSTACK_SERVER_PROPERTIES::FLAVOR_ID => $osServer->flavor->id, OPENSTACK_SERVER_PROPERTIES::HOST_ID => $osServer->hostId, SERVER_PROPERTIES::ARCHITECTURE => $message->architecture));
             } elseif ($dbserver->isCloudstack()) {
                 $dbserver->SetProperties(array(CLOUDSTACK_SERVER_PROPERTIES::SERVER_ID => $message->cloudstack->instanceId, CLOUDSTACK_SERVER_PROPERTIES::CLOUD_LOCATION => $message->cloudstack->availZone, SERVER_PROPERTIES::ARCHITECTURE => $message->architecture));
             } else {
                 switch ($dbserver->platform) {
                     case SERVER_PLATFORMS::EC2:
                         $dbserver->SetProperties(array(EC2_SERVER_PROPERTIES::AMIID => $message->awsAmiId, EC2_SERVER_PROPERTIES::INSTANCE_ID => $message->awsInstanceId, EC2_SERVER_PROPERTIES::INSTANCE_TYPE => $message->awsInstanceType, EC2_SERVER_PROPERTIES::AVAIL_ZONE => $message->awsAvailZone, EC2_SERVER_PROPERTIES::REGION => substr($message->awsAvailZone, 0, -1), SERVER_PROPERTIES::ARCHITECTURE => $message->architecture));
                         break;
                     case SERVER_PLATFORMS::EUCALYPTUS:
                         $dbserver->SetProperties(array(EUCA_SERVER_PROPERTIES::EMIID => $message->awsAmiId, EUCA_SERVER_PROPERTIES::INSTANCE_ID => $message->awsInstanceId, EUCA_SERVER_PROPERTIES::INSTANCE_TYPE => $message->awsInstanceType, EUCA_SERVER_PROPERTIES::AVAIL_ZONE => $message->awsAvailZone, SERVER_PROPERTIES::ARCHITECTURE => $message->architecture));
                         break;
                     case SERVER_PLATFORMS::GCE:
                         $dbserver->SetProperties(array(GCE_SERVER_PROPERTIES::CLOUD_LOCATION => $message->{$dbserver->platform}->cloudLocation, GCE_SERVER_PROPERTIES::SERVER_ID => $message->{$dbserver->platform}->serverId, GCE_SERVER_PROPERTIES::SERVER_NAME => $message->{$dbserver->platform}->serverName, GCE_SERVER_PROPERTIES::MACHINE_TYPE => $message->{$dbserver->platform}->machineType, SERVER_PROPERTIES::ARCHITECTURE => $message->architecture));
                         break;
                     case SERVER_PLATFORMS::NIMBULA:
                         $dbserver->SetProperties(array(NIMBULA_SERVER_PROPERTIES::NAME => $message->serverName, SERVER_PROPERTIES::ARCHITECTURE => $message->architecture));
                         break;
                     case SERVER_PLATFORMS::RACKSPACE:
                         $env = $dbserver->GetEnvironmentObject();
                         $cs = Scalr_Service_Cloud_Rackspace::newRackspaceCS($env->getPlatformConfigValue(RackspacePlatformModule::USERNAME, true, $dbserver->GetProperty(RACKSPACE_SERVER_PROPERTIES::DATACENTER)), $env->getPlatformConfigValue(RackspacePlatformModule::API_KEY, true, $dbserver->GetProperty(RACKSPACE_SERVER_PROPERTIES::DATACENTER)), $dbserver->GetProperty(RACKSPACE_SERVER_PROPERTIES::DATACENTER));
                         $csServer = null;
                         $list = $cs->listServers(true);
                         if ($list) {
                             foreach ($list->servers as $_tmp) {
                                 if ($_tmp->addresses->public && in_array($message->remoteIp, $_tmp->addresses->public)) {
                                     $csServer = $_tmp;
                                 }
                             }
                         }
                         if (!$csServer) {
                             $this->logger->error(sprintf("Server not found on CloudServers (server_id: %s, remote_ip: %s, local_ip: %s)", $dbserver->serverId, $message->remoteIp, $message->localIp));
                             return;
                         }
                         $dbserver->SetProperties(array(RACKSPACE_SERVER_PROPERTIES::SERVER_ID => $csServer->id, RACKSPACE_SERVER_PROPERTIES::NAME => $csServer->name, RACKSPACE_SERVER_PROPERTIES::IMAGE_ID => $csServer->imageId, RACKSPACE_SERVER_PROPERTIES::FLAVOR_ID => $csServer->flavorId, RACKSPACE_SERVER_PROPERTIES::HOST_ID => $csServer->hostId, SERVER_PROPERTIES::ARCHITECTURE => $message->architecture));
                         break;
                 }
             }
         }
         //TODO: search for existing bundle task
         // Bundle image
         $creInfo = new ServerSnapshotCreateInfo($dbserver, $dbserver->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_ROLE_NAME), SERVER_REPLACEMENT_TYPE::NO_REPLACE);
         $bundleTask = BundleTask::Create($creInfo);
         $bundleTask->osFamily = $message->dist->distributor;
         $bundleTask->osName = $message->dist->codename;
         $bundleTask->osVersion = $message->dist->release;
         if (in_array($message->dist->distributor, array('oel', 'redhat', 'scientific')) && $dbserver->platform == SERVER_PLATFORMS::EC2) {
             $bundleTask->bundleType = SERVER_SNAPSHOT_CREATION_TYPE::EC2_EBS_HVM;
         }
         $bundleTask->setDate("started");
         $bundleTask->createdByEmail = $dbserver->GetProperty(SERVER_PROPERTIES::LAUNCHED_BY_EMAIL);
         $bundleTask->createdById = $dbserver->GetProperty(SERVER_PROPERTIES::LAUNCHED_BY_ID);
         if ($importVersion == 2) {
             $bundleTask->status = SERVER_SNAPSHOT_CREATION_STATUS::ESTABLISHING_COMMUNICATION;
         }
         $bundleTask->Save();
         $dbserver->SetProperty(SERVER_PROPERTIES::SZR_IMPORTING_BUNDLE_TASK_ID, $bundleTask->id);
     }
 }
Exemplo n.º 13
0
 public function xServerCancelAction()
 {
     if (!$this->getParam('serverId')) {
         throw new Exception(_('Server not found'));
     }
     $dbServer = DBServer::LoadByID($this->getParam('serverId'));
     $this->user->getPermissions()->validate($dbServer);
     $bt_id = $this->db->GetOne("\n            SELECT id FROM bundle_tasks\n            WHERE server_id=? AND prototype_role_id='0' AND status NOT IN (?,?,?)\n            LIMIT 1\n        ", array($dbServer->serverId, SERVER_SNAPSHOT_CREATION_STATUS::FAILED, SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS, SERVER_SNAPSHOT_CREATION_STATUS::CANCELLED));
     if ($bt_id) {
         $BundleTask = BundleTask::LoadById($bt_id);
         $BundleTask->SnapshotCreationFailed("Server was cancelled before snapshot was created.");
     }
     if ($dbServer->status == SERVER_STATUS::IMPORTING) {
         $dbServer->Remove();
     } else {
         $dbServer->terminate(DBServer::TERMINATE_REASON_OPERATION_CANCELLATION, true, $this->user);
     }
     $this->response->success("Server successfully cancelled and removed from database.");
 }
Exemplo n.º 14
0
 public function xServerCancelAction()
 {
     if (!$this->getParam('serverId')) {
         throw new Exception(_('Server not found'));
     }
     $dbServer = DBServer::LoadByID($this->getParam('serverId'));
     $this->user->getPermissions()->validate($dbServer);
     $bt_id = $this->db->GetOne("SELECT id FROM bundle_tasks WHERE server_id=? AND\n\t\t\tprototype_role_id='0' AND status NOT IN (?,?,?)", array($dbServer->serverId, SERVER_SNAPSHOT_CREATION_STATUS::FAILED, SERVER_SNAPSHOT_CREATION_STATUS::SUCCESS, SERVER_SNAPSHOT_CREATION_STATUS::CANCELLED));
     if ($bt_id) {
         $BundleTask = BundleTask::LoadById($bt_id);
         $BundleTask->SnapshotCreationFailed("Server was cancelled before snapshot was created.");
     }
     $dbServer->Delete();
     $this->response->success("Server successfully cancelled and removed from database.");
 }
Exemplo n.º 15
0
 /**
  * Called when instance going down
  *
  * @param HostDownEvent $event
  */
 public function OnHostDown(HostDownEvent $event)
 {
     if ($event->DBServer->IsRebooting()) {
         return;
     }
     //         if ($event->DBServer->status != SERVER_STATUS::TROUBLESHOOTING) {
     //             $event->DBServer->status = SERVER_STATUS::TERMINATED;
     //             $event->DBServer->dateShutdownScheduled = date("Y-m-d H:i:s");
     //         }
     $this->DB->Execute("UPDATE servers_history SET scu_collecting = '0' WHERE server_id = ?", array($event->DBServer->serverId));
     //TODO: move to alerts;
     $this->DB->Execute("UPDATE server_alerts SET status='resolved' WHERE server_id = ?", array($event->DBServer->serverId));
     try {
         $DBFarmRole = $event->DBServer->GetFarmRoleObject();
         if ($DBFarmRole->GetSetting(DBFarmRole::SETTING_MYSQL_BCP_SERVER_ID) == $event->DBServer->serverId) {
             $DBFarmRole->SetSetting(DBFarmRole::SETTING_MYSQL_IS_BCP_RUNNING, 0, DBFarmRole::TYPE_LCL);
         }
         if ($DBFarmRole->GetSetting(DBFarmRole::SETTING_MYSQL_BUNDLE_SERVER_ID) == $event->DBServer->serverId) {
             $DBFarmRole->SetSetting(DBFarmRole::SETTING_MYSQL_IS_BUNDLE_RUNNING, 0, DBFarmRole::TYPE_LCL);
         }
         if ($DBFarmRole->GetSetting(Scalr_Db_Msr::DATA_BACKUP_SERVER_ID) == $event->DBServer->serverId) {
             $DBFarmRole->SetSetting(Scalr_Db_Msr::DATA_BACKUP_IS_RUNNING, 0, DBFarmRole::TYPE_LCL);
         }
         if ($DBFarmRole->GetSetting(Scalr_Db_Msr::DATA_BUNDLE_SERVER_ID) == $event->DBServer->serverId) {
             $DBFarmRole->SetSetting(Scalr_Db_Msr::DATA_BUNDLE_IS_RUNNING, 0, DBFarmRole::TYPE_LCL);
         }
     } catch (Exception $e) {
     }
     if ($event->replacementDBServer) {
         $event->replacementDBServer->replaceServerID = null;
         $event->replacementDBServer->Save();
     }
     //Check active bundle task:
     $bundle_task_id = $this->DB->GetOne("SELECT id FROM bundle_tasks WHERE server_id=? AND status IN (?,?) LIMIT 1", array($event->DBServer->serverId, SERVER_SNAPSHOT_CREATION_STATUS::PENDING, SERVER_SNAPSHOT_CREATION_STATUS::IN_PROGRESS));
     if ($bundle_task_id) {
         $BundleTask = BundleTask::LoadById($bundle_task_id);
         $BundleTask->SnapshotCreationFailed("Server was terminated before image was created.");
     }
 }
Exemplo n.º 16
0
 /**
  * Called when instance going down
  *
  * @param HostDownEvent $event
  */
 public function OnHostDown(HostDownEvent $event)
 {
     if ($event->DBServer->IsRebooting()) {
         return;
     }
     $event->DBServer->status = SERVER_STATUS::TERMINATED;
     $event->DBServer->dateShutdownScheduled = date("Y-m-d H:i:s");
     $this->DB->Execute("UPDATE servers_history SET\r\n\t\t\t\tdtterminated_scalr\t= NOW()\r\n\t\t\t\tWHERE server_id = ?\r\n\t\t\t", array($event->DBServer->serverId));
     try {
         $DBFarmRole = $event->DBServer->GetFarmRoleObject();
         if ($DBFarmRole->GetSetting(DBFarmRole::SETTING_MYSQL_BCP_SERVER_ID) == $event->DBServer->serverId) {
             $DBFarmRole->SetSetting(DBFarmRole::SETTING_MYSQL_IS_BCP_RUNNING, 0);
         }
         if ($DBFarmRole->GetSetting(DBFarmRole::SETTING_MYSQL_BUNDLE_SERVER_ID) == $event->DBServer->serverId) {
             $DBFarmRole->SetSetting(DBFarmRole::SETTING_MYSQL_IS_BUNDLE_RUNNING, 0);
         }
         if ($DBFarmRole->GetSetting(Scalr_Db_Msr::DATA_BACKUP_SERVER_ID) == $event->DBServer->serverId) {
             $DBFarmRole->SetSetting(Scalr_Db_Msr::DATA_BACKUP_IS_RUNNING, 0);
         }
         if ($DBFarmRole->GetSetting(Scalr_Db_Msr::DATA_BUNDLE_SERVER_ID) == $event->DBServer->serverId) {
             $DBFarmRole->SetSetting(Scalr_Db_Msr::DATA_BUNDLE_IS_RUNNING, 0);
         }
     } catch (Exception $e) {
     }
     if ($event->replacementDBServer) {
         $event->replacementDBServer->replaceServerID = null;
         $event->replacementDBServer->Save();
     }
     //Check active bundle task:
     $bundle_task_id = $this->DB->GetOne("SELECT id FROM bundle_tasks WHERE server_id=? AND status IN (?,?)", array($event->DBServer->serverId, SERVER_SNAPSHOT_CREATION_STATUS::PENDING, SERVER_SNAPSHOT_CREATION_STATUS::IN_PROGRESS));
     if ($bundle_task_id) {
         $BundleTask = BundleTask::LoadById($bundle_task_id);
         $BundleTask->SnapshotCreationFailed("Server was terminated before image was created.");
     }
     //
     //
     //
     //TODO:
     $farminfo = $this->DB->GetRow("SELECT * FROM farms WHERE id=?", array($this->FarmID));
     if ($farminfo['status'] == FARM_STATUS::SYNCHRONIZING) {
         $event->DBServer->SkipEBSObserver = true;
         $farm_servers_count = $this->DB->GetOne("SELECT COUNT(*) FROM servers WHERE farm_id=? and server_id != ? and status != ?", array($this->FarmID, $event->DBServer->serverId, SERVER_STATUS::TERMINATED));
         if ($farm_servers_count == 0) {
             $this->DB->Execute("UPDATE farms SET status=? WHERE id=?", array(FARM_STATUS::TERMINATED, $this->FarmID));
         }
     }
     $event->DBServer->Save();
 }
Exemplo n.º 17
0
 /**
  * @param   bool        $devel
  * @param   string      $serverId
  * @throws  Exception
  */
 public function builderAction($devel = false, $serverId = null)
 {
     $this->request->restrictAccess(Acl::RESOURCE_IMAGES_ENVIRONMENT, Acl::PERM_IMAGES_ENVIRONMENT_BUILD);
     // check for environment
     $this->getEnvironmentId();
     $enabledPlatforms = self::loadController('Platforms')->getEnabledPlatforms(false);
     if (empty($enabledPlatforms)) {
         $this->response->failure('Please <a href="#/account/environments?envId=' . $this->getEnvironmentId() . '">configure cloud credentials</a> before using Role Builder.', true);
         return;
     }
     $platforms = array();
     foreach ($enabledPlatforms as $k => $v) {
         if (in_array($k, array(SERVER_PLATFORMS::IDCF, SERVER_PLATFORMS::EC2, SERVER_PLATFORMS::GCE, SERVER_PLATFORMS::RACKSPACENG_US, SERVER_PLATFORMS::RACKSPACENG_UK))) {
             $platforms[$k] = array('name' => $v);
         }
     }
     if (empty($platforms)) {
         $this->response->failure('The Role Builder does not support your enabled clouds. <br/>Please <a href="#/roles/import">Create a role from Non-Scalr server</a> instead.', true);
         return;
     }
     $images = json_decode(file_get_contents(APPPATH . '/www/storage/images' . ($devel ? '-dev' : '') . '.json'), true);
     foreach ($platforms as $k => $v) {
         if ($k == SERVER_PLATFORMS::EC2) {
             $locations = PlatformFactory::NewPlatform($k)->getLocations($this->environment);
             $ccProps = $this->getEnvironment()->keychain(SERVER_PLATFORMS::EC2)->properties;
             $enabledInstanceStore = $ccProps[CloudCredentialsProperty::AWS_CERTIFICATE] && $ccProps[CloudCredentialsProperty::AWS_PRIVATE_KEY];
             $platforms[$k]['images'] = array();
             foreach ($images[$k] as $image) {
                 if (isset($locations[$image['cloud_location']]) && ($enabledInstanceStore || $image['root_device_type'] != 'instance-store')) {
                     $platforms[$k]['images'][] = $image;
                 }
             }
         } else {
             $platforms[$k]['images'] = !empty($images[$k]) ? $images[$k] : [];
         }
     }
     $server = null;
     if ($serverId) {
         $dbServer = DBServer::LoadByID($serverId);
         $this->user->getPermissions()->validate($dbServer);
         if ($dbServer->status != SERVER_STATUS::TEMPORARY) {
             throw new Exception('Server is not in role building state');
         }
         $bundleTaskId = $this->db->GetOne("SELECT id FROM bundle_tasks WHERE server_id = ? ORDER BY dtadded DESC LIMIT 1", array($dbServer->serverId));
         $bundleTask = BundleTask::LoadById($bundleTaskId);
         $server = array('serverId' => $dbServer->serverId, 'platform' => $dbServer->platform, 'bundleTaskId' => $bundleTaskId, 'object' => $bundleTask->object, 'imageId' => $dbServer->GetProperty(SERVER_PROPERTIES::SZR_IMPORTING_IMAGE_ID));
     }
     $this->response->page('ui/roles/builder.js', array('platforms' => $platforms, 'environment' => '#/account/environments/view?envId=' . $this->getEnvironmentId(), 'server' => $server), array('ui/services/chef/chefsettings.js', 'ui/bundletasks/view.js'), array('ui/roles/builder.css'));
 }