Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion 3 api/src/main/java/com/cloud/storage/Snapshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

public interface Snapshot extends ControlledEntity, Identity, InternalIdentity, StateObject<Snapshot.State> {
public enum Type {
MANUAL, RECURRING, TEMPLATE, HOURLY, DAILY, WEEKLY, MONTHLY;
MANUAL, RECURRING, TEMPLATE, HOURLY, DAILY, WEEKLY, MONTHLY, INTERNAL;
private int max = 8;

public void setMax(int max) {
Expand Down Expand Up @@ -71,6 +71,7 @@ enum LocationType {
}

public static final long MANUAL_POLICY_ID = 0L;
public static final long INTERNAL_POLICY_ID = 7L;

@Override
long getAccountId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.cloud.template.VirtualMachineTemplate;
import com.cloud.user.Account;
import com.cloud.uservm.UserVm;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.vm.VirtualMachine;
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
import org.apache.cloudstack.api.ACL;
Expand Down Expand Up @@ -108,23 +109,27 @@ public void create() throws ResourceAllocationException {
s_logger.info("The template id recorded is: " + template.getId());
setTemporaryTemlateId(template.getId());
_templateService.createPrivateTemplate(this);
_snapshotService.deleteSnapshot(getTemporarySnapShotId());
UserVm vmRecord = _userVmService.recordVirtualMachineToDB(this);
if (vmRecord == null) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "unable to record a new VM to db!");
}
setEntityId(vmRecord.getId());
setEntityUuid(vmRecord.getUuid());
} catch (ResourceUnavailableException | InsufficientCapacityException e) {
}
catch (ResourceUnavailableException | InsufficientCapacityException e) {
s_logger.warn("Exception: ", e);
throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, e.getMessage());
} catch (InvalidParameterValueException e) {
s_logger.warn("Exception: ", e);
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
} catch (ServerApiException e) {
throw new ServerApiException(e.getErrorCode(), e.getDescription());
} catch (CloudRuntimeException e) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
} finally {
if (getTemporaryTemlateId() != null) {
// TODO: delete template in the service
s_logger.warn("clearing the temporary template: " + getTemporaryTemlateId());
if (getTemporarySnapShotId() != null) {
_snapshotService.deleteSnapshot(getTemporarySnapShotId());
s_logger.warn("clearing the temporary snapshot: " + getTemporarySnapShotId());
}
}
}
Expand All @@ -138,7 +143,7 @@ public String getVMName() {
}

public String getTemplateName() {
return getVMName() + "-QA";
return (getVMName() + "-Clone-" + _uuidMgr.generateUuid(VirtualMachineTemplate.class, null)).substring(0, 32);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ public Pair<List<? extends Snapshot>, Integer> listSnapshots(ListSnapshotsCmd cm
sb.and("snapshotTypeEQ", sb.entity().getsnapshotType(), SearchCriteria.Op.IN);
sb.and("snapshotTypeNEQ", sb.entity().getsnapshotType(), SearchCriteria.Op.NEQ);
sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);

sb.and("snapshotTypeInternal", sb.entity().getsnapshotType(), SearchCriteria.Op.NEQ);
if (tags != null && !tags.isEmpty()) {
SearchBuilder<ResourceTagVO> tagSearch = _resourceTagDao.createSearchBuilder();
for (int count = 0; count < tags.size(); count++) {
Expand Down Expand Up @@ -737,7 +737,7 @@ public Pair<List<? extends Snapshot>, Integer> listSnapshots(ListSnapshotsCmd cm
// Show only MANUAL and RECURRING snapshot types
sc.setParameters("snapshotTypeNEQ", Snapshot.Type.TEMPLATE.ordinal());
}

sc.setParameters("snapshotTypeInternal", Type.INTERNAL.ordinal());
Pair<List<SnapshotVO>, Integer> result = _snapshotDao.searchAndCount(sc, searchFilter);
return new Pair<List<? extends Snapshot>, Integer>(result.first(), result.second());
}
Expand Down Expand Up @@ -1027,7 +1027,13 @@ public List<SnapshotScheduleVO> findRecurringSnapshotSchedule(ListRecurringSnaps
private Type getSnapshotType(Long policyId) {
if (policyId.equals(Snapshot.MANUAL_POLICY_ID)) {
return Type.MANUAL;
} else {
}

else if (policyId.equals(Snapshot.INTERNAL_POLICY_ID)) {
return Type.INTERNAL;
}

else {
SnapshotPolicyVO spstPolicyVO = _snapshotPolicyDao.findById(policyId);
IntervalType intvType = DateUtil.getIntervalType(spstPolicyVO.getInterval());
return getSnapshotType(intvType);
Expand Down
13 changes: 3 additions & 10 deletions 13 server/src/main/java/com/cloud/template/TemplateManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1906,17 +1906,14 @@ public VMTemplateVO createPrivateTemplateRecord(CloneVMCmd cmd, Account template
if (guestOS == null) {
throw new InvalidParameterValueException("GuestOS with ID: " + guestOSId + " does not exist.");
}

// get snapshot from this step


Long nextTemplateId = _tmpltDao.getNextInSequence(Long.class, "id");
s_logger.info("Creating snapshot for the tempalte creation");
SnapshotVO snapshot = (SnapshotVO) volumeService.allocSnapshot(volumeId, Snapshot.MANUAL_POLICY_ID, curVm.getDisplayName() + "-Clone-" + nextTemplateId, null);
SnapshotVO snapshot = (SnapshotVO) volumeService.allocSnapshot(volumeId, Snapshot.INTERNAL_POLICY_ID, curVm.getDisplayName() + "-Clone-" + nextTemplateId, null);
if (snapshot == null) {
throw new CloudRuntimeException("Unable to create a snapshot during the template creation recording");
}
Snapshot snapshotEntity = volumeService.takeSnapshot(volumeId, Snapshot.MANUAL_POLICY_ID, snapshot.getId(), caller, false, null, false, new HashMap<>());
Snapshot snapshotEntity = volumeService.takeSnapshot(volumeId, Snapshot.INTERNAL_POLICY_ID, snapshot.getId(), caller, false, null, false, new HashMap<>());
if (snapshotEntity == null) {
throw new CloudRuntimeException("Error when creating the snapshot entity");
}
Expand Down Expand Up @@ -1991,11 +1988,7 @@ public VMTemplateVO createPrivateTemplateRecord(CloneVMCmd cmd, Account template
snapshot.getSize());
}

if (template != null) {
return template;
} else {
throw new CloudRuntimeException("Failed to create a template");
}
return template;
}

@Override
Expand Down
50 changes: 25 additions & 25 deletions 50 server/src/main/java/com/cloud/vm/UserVmManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4648,12 +4648,12 @@ public Optional<UserVm> cloneVirtualMachine(CloneVMCmd cmd, VolumeApiService vol
try {
for (VolumeVO dataDisk : dataDisks) {
long diskId = dataDisk.getId();
SnapshotVO dataSnapShot = (SnapshotVO) volumeService.allocSnapshot(diskId, Snapshot.MANUAL_POLICY_ID, "DataDisk-Clone" + dataDisk.getName(), null);
SnapshotVO dataSnapShot = (SnapshotVO) volumeService.allocSnapshot(diskId, Snapshot.INTERNAL_POLICY_ID, "DataDisk-Clone" + dataDisk.getName(), null);
if (dataSnapShot == null) {
throw new CloudRuntimeException("Unable to allocate snapshot of data disk: " + dataDisk.getId() + " name: " + dataDisk.getName());
}
createdSnapshots.add(dataSnapShot);
SnapshotVO snapshotEntity = (SnapshotVO) volumeService.takeSnapshot(diskId, Snapshot.MANUAL_POLICY_ID, dataSnapShot.getId(), caller, false, null, false, new HashMap<>());
SnapshotVO snapshotEntity = (SnapshotVO) volumeService.takeSnapshot(diskId, Snapshot.INTERNAL_POLICY_ID, dataSnapShot.getId(), caller, false, null, false, new HashMap<>());
if (snapshotEntity == null) {
throw new CloudRuntimeException("Error when creating the snapshot entity");
}
Expand All @@ -4674,6 +4674,10 @@ public Optional<UserVm> cloneVirtualMachine(CloneVMCmd cmd, VolumeApiService vol
VolumeVO volumeEntity = (VolumeVO) volumeService.cloneDataVolume(cmd, snapshotEntity.getId(), newDatadisk);
createdVolumes.add(volumeEntity);
}

for (VolumeVO createdVol : createdVolumes) {
volumeService.attachVolumeToVm(cmd, createdVol.getId(), createdVol.getDeviceId());
}
} catch (CloudRuntimeException e){
s_logger.warn("data disk process failed during clone, clearing the temporary resources...");
for (VolumeVO dataDiskToClear : createdVolumes) {
Expand All @@ -4683,6 +4687,7 @@ public Optional<UserVm> cloneVirtualMachine(CloneVMCmd cmd, VolumeApiService vol
if (newDatadisk != null) {
volumeService.destroyVolume(newDatadisk.getId(), caller, true, false);
}
destroyVm(vmId, true);
throw new CloudRuntimeException(e.getMessage());
} finally {
// clear the temporary data snapshots
Expand All @@ -4692,16 +4697,6 @@ public Optional<UserVm> cloneVirtualMachine(CloneVMCmd cmd, VolumeApiService vol
}
}

for (VolumeVO createdVol : createdVolumes) {
try {
volumeService.attachVolumeToVm(cmd, createdVol.getId(), createdVol.getDeviceId());
} catch (CloudRuntimeException e) {
s_logger.warn("data disk: " + createdVol.getId() + " attachment to VM " + vmId + " failed due to" + e.getMessage());
s_logger.info("Clearing the data disk: " + createdVol.getId());
volumeService.destroyVolume(createdVol.getId(), caller, true, true);
}
}

// start the VM if successfull
Long podId = curVm.getPodIdToDeployIn();
Long clusterId = null;
Expand Down Expand Up @@ -5756,22 +5751,27 @@ public UserVm recordVirtualMachineToDB(CloneVMCmd cmd) throws ConcurrentOperatio
mapToLong(AffinityGroupVO::getId).
boxed().
collect(Collectors.toList());
if (dataCenter.getNetworkType() == NetworkType.Basic) {
vmResult = createBasicSecurityGroupVirtualMachine(dataCenter, serviceOffering, template, securityGroupIdList, curAccount, hostName, displayName, diskOfferingId,
size, group, hypervisorType, cmd.getHttpMethod(), userData, sshKeyPair, ipToNetoworkMap, addr, isDisplayVM, keyboard, affinityGroupIdList,
curVm.getDetails() == null ? new HashMap<>() : curVm.getDetails(), cmd.getCustomId(), new HashMap<>(),
null, new HashMap<>(), dynamicScalingEnabled);
} else {
if (dataCenter.isSecurityGroupEnabled()) {
vmResult = createAdvancedSecurityGroupVirtualMachine(dataCenter, serviceOffering, template, networkIds, securityGroupIdList, curAccount, hostName,
displayName, diskOfferingId, size, group, hypervisorType, cmd.getHttpMethod(), userData, sshKeyPair, ipToNetoworkMap, addr, isDisplayVM, keyboard,
affinityGroupIdList, curVm.getDetails() == null ? new HashMap<>() : curVm.getDetails(), cmd.getCustomId(), new HashMap<>(),
try {
if (dataCenter.getNetworkType() == NetworkType.Basic) {
vmResult = createBasicSecurityGroupVirtualMachine(dataCenter, serviceOffering, template, securityGroupIdList, curAccount, hostName, displayName, diskOfferingId,
size, group, hypervisorType, cmd.getHttpMethod(), userData, sshKeyPair, ipToNetoworkMap, addr, isDisplayVM, keyboard, affinityGroupIdList,
curVm.getDetails() == null ? new HashMap<>() : curVm.getDetails(), cmd.getCustomId(), new HashMap<>(),
null, new HashMap<>(), dynamicScalingEnabled);
} else {
vmResult = createAdvancedVirtualMachine(dataCenter, serviceOffering, template, networkIds, curAccount, hostName, displayName, diskOfferingId, size, group,
hypervisorType, cmd.getHttpMethod(), userData, sshKeyPair, ipToNetoworkMap, addr, isDisplayVM, keyboard, affinityGroupIdList, curVm.getDetails() == null ? new HashMap<>() : curVm.getDetails(),
cmd.getCustomId(), new HashMap<>(), null, new HashMap<>(), dynamicScalingEnabled);
if (dataCenter.isSecurityGroupEnabled()) {
vmResult = createAdvancedSecurityGroupVirtualMachine(dataCenter, serviceOffering, template, networkIds, securityGroupIdList, curAccount, hostName,
displayName, diskOfferingId, size, group, hypervisorType, cmd.getHttpMethod(), userData, sshKeyPair, ipToNetoworkMap, addr, isDisplayVM, keyboard,
affinityGroupIdList, curVm.getDetails() == null ? new HashMap<>() : curVm.getDetails(), cmd.getCustomId(), new HashMap<>(),
null, new HashMap<>(), dynamicScalingEnabled);
} else {
vmResult = createAdvancedVirtualMachine(dataCenter, serviceOffering, template, networkIds, curAccount, hostName, displayName, diskOfferingId, size, group,
hypervisorType, cmd.getHttpMethod(), userData, sshKeyPair, ipToNetoworkMap, addr, isDisplayVM, keyboard, affinityGroupIdList, curVm.getDetails() == null ? new HashMap<>() : curVm.getDetails(),
cmd.getCustomId(), new HashMap<>(), null, new HashMap<>(), dynamicScalingEnabled);
}
}
} catch (CloudRuntimeException e) {
_templateMgr.delete(curAccount.getId(), template.getId(), zoneId);
throw new CloudRuntimeException("Unable to create the VM record");
}
return vmResult;
}
Expand Down
12 changes: 12 additions & 0 deletions 12 usage/src/main/java/com/cloud/usage/UsageManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
import javax.inject.Inject;
import javax.naming.ConfigurationException;

import com.cloud.storage.Snapshot;
import com.cloud.storage.SnapshotVO;
import com.cloud.storage.dao.SnapshotDao;
import org.apache.cloudstack.quota.QuotaAlertManager;
import org.apache.cloudstack.quota.QuotaManager;
import org.apache.cloudstack.quota.QuotaStatement;
Expand Down Expand Up @@ -162,6 +165,8 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna
private QuotaAlertManager _alertManager;
@Inject
private QuotaStatement _quotaStatement;
@Inject
private SnapshotDao _snapshotDao;

private String _version = null;
private final Calendar _jobExecTime = Calendar.getInstance();
Expand Down Expand Up @@ -1633,6 +1638,13 @@ private void createSnapshotHelperEvent(UsageEventVO event) {
long zoneId = -1L;

long snapId = event.getResourceId();

SnapshotVO snapshotInstance = _snapshotDao.findById(snapId);

if (snapshotInstance != null && snapshotInstance.getsnapshotType() == Snapshot.Type.INTERNAL.ordinal()) {
return;
}

if (EventTypes.EVENT_SNAPSHOT_CREATE.equals(event.getType())) {
if (usageSnapshotSelection){
snapSize = event.getVirtualSize();
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.