Skip to content

Commit

Permalink
Clean up files have been read in workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
baixinsui committed Jul 30, 2024
1 parent 402be6f commit ac618e0
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ public class ServiceOrderEntity {
@Convert(converter = ObjectJsonConverter.class)
private List<DeployResource> 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<String, String> 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<String, String> previousDeployedResultProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -165,9 +166,11 @@ private void asyncExecDeploy(DeployTask task) {
openTofuResult.setCommandStdError(tfEx.getMessage());
}
openTofuResult.setTerraformState(executor.getTerraformState());
openTofuResult.setImportantFileContentMap(executor.getImportantFilesContent());
Map<String, String> importantFilesContent = executor.getImportantFilesContent();
openTofuResult.setImportantFileContentMap(importantFilesContent);
openTofuDeploymentResultCallbackManager.deployCallback(task.getServiceId(),
openTofuResult);
deleteStoredFiles(workspace, importantFilesContent.keySet());
});
}

Expand All @@ -188,10 +191,11 @@ private void asyncExecDestroy(DeployTask task, String tfState) {
openTofuResult.setCommandStdError(tfEx.getMessage());
}
openTofuResult.setTerraformState(executor.getTerraformState());
openTofuResult.setImportantFileContentMap(executor.getImportantFilesContent());
Map<String, String> importantFilesContent = executor.getImportantFilesContent();
openTofuResult.setImportantFileContentMap(importantFilesContent);
openTofuDeploymentResultCallbackManager.destroyCallback(task.getServiceId(),
openTofuResult,
task.getDeploymentScenario());
openTofuResult, task.getDeploymentScenario());
deleteStoredFiles(workspace, importantFilesContent.keySet());
});
}

Expand All @@ -213,9 +217,11 @@ private void asyncExecModify(DeployTask task, String tfState) {
openTofuResult.setCommandStdError(tfEx.getMessage());
}
openTofuResult.setTerraformState(executor.getTerraformState());
openTofuResult.setImportantFileContentMap(executor.getImportantFilesContent());
Map<String, String> importantFilesContent = executor.getImportantFilesContent();
openTofuResult.setImportantFileContentMap(importantFilesContent);
openTofuDeploymentResultCallbackManager.modifyCallback(task.getServiceId(),
openTofuResult);
deleteStoredFiles(workspace, importantFilesContent.keySet());
});
}

Expand All @@ -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<Path> 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<String> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public class OpenTofuLocalExecutor {
* @param deployResultFileUtils file tool class.
*/
OpenTofuLocalExecutor(Map<String, String> env,
Map<String, Object> variables,
String workspace,
@Nullable String subDirectory,
DeployResultFileUtils deployResultFileUtils) {
Map<String, Object> variables,
String workspace,
@Nullable String subDirectory,
DeployResultFileUtils deployResultFileUtils) {
this.env = env;
this.variables = variables;
this.workspace =
Expand Down Expand Up @@ -221,17 +221,12 @@ public Map<String, String> getImportantFilesContent() {
if (workPath.isDirectory() && workPath.exists()) {
File[] files = workPath.listFiles();
if (Objects.nonNull(files)) {
List<File> 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;
Expand Down Expand Up @@ -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;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -164,9 +165,11 @@ private void asyncExecDeploy(DeployTask task) {
terraformResult.setCommandStdError(tfEx.getMessage());
}
terraformResult.setTerraformState(executor.getTerraformState());
terraformResult.setImportantFileContentMap(executor.getImportantFilesContent());
Map<String, String> importantFileContentMap = executor.getImportantFilesContent();
terraformResult.setImportantFileContentMap(importantFileContentMap);
terraformDeploymentResultCallbackManager.deployCallback(task.getServiceId(),
terraformResult);
deleteStoredFiles(workspace, importantFileContentMap.keySet());
});
}

Expand All @@ -187,9 +190,11 @@ private void asyncExecDestroy(DeployTask task, String tfState) {
terraformResult.setCommandStdError(tfEx.getMessage());
}
terraformResult.setTerraformState(executor.getTerraformState());
terraformResult.setImportantFileContentMap(executor.getImportantFilesContent());
Map<String, String> importantFileContentMap = executor.getImportantFilesContent();
terraformResult.setImportantFileContentMap(importantFileContentMap);
terraformDeploymentResultCallbackManager.destroyCallback(task.getServiceId(),
terraformResult, task.getDeploymentScenario());
deleteStoredFiles(workspace, importantFileContentMap.keySet());
});
}

Expand All @@ -211,9 +216,11 @@ private void asyncExecModify(DeployTask task, String tfState) {
terraformResult.setCommandStdError(tfEx.getMessage());
}
terraformResult.setTerraformState(executor.getTerraformState());
terraformResult.setImportantFileContentMap(executor.getImportantFilesContent());
Map<String, String> importantFileContentMap = executor.getImportantFilesContent();
terraformResult.setImportantFileContentMap(importantFileContentMap);
terraformDeploymentResultCallbackManager.modifyCallback(task.getServiceId(),
terraformResult);
deleteStoredFiles(workspace, importantFileContentMap.keySet());
});
}

Expand All @@ -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<Path> 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<String> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public class TerraformLocalExecutor {
* @param deployResultFileUtils file tool class.
*/
TerraformLocalExecutor(Map<String, String> env,
Map<String, Object> variables,
String workspace,
@Nullable String subDirectory,
DeployResultFileUtils deployResultFileUtils) {
Map<String, Object> variables,
String workspace,
@Nullable String subDirectory,
DeployResultFileUtils deployResultFileUtils) {
this.env = env;
this.variables = variables;
this.workspace =
Expand Down Expand Up @@ -220,17 +220,12 @@ public Map<String, String> getImportantFilesContent() {
if (workPath.isDirectory() && workPath.exists()) {
File[] files = workPath.listFiles();
if (Objects.nonNull(files)) {
List<File> 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;
Expand Down Expand Up @@ -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;
}


Expand Down

0 comments on commit ac618e0

Please sign in to comment.