public function CreateServerSnapshot(BundleTask $BundleTask)
{
$DBServer = DBServer::LoadByID($BundleTask->serverId);
if ($BundleTask->osFamily == 'windows' || $DBServer->osType == 'windows') {
$BundleTask->bundleType = \SERVER_SNAPSHOT_CREATION_TYPE::GCE_WINDOWS;
$BundleTask->status = \SERVER_SNAPSHOT_CREATION_STATUS::IN_PROGRESS;
$gce = $this->getClient($DBServer->GetEnvironmentObject());
$projectId = $DBServer->GetEnvironmentObject()->keychain(SERVER_PLATFORMS::GCE)->properties[Entity\CloudCredentialsProperty::GCE_PROJECT_ID];
//Set root disk auto-remove to false
try {
$instance = $gce->instances->get($projectId, $DBServer->GetCloudLocation(), $DBServer->GetProperty(\GCE_SERVER_PROPERTIES::SERVER_NAME));
$disks = $instance->getDisks();
if (!count($disks)) {
throw new Exception('No disks were found');
}
$op1 = $gce->instances->setDiskAutoDelete($projectId, $DBServer->GetCloudLocation(), $DBServer->GetProperty(\GCE_SERVER_PROPERTIES::SERVER_NAME), false, $disks[0]['deviceName']);
$BundleTask->Log("Calling setDiskAutoDelete(false) for root device. Operation: {$op1->name}");
} catch (Exception $e) {
$BundleTask->Log("Unable to perform setDiskAutoDelete(false) for ROOT device: " . $e->getMessage());
}
//TODO: Check operation status
sleep(2);
// Kill VM
try {
$op2 = $gce->instances->delete($projectId, $DBServer->GetCloudLocation(), $DBServer->GetProperty(\GCE_SERVER_PROPERTIES::SERVER_NAME));
$BundleTask->Log("Terminating VM. Operation: {$op2->name}");
} catch (Exception $e) {
if (stristr($e->getMessage(), "not found")) {
} else {
$BundleTask->Log("Unable to terminate VM: " . $e->getMessage());
}
}
$BundleTask->setMetaData(array('gceSnapshotOpPhase1Id' => $op1->name, 'gceSnapshotOpPhase2Id' => $op2->name, 'gceSnapshotZone' => $DBServer->cloudLocationZone, 'gceSnapshotDeviceName' => $this->getObjectName($disks[0]['source'])));
} else {
$BundleTask->status = \SERVER_SNAPSHOT_CREATION_STATUS::IN_PROGRESS;
$BundleTask->bundleType = \SERVER_SNAPSHOT_CREATION_TYPE::GCE_STORAGE;
// identifier of google cloud resource must start from [a-z]
$imageName = (preg_match('/^[^a-z]/', $BundleTask->roleName) ? 'i' : '') . $BundleTask->roleName;
$msg = new \Scalr_Messaging_Msg_Rebundle($BundleTask->id, $imageName, array());
if (!$DBServer->SendMessage($msg)) {
$BundleTask->SnapshotCreationFailed("Cannot send rebundle message to server. Please check event log for more details.");
return;
} else {
$BundleTask->Log(sprintf(_("Snapshot creating initialized (MessageID: %s). Bundle task status changed to: %s"), $msg->messageId, $BundleTask->status));
}
}
$BundleTask->setDate('started');
$BundleTask->Save();
}