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 4bf370c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 37 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 @@ -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 = readFileContentAndDelete(file);
fileContentMap.put(file.getName(), content);
}
}
});
}
}
return fileContentMap;
Expand Down Expand Up @@ -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;
}


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 = readFileContentAndDelete(file);
fileContentMap.put(file.getName(), content);
}
}
});
}
}
return fileContentMap;
Expand Down Expand Up @@ -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;
}


Expand Down

0 comments on commit 4bf370c

Please sign in to comment.