Skip to content

Commit

Permalink
Download Volume Snapshots (apache#8878)
Browse files Browse the repository at this point in the history
Co-authored-by: Rodrigo D. Lopez <[email protected]>
  • Loading branch information
gpordeus and RodrigoDLopez authored Aug 21, 2024
1 parent f5c7729 commit 9b22cd5
Show file tree
Hide file tree
Showing 19 changed files with 417 additions and 60 deletions.
2 changes: 2 additions & 0 deletions api/src/main/java/com/cloud/event/EventTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ public class EventTypes {
public static final String EVENT_SNAPSHOT_OFF_PRIMARY = "SNAPSHOT.OFF_PRIMARY";
public static final String EVENT_SNAPSHOT_DELETE = "SNAPSHOT.DELETE";
public static final String EVENT_SNAPSHOT_REVERT = "SNAPSHOT.REVERT";
public static final String EVENT_SNAPSHOT_EXTRACT = "SNAPSHOT.EXTRACT";
public static final String EVENT_SNAPSHOT_POLICY_CREATE = "SNAPSHOTPOLICY.CREATE";
public static final String EVENT_SNAPSHOT_POLICY_UPDATE = "SNAPSHOTPOLICY.UPDATE";
public static final String EVENT_SNAPSHOT_POLICY_DELETE = "SNAPSHOTPOLICY.DELETE";
Expand Down Expand Up @@ -897,6 +898,7 @@ public class EventTypes {
// Snapshots
entityEventDetails.put(EVENT_SNAPSHOT_CREATE, Snapshot.class);
entityEventDetails.put(EVENT_SNAPSHOT_DELETE, Snapshot.class);
entityEventDetails.put(EVENT_SNAPSHOT_EXTRACT, Snapshot.class);
entityEventDetails.put(EVENT_SNAPSHOT_ON_PRIMARY, Snapshot.class);
entityEventDetails.put(EVENT_SNAPSHOT_OFF_PRIMARY, Snapshot.class);
entityEventDetails.put(EVENT_SNAPSHOT_POLICY_CREATE, SnapshotPolicy.class);
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/com/cloud/storage/Upload.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static enum Status {
}

public static enum Type {
VOLUME, TEMPLATE, ISO
VOLUME, SNAPSHOT, TEMPLATE, ISO
}

public static enum Mode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.cloudstack.api.command.user.snapshot.CopySnapshotCmd;
import org.apache.cloudstack.api.command.user.snapshot.CreateSnapshotPolicyCmd;
import org.apache.cloudstack.api.command.user.snapshot.DeleteSnapshotPoliciesCmd;
import org.apache.cloudstack.api.command.user.snapshot.ExtractSnapshotCmd;
import org.apache.cloudstack.api.command.user.snapshot.ListSnapshotPoliciesCmd;
import org.apache.cloudstack.api.command.user.snapshot.ListSnapshotsCmd;
import org.apache.cloudstack.api.command.user.snapshot.UpdateSnapshotPolicyCmd;
Expand Down Expand Up @@ -106,6 +107,16 @@ Snapshot allocSnapshot(Long volumeId, Long policyId, String snapshotName, Snapsh
*/
Snapshot createSnapshot(Long volumeId, Long policyId, Long snapshotId, Account snapshotOwner);

/**
* Extracts the snapshot to a particular location.
*
* @param cmd
* the command specifying url (where the snapshot needs to be extracted to), zoneId (zone where the snapshot exists) and
* id (the id of the snapshot)
*
*/
String extractSnapshot(ExtractSnapshotCmd cmd);

/**
* Archives a snapshot from primary storage to secondary storage.
* @param id Snapshot ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,11 @@ public interface ResponseGenerator {

SecurityGroupResponse createSecurityGroupResponse(SecurityGroup group);

ExtractResponse createExtractResponse(Long uploadId, Long id, Long zoneId, Long accountId, String mode, String url);
ExtractResponse createImageExtractResponse(Long id, Long zoneId, Long accountId, String mode, String url);

ExtractResponse createExtractResponse(Long id, Long zoneId, Long accountId, String mode, String url);
ExtractResponse createVolumeExtractResponse(Long id, Long zoneId, Long accountId, String mode, String url);

ExtractResponse createSnapshotExtractResponse(Long id, Long zoneId, Long accountId, String url);

String toSerializedString(CreateCmdResponse response, String responseType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void execute() {
CallContext.current().setEventDetails(getEventDescription());
String uploadUrl = _templateService.extract(this);
if (uploadUrl != null) {
ExtractResponse response = _responseGenerator.createExtractResponse(id, zoneId, getEntityOwnerId(), mode, uploadUrl);
ExtractResponse response = _responseGenerator.createImageExtractResponse(id, zoneId, getEntityOwnerId(), mode, uploadUrl);
response.setResponseName(getCommandName());
response.setObjectName("iso");
this.setResponseObject(response);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.cloudstack.api.command.user.snapshot;

import com.cloud.event.EventTypes;
import com.cloud.storage.Snapshot;
import com.cloud.user.Account;
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
import org.apache.cloudstack.api.ACL;
import org.apache.cloudstack.api.APICommand;
import org.apache.cloudstack.api.ApiCommandResourceType;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.ApiErrorCode;
import org.apache.cloudstack.api.BaseAsyncCmd;
import org.apache.cloudstack.api.Parameter;
import org.apache.cloudstack.api.ServerApiException;
import org.apache.cloudstack.api.response.ExtractResponse;
import org.apache.cloudstack.api.response.SnapshotResponse;
import org.apache.cloudstack.api.response.ZoneResponse;
import org.apache.cloudstack.context.CallContext;

@APICommand(name = "extractSnapshot", description = "Returns a download URL for extracting a snapshot. It must be in the Backed Up state.", since = "4.20.0",
responseObject = ExtractResponse.class, entityType = {Snapshot.class}, requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
public class ExtractSnapshotCmd extends BaseAsyncCmd {


/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////

@ACL(accessType = AccessType.OperateEntry)
@Parameter(name=ApiConstants.ID, type=CommandType.UUID, entityType=SnapshotResponse.class, required=true, since="4.20.0", description="the ID of the snapshot")
private Long id;

@Parameter(name = ApiConstants.ZONE_ID, type = CommandType.UUID, entityType = ZoneResponse.class, required = true, since="4.20.0",
description = "the ID of the zone where the snapshot is located")
private Long zoneId;

/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////

public Long getId() {
return id;
}

public Long getZoneId() {
return zoneId;
}

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////

@Override
public ApiCommandResourceType getApiResourceType() {
return ApiCommandResourceType.Snapshot;
}

@Override
public Long getApiResourceId() {
return getId();
}

/**
* @return ID of the snapshot to extract, if any. Otherwise returns the ACCOUNT_ID_SYSTEM, so ERROR events will be traceable.
*/
@Override
public long getEntityOwnerId() {
Snapshot snapshot = _entityMgr.findById(Snapshot.class, getId());
if (snapshot != null) {
return snapshot.getAccountId();
}

return Account.ACCOUNT_ID_SYSTEM;
}

@Override
public String getEventType() {
return EventTypes.EVENT_SNAPSHOT_EXTRACT;
}

@Override
public String getEventDescription() {
return "Snapshot extraction job";
}

@Override
public void execute() {
CallContext.current().setEventDetails("Snapshot ID: " + this._uuidMgr.getUuid(Snapshot.class, getId()));
String uploadUrl = _snapshotService.extractSnapshot(this);
logger.info("Extract URL [{}] of snapshot [{}].", uploadUrl, id);
if (uploadUrl != null) {
ExtractResponse response = _responseGenerator.createSnapshotExtractResponse(id, zoneId, getEntityOwnerId(), uploadUrl);
response.setResponseName(getCommandName());
this.setResponseObject(response);
} else {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to extract snapshot");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ public void execute() {
CallContext.current().setEventDetails(getEventDescription());
String uploadUrl = _templateService.extract(this);
if (uploadUrl != null) {
ExtractResponse response = _responseGenerator.createExtractResponse(id, zoneId, getEntityOwnerId(), mode, uploadUrl);
ExtractResponse response = _responseGenerator.createImageExtractResponse(id, zoneId, getEntityOwnerId(), mode, uploadUrl);
response.setResponseName(getCommandName());
response.setObjectName("template");
this.setResponseObject(response);
} else {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to extract template");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
import org.apache.cloudstack.api.response.ZoneResponse;
import org.apache.cloudstack.context.CallContext;

import com.cloud.dc.DataCenter;
import com.cloud.event.EventTypes;
import com.cloud.storage.Upload;
import com.cloud.storage.Volume;
import com.cloud.user.Account;

Expand Down Expand Up @@ -124,20 +122,8 @@ public void execute() {
CallContext.current().setEventDetails("Volume Id: " + this._uuidMgr.getUuid(Volume.class, getId()));
String uploadUrl = _volumeService.extractVolume(this);
if (uploadUrl != null) {
ExtractResponse response = new ExtractResponse();
ExtractResponse response = _responseGenerator.createVolumeExtractResponse(id, zoneId, getEntityOwnerId(), mode, uploadUrl);
response.setResponseName(getCommandName());
response.setObjectName("volume");
Volume vol = _entityMgr.findById(Volume.class, id);
response.setId(vol.getUuid());
response.setName(vol.getName());
DataCenter zone = _entityMgr.findById(DataCenter.class, zoneId);
response.setZoneId(zone.getUuid());
response.setZoneName(zone.getName());
response.setMode(mode);
response.setState(Upload.Status.DOWNLOAD_URL_CREATED.toString());
Account account = _entityMgr.findById(Account.class, getEntityOwnerId());
response.setAccountId(account.getUuid());
response.setUrl(uploadUrl);
setResponseObject(response);
} else {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to extract volume");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ public class SnapshotDataStoreVO implements StateObject<ObjectInDataStoreStateMa
@Column(name = "install_path")
private String installPath;

@Column(name = "download_url", length = 2048)
private String extractUrl;

@Column(name = "download_url_created")
@Temporal(value = TemporalType.TIMESTAMP)
private Date extractUrlCreated = null;

@Column(name = "update_count", updatable = true, nullable = false)
protected long updatedCount;

Expand Down Expand Up @@ -310,6 +317,22 @@ public void setVolumeId(Long volumeId) {
this.volumeId = volumeId;
}

public String getExtractUrl() {
return extractUrl;
}

public void setExtractUrl(String extractUrl) {
this.extractUrl = extractUrl;
}

public Date getExtractUrlCreated() {
return extractUrlCreated;
}

public void setExtractUrlCreated(Date extractUrlCreated) {
this.extractUrlCreated = extractUrlCreated;
}

public void setCreated(Date created) {
this.created = created;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ CREATE TABLE IF NOT EXISTS `cloud_usage`.`quota_email_configuration`(
-- Add `is_implicit` column to `host_tags` table
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.host_tags', 'is_implicit', 'int(1) UNSIGNED NOT NULL DEFAULT 0 COMMENT "If host tag is implicit or explicit" ');

-- Fields related to Snapshot Extraction
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.snapshot_store_ref', 'download_url', 'varchar(2048) DEFAULT NULL');
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.snapshot_store_ref', 'download_url_created', 'datetime DEFAULT NULL');

-- Webhooks feature
DROP TABLE IF EXISTS `cloud`.`webhook`;
CREATE TABLE `cloud`.`webhook` (
Expand Down
56 changes: 23 additions & 33 deletions server/src/main/java/com/cloud/api/ApiResponseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,6 @@
import com.cloud.storage.SnapshotVO;
import com.cloud.storage.StoragePool;
import com.cloud.storage.Upload;
import com.cloud.storage.UploadVO;
import com.cloud.storage.VMTemplateVO;
import com.cloud.storage.Volume;
import com.cloud.storage.VolumeVO;
Expand Down Expand Up @@ -1914,58 +1913,49 @@ public SecurityGroupResponse createSecurityGroupResponse(SecurityGroup group) {
return listSgs.get(0);
}

//TODO: we need to deprecate uploadVO, since extract is done in a synchronous fashion
@Override
public ExtractResponse createExtractResponse(Long id, Long zoneId, Long accountId, String mode, String url) {

private ExtractResponse createExtractResponse (Long zoneId, Long accountId, String url) {
ExtractResponse response = new ExtractResponse();
response.setObjectName("template");
VMTemplateVO template = ApiDBUtils.findTemplateById(id);
response.setId(template.getUuid());
response.setName(template.getName());
if (zoneId != null) {
DataCenter zone = ApiDBUtils.findZoneById(zoneId);
response.setZoneId(zone.getUuid());
response.setZoneName(zone.getName());
}
response.setMode(mode);
response.setUrl(url);
response.setState(Upload.Status.DOWNLOAD_URL_CREATED.toString());
Account account = ApiDBUtils.findAccountById(accountId);
response.setAccountId(account.getUuid());
return response;
}

@Override
public ExtractResponse createVolumeExtractResponse(Long id, Long zoneId, Long accountId, String mode, String url) {
ExtractResponse response = createExtractResponse(zoneId, accountId, url);
response.setObjectName("volume");
response.setMode(mode);
Volume volume = ApiDBUtils.findVolumeById(id);
response.setId(volume.getUuid());
response.setName(volume.getName());
return response;
}

@Override
public ExtractResponse createExtractResponse(Long uploadId, Long id, Long zoneId, Long accountId, String mode, String url) {
public ExtractResponse createSnapshotExtractResponse(Long id, Long zoneId, Long accountId, String url) {
ExtractResponse response = createExtractResponse(zoneId, accountId, url);
response.setObjectName("snapshot");
Snapshot snapshot = ApiDBUtils.findSnapshotById(id);
response.setId(snapshot.getUuid());
response.setName(snapshot.getName());
return response;
}

ExtractResponse response = new ExtractResponse();
response.setObjectName("template");
@Override
public ExtractResponse createImageExtractResponse(Long id, Long zoneId, Long accountId, String mode, String url) {
ExtractResponse response = createExtractResponse(zoneId, accountId, url);
response.setMode(mode);
VMTemplateVO template = ApiDBUtils.findTemplateById(id);
response.setId(template.getUuid());
response.setName(template.getName());
if (zoneId != null) {
DataCenter zone = ApiDBUtils.findZoneById(zoneId);
response.setZoneId(zone.getUuid());
response.setZoneName(zone.getName());
}
response.setMode(mode);
if (uploadId == null) {
// region-wide image store
response.setUrl(url);
response.setState(Upload.Status.DOWNLOAD_URL_CREATED.toString());
} else {
UploadVO uploadInfo = ApiDBUtils.findUploadById(uploadId);
response.setUploadId(uploadInfo.getUuid());
response.setState(uploadInfo.getUploadState().toString());
response.setUrl(uploadInfo.getUploadUrl());
}
Account account = ApiDBUtils.findAccountById(accountId);
response.setAccountId(account.getUuid());

return response;

}

@Override
Expand Down
2 changes: 1 addition & 1 deletion server/src/main/java/com/cloud/configuration/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ public enum Config {
Boolean.class,
"disable.extraction",
"false",
"Flag for disabling extraction of template, isos and volumes",
"Flag for disabling extraction of templates, isos, snapshots and volumes",
null),
ExtractURLExpirationInterval(
"Advanced",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@
import org.apache.cloudstack.api.command.user.snapshot.CreateSnapshotPolicyCmd;
import org.apache.cloudstack.api.command.user.snapshot.DeleteSnapshotCmd;
import org.apache.cloudstack.api.command.user.snapshot.DeleteSnapshotPoliciesCmd;
import org.apache.cloudstack.api.command.user.snapshot.ExtractSnapshotCmd;
import org.apache.cloudstack.api.command.user.snapshot.ListSnapshotPoliciesCmd;
import org.apache.cloudstack.api.command.user.snapshot.ListSnapshotsCmd;
import org.apache.cloudstack.api.command.user.snapshot.RevertSnapshotCmd;
Expand Down Expand Up @@ -3755,6 +3756,7 @@ public List<Class<?>> getCommands() {
cmdList.add(CreateSnapshotFromVMSnapshotCmd.class);
cmdList.add(CopySnapshotCmd.class);
cmdList.add(DeleteSnapshotCmd.class);
cmdList.add(ExtractSnapshotCmd.class);
cmdList.add(ArchiveSnapshotCmd.class);
cmdList.add(CreateSnapshotPolicyCmd.class);
cmdList.add(UpdateSnapshotPolicyCmd.class);
Expand Down
Loading

0 comments on commit 9b22cd5

Please sign in to comment.