Skip to content

Commit

Permalink
Merge pull request eclipse-xpanse#3 from baixinsui/feat/clean_workspace
Browse files Browse the repository at this point in the history
Add config option to delete workspaces after the deployment is done
  • Loading branch information
swaroopar authored Feb 2, 2024
2 parents 0533551 + ee79422 commit 319e802
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 28 deletions.
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,19 @@ The below property names can be changed in the following ways
1. passing the property values to the server startup command as ``--${property-name}=${property-value}``
2. Setting corresponding environment variables before starting the server.

| property name | environment variable | default value | description |
|------------------------------------|------------------------------------|--------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------|
| opentofu_binary_path | OPENTOFU_BINARY_PATH | OpenTofu available on syspath | The path to the opentofu binary |
| opentofu.root.module.directory | OPENTOFU_ROOT_MODULE_DIRECTORY | /tmp on Linux<br/>\AppData\Local\Temp on Windows | The path to the parent directory where all opentofu module directories will be stored at as subdirs |
| log.opentofu.stdout.stderr | LOG_OPENTOFU_STDOUT_STDERR | true | Controls if the command execution output must be logged. If disabled, the output is only returned in the API response |
| opentofu.log.level | OPENTOFU_LOG_LEVEL | INFO | Controls the log level of the opentofu binary. Allowed values are INFO, DEBUG, TRACE, WARN and ERROR |
| authorization.token.type | AUTHORIZATION_TOKEN_TYPE | JWT | Authorization server authentication Type, allowed values: OpaqueToken or JWT |
| authorization.server.endpoint | AUTHORIZATION_SERVER_ENDPOINT | | The endpoint value of the authorization server |
| authorization.api.client.id | AUTHORIZATION_API_CLIENT_ID | | The ID value of the authorization server API client |
| authorization.api.client.secret | AUTHORIZATION_API_CLIENT_SECRET | | The secret value of the authorization server API client |
| authorization.swagger.ui.client.id | AUTHORIZATION_SWAGGER_UI_CLIENT_ID | | The ID value of the authorization server swagger-ui client |
| otel.exporter.otlp.endpoint | OTEL_EXPORTER_OTLP_ENDPOINT | http://localhost:4317 | URL of the OTEL collector |
| property name | environment variable | default value | description |
|------------------------------------------|------------------------------------------|---------------------------------------------------|------------------------------------------------------------------------------------------------------------------------|
| opentofu_binary_path | OPENTOFU_BINARY_PATH | OpenTofu available on syspath | The path to the opentofu binary |
| opentofu.root.module.directory | OPENTOFU_ROOT_MODULE_DIRECTORY | /tmp on Linux<br/>\AppData\Local\Temp on Windows | The path to the parent directory where all opentofu module directories will be stored at as subdirs |
| log.opentofu.stdout.stderr | LOG_OPENTOFU_STDOUT_STDERR | true | Controls if the command execution output must be logged. If disabled, the output is only returned in the API response |
| opentofu.log.level | OPENTOFU_LOG_LEVEL | INFO | Controls the log level of the opentofu binary. Allowed values are INFO, DEBUG, TRACE, WARN and ERROR |
| authorization.token.type | AUTHORIZATION_TOKEN_TYPE | JWT | Authorization server authentication Type, allowed values: OpaqueToken or JWT |
| authorization.server.endpoint | AUTHORIZATION_SERVER_ENDPOINT | | The endpoint value of the authorization server |
| authorization.api.client.id | AUTHORIZATION_API_CLIENT_ID | | The ID value of the authorization server API client |
| authorization.api.client.secret | AUTHORIZATION_API_CLIENT_SECRET | | The secret value of the authorization server API client |
| authorization.swagger.ui.client.id | AUTHORIZATION_SWAGGER_UI_CLIENT_ID | | The ID value of the authorization server swagger-ui client |
| otel.exporter.otlp.endpoint | OTEL_EXPORTER_OTLP_ENDPOINT | http://localhost:4317 | URL of the OTEL collector |
| clean.workspace.after.deployment.enabled | CLEAN_WORKSPACE_AFTER_DEPLOYMENT_ENABLED | true | Whether to clean up the workspace after deployment is done,allowed values: true or false. Default value is true |

## Run Application

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.eclipse.xpanse.tofu.maker.opentofu.utils.SystemCmdResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
Expand All @@ -64,6 +65,9 @@ public class OpenTofuDirectoryService {
private final OpenTofuExecutor executor;
private final RestTemplate restTemplate;

@Value("${deployment.clean.workspace.enabled:true}")
private Boolean cleanWorkspaceAfterDeployment;

@Autowired
public OpenTofuDirectoryService(OpenTofuExecutor executor, RestTemplate restTemplate) {
this.executor = executor;
Expand Down Expand Up @@ -138,9 +142,11 @@ public OpenTofuResult deployFromDirectory(OpenTofuDeployFromDirectoryRequest req
result.setCommandStdError(tfEx.getMessage());
}
String workspace = executor.getModuleFullPath(moduleDirectory);
OpenTofuResult openTofuResult = transSystemCmdResultToOpenTofuResult(
result, workspace, null);
deleteWorkspace(workspace);
OpenTofuResult openTofuResult =
transSystemCmdResultToOpenTofuResult(result, workspace, null);
if (cleanWorkspaceAfterDeployment) {
deleteWorkspace(workspace);
}
return openTofuResult;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public void createTfStateFile(String tfState, String moduleLocation) {
openTofuExecutor.getModuleFullPath(moduleLocation)
+ File.separator
+ STATE_FILE_NAME;
try (FileWriter scriptWriter = new FileWriter(fileName)) {
boolean overwrite = new File(fileName).exists();
try (FileWriter scriptWriter = new FileWriter(fileName, overwrite)) {
scriptWriter.write(tfState);
log.info("tfState file create success, fileName: {}", fileName);
} catch (IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void asyncDestroyWithScripts(OpenTofuAsyncDestroyFromScriptsRequest reque
private void buildDeployEnv(List<String> scripts, UUID uuid) {
String workspace = executor.getModuleFullPath(uuid.toString());
buildWorkspace(workspace);
buildScriptFiles(workspace, scripts);
buildScriptFiles(workspace, uuid, scripts);
}

private void buildDestroyEnv(List<String> scripts, String tfState, UUID uuid) {
Expand All @@ -158,22 +158,24 @@ private void buildWorkspace(String workspace) {
log.info("workspace create success,Working directory is " + ws.getAbsolutePath());
}

private void buildScriptFiles(String workspace, List<String> scripts) {
private void buildScriptFiles(String workspace, UUID uuid, List<String> scripts) {
log.info("start build OpenTofu script");
if (CollectionUtils.isEmpty(scripts)) {
throw new OpenTofuExecutorException("OpenTofu scripts create error, OpenTofu "
+ "scripts not exists");
}
StringBuilder scriptBuilder = new StringBuilder();
for (String script : scripts) {
String fileName =
workspace + File.separator + UUID.randomUUID() + FILE_SUFFIX;
try (FileWriter scriptWriter = new FileWriter(fileName)) {
scriptWriter.write(script);
log.info("OpenTofu script create success, fileName: {}", fileName);
} catch (IOException ex) {
log.error("OpenTofu script create failed.", ex);
throw new OpenTofuExecutorException("OpenTofu script create failed.", ex);
}
scriptBuilder.append(script).append(System.lineSeparator());
}
String fileName = workspace + File.separator + uuid + FILE_SUFFIX;
boolean overwrite = new File(fileName).exists();
try (FileWriter scriptWriter = new FileWriter(fileName, overwrite)) {
scriptWriter.write(scriptBuilder.toString());
log.info("OpenTofu script create success, fileName: {}", fileName);
} catch (IOException ex) {
log.error("OpenTofu script create failed.", ex);
throw new OpenTofuExecutorException("OpenTofu script create failed.", ex);
}
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ opentofu.binary.location=
opentofu.log.level=INFO
otel.exporter.otlp.enabled=false
opentofu.root.module.directory=
deployment.clean.workspace.enabled=true

0 comments on commit 319e802

Please sign in to comment.