Skip to content

Commit

Permalink
✨ : add stack mgmt
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Jun 4, 2019
1 parent 9b18891 commit 0e8a14d
Show file tree
Hide file tree
Showing 18 changed files with 930 additions and 2 deletions.
23 changes: 23 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,34 @@
<artifactId>font-awesome</artifactId>
<version>5.8.2</version>
</dependency>
<dependency>
<groupId>org.webjars.npm</groupId>
<artifactId>ace-builds</artifactId>
<version>1.4.4</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>

<dependency>
<groupId>com.spotify</groupId>
<artifactId>docker-client</artifactId>
<version>8.16.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.27</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.18</version>
</dependency>

</dependencies>

<build>
Expand Down
25 changes: 24 additions & 1 deletion src/main/java/io/codeka/gaia/Gaia.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.codeka.gaia;

import io.codeka.gaia.bo.Stack;
import io.codeka.gaia.bo.TerraformModule;
import io.codeka.gaia.bo.TerraformVariable;
import io.codeka.gaia.repository.StackRepository;
import io.codeka.gaia.repository.TerraformModuleRepository;
import io.codeka.gaia.repository.TerraformStateRepository;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -12,6 +14,7 @@
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import java.util.List;
import java.util.UUID;

@SpringBootApplication
@EnableMongoRepositories
Expand All @@ -23,10 +26,13 @@ public static void main(String[] args) {

@Bean
@Autowired
CommandLineRunner cli(TerraformModuleRepository repository, TerraformStateRepository terraformStateRepository){
CommandLineRunner cli(TerraformModuleRepository repository,
TerraformStateRepository terraformStateRepository,
StackRepository stackRepository){
return args -> {
repository.deleteAll();
terraformStateRepository.deleteAll();
stackRepository.deleteAll();

// create dummy module for tests
var module = new TerraformModule();
Expand All @@ -49,6 +55,23 @@ CommandLineRunner cli(TerraformModuleRepository repository, TerraformStateReposi
module.setVariables(List.of(tvar, tvar2));

repository.saveAll(List.of(module));


var stack = new Stack();
stack.setId("5a215b6b-fe53-4afa-85f0-a10175a7f264");
stack.setName("mongo-instance-1");
stack.setModuleId("e01f9925-a559-45a2-8a55-f93dc434c676");
stack.getVariableValues().put("mongo_exposed_port", "27117");
stack.setProviderSpec("provider \"poulp\" {\n" +
" host = \"unix:///var/run/docker.sock\"\n" +
"}");
stackRepository.save(stack);

var stack2 = new Stack();
stack2.setId("143773fa-4c2e-4baf-a7fb-79d23e01c5ca");
stack2.setName("mongo-instance-2");
stack2.setModuleId("e01f9925-a559-45a2-8a55-f93dc434c676");
stackRepository.save(stack2);
};
}

Expand Down
80 changes: 80 additions & 0 deletions src/main/java/io/codeka/gaia/bo/Stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package io.codeka.gaia.bo;

import io.codeka.gaia.bo.backend.Backend;

import java.util.HashMap;
import java.util.Map;

/**
* This class represents a terraform modules instances.
*
* It references the module it is based on, with all the values of its variables.
* It also has a backend configuration, and a provider configuration (in terraform terms).
*/
public class Stack {

/**
* This stack's id
*/
private String id;

/**
* The id of the referenced module
*/
private String moduleId;

/**
* The variable values of the module
*/
private Map<String, String> variableValues = new HashMap<>();

/**
* The name of the stack
*/
private String name;

/**
* The provider spec
*/
private String providerSpec;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getModuleId() {
return moduleId;
}

public void setModuleId(String moduleId) {
this.moduleId = moduleId;
}

public Map<String, String> getVariableValues() {
return variableValues;
}

public void setVariableValues(Map<String, String> variableValues) {
this.variableValues = variableValues;
}

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

public String getName() {
return name;
}

public String getProviderSpec() {
return providerSpec;
}

public void setProviderSpec(String providerSpec) {
this.providerSpec = providerSpec;
}
}
20 changes: 20 additions & 0 deletions src/main/java/io/codeka/gaia/bo/TerraformBackend.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.codeka.gaia.bo;

import io.codeka.gaia.bo.backend.Terraform;

public class TerraformBackend {

private Terraform terraform;

public Terraform getTerraform() {
if(terraform == null){
terraform = new Terraform();
}
return terraform;
}

public void setTerraform(Terraform terraform) {
this.terraform = terraform;
}

}
16 changes: 16 additions & 0 deletions src/main/java/io/codeka/gaia/bo/backend/Backend.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.codeka.gaia.bo.backend;

public class Backend {
private Http http;

public Http getHttp() {
if(http == null){
http = new Http();
}
return http;
}

public void setHttp(Http http) {
this.http = http;
}
}
13 changes: 13 additions & 0 deletions src/main/java/io/codeka/gaia/bo/backend/Http.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.codeka.gaia.bo.backend;

public class Http {
private String address;

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}
}
16 changes: 16 additions & 0 deletions src/main/java/io/codeka/gaia/bo/backend/Terraform.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.codeka.gaia.bo.backend;

public class Terraform {
private Backend backend;

public Backend getBackend() {
if(backend == null){
backend = new Backend();
}
return backend;
}

public void setBackend(Backend backend) {
this.backend = backend;
}
}
16 changes: 16 additions & 0 deletions src/main/java/io/codeka/gaia/config/RepositoryConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.codeka.gaia.config;

import io.codeka.gaia.bo.Stack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;

@Configuration
public class RepositoryConfig {

@Autowired
public void configureRest(RepositoryRestConfiguration repositoryRestConfiguration){
repositoryRestConfiguration.exposeIdsFor(Stack.class);

}
}
45 changes: 45 additions & 0 deletions src/main/java/io/codeka/gaia/controller/StackController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.codeka.gaia.controller;

import io.codeka.gaia.repository.StackRepository;
import io.codeka.gaia.repository.TerraformModuleRepository;
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;
import org.springframework.web.bind.annotation.*;


@Controller
public class StackController {

private TerraformModuleRepository terraformModuleRepository;

private StackRepository stackRepository;

private Logger log = LoggerFactory.getLogger(StackController.class);

@Autowired
public StackController(TerraformModuleRepository terraformModuleRepository, StackRepository stackRepository) {
this.terraformModuleRepository = terraformModuleRepository;
this.stackRepository = stackRepository;
}

@GetMapping("/stacks")
public String listStacks(Model model){
model.addAttribute("stacks", stackRepository.findAll());

return "stacks";
}

@GetMapping("/stacks/{stackId}")
public String editStack(@PathVariable String stackId, Model model){
// checking if the stack exists
// TODO throw an exception (404) if not
if(stackRepository.existsById(stackId)){
model.addAttribute("stackId", stackId);
}
return "stack";
}

}
12 changes: 12 additions & 0 deletions src/main/java/io/codeka/gaia/repository/StackRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.codeka.gaia.repository;

import io.codeka.gaia.bo.Stack;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

/**
* Repository for stacks
*/
@RepositoryRestResource
public interface StackRepository extends MongoRepository<Stack, String>{
}
16 changes: 16 additions & 0 deletions src/main/java/io/codeka/gaia/service/RunTerraformService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.codeka.gaia.service;

import com.spotify.docker.client.DefaultDockerClient;
import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.exceptions.DockerCertificateException;
import com.spotify.docker.client.exceptions.DockerException;
import org.springframework.stereotype.Service;

@Service
public class RunTerraformService {


public void runTerraform() throws DockerException, InterruptedException, DockerCertificateException {

}
}
Loading

0 comments on commit 0e8a14d

Please sign in to comment.