Scalr\Modules\Platforms\Openstack\OpenstackPlatformModule::CreateServerSnapshot PHP Method

CreateServerSnapshot() public method

See also: Scalr\Modules\PlatformModuleInterface::CreateServerSnapshot()
public CreateServerSnapshot ( BundleTask $BundleTask )
$BundleTask BundleTask
    public function CreateServerSnapshot(BundleTask $BundleTask)
    {
        $DBServer = DBServer::LoadByID($BundleTask->serverId);
        if ($BundleTask->osFamily == 'windows' || $DBServer->osType == 'windows') {
            $BundleTask->bundleType = \SERVER_SNAPSHOT_CREATION_TYPE::OSTACK_WINDOWS;
            $BundleTask->Log(sprintf(_("Selected platform snapshot type: %s"), $BundleTask->bundleType));
            //prepare bundle
            $BundleTask->Log(sprintf(_("Sending 'prepare' command to scalarizr")));
            $prepare = $DBServer->scalarizr->image->prepare($BundleTask->roleName);
            $BundleTask->Log(sprintf(_("Prepare result: %s"), json_encode($prepare)));
            $createImage = true;
            /*
            if ($BundleTask->status == \SERVER_SNAPSHOT_CREATION_STATUS::PENDING) {
                $BundleTask->bundleType = \SERVER_SNAPSHOT_CREATION_TYPE::OSTACK_WINDOWS;
                $BundleTask->Log(sprintf(_("Selected platform snapshotting type: %s"), $BundleTask->bundleType));
                $BundleTask->status = \SERVER_SNAPSHOT_CREATION_STATUS::PREPARING;
                $BundleTask->Save();
                try {
                    $msg = $DBServer->SendMessage(new \Scalr_Messaging_Msg_Win_PrepareBundle($BundleTask->id), false, true);
                    if ($msg) {
                        $BundleTask->Log(sprintf(
                            _("PrepareBundle message sent. MessageID: %s. Bundle task status changed to: %s"),
                            $msg->messageId, $BundleTask->status
                        ));
                    } else {
                        throw new \Exception("Cannot send message");
                    }
                } catch (\Exception $e) {
                    $BundleTask->SnapshotCreationFailed("Cannot send PrepareBundle message to server.");
            
                    return false;
                }
            } elseif ($BundleTask->status == \SERVER_SNAPSHOT_CREATION_STATUS::PREPARING) {
            
            }
            */
        } else {
            $BundleTask->bundleType = \SERVER_SNAPSHOT_CREATION_TYPE::OSTACK_LINUX;
            $BundleTask->Log(sprintf(_("Selected platform snapshot type: %s"), $BundleTask->bundleType));
            //prepare bundle
            $BundleTask->Log(sprintf(_("Sending 'prepare' command to scalarizr")));
            $prepare = $DBServer->scalarizr->image->prepare($BundleTask->roleName);
            $BundleTask->Log(sprintf(_("Prepare result: %s"), json_encode($prepare)));
            $createImage = true;
            /*
            $BundleTask->status = \SERVER_SNAPSHOT_CREATION_STATUS::IN_PROGRESS;
            
            $msg = new \Scalr_Messaging_Msg_Rebundle(
                $BundleTask->id,
                $BundleTask->roleName,
                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();
            */
        }
        if ($createImage) {
            try {
                $client = $this->getOsClient($DBServer->GetEnvironmentObject(), $DBServer->GetProperty(\OPENSTACK_SERVER_PROPERTIES::CLOUD_LOCATION));
                $imageId = $client->servers->createImage($DBServer->GetProperty(\OPENSTACK_SERVER_PROPERTIES::SERVER_ID), $BundleTask->roleName . "-" . date("YmdHi"));
                $BundleTask->status = \SERVER_SNAPSHOT_CREATION_STATUS::IN_PROGRESS;
                $BundleTask->snapshotId = $imageId;
                $BundleTask->Log(sprintf(_("Snapshot creating initialized (ImageID: %s). Bundle task status changed to: %s"), $BundleTask->snapshotId, $BundleTask->status));
                $BundleTask->setDate('started');
                $BundleTask->Save();
            } catch (\Exception $e) {
                $BundleTask->SnapshotCreationFailed($e->getMessage());
                return;
            }
        }
        return true;
    }