Skip to content

Commit

Permalink
✨ : add terraform version management for modules
Browse files Browse the repository at this point in the history
  • Loading branch information
cdubuisson committed Jul 25, 2019
1 parent bcd4c75 commit 1a3c399
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 15 deletions.
10 changes: 10 additions & 0 deletions src/main/java/io/codeka/gaia/bo/Job.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public void setDateTime(LocalDateTime dateTime) {

private JobType jobType;

private String cliVersion;

public String getId() {
return id;
}
Expand Down Expand Up @@ -89,4 +91,12 @@ public JobType getType() {
return this.jobType;
}

public String getCliVersion() {
return cliVersion;
}

public void setCliVersion(String cliVersion) {
this.cliVersion = cliVersion;
}

}
11 changes: 11 additions & 0 deletions src/main/java/io/codeka/gaia/bo/TerraformModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class TerraformModule {
private String name;
private String description;

private String cliVersion;

public String getId() {
return id;
}
Expand Down Expand Up @@ -71,4 +73,13 @@ public List<TerraformVariable> getVariables() {
public void setVariables(List<TerraformVariable> variables) {
this.variables = variables;
}

public String getCliVersion() {
return cliVersion;
}

public void setCliVersion(String cliVersion) {
this.cliVersion = cliVersion;
}

}
6 changes: 5 additions & 1 deletion src/main/java/io/codeka/gaia/runner/StackRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ private int runContainerForJob(Job job, String script) {
// FIXME This is certainly no thread safe !
var containerConfig = containerConfigBuilder
.env(settings.env())
.image("hashicorp/terraform:" + job.getCliVersion())
.build();

// pull the image
dockerClient.pull("hashicorp/terraform:0.12.3");
dockerClient.pull("hashicorp/terraform:" + job.getCliVersion());

var containerCreation = dockerClient.createContainer(containerConfig);
var containerId = containerCreation.id();
Expand Down Expand Up @@ -102,6 +103,7 @@ private int runContainerForJob(Job job, String script) {
@Async
public void apply(Job job, TerraformModule module, Stack stack) {
this.jobs.put(job.getId(), job);
job.setCliVersion(module.getCliVersion());
job.start(JobType.RUN);

var applyScript = stackCommandBuilder.buildApplyScript(stack, module);
Expand Down Expand Up @@ -134,6 +136,7 @@ public void apply(Job job, TerraformModule module, Stack stack) {
@Async
public void plan(Job job, TerraformModule module, Stack stack) {
this.jobs.put(job.getId(), job);
job.setCliVersion(module.getCliVersion());
job.start(JobType.PREVIEW);

var planScript = stackCommandBuilder.buildPlanScript(stack, module);
Expand Down Expand Up @@ -181,6 +184,7 @@ public Job getJob(String jobId) {
@Async
public void stop(Job job, TerraformModule module, Stack stack) {
this.jobs.put(job.getId(), job);
job.setCliVersion(module.getCliVersion());
job.start(JobType.STOP);

var destroyScript = stackCommandBuilder.buildDestroyScript(stack, module);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ DockerClient client(Settings settings) {
@Bean
ContainerConfig.Builder containerConfig(){
return ContainerConfig.builder()
.image("hashicorp/terraform:0.12.3")
// bind mounting the docker sock (to be able to use docker provider in terraform)
.hostConfig(HostConfig.builder().binds(HostConfig.Bind.builder().from("/var/run/docker.sock").to("/var/run/docker.sock").build()).build())
// resetting entrypoint to empty
Expand Down
7 changes: 6 additions & 1 deletion src/main/resources/static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -551,12 +551,17 @@ a:focus {
margin-top: 0;
}

.block_head{
.block_head, .block_head_flex {
padding: 18px 25px 15px;
width: 100%;
border-bottom: solid #f3f3f3 2px;
}

.block_head_flex {
display: flex;
justify-content: space-between;
}

.block h2 {
line-height: normal;
font-size: 21px;
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/templates/job.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@
<div class="row">
<div class="col-md-12">
<div class="block">
<div class="block_head">
<div class="block_head_flex">
<h2>Stack {{stack.name}}</h2>
<img :src="'https://img.shields.io/badge/terraform-' + job.cliVersion + '-blue.svg?style=for-the-badge'" :alt="job.cliVersion" :title="job.cliVersion" />
</div>
<div class="block_content">

Expand Down
25 changes: 14 additions & 11 deletions src/main/resources/templates/module.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,35 @@ <h2> Module </h2>
<template id="template">
<div class="block">
<div class="block_head">
<h2>Module {{name}}</h2>
<h2>Module {{module.name}}</h2>
</div>

<div class="block_content">
<form v-on:submit.prevent="post">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" v-model="name">
<input type="text" class="form-control" id="name" v-model="module.name">
</div>
<div class="form-group">
<label for="name">Description</label>
<input type="text" class="form-control" id="description" v-model="description">
<label for="description">Description</label>
<input type="text" class="form-control" id="description" v-model="module.description">
</div>
<div class="form-group">
<label for="cliVersion">CLI Version</label>
<b-form-select class="form-control" id="cliVersion" v-model="module.cliVersion" :options="versions"></b-form-select>
</div>
<div class="form-group">
<label for="gitRepositoryUrl">Git Repository URL</label>
<input type="text" class="form-control" id="gitRepositoryUrl" v-model="gitRepositoryUrl">
<input type="text" class="form-control" id="gitRepositoryUrl" v-model="module.gitRepositoryUrl">
</div>
<div class="form-group">
<label for="directory">Git repository directory</label>
<input type="text" class="form-control" id="directory" v-model="directory">
<input type="text" class="form-control" id="directory" v-model="module.directory">
</div>

<h2>Variables <button type="button" class="btn btn-success" @click="addVar()">+</button></h2>

<div class="form-row align-items-end" v-for="(modVar,modVarIdx) in variables">
<div class="form-row align-items-end" v-for="(modVar,modVarIdx) in module.variables">
<div class="form-group col-md-3" >
<label for="var-name">Name: </label>
<input type="text" class="form-control" id="var-name" v-model="modVar.name">
Expand Down Expand Up @@ -104,17 +108,16 @@ <h2>Variables <button type="button" class="btn btn-success" @click="addVar()">+<
<script src="/webjars/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<script src="/webjars/vue/2.5.16/vue.js"></script>
<script src="/webjars/bootstrap-vue/2.0.0-rc.26/dist/bootstrap-vue.js"></script>

<div th:replace="helpers/messenger"></div>

<script th:inline="javascript" type="application/ecmascript">
const moduleId = [[${module.id}]];

let moduleData;

$.get(`/api/terraformModules/${moduleId}`)
.then(module => $.get('/api/terraform-cli/versions').then(versions => ({module, versions})))
.then(data => {
moduleData = data;
new Vue({
el: "#app",
data,
Expand All @@ -127,7 +130,7 @@ <h2>Variables <button type="button" class="btn btn-success" @click="addVar()">+<
});
$.ajax({
url: `/api/terraformModules/${moduleId}`,
data: JSON.stringify(moduleData),
data: JSON.stringify(data.module),
contentType: "application/json",
method: "PUT",
success: () => message.update({type: "success", message: "Module saved."}),
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/templates/modules.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ <h2>Modules</h2>
<div th:each="module: ${modules}" class="card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title" th:text="${module.name}">Card title</h5>
<img sec:authorize="hasRole('ADMIN')" th:attr="src=@{https://img.shields.io/badge/terraform-{version}-blue.svg?style=flat-square(version=${module.cliVersion})},alt=${module.cliVersion},title=${module.cliVersion}" style="margin-bottom: .75rem" />
<p class="card-text" th:text="${module.description}">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" sec:authorize="hasRole('ADMIN')" th:href="@{/modules/{moduleId}(moduleId=${module.id})}" data-toggle="tooltip" title="Edit this module" class="btn btn-primary"><i class="far fa-edit"></i></a>
<a href="#" th:href="@{/modules/{moduleId}/run(moduleId=${module.id})}" data-toggle="tooltip" title="Run this module" class="btn btn-primary"><i class="fas fa-rocket"></i></a>
Expand Down

0 comments on commit 1a3c399

Please sign in to comment.