From 4bf370c62edb30e670a93e50446e072781362dc4 Mon Sep 17 00:00:00 2001 From: baixinsui Date: Tue, 30 Jul 2024 11:12:21 +0800 Subject: [PATCH] Clean up files have been read in workspace --- .../serviceorder/ServiceOrderEntity.java | 4 +-- .../deployment/DeployResultManager.java | 16 +++++++-- .../opentofulocal/OpenTofuLocalExecutor.java | 36 ++++++++++--------- .../TerraformLocalExecutor.java | 36 ++++++++++--------- 4 files changed, 55 insertions(+), 37 deletions(-) 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/OpenTofuLocalExecutor.java b/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/deployers/opentofu/opentofulocal/OpenTofuLocalExecutor.java index 9e71e46094..8c091f4fca 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 = readFileContentAndDelete(file); + fileContentMap.put(file.getName(), content); } - } + }); } } return fileContentMap; @@ -267,8 +262,17 @@ 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 readFileContentAndDelete(File file) { + String fileContent = ""; + try { + fileContent = Files.readString(file.toPath()); + boolean deleteResult = Files.deleteIfExists(file.toPath()); + log.info("Read file content with name:{} successfully. Delete result:{}", + file.getName(), deleteResult); + } 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/TerraformLocalExecutor.java b/modules/deployment/src/main/java/org/eclipse/xpanse/modules/deployment/deployers/terraform/terraformlocal/TerraformLocalExecutor.java index 5909ea902d..952497856e 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 = readFileContentAndDelete(file); + fileContentMap.put(file.getName(), content); } - } + }); } } return fileContentMap; @@ -266,8 +261,17 @@ 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 readFileContentAndDelete(File file) { + String fileContent = ""; + try { + fileContent = Files.readString(file.toPath()); + boolean deleteResult = Files.deleteIfExists(file.toPath()); + log.info("Read file content with name:{} successfully. Delete result:{}", + file.getName(), deleteResult); + } catch (IOException e) { + log.error("Read file content with name:{} error.", file.getName(), e); + } + return fileContent; }