Skip to content

Commit

Permalink
✨ : generate tfvar file for the runner
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Sep 11, 2020
1 parent 6a6bb77 commit 8bb0b86
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 304 deletions.
25 changes: 25 additions & 0 deletions src/main/java/io/gaia_app/runner/RunnerController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.gaia_app.runner;

import io.gaia_app.stacks.repository.StackRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* Controller for the operations that are called by the runner only
*/
@RestController
@RequestMapping("/api/runner")
public class RunnerController {

@Autowired
private StackRepository stackRepository;

@GetMapping(value = "/stacks/{id}.tfvars", produces = "text/plain")
public String tfvars(@PathVariable String id){
var stack = stackRepository.findById(id).orElseThrow();
return stack.tfvars();
}
}
80 changes: 6 additions & 74 deletions src/main/java/io/gaia_app/runner/StackCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,13 @@
import io.gaia_app.stacks.bo.Job;
import io.gaia_app.stacks.bo.Stack;
import io.gaia_app.stacks.bo.mustache.TerraformScript;
import io.gaia_app.modules.bo.TerraformModule;
import io.gaia_app.stacks.bo.Job;
import io.gaia_app.stacks.bo.Stack;
import io.gaia_app.stacks.bo.mustache.TerraformScript;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.function.BiFunction;

/**
* A builder class to create stack commands
Expand Down Expand Up @@ -56,8 +51,7 @@ private String evalGitRepositoryUrl(TerraformModule module) {
.orElse(url);
}

private String buildScript(Job job, Stack stack, TerraformModule module,
BiFunction<Stack, TerraformModule, String> command) {
private String buildScript(Job job, Stack stack, TerraformModule module, String command) {
var script = new TerraformScript()
.setExternalUrl(settings.getExternalUrl())
.setStateApiUser(runnerApiSecurityProperties.getUsername())
Expand All @@ -70,7 +64,7 @@ private String buildScript(Job job, Stack stack, TerraformModule module,
script.setGitDirectory(module.getDirectory());
}

script.setCommand(command.apply(stack, module));
script.setCommand(command);

var writer = new StringWriter();
try {
Expand All @@ -82,31 +76,13 @@ private String buildScript(Job job, Stack stack, TerraformModule module,
return StringUtils.EMPTY;
}

private String buildCommand(Stack stack, TerraformModule module, String command) {
var varFormatString = "-var \"%s=%s\" ";
var variablesBuilder = new StringBuilder();

module.getVariables().forEach(terraformVariable -> {

var name = terraformVariable.getName();
String value = terraformVariable.getDefaultValue();
// try getting the value from the stack
if (stack.getVariableValues().containsKey(name)) {
value = stack.getVariableValues().get(name);
}
variablesBuilder.append(String.format(varFormatString, name, value));
});

return String.format("%s %s", command, variablesBuilder.toString());
}

/**
* builds the terraform plan script
*
* @return
*/
String buildPlanScript(Job job, Stack stack, TerraformModule module) {
return buildScript(job, stack, module, this::buildPlanCommand);
return buildScript(job, stack, module, "terraform plan -detailed-exitcode");
}

/**
Expand All @@ -115,7 +91,7 @@ String buildPlanScript(Job job, Stack stack, TerraformModule module) {
* @return
*/
String buildApplyScript(Job job, Stack stack, TerraformModule module) {
return buildScript(job, stack, module, this::buildApplyCommand);
return buildScript(job, stack, module, "terraform apply -auto-approve");
}

/**
Expand All @@ -124,7 +100,7 @@ String buildApplyScript(Job job, Stack stack, TerraformModule module) {
* @return
*/
String buildPlanDestroyScript(Job job, Stack stack, TerraformModule module) {
return buildScript(job, stack, module, this::buildPlanDestroyCommand);
return buildScript(job, stack, module, "terraform plan -destroy -detailed-exitcode");
}

/**
Expand All @@ -133,51 +109,7 @@ String buildPlanDestroyScript(Job job, Stack stack, TerraformModule module) {
* @return
*/
String buildDestroyScript(Job job, Stack stack, TerraformModule module) {
return buildScript(job, stack, module, this::buildDestroyCommand);
}

/**
* builds the terraform plan command
*
* @param stack
* @param module
* @return
*/
String buildPlanCommand(Stack stack, TerraformModule module) {
return buildCommand(stack, module, "terraform plan -detailed-exitcode");
}

/**
* builds the terraform apply command
*
* @param stack
* @param module
* @return
*/
String buildApplyCommand(Stack stack, TerraformModule module) {
return buildCommand(stack, module, "terraform apply -auto-approve");
}

/**
* builds the terraform plan destroy command
*
* @param stack
* @param module
* @return
*/
String buildPlanDestroyCommand(Stack stack, TerraformModule module) {
return buildCommand(stack, module, "terraform plan -destroy -detailed-exitcode");
}

/**
* builds the terraform destroy command
*
* @param stack
* @param module
* @return
*/
String buildDestroyCommand(Stack stack, TerraformModule module) {
return buildCommand(stack, module, "terraform destroy -auto-approve");
return buildScript(job, stack, module, "terraform destroy -auto-approve");
}

}
20 changes: 20 additions & 0 deletions src/main/java/io/gaia_app/stacks/bo/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,24 @@ public String getCredentialsId() {
public void setCredentialsId(String credentialsId) {
this.credentialsId = credentialsId;
}

/**
* Builds tfvars file for this stack
* @return a string with the content of the tfvars file
*/
public String tfvars() {
var varLine = "%s = \"%s\"\n";
var variablesBuilder = new StringBuilder();

module.getVariables().forEach(terraformVariable -> {
var variableName = terraformVariable.getName();
var variableValue = terraformVariable.getDefaultValue();
// try getting the value
if (this.variableValues.containsKey(variableName)) {
variableValue = this.variableValues.get(variableName);
}
variablesBuilder.append(String.format(varLine, variableName, variableValue));
});
return variablesBuilder.toString();
}
}
6 changes: 6 additions & 0 deletions src/main/resources/mustache/terraform.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ set -e

echo '[gaia] using image {{terraformImage}}'

echo '[gaia] installing curl'
apk -q add curl

echo '[gaia] cloning {{gitRepositoryUrl}}' | awk '{ sub(/oauth2:(.*)@/, "oauth2:[MASKED]@");}1'
git clone {{gitRepositoryUrl}} module

Expand All @@ -19,6 +22,9 @@ echo 'terraform {
}
}' > backend.tf

echo '[gaia] generating tfvars variable file'
curl --silent --user {{stateApiUser}}:{{stateApiPassword}} {{externalUrl}}/api/runner/stacks/{{stackId}}.tfvars > gaia.auto.tfvars

echo '[gaia] running terraform init'
terraform version
terraform init
Expand Down

0 comments on commit 8bb0b86

Please sign in to comment.