Skip to content

Commit

Permalink
plan route and import fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerry Wilson committed Apr 19, 2017
1 parent b3bed72 commit 7e4c037
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 31 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Continuous integration service for [Terraform](https://terraform.io).

## Current State
* Requires terraform repositories to be checked out to a directory (CHECKOUT_DIR) and to have a terraform.tfplan

## Planned Features
* Securely stores template variables
* Generates plans when changes committed to source
Expand All @@ -11,6 +14,15 @@ Continuous integration service for [Terraform](https://terraform.io).
* Backend written in go
* Frontend vue.js app

### API Endpoints

#### Projects

* `/status` - (GET) Get service status
* `/api/projects` - (GET, PUT) List all projects, create project
* `/api/projects/{guid}` - (POST, DELETE) Update or delete projects
* `/api/plan/{guid}` - (GET) Return the current plan associated with the project guid

## Testing
* `make test`

Expand Down
33 changes: 16 additions & 17 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ import (
"strconv"

"github.com/hashicorp/logutils"
"github.com/webdevwilson/terraform-ui/persist"
"github.com/webdevwilson/terraform-ui/task"
"github.com/webdevwilson/terraform-ci/persist"
"github.com/webdevwilson/terraform-ci/task"
)

// Settings contains all the configuration values for the service
type Settings struct {
LogLevel string
SiteRoot string
Port int
Store persist.Store
Executor *task.Executor
LogLevel string
SiteRoot string
CheckoutDirectory string
Port int
Store persist.Store
Executor *task.Executor
}

var settings *Settings
Expand All @@ -33,10 +34,17 @@ func init() {

executor := task.NewExecutor(store)

workingDir, err := os.Getwd()

if err != nil {
log.Fatalf("[FATAL] Failed to get current working directory: %s", workingDir)
}

// initialize settings
settings = &Settings{
envOr("LOG_LEVEL", "INFO"),
envOrFunc("SITE_ROOT", defaultSiteRoot),
envOr("SITE_ROOT", path.Join(workingDir, "site", "dist")),
envOr("CHECKOUT_DIR", path.Join(workingDir, ".state", "projects")),
envOrInt("PORT", 3000),
store,
executor,
Expand Down Expand Up @@ -66,15 +74,6 @@ func defaultStatePath() (statePath string) {
return
}

func defaultSiteRoot() (siteRoot string) {
wd, err := os.Getwd()
if err != nil {
log.Fatalf("[FATAL] Failed to get current working directory: %s", wd)
}
siteRoot = path.Join(wd, "site", "dist")
return
}

// env returns environment variables. fatal error if it does not exist
func env(name string) (v string) {
if v = os.Getenv(name); len(v) == 0 {
Expand Down
39 changes: 34 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,49 @@ package main

import (
"log"
"path"
"path/filepath"

"fmt"

"github.com/webdevwilson/terraform-ui/config"
"github.com/webdevwilson/terraform-ui/routes"
_ "github.com/webdevwilson/terraform-ui/task"
"github.com/webdevwilson/terraform-ci/config"
"github.com/webdevwilson/terraform-ci/model"
"github.com/webdevwilson/terraform-ci/routes"
_ "github.com/webdevwilson/terraform-ci/task"
)

func main() {
port := config.Get().Port
settings := config.Get()
port := settings.Port
log.Print(fmt.Sprintf("[INFO] Listening on port %d ...", port))

routes.StartServer(port)
// this should be temporary, until we can get a proper CRUD frontend
bootstrapProjects(settings.CheckoutDirectory)

go routes.StartServer(port)

for {
}
}

func bootstrapProjects(dir string) {
dirs, err := filepath.Glob(path.Join(dir, "*"))
if err != nil {
log.Fatalf("[FATAL] Error encountered loading projects: %s", err)
}

for _, dir := range dirs {
name := path.Base(dir)
prj, err := model.GetProjectByName(name)

if err != nil {
log.Fatalf("[FATAL] Error bootstrapping projects: %s", err)
}

if prj == nil {
model.CreateProject(&model.Project{
Name: name,
})
}
}
}
27 changes: 25 additions & 2 deletions model/project.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package model

import (
"github.com/webdevwilson/terraform-ui/config"
"github.com/webdevwilson/terraform-ui/persist"
"github.com/webdevwilson/terraform-ci/config"
"github.com/webdevwilson/terraform-ci/persist"
)

const projectNS = "projects"
Expand Down Expand Up @@ -32,6 +32,7 @@ func ListProjects() (projects []Project, err error) {
projects = make([]Project, len(guids))
for i, guid := range guids {
err = store.Get(projectNS, guid, &projects[i])
projects[i].GUID = guid
if err != nil {
return
}
Expand All @@ -53,6 +54,28 @@ func GetProject(guid string) (*Project, error) {
return &prj, err
}

// GetProjectByName returns the named project
func GetProjectByName(name string) (*Project, error) {

projects, err := store.List(projectNS)
if err != nil {
return nil, err
}

for _, guid := range projects {
prj, err := GetProject(guid)

if err != nil {
return nil, err
}

if prj.Name == name {
return prj, nil
}
}
return nil, nil
}

// CreateProject
func CreateProject(prj *Project) (err error) {
var guid string
Expand Down
4 changes: 2 additions & 2 deletions routes/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"sync"

"github.com/gorilla/mux"
"github.com/webdevwilson/terraform-ui/config"
"github.com/webdevwilson/terraform-ui/persist"
"github.com/webdevwilson/terraform-ci/config"
"github.com/webdevwilson/terraform-ci/persist"
)

// Route function interface
Expand Down
2 changes: 1 addition & 1 deletion routes/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/json"

"github.com/gorilla/mux"
"github.com/webdevwilson/terraform-ui/model"
"github.com/webdevwilson/terraform-ci/model"
)

func init() {
Expand Down
4 changes: 2 additions & 2 deletions routes/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"strings"

"github.com/stretchr/testify/assert"
"github.com/webdevwilson/terraform-ui/config"
"github.com/webdevwilson/terraform-ui/model"
"github.com/webdevwilson/terraform-ci/config"
"github.com/webdevwilson/terraform-ci/model"
)

var prjs = []model.Project{
Expand Down
2 changes: 1 addition & 1 deletion routes/site.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package routes

import "github.com/webdevwilson/terraform-ui/config"
import "github.com/webdevwilson/terraform-ci/config"

func init() {
//r := Router()
Expand Down
2 changes: 1 addition & 1 deletion task/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"runtime/debug"

uuid "github.com/nu7hatch/gouuid"
"github.com/webdevwilson/terraform-ui/persist"
"github.com/webdevwilson/terraform-ci/persist"
)

const persistNamespace = "executions"
Expand Down

0 comments on commit 7e4c037

Please sign in to comment.