From 72e2d63684b4db85e0d3df974e1525b5e15e6fa9 Mon Sep 17 00:00:00 2001 From: Julien WITTOUCK Date: Tue, 4 Jun 2019 08:32:35 +0200 Subject: [PATCH] :sparkles: : add envVars mgmt resolves #2 --- src/main/java/io/codeka/gaia/bo/Settings.java | 48 ++++++++++++++++++- .../gaia/controller/SettingsController.java | 4 +- .../io/codeka/gaia/runner/StackRunner.java | 11 +++-- .../gaia/runner/config/DockerConfig.java | 6 +-- src/main/resources/templates/settings.html | 30 +++++++++++- 5 files changed, 86 insertions(+), 13 deletions(-) diff --git a/src/main/java/io/codeka/gaia/bo/Settings.java b/src/main/java/io/codeka/gaia/bo/Settings.java index cda8326eb..0bc57d589 100644 --- a/src/main/java/io/codeka/gaia/bo/Settings.java +++ b/src/main/java/io/codeka/gaia/bo/Settings.java @@ -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 */ @@ -18,6 +24,11 @@ public class Settings { @Value("${:http://localhost:${server.port:8080}}") private String externalUrl; + /** + * Environment variables for the runner + */ + private List envVars = Collections.emptyList(); + public String getExternalUrl() { return externalUrl; } @@ -25,4 +36,39 @@ public String getExternalUrl() { public void setExternalUrl(String externalUrl) { this.externalUrl = externalUrl; } + + public List getEnvVars() { + return envVars; + } + + public void setEnvVars(List envVars) { + this.envVars = envVars; + } + + public List 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; + } + } } diff --git a/src/main/java/io/codeka/gaia/controller/SettingsController.java b/src/main/java/io/codeka/gaia/controller/SettingsController.java index ae41c802c..d86f4dbdd 100644 --- a/src/main/java/io/codeka/gaia/controller/SettingsController.java +++ b/src/main/java/io/codeka/gaia/controller/SettingsController.java @@ -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()); } } diff --git a/src/main/java/io/codeka/gaia/runner/StackRunner.java b/src/main/java/io/codeka/gaia/runner/StackRunner.java index 0f2d41a43..51ff53aac 100644 --- a/src/main/java/io/codeka/gaia/runner/StackRunner.java +++ b/src/main/java/io/codeka/gaia/runner/StackRunner.java @@ -29,7 +29,7 @@ public class StackRunner { private DockerClient dockerClient; - private ContainerConfig containerConfig; + private ContainerConfig.Builder containerConfigBuilder; private Settings settings; @@ -38,9 +38,9 @@ public class StackRunner { private Map 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; } @@ -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(); diff --git a/src/main/java/io/codeka/gaia/runner/config/DockerConfig.java b/src/main/java/io/codeka/gaia/runner/config/DockerConfig.java index d8c7d5c4c..3d05d9793 100644 --- a/src/main/java/io/codeka/gaia/runner/config/DockerConfig.java +++ b/src/main/java/io/codeka/gaia/runner/config/DockerConfig.java @@ -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 @@ -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()) @@ -44,8 +43,7 @@ ContainerConfig containerConfig(){ .attachStderr(true) .stdinOnce(true) .openStdin(true) - .tty(false) - .build(); + .tty(false); } } diff --git a/src/main/resources/templates/settings.html b/src/main/resources/templates/settings.html index 9c9dc7a8f..c6457e4e3 100644 --- a/src/main/resources/templates/settings.html +++ b/src/main/resources/templates/settings.html @@ -62,6 +62,24 @@

Settings

This is gaia's external URL. This is needed to allow terraform backend support. +
+

Environment Variables

+ This is the environment variables which are fed to the runner. +
+
+
+ + +
+
+ + +
+
+ +
+
+ @@ -75,11 +93,19 @@

Settings