Skip to content

Commit 9f20c8d

Browse files
João JandreJoaoJandre
authored andcommitted
Very minor refactor
1 parent 22c5bb3 commit 9f20c8d

9 files changed

Lines changed: 59 additions & 49 deletions

File tree

api/src/main/java/org/apache/cloudstack/api/command/user/backup/DownloadValidationScreenshotCmd.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.cloud.event.EventTypes;
2020
import com.cloud.exception.ConcurrentOperationException;
2121
import com.cloud.exception.InsufficientCapacityException;
22+
import com.cloud.exception.InvalidParameterValueException;
2223
import com.cloud.exception.NetworkRuleConflictException;
2324
import com.cloud.exception.ResourceAllocationException;
2425
import com.cloud.exception.ResourceUnavailableException;
@@ -72,12 +73,15 @@ public String getEventType() {
7273

7374
@Override
7475
public String getEventDescription() {
75-
return "Downloading validation screenshot of backup " + getBackupId();
76+
Backup backup = _entityMgr.findById(Backup.class, getBackupId());
77+
if (backup == null) {
78+
throw new InvalidParameterValueException(String.format("Unable to find backup with ID [%s]", getBackupId()));
79+
}
80+
return "Downloading validation screenshot of backup " + backup.getUuid();
7681
}
7782

7883
@Override
79-
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException,
80-
NetworkRuleConflictException {
84+
public void execute() {
8185
ExtractResponse response = internalBackupService.downloadScreenshot(getBackupId());
8286
response.setResponseName(getCommandName());
8387
response.setObjectName(getCommandName());

api/src/main/java/org/apache/cloudstack/api/command/user/network/CreateNetworkCmd.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ public CreateNetworkCmd(long networkOfferingId, String name, String displayText,
430430
this.subdomainAccess = subdomainAccess;
431431
this.displayNetwork = displayNetwork;
432432
}
433+
433434
@Override
434435
public String getCommandName() {
435436
return s_name;

plugins/backup/kboss/src/main/java/org/apache/cloudstack/backup/KbossBackupProvider.java

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,7 @@ protected void endBackupChainIfConfigured(BackupVO backupVO) {
12361236

12371237
// Get updated record
12381238
InternalBackupJoinVO backupJoinVO = internalBackupJoinDao.findById(backupVO.getId());
1239-
if (backupJoinVO.getCurrent() || (!backupChildren.isEmpty() && backupChildren.get(backupChildren.size() -1).getCurrent())) {
1239+
if (backupJoinVO.getCurrent() || (!backupChildren.isEmpty() && backupChildren.get(backupChildren.size() - 1).getCurrent())) {
12401240
logger.info("As [{}] is true, we are ending the backup chain for VM [{}]. The next backup will be a full backup.",
12411241
BackupValidationServiceController.backupValidationEndChainOnFail.toString());
12421242
endBackupChain(userVmDao.findById(backupVO.getVmId()));
@@ -2206,32 +2206,32 @@ protected boolean processValidationAnswer(Answer answer, BackupVO backupVO, User
22062206
setBackupAsInvalidAndSendAlert(backupVO, msg);
22072207
return false;
22082208
}
2209-
if (answer instanceof ValidateKbossVmAnswer) {
2210-
ValidateKbossVmAnswer validateKbossVmAnswer = (ValidateKbossVmAnswer)answer;
2211-
boolean result = true;
2212-
String msg = String.format("Backup [%s] was validated using dummy VM [%s]. The backup was deemed invalid due to: ", backupVO.getUuid(), validationVm.getName());
2213-
if (validateKbossVmCommand.isWaitForBoot() && !validateKbossVmAnswer.isBootValidated()) {
2214-
result = false;
2215-
msg += "\n - The VM did not boot within the expected time.";
2216-
}
2217-
if (validateKbossVmCommand.isExecuteScript() && validateKbossVmAnswer.getScriptResult() != null) {
2218-
result = false;
2219-
msg += "\n - The script did not output the expected output. Captured output: " + validateKbossVmAnswer.getScriptResult();
2220-
}
2221-
if (validateKbossVmCommand.isTakeScreenshot() && validateKbossVmAnswer.getScreenshotPath() == null) {
2222-
result = false;
2223-
msg += "\n - We were unable to take a screenshot of the VM.";
2224-
} else if (validateKbossVmCommand.isTakeScreenshot()) {
2225-
logger.debug("Saving validation screenshot path [{}] to the backup details of backup [{}].", validateKbossVmAnswer.getScreenshotPath(), backupVO.getUuid());
2226-
backupDetailDao.addDetail(backupVO.getId(), SCREENSHOT_PATH, validateKbossVmAnswer.getScreenshotPath(), false);
2227-
}
2228-
if (!result) {
2229-
setBackupAsInvalidAndSendAlert(backupVO, msg);
2230-
}
2231-
2232-
return result;
2209+
if (!(answer instanceof ValidateKbossVmAnswer)) {
2210+
return false;
22332211
}
2234-
return false;
2212+
ValidateKbossVmAnswer validateKbossVmAnswer = (ValidateKbossVmAnswer)answer;
2213+
boolean result = true;
2214+
String msg = String.format("Backup [%s] was validated using dummy VM [%s]. The backup was deemed invalid due to: ", backupVO.getUuid(), validationVm.getName());
2215+
if (validateKbossVmCommand.isWaitForBoot() && !validateKbossVmAnswer.isBootValidated()) {
2216+
result = false;
2217+
msg += "\n - The VM did not boot within the expected time.";
2218+
}
2219+
if (validateKbossVmCommand.isExecuteScript() && validateKbossVmAnswer.getScriptResult() != null) {
2220+
result = false;
2221+
msg += "\n - The script did not output the expected output. Captured output: " + validateKbossVmAnswer.getScriptResult();
2222+
}
2223+
if (validateKbossVmCommand.isTakeScreenshot() && validateKbossVmAnswer.getScreenshotPath() == null) {
2224+
result = false;
2225+
msg += "\n - We were unable to take a screenshot of the VM.";
2226+
} else if (validateKbossVmCommand.isTakeScreenshot()) {
2227+
logger.debug("Saving validation screenshot path [{}] to the backup details of backup [{}].", validateKbossVmAnswer.getScreenshotPath(), backupVO.getUuid());
2228+
backupDetailDao.addDetail(backupVO.getId(), SCREENSHOT_PATH, validateKbossVmAnswer.getScreenshotPath(), false);
2229+
}
2230+
if (!result) {
2231+
setBackupAsInvalidAndSendAlert(backupVO, msg);
2232+
}
2233+
2234+
return result;
22352235
}
22362236

22372237
protected void handleBackupExceptionInRestore(VirtualMachine vm, BackupException jobResult) {
@@ -2566,15 +2566,17 @@ protected void configureValidationSteps(ValidateKbossVmCommand cmd, BackupVO bac
25662566
BackupValidationServiceController.backupValidationBootDefaultTimeout.valueIn(backup.getAccountId()));
25672567
break;
25682568
case execute_command:
2569-
configureValidationScript(cmd, backup.getVmId(), backup.getAccountId());
2569+
configureValidationScript(cmd, backup);
25702570
break;
25712571
}
25722572
}
25732573
}
25742574

2575-
protected void configureValidationScript(ValidateKbossVmCommand cmd, long vmId, long accountId) {
2575+
protected void configureValidationScript(ValidateKbossVmCommand cmd, BackupVO backupVO) {
2576+
long vmId = backupVO.getVmId();
25762577
VMInstanceDetailVO script = vmInstanceDetailsDao.findDetail(vmId, VmDetailConstants.VALIDATION_COMMAND);
25772578
if (script == null) {
2579+
logger.warn("Execute command step was configured but no script given. Ignoring this step for backup [{}].", backupVO.getUuid());
25782580
return;
25792581
}
25802582
cmd.setExecuteScript(true);
@@ -2585,7 +2587,7 @@ protected void configureValidationScript(ValidateKbossVmCommand cmd, long vmId,
25852587
cmd.setExpectedResult(scriptExpectedResult != null ? scriptExpectedResult.getValue() : "0");
25862588
VMInstanceDetailVO scriptTimeout = vmInstanceDetailsDao.findDetail(vmId, VmDetailConstants.VALIDATION_COMMAND_TIMEOUT);
25872589
cmd.setScriptTimeout(scriptTimeout != null ? Integer.valueOf(scriptTimeout.getValue()) :
2588-
BackupValidationServiceController.backupValidationScriptDefaultTimeout.valueIn(accountId));
2590+
BackupValidationServiceController.backupValidationScriptDefaultTimeout.valueIn(backupVO.getId()));
25892591
}
25902592

25912593
protected void createBasicBackupDetails(Long imageStoreId, Long parentId, BackupVO backupVO) {

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCleanupKbossValidationCommandWrapper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public Answer execute(CleanupKbossValidationCommand command, LibvirtComputingRes
3333
return new Answer(command);
3434
}
3535

36+
/**
37+
* The objective of this command is to remove the secondary storage references after the validation VM was stopped.
38+
* Since the `getStoragePoolByURI` and `deleteStoragePool` have a reference counter, where the first method increases the count and the second one decreases the count,
39+
* we must call the deleteStoragePool twice so that the command is count negative.
40+
* */
3641
private void cleanupSecondaryStorages(CleanupKbossValidationCommand command, KVMStoragePoolManager storagePoolMgr) {
3742
logger.info("Cleaning up secondary storage references after backup validation process using VM [{}].", command.getVmName());
3843
for (String secondaryUrl : command.getSecondaryStorages()) {

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtValidateKbossVmCommandWrapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ private String runScript(ValidateKbossVmCommand command, Domain vm) throws Libvi
155155
}
156156
String script = command.getScriptToExecute();
157157
if (script == null) {
158+
logger.warn("This command is malformed, we should execute an script for VM [{}], but no script was configured. Please review the original VM configurations.", vm.getName());
158159
return null;
159160
}
160161
String arguments = command.getScriptArguments();

server/src/main/java/com/cloud/vm/UserVmManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public interface UserVmManager extends UserVmService {
113113

114114
ConfigKey<Boolean> EnforceResourceLimitOnValidationVm = new ConfigKey<Boolean>(
115115
"Advanced", Boolean.class, "enforce.resource.limit.on.backup.validation.vm", "false", "If set to true, validation VMs will be accounted in the resource limit of the " +
116-
"account/domain.",true, ConfigKey.Scope.Account);
116+
"account/domain.", true, ConfigKey.Scope.Account);
117117

118118
static final int MAX_USER_DATA_LENGTH_BYTES = 2048;
119119

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10185,7 +10185,6 @@ public UserVm allocateVMForValidation(long backupId, HypervisorType hypervisor)
1018510185
return vm;
1018610186
}
1018710187

10188-
1018910188
private Network getValidationNetwork(long zoneId) {
1019010189
NetworkVO networkVo = _networkDao.findByZoneIdAndAccountIdAndGuestType(zoneId, Account.ACCOUNT_ID_SYSTEM, GuestType.Shared);
1019110190
AccountVO accountVO = _accountDao.findById(Account.ACCOUNT_ID_SYSTEM);

server/src/main/java/org/apache/cloudstack/backup/BackupValidationServiceController.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,11 @@ protected void searchAndDispatchJobs() {
141141
}
142142
}
143143

144-
145-
146144
@Override
147145
protected void submitQueuedJob(InternalBackupServiceJobVO job, long zoneId, String logId) {
148146
executor.submit(() -> startBackupValidation(job, zoneId, logId));
149147
}
150148

151-
152149
@Override
153150
protected List<InternalBackupServiceJobVO> getLostJobs(ClusterVO clusterVO, Calendar date, List<HostVO> hostVOS) {
154151
date.add(Calendar.SECOND, (int)Math.round(InternalBackupProvider.backupValidationTimeout.valueIn(clusterVO.getId()) * -RESCHEDULE_TO_TIMEOUT_RATIO));

services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,18 +2185,19 @@ protected Answer deleteBackup(DeleteCommand cmd) {
21852185
}
21862186

21872187
protected BackupDeleteAnswer deleteScreenshot(DeleteCommand cmd, String screenshotRelativePath, String parent) {
2188-
if (screenshotRelativePath != null) {
2189-
if (screenshotRelativePath.startsWith(File.separator)) {
2190-
screenshotRelativePath = screenshotRelativePath.substring(1);
2191-
}
2192-
String fullScreenshotPath = parent + screenshotRelativePath;
2193-
logger.debug("Deleting screenshot at [{}].", fullScreenshotPath);
2194-
String screenshotDeleteResult = deleteLocalFile(fullScreenshotPath);
2195-
if (screenshotDeleteResult != null) {
2196-
String details = String.format("Failed to delete backup validation screenshot [%s] with result [%s]. ", fullScreenshotPath, screenshotDeleteResult);
2197-
logger.warn(details);
2198-
return new BackupDeleteAnswer(cmd, false, details);
2199-
}
2188+
if (screenshotRelativePath == null) {
2189+
return null;
2190+
}
2191+
if (screenshotRelativePath.startsWith(File.separator)) {
2192+
screenshotRelativePath = screenshotRelativePath.substring(1);
2193+
}
2194+
String fullScreenshotPath = parent + screenshotRelativePath;
2195+
logger.debug("Deleting screenshot at [{}].", fullScreenshotPath);
2196+
String screenshotDeleteResult = deleteLocalFile(fullScreenshotPath);
2197+
if (screenshotDeleteResult != null) {
2198+
String details = String.format("Failed to delete backup validation screenshot [%s] with result [%s]. ", fullScreenshotPath, screenshotDeleteResult);
2199+
logger.warn(details);
2200+
return new BackupDeleteAnswer(cmd, false, details);
22002201
}
22012202
return null;
22022203
}

0 commit comments

Comments
 (0)