Skip to content

Commit

Permalink
Initial OpenTofu support (#675)
Browse files Browse the repository at this point in the history
* Initial OpenTofu support
  • Loading branch information
alfespa17 authored Jan 17, 2024
1 parent 84d476c commit 13636b1
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class ExecutorContext {
private String accessToken;
private String moduleSshKey;
private String commitId;
private boolean tofu;
private HashMap<String, String> environmentVariables;
private HashMap<String, String> variables;
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public boolean execute(Job job, String stepId, Flow flow) {
executorContext.setModuleSshKey(ssh.get().getPrivateKey());
}
}
executorContext.setTofu(job.getWorkspace().isTofu());
executorContext.setCommitId(job.getCommitId());
executorContext.setFolder(job.getWorkspace().getFolder());
executorContext.setRefresh(job.isRefresh());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public class Workspace extends GenericAuditFields {
@Column(name = "deleted")
private boolean deleted;

@Column(name = "tofu")
private boolean tofu = false;

@Column(name = "module_ssh_key")
private String moduleSshKey;

Expand Down
1 change: 1 addition & 0 deletions api/src/main/resources/db/changelog/changelog-demo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
<include file="/db/changelog/demo-data/gcp.xml"/>
<include file="/db/changelog/demo-data/simple.xml"/>
<include file="/db/changelog/demo-data/simple-tag.xml"/>
<include file="/db/changelog/demo-data/demo-tofu.xml"/>
</databaseChangeLog>
1 change: 1 addition & 0 deletions api/src/main/resources/db/changelog/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@
<include file="/db/changelog/local/changelog-2.19.0-allow-refresh.xml"/>
<include file="/db/changelog/local/changelog-2.19.0-vcs-module-ssh.xml"/>
<include file="/db/changelog/local/changelog-2.19.0-workspace-folder-length.xml"/>
<include file="/db/changelog/local/changelog-2.19.0-workspace-tofu.xml"/>
</databaseChangeLog>
9 changes: 9 additions & 0 deletions api/src/main/resources/db/changelog/demo-data/demo-tofu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.3.xsd">
<changeSet author="demo-data" id="demo-6">
<update tableName="workspace">
<column name="tofu" valueBoolean="false"/>
</update>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.3.xsd">
<changeSet id="33" author="alfespa17@gmail.com">
<addColumn tableName="workspace" >
<column name="tofu" type="boolean"/>
</addColumn>
<update tableName="workspace">
<column name="tofu" valueBoolean="false"/>
</update>
</changeSet>
</databaseChangeLog>
2 changes: 1 addition & 1 deletion executor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<properties>
<java.version>17</java.version>
<commons-text.version>1.10.0</commons-text.version>
<terraform-spring-boot-starter.version>0.11.2</terraform-spring-boot-starter.version>
<terraform-spring-boot-starter.version>0.12.0</terraform-spring-boot-starter.version>
<terrakube-client-starter.version>0.12.0</terrakube-client-starter.version>
<commons-io.version>2.8.0</commons-io.version>
<commons-codec>1.15</commons-codec>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class TerraformJob {
private boolean refreshOnly;
private String moduleSshKey;
private String commitId;
private boolean tofu;
private HashMap<String, String> environmentVariables;
private HashMap<String, String> variables;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public ExecutorJobResult plan(TerraformJob terraformJob, File workingDirectory,

scriptBeforeSuccessPlan = executePreOperationScripts(terraformJob, terraformWorkingDir, planOutput);

showTerraformMessage("PLAN", planOutput);
showTerraformMessage(terraformJob,"PLAN", planOutput);

if (scriptBeforeSuccessPlan)
if (isDestroy) {
Expand Down Expand Up @@ -180,7 +180,7 @@ public ExecutorJobResult apply(TerraformJob terraformJob, File workingDirectory)

scriptBeforeSuccess = executePreOperationScripts(terraformJob, terraformWorkingDir, applyOutput);

showTerraformMessage("APPLY", applyOutput);
showTerraformMessage(terraformJob, "APPLY", applyOutput);

if (scriptBeforeSuccess) {
TerraformProcessData terraformProcessData = getTerraformProcessData(terraformJob, terraformWorkingDir);
Expand Down Expand Up @@ -245,7 +245,7 @@ public ExecutorJobResult destroy(TerraformJob terraformJob, File workingDirector

scriptBeforeSuccess = executePreOperationScripts(terraformJob, terraformWorkingDir, outputDestroy);

showTerraformMessage("DESTROY", outputDestroy);
showTerraformMessage(terraformJob, "DESTROY", outputDestroy);

if (scriptBeforeSuccess) {
execution = terraformClient.destroy(
Expand Down Expand Up @@ -410,15 +410,19 @@ private void initBanner(TerraformJob terraformJob, Consumer<String> output) {
output.accept(
colorize("Initializing Terrakube Job " + terraformJob.getJobId() + " Step " + terraformJob.getStepId(),
colorMessage));
output.accept(colorize("Running Terraform " + terraformJob.getTerraformVersion(), colorMessage));
output.accept(colorize(String.format("Running %s ", getIaCType(terraformJob)) + terraformJob.getTerraformVersion(), colorMessage));
output.accept(colorize("\n\n" + STEP_SEPARATOR, colorMessage));
output.accept(colorize("Running Terraform Init: ", colorMessage));
output.accept(colorize(String.format("Running %s Init: ",getIaCType(terraformJob)), colorMessage));
}

private void showTerraformMessage(String operation, Consumer<String> output) throws InterruptedException {
private String getIaCType(TerraformJob terraformJob){
return terraformJob.isTofu() ? "Tofu": "Terraform";
}

private void showTerraformMessage(TerraformJob terraformJob, String operation, Consumer<String> output) throws InterruptedException {
AnsiFormat colorMessage = new AnsiFormat(GREEN_TEXT(), BLACK_BACK(), BOLD());
output.accept(colorize(STEP_SEPARATOR, colorMessage));
output.accept(colorize("Running Terraform " + operation, colorMessage));
output.accept(colorize(String.format("Running %s ",getIaCType(terraformJob)) + operation, colorMessage));
output.accept(colorize(STEP_SEPARATOR, colorMessage));
Thread.sleep(2000);
}
Expand Down Expand Up @@ -456,6 +460,7 @@ private TerraformProcessData getTerraformProcessData(TerraformJob terraformJob,
.workingDirectory(workingDirectory)
.refresh(terraformJob.isRefresh())
.refreshOnly(terraformJob.isRefreshOnly())
.tofu(terraformJob.isTofu())
.sshFile(sshKeyFile)
.build();

Expand Down
27 changes: 20 additions & 7 deletions ui/src/domain/Workspaces/Details.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ export const WorkspaceDetails = (props) => {
executionMode: values.executionMode,
moduleSshKey: values.moduleSshKey,
terraformVersion: values.terraformVersion,
tofu: values.tofu,
},
},
};
Expand Down Expand Up @@ -798,6 +799,7 @@ export const WorkspaceDetails = (props) => {
moduleSshKey: workspace.data.attributes.moduleSshKey,
executionMode:
workspace.data.attributes.executionMode,
tofu: workspace.data.attributes.tofu,
}}
layout="vertical"
name="form-settings"
Expand Down Expand Up @@ -857,15 +859,26 @@ export const WorkspaceDetails = (props) => {
</Form.Item>

<Form.Item
name="executionMode"
label="Execution Mode"
extra="Use this option with terraform remote state/cloud block if you want to execute Terraform CLI remotely and just upload the state to Terrakube"
name="tofu"
valuePropName="checked"
label="Use OpenTofu"
tooltip={{
title: "Use OpenTofu",
icon: <InfoCircleOutlined />,
}}
>
<Switch />
</Form.Item>
<Form.Item
name="executionMode"
label="Execution Mode"
extra="Use this option with terraform remote state/cloud block if you want to execute Terraform CLI remotely and just upload the state to Terrakube"
>
<Select
defaultValue={
workspace.data.attributes.executionMode
}
style={{ width: 250 }}
defaultValue={
workspace.data.attributes.executionMode
}
style={{ width: 250 }}
>
<Option key="remote">remote</Option>
<Option key="local">local</Option>
Expand Down

0 comments on commit 13636b1

Please sign in to comment.