Skip to content

Commit

Permalink
✨ : add envVars mgmt
Browse files Browse the repository at this point in the history
resolves #2
  • Loading branch information
juwit committed Jun 4, 2019
1 parent 23db361 commit 72e2d63
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 13 deletions.
48 changes: 47 additions & 1 deletion src/main/java/io/codeka/gaia/bo/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.util.Pair;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Gaia settings
*/
Expand All @@ -18,11 +24,51 @@ public class Settings {
@Value("${:http://localhost:${server.port:8080}}")
private String externalUrl;

/**
* Environment variables for the runner
*/
private List<EnvVar> envVars = Collections.emptyList();

public String getExternalUrl() {
return externalUrl;
}

public void setExternalUrl(String externalUrl) {
this.externalUrl = externalUrl;
}

public List<EnvVar> getEnvVars() {
return envVars;
}

public void setEnvVars(List<EnvVar> envVars) {
this.envVars = envVars;
}

public List<String> env() {
return this.envVars.stream()
.map(envVar -> envVar.name + "=" + envVar.value)
.collect(Collectors.toList());
}

static class EnvVar{
String name;
String value;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public String settings(Model model){
}

@PutMapping("/settings")
public String saveModule(@RequestBody Settings settings){
public void saveModule(@RequestBody Settings settings){
// update global settings bean
this.settings.setExternalUrl( settings.getExternalUrl() );
return "settings";
this.settings.setEnvVars(settings.getEnvVars());
}

}
11 changes: 7 additions & 4 deletions src/main/java/io/codeka/gaia/runner/StackRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class StackRunner {

private DockerClient dockerClient;

private ContainerConfig containerConfig;
private ContainerConfig.Builder containerConfigBuilder;

private Settings settings;

Expand All @@ -38,9 +38,9 @@ public class StackRunner {
private Map<String, Job> jobs = new HashMap<>();

@Autowired
public StackRunner(DockerClient dockerClient, ContainerConfig containerConfig, Settings settings, StackCommandBuilder stackCommandBuilder) {
public StackRunner(DockerClient dockerClient, ContainerConfig.Builder containerConfigBuilder, Settings settings, StackCommandBuilder stackCommandBuilder) {
this.dockerClient = dockerClient;
this.containerConfig = containerConfig;
this.containerConfigBuilder = containerConfigBuilder;
this.settings = settings;
this.stackCommandBuilder = stackCommandBuilder;
}
Expand All @@ -51,7 +51,10 @@ public void run(Job job, TerraformModule module, Stack stack) {
job.start();

try{
System.err.println("Create container");
// FIXME This is certainly no thread safe
var containerConfig = containerConfigBuilder.env(settings.env()).build();

System.out.println("Create container");
var containerCreation = dockerClient.createContainer(containerConfig);
var containerId = containerCreation.id();

Expand Down
6 changes: 2 additions & 4 deletions src/main/java/io/codeka/gaia/runner/config/DockerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.spotify.docker.client.messages.HostConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

/**
* Configuration of the docker client
Expand All @@ -31,7 +30,7 @@ DockerClient client() throws DockerException, InterruptedException {
}

@Bean
ContainerConfig containerConfig(){
ContainerConfig.Builder containerConfig(){
return ContainerConfig.builder()
.image("hashicorp/terraform:0.11.14")
.hostConfig(HostConfig.builder().binds(HostConfig.Bind.builder().from("/var/run/docker.sock").to("/var/run/docker.sock").build()).build())
Expand All @@ -44,8 +43,7 @@ ContainerConfig containerConfig(){
.attachStderr(true)
.stdinOnce(true)
.openStdin(true)
.tty(false)
.build();
.tty(false);
}

}
30 changes: 28 additions & 2 deletions src/main/resources/templates/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ <h2>Settings</h2>
<small>This is gaia's external URL. This is needed to allow terraform backend support.</small>
</div>

<div class="heading1">
<h2>Environment Variables <button type="button" class="btn btn-success" @click="addEnvVar()">+</button></h2>
<small>This is the environment variables which are fed to the runner.</small>
</div>
<div class="form-row align-items-end" v-for="(envVar, index) in envVars">
<div class="form-group col-md-4" >
<label for="var-name">Name: </label>
<input type="text" class="form-control" id="var-name" v-model="envVar.name">
</div>
<div class="form-group col-md-4" >
<label for="var-value">Value: </label>
<input type="text" class="form-control" id="var-value" v-model="envVar.value">
</div>
<div class="form-group col-md-1" >
<button type="button" class="form-control btn btn-danger" @click="removeEnvVar(index)">-</button>
</div>
</div>

<button class="btn btn-primary" onclick="saveSettings()"><i class="far fa-save"></i> Save</button>
</div>
</div>
Expand All @@ -75,11 +93,19 @@ <h2>Settings</h2>

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

const envVars = settings.envVars;
new Vue({
el: "#app",
data : settings,
template: "#template"
template: "#template",
methods: {
removeEnvVar(index){
settings.envVars.splice(index, 1);
},
addEnvVar(){
settings.envVars.push({});
}
}
});

function saveSettings(){
Expand Down

0 comments on commit 72e2d63

Please sign in to comment.