From 57c40d88ba727690955fda259af1f1efb0967b75 Mon Sep 17 00:00:00 2001 From: Julien WITTOUCK Date: Mon, 3 Jun 2019 20:29:29 +0200 Subject: [PATCH] :sparkles: : settings- add external url mgmt resolves #6 --- src/main/java/io/codeka/gaia/bo/Settings.java | 26 +++++ .../gaia/controller/SettingsController.java | 38 +++++++ .../gaia/controller/StackController.java | 9 +- .../io/codeka/gaia/runner/StackRunner.java | 9 +- .../resources/templates/layout/sidebar.html | 2 +- src/main/resources/templates/settings.html | 105 ++++++++++++++++++ src/main/resources/templates/stack.html | 20 ++-- .../java/io/codeka/gaia/bo/SettingsTest.java | 27 +++++ 8 files changed, 221 insertions(+), 15 deletions(-) create mode 100644 src/main/java/io/codeka/gaia/bo/Settings.java create mode 100644 src/main/java/io/codeka/gaia/controller/SettingsController.java create mode 100644 src/main/resources/templates/settings.html create mode 100644 src/test/java/io/codeka/gaia/bo/SettingsTest.java diff --git a/src/main/java/io/codeka/gaia/bo/Settings.java b/src/main/java/io/codeka/gaia/bo/Settings.java new file mode 100644 index 000000000..c1137c0c2 --- /dev/null +++ b/src/main/java/io/codeka/gaia/bo/Settings.java @@ -0,0 +1,26 @@ +package io.codeka.gaia.bo; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; + +/** + * Gaia settings + */ +@ConfigurationProperties(prefix = "gaia") +@Component +public class Settings { + + /** + * Gaia's external url (used to allow runners to speak to Gaia) + */ + private String externalUrl; + + public String getExternalUrl() { + return externalUrl; + } + + public void setExternalUrl(String externalUrl) { + this.externalUrl = externalUrl; + } +} diff --git a/src/main/java/io/codeka/gaia/controller/SettingsController.java b/src/main/java/io/codeka/gaia/controller/SettingsController.java new file mode 100644 index 000000000..ae41c802c --- /dev/null +++ b/src/main/java/io/codeka/gaia/controller/SettingsController.java @@ -0,0 +1,38 @@ +package io.codeka.gaia.controller; + +import io.codeka.gaia.bo.Settings; +import io.codeka.gaia.bo.TerraformModule; +import io.codeka.gaia.repository.TerraformModuleRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +/** + * The controller for the settings mgmt + */ +@Controller +public class SettingsController { + + private Settings settings; + + @Autowired + public SettingsController(Settings settings) { + this.settings = settings; + } + + @GetMapping("/settings") + public String settings(Model model){ + model.addAttribute("settings", settings); + + return "settings"; + } + + @PutMapping("/settings") + public String saveModule(@RequestBody Settings settings){ + // update global settings bean + this.settings.setExternalUrl( settings.getExternalUrl() ); + return "settings"; + } + +} diff --git a/src/main/java/io/codeka/gaia/controller/StackController.java b/src/main/java/io/codeka/gaia/controller/StackController.java index 0c3e348e2..c785e9d87 100644 --- a/src/main/java/io/codeka/gaia/controller/StackController.java +++ b/src/main/java/io/codeka/gaia/controller/StackController.java @@ -1,11 +1,10 @@ package io.codeka.gaia.controller; import io.codeka.gaia.bo.Job; +import io.codeka.gaia.bo.Settings; import io.codeka.gaia.repository.StackRepository; import io.codeka.gaia.repository.TerraformModuleRepository; import io.codeka.gaia.runner.StackRunner; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -25,11 +24,14 @@ public class StackController { private TerraformModuleRepository terraformModuleRepository; + private Settings settings; + @Autowired - public StackController(StackRepository stackRepository, StackRunner stackRunner, TerraformModuleRepository terraformModuleRepository) { + public StackController(StackRepository stackRepository, StackRunner stackRunner, TerraformModuleRepository terraformModuleRepository, Settings settings) { this.stackRepository = stackRepository; this.stackRunner = stackRunner; this.terraformModuleRepository = terraformModuleRepository; + this.settings = settings; } @GetMapping("/stacks") @@ -45,6 +47,7 @@ public String editStack(@PathVariable String stackId, Model model){ // TODO throw an exception (404) if not if(stackRepository.existsById(stackId)){ model.addAttribute("stackId", stackId); + model.addAttribute("settings", settings); } return "stack"; } diff --git a/src/main/java/io/codeka/gaia/runner/StackRunner.java b/src/main/java/io/codeka/gaia/runner/StackRunner.java index 7758adda6..003a6c06d 100644 --- a/src/main/java/io/codeka/gaia/runner/StackRunner.java +++ b/src/main/java/io/codeka/gaia/runner/StackRunner.java @@ -2,9 +2,9 @@ import com.spotify.docker.client.DockerClient; import com.spotify.docker.client.LogStream; -import com.spotify.docker.client.exceptions.DockerException; import com.spotify.docker.client.messages.ContainerConfig; import io.codeka.gaia.bo.Job; +import io.codeka.gaia.bo.Settings; import io.codeka.gaia.bo.Stack; import io.codeka.gaia.bo.TerraformModule; import org.apache.commons.io.IOUtils; @@ -31,12 +31,15 @@ public class StackRunner { private ContainerConfig containerConfig; + private Settings settings; + private Map jobs = new HashMap<>(); @Autowired - public StackRunner(DockerClient dockerClient, ContainerConfig containerConfig) { + public StackRunner(DockerClient dockerClient, ContainerConfig containerConfig, Settings settings) { this.dockerClient = dockerClient; this.containerConfig = containerConfig; + this.settings = settings; } @Async @@ -97,7 +100,7 @@ public void run(Job job, TerraformModule module, Stack stack) { var backendGenCommand = String.format("echo \"terraform {\n" + " backend \\\"http\\\" {\n" + - "\t\taddress=\\\"http://172.17.0.1:8080/api/state/%s\\\"\n" + + "\t\taddress=\\\""+settings.getExternalUrl()+"/api/state/%s\\\"\n" + "\t}\n" + "}\n\" > backend.tf", stack.getId()); diff --git a/src/main/resources/templates/layout/sidebar.html b/src/main/resources/templates/layout/sidebar.html index 914311a28..7d1a7712d 100644 --- a/src/main/resources/templates/layout/sidebar.html +++ b/src/main/resources/templates/layout/sidebar.html @@ -23,7 +23,7 @@

Menu

  • Dashboard
  • Modules
  • Stacks
  • - +
  • Settings
  • diff --git a/src/main/resources/templates/settings.html b/src/main/resources/templates/settings.html new file mode 100644 index 000000000..9c9dc7a8f --- /dev/null +++ b/src/main/resources/templates/settings.html @@ -0,0 +1,105 @@ + + + + + Gaia - Edit module + + + +
    +
    + +
    + + +
    + +
    + +
    +
    +
    +
    +
    +

    Gaia's Settings

    +
    +
    +
    +
    +
    +
    + +
    + +
    +
    +
    +
    +
    + +
    +
    +
    + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/stack.html b/src/main/resources/templates/stack.html index f89f014a8..f3da5233e 100644 --- a/src/main/resources/templates/stack.html +++ b/src/main/resources/templates/stack.html @@ -44,14 +44,14 @@
    -

    Stack {{name}}

    +

    Stack {{stack.name}}

    - +
    @@ -81,7 +81,7 @@

    Backend configuration

    @@ -90,7 +90,7 @@

    Backend configuration

    "terraform" : { "backend" : { "http": { - "address": "http://gaia:8080/api/state/{{id}}" + "address": "{{settings.externalUrl}}/api/state/{{stack.id}}" } } } @@ -112,7 +112,7 @@

    Provider configuration

    -
    {{ providerSpec }}
    +
    {{ stack.providerSpec }}
    @@ -130,10 +130,10 @@

    Module variables values

    -
    +
    - + {{modVar.description}}
    @@ -156,6 +156,7 @@

    Module variables values