diff --git a/modules/database/src/main/java/org/eclipse/xpanse/modules/database/serviceorder/ServiceOrderEntity.java b/modules/database/src/main/java/org/eclipse/xpanse/modules/database/serviceorder/ServiceOrderEntity.java index 4e29ad90e7..2f633f8aab 100644 --- a/modules/database/src/main/java/org/eclipse/xpanse/modules/database/serviceorder/ServiceOrderEntity.java +++ b/modules/database/src/main/java/org/eclipse/xpanse/modules/database/serviceorder/ServiceOrderEntity.java @@ -80,12 +80,12 @@ public class ServiceOrderEntity { @Convert(converter = ObjectJsonConverter.class) private List previousDeployedResources; - @Column(name = "PREVIOUS_DEPLOYED_RESULT_PROPERTY", columnDefinition = "json") + @Column(name = "PREVIOUS_DEPLOYED_SERVICE_PROPERTY", columnDefinition = "json") @Type(value = JsonType.class) @Convert(converter = ObjectJsonConverter.class) private Map previousDeployedServiceProperties; - @Column(name = "PREVIOUS_DEPLOYED_SERVICE_PROPERTY", columnDefinition = "json") + @Column(name = "PREVIOUS_DEPLOYED_RESULT_PROPERTY", columnDefinition = "json") @Type(value = JsonType.class) @Convert(converter = ObjectJsonConverter.class) private Map previousDeployedResultProperties; diff --git a/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/DeployResultManager.java b/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/DeployResultManager.java index 41b0e4049d..2b211646de 100644 --- a/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/DeployResultManager.java +++ b/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/DeployResultManager.java @@ -201,13 +201,23 @@ public void updateServiceOrderTaskWithDeployResult(DeployResult deployResult, } ServiceOrderEntity entityToUpdate = new ServiceOrderEntity(); BeanUtils.copyProperties(storedEntity, entityToUpdate); + DeployerTaskStatus deployerTaskStatus = deployResult.getState(); if (deployResult.getIsTaskSuccessful()) { - entityToUpdate.setTaskStatus(TaskStatus.SUCCESSFUL); + // When the status is rollback_success, the deployment order status should be failed. + if (deployerTaskStatus == DeployerTaskStatus.ROLLBACK_SUCCESS) { + entityToUpdate.setTaskStatus(TaskStatus.FAILED); + } else { + entityToUpdate.setTaskStatus(TaskStatus.SUCCESSFUL); + } entityToUpdate.setCompletedTime(OffsetDateTime.now()); } else { - entityToUpdate.setTaskStatus(TaskStatus.FAILED); entityToUpdate.setErrorMsg(deployResult.getMessage()); - entityToUpdate.setCompletedTime(OffsetDateTime.now()); + // When the status is deploy_failed, the order status should be in-progress util the + // rollback is done. + if (deployerTaskStatus != DeployerTaskStatus.DEPLOY_FAILED) { + entityToUpdate.setTaskStatus(TaskStatus.FAILED); + entityToUpdate.setCompletedTime(OffsetDateTime.now()); + } } serviceOrderStorage.storeAndFlush(entityToUpdate); } diff --git a/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/deployers/opentofu/opentofulocal/OpenTofuLocalDeployment.java b/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/deployers/opentofu/opentofulocal/OpenTofuLocalDeployment.java index d445887688..dedf6dc0a1 100644 --- a/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/deployers/opentofu/opentofulocal/OpenTofuLocalDeployment.java +++ b/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/deployers/opentofu/opentofulocal/OpenTofuLocalDeployment.java @@ -17,6 +17,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Objects; +import java.util.Set; import java.util.UUID; import java.util.concurrent.Executor; import java.util.stream.Stream; @@ -165,9 +166,11 @@ private void asyncExecDeploy(DeployTask task) { openTofuResult.setCommandStdError(tfEx.getMessage()); } openTofuResult.setTerraformState(executor.getTerraformState()); - openTofuResult.setImportantFileContentMap(executor.getImportantFilesContent()); + Map importantFilesContent = executor.getImportantFilesContent(); + openTofuResult.setImportantFileContentMap(importantFilesContent); openTofuDeploymentResultCallbackManager.deployCallback(task.getServiceId(), openTofuResult); + deleteStoredFiles(workspace, importantFilesContent.keySet()); }); } @@ -188,10 +191,11 @@ private void asyncExecDestroy(DeployTask task, String tfState) { openTofuResult.setCommandStdError(tfEx.getMessage()); } openTofuResult.setTerraformState(executor.getTerraformState()); - openTofuResult.setImportantFileContentMap(executor.getImportantFilesContent()); + Map importantFilesContent = executor.getImportantFilesContent(); + openTofuResult.setImportantFileContentMap(importantFilesContent); openTofuDeploymentResultCallbackManager.destroyCallback(task.getServiceId(), - openTofuResult, - task.getDeploymentScenario()); + openTofuResult, task.getDeploymentScenario()); + deleteStoredFiles(workspace, importantFilesContent.keySet()); }); } @@ -213,9 +217,11 @@ private void asyncExecModify(DeployTask task, String tfState) { openTofuResult.setCommandStdError(tfEx.getMessage()); } openTofuResult.setTerraformState(executor.getTerraformState()); - openTofuResult.setImportantFileContentMap(executor.getImportantFilesContent()); + Map importantFilesContent = executor.getImportantFilesContent(); + openTofuResult.setImportantFileContentMap(importantFilesContent); openTofuDeploymentResultCallbackManager.modifyCallback(task.getServiceId(), openTofuResult); + deleteStoredFiles(workspace, importantFilesContent.keySet()); }); } @@ -233,21 +239,34 @@ public String getDeploymentPlanAsJson(DeployTask task) { @Override public void deleteTaskWorkspace(UUID taskId) { String workspace = getWorkspacePath(taskId); - deleteWorkSpace(workspace); + deleteWorkspace(workspace); } /** * delete workspace. */ - private void deleteWorkSpace(String workspace) { + private void deleteWorkspace(String workspace) { Path path = Paths.get(workspace); try (Stream pathStream = Files.walk(path)) { pathStream.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); } catch (Exception e) { - log.error(e.getMessage(), e); + log.error("Delete workspace:{} error.", workspace, e); } } + + private void deleteStoredFiles(String workspace, Set fileNames) { + fileNames.forEach(fileName -> { + try { + String path = workspace + File.separator + fileName; + File file = new File(path); + Files.deleteIfExists(file.toPath()); + } catch (IOException e) { + log.error("Delete file with name:{} error.", fileName, e); + } + }); + } + /** * Get an OpenTofuExecutor. * diff --git a/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/deployers/opentofu/opentofulocal/OpenTofuLocalExecutor.java b/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/deployers/opentofu/opentofulocal/OpenTofuLocalExecutor.java index 9e71e46094..8635f21e40 100644 --- a/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/deployers/opentofu/opentofulocal/OpenTofuLocalExecutor.java +++ b/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/deployers/opentofu/opentofulocal/OpenTofuLocalExecutor.java @@ -55,10 +55,10 @@ public class OpenTofuLocalExecutor { * @param deployResultFileUtils file tool class. */ OpenTofuLocalExecutor(Map env, - Map variables, - String workspace, - @Nullable String subDirectory, - DeployResultFileUtils deployResultFileUtils) { + Map variables, + String workspace, + @Nullable String subDirectory, + DeployResultFileUtils deployResultFileUtils) { this.env = env; this.variables = variables; this.workspace = @@ -221,17 +221,12 @@ public Map getImportantFilesContent() { if (workPath.isDirectory() && workPath.exists()) { File[] files = workPath.listFiles(); if (Objects.nonNull(files)) { - List importantFiles = Arrays.stream(files) - .filter(file -> file.isFile() && !isExcludedFile(file.getName())).toList(); - for (File importantFile : importantFiles) { - try { - String content = readFile(importantFile); - fileContentMap.put(importantFile.getName(), content); - } catch (IOException e) { - log.error("Read content of file with name:{} error.", - importantFile.getName(), e); + Arrays.stream(files).forEach(file -> { + if (file.isFile() && !isExcludedFile(file.getName())) { + String content = readFileContent(file); + fileContentMap.put(file.getName(), content); } - } + }); } } return fileContentMap; @@ -267,8 +262,15 @@ private boolean isExcludedFile(String fileName) { return EXCLUDED_FILE_SUFFIX_LIST.contains(fileSuffix); } - private String readFile(File file) throws IOException { - return Files.readString(file.toPath()); + private String readFileContent(File file) { + String fileContent = ""; + try { + fileContent = Files.readString(file.toPath()); + log.info("Read file content with name:{} successfully.", file.getName()); + } catch (IOException e) { + log.error("Read file content with name:{} error.", file.getName(), e); + } + return fileContent; } diff --git a/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/deployers/terraform/terraformlocal/TerraformLocalDeployment.java b/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/deployers/terraform/terraformlocal/TerraformLocalDeployment.java index 4d604f4276..f4695c3db6 100644 --- a/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/deployers/terraform/terraformlocal/TerraformLocalDeployment.java +++ b/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/deployers/terraform/terraformlocal/TerraformLocalDeployment.java @@ -16,6 +16,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Objects; +import java.util.Set; import java.util.UUID; import java.util.concurrent.Executor; import java.util.stream.Stream; @@ -164,9 +165,11 @@ private void asyncExecDeploy(DeployTask task) { terraformResult.setCommandStdError(tfEx.getMessage()); } terraformResult.setTerraformState(executor.getTerraformState()); - terraformResult.setImportantFileContentMap(executor.getImportantFilesContent()); + Map importantFileContentMap = executor.getImportantFilesContent(); + terraformResult.setImportantFileContentMap(importantFileContentMap); terraformDeploymentResultCallbackManager.deployCallback(task.getServiceId(), terraformResult); + deleteStoredFiles(workspace, importantFileContentMap.keySet()); }); } @@ -187,9 +190,11 @@ private void asyncExecDestroy(DeployTask task, String tfState) { terraformResult.setCommandStdError(tfEx.getMessage()); } terraformResult.setTerraformState(executor.getTerraformState()); - terraformResult.setImportantFileContentMap(executor.getImportantFilesContent()); + Map importantFileContentMap = executor.getImportantFilesContent(); + terraformResult.setImportantFileContentMap(importantFileContentMap); terraformDeploymentResultCallbackManager.destroyCallback(task.getServiceId(), terraformResult, task.getDeploymentScenario()); + deleteStoredFiles(workspace, importantFileContentMap.keySet()); }); } @@ -211,9 +216,11 @@ private void asyncExecModify(DeployTask task, String tfState) { terraformResult.setCommandStdError(tfEx.getMessage()); } terraformResult.setTerraformState(executor.getTerraformState()); - terraformResult.setImportantFileContentMap(executor.getImportantFilesContent()); + Map importantFileContentMap = executor.getImportantFilesContent(); + terraformResult.setImportantFileContentMap(importantFileContentMap); terraformDeploymentResultCallbackManager.modifyCallback(task.getServiceId(), terraformResult); + deleteStoredFiles(workspace, importantFileContentMap.keySet()); }); } @@ -231,21 +238,33 @@ public String getDeploymentPlanAsJson(DeployTask task) { @Override public void deleteTaskWorkspace(UUID taskId) { String workspace = getWorkspacePath(taskId); - deleteWorkSpace(workspace); + deleteWorkspace(workspace); } /** * delete workspace. */ - private void deleteWorkSpace(String workspace) { + private void deleteWorkspace(String workspace) { Path path = Paths.get(workspace); try (Stream pathStream = Files.walk(path)) { pathStream.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); } catch (Exception e) { - log.error(e.getMessage(), e); + log.error("Delete workspace:{} error.", workspace, e); } } + private void deleteStoredFiles(String workspace, Set fileNames) { + fileNames.forEach(fileName -> { + try { + String path = workspace + File.separator + fileName; + File file = new File(path); + Files.deleteIfExists(file.toPath()); + } catch (IOException e) { + log.error("Delete file with name:{} error.", fileName, e); + } + }); + } + /** * Get a TerraformExecutor. * diff --git a/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/deployers/terraform/terraformlocal/TerraformLocalExecutor.java b/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/deployers/terraform/terraformlocal/TerraformLocalExecutor.java index 5909ea902d..7ba2ab29ba 100644 --- a/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/deployers/terraform/terraformlocal/TerraformLocalExecutor.java +++ b/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/deployers/terraform/terraformlocal/TerraformLocalExecutor.java @@ -54,10 +54,10 @@ public class TerraformLocalExecutor { * @param deployResultFileUtils file tool class. */ TerraformLocalExecutor(Map env, - Map variables, - String workspace, - @Nullable String subDirectory, - DeployResultFileUtils deployResultFileUtils) { + Map variables, + String workspace, + @Nullable String subDirectory, + DeployResultFileUtils deployResultFileUtils) { this.env = env; this.variables = variables; this.workspace = @@ -220,17 +220,12 @@ public Map getImportantFilesContent() { if (workPath.isDirectory() && workPath.exists()) { File[] files = workPath.listFiles(); if (Objects.nonNull(files)) { - List importantFiles = Arrays.stream(files) - .filter(file -> file.isFile() && !isExcludedFile(file.getName())).toList(); - for (File importantFile : importantFiles) { - try { - String content = readFile(importantFile); - fileContentMap.put(importantFile.getName(), content); - } catch (IOException e) { - log.error("Read content of file with name:{} error.", - importantFile.getName(), e); + Arrays.stream(files).forEach(file -> { + if (file.isFile() && !isExcludedFile(file.getName())) { + String content = readFileContent(file); + fileContentMap.put(file.getName(), content); } - } + }); } } return fileContentMap; @@ -266,8 +261,15 @@ private boolean isExcludedFile(String fileName) { return EXCLUDED_FILE_SUFFIX_LIST.contains(fileSuffix); } - private String readFile(File file) throws IOException { - return Files.readString(file.toPath()); + private String readFileContent(File file) { + String fileContent = ""; + try { + fileContent = Files.readString(file.toPath()); + log.info("Read file content with name:{} successfully.", file.getName()); + } catch (IOException e) { + log.error("Read file content with name:{} error.", file.getName(), e); + } + return fileContent; }