Skip to content

Commit

Permalink
Add standalone mode
Browse files Browse the repository at this point in the history
  • Loading branch information
im2nguyen committed Jul 9, 2021
1 parent ac1b0af commit afa64cc
Show file tree
Hide file tree
Showing 21 changed files with 312 additions and 22,700 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
rover
rover
.DS_Store
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ $ docker run --rm -it -p 9000:9000 -v $(pwd):/src im2nguyen/rover

Once Rover runs on `localhost:9000`, navigate to it to find the visualization!

## Standalone mode

Standalone mode generates a `rover.zip` file containing all the static assets.

```
$ docker run --rm -it -p 9000:9000 -v $(pwd):/src im2nguyen/rover -standalone true
```

After all the assets are generated, unzip `rover.zip` and open `rover/index.html` in your favorite web browser.

## Set environment variables

Use `--env` or `--env-file` to set environment variables in the Docker container. For example, you can save your AWS credentials to an `.env` file.

```
Expand All @@ -45,6 +57,8 @@ Then, add it as environment variables to your Docker container with `--env-file`
$ docker run --rm -it -p 9000:9000 -v $(pwd):/src --env-file ./.env im2nguyen/rover
```

## Define tfvars and Terraform variables

Use `-tfVarsFile` or `-tfVar` to define variables. For example, you can run the following in the `example/random-test` directory to overload variables.

```
Expand Down
73 changes: 22 additions & 51 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package main

import (
"bytes"
"context"
"embed"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/fs"
"io/ioutil"
"log"
Expand Down Expand Up @@ -44,11 +42,14 @@ func (i *arrayFlags) Set(value string) error {
func main() {
log.Println("Starting Rover...")

var tfPath, workingDir, name string
var tfPath, workingDir, name, zipFileName string
var standalone bool
var tfVarsFiles, tfVars arrayFlags
flag.StringVar(&tfPath, "tfPath", "/usr/local/bin/terraform", "Path to Terraform binary")
flag.StringVar(&workingDir, "workingDir", ".", "Path to Terraform configuration")
flag.StringVar(&name, "name", "rover", "Configuration name")
flag.StringVar(&zipFileName, "zipFileName", "rover", "Standalone zip file name")
flag.BoolVar(&standalone, "standalone", false, "Generate standalone HTML files")
flag.Var(&tfVarsFiles, "tfVarsFile", "Path to *.tfvars files")
flag.Var(&tfVars, "tfVar", "Terraform variable (key=value)")
flag.Parse()
Expand All @@ -58,6 +59,7 @@ func main() {

// Generate assets
plan, rso, mapDM, graph := generateAssets(name, workingDir, tfPath, parsedTfVarsFiles, parsedTfVars)
log.Println("Done generating assets.")

// Save to file (debug)
// saveJSONToFile(name, "plan", "output", plan)
Expand All @@ -66,58 +68,29 @@ func main() {
// saveJSONToFile(name, "graph", "output", graph)

// Embed frontend
stripped, err := fs.Sub(frontend, "ui/dist")
fe, err := fs.Sub(frontend, "ui/dist")
if err != nil {
log.Fatalln(err)
}
frontendFS := http.FileServer(http.FS(stripped))

http.Handle("/", frontendFS)
http.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
fileType := strings.Replace(r.URL.Path, "/api/", "", 1)

var j []byte
var err error

enableCors(&w)

switch fileType {
case "plan":
j, err = json.Marshal(plan)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error producing JSON: %s\n", err))
}
case "rso":
j, err = json.Marshal(rso)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error producing JSON: %s\n", err))
}
case "map":
j, err = json.Marshal(mapDM)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error producing JSON: %s\n", err))
}
case "graph":
j, err = json.Marshal(graph)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error producing JSON: %s\n", err))
}
default:
io.WriteString(w, "Please enter a valid file type: plan, rso, map, graph\n")
}
frontendFS := http.FileServer(http.FS(fe))

w.Header().Set("Content-Type", "application/json")
io.Copy(w, bytes.NewReader(j))
})
if standalone {
err = generateZip(fe,
fmt.Sprintf("%s.zip", zipFileName),
plan, rso, mapDM, graph,
)
if err != nil {
log.Fatalln(err)
}

log.Println("Done generating assets.")
log.Println("Rover is running on localhost:9000")
log.Printf("Generated zip file: %s.zip\n", zipFileName)
return
}

err = http.ListenAndServe(":9000", nil)
err = startServer(frontendFS, plan, rso, mapDM, graph)
if err != nil {
log.Fatalf("Could not start server: %s\n", err.Error())
}

}

func generateAssets(name string, workingDir string, tfPath string, tfVarsFiles []string, tfVars []string) (*tfjson.Plan, *ResourcesOverview, *Map, Graph) {
Expand Down Expand Up @@ -234,17 +207,15 @@ func saveJSONToFile(prefix string, fileType string, path string, j interface{})
}

f, err := os.Create(fmt.Sprintf("%s/%s-%s.json", newpath, prefix, fileType))

if err != nil {
log.Fatal(err)
}

defer f.Close()

_, err2 := f.WriteString(string(b))

if err2 != nil {
log.Fatal(err2)
_, err = f.WriteString(string(b))
if err != nil {
log.Fatal(err)
}

// log.Printf("Saved to %s", fmt.Sprintf("%s/%s-%s.json", newpath, prefix, fileType))
Expand Down
64 changes: 64 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"

tfjson "github.com/hashicorp/terraform-json"
)

func startServer(frontendFS http.Handler, plan *tfjson.Plan, rso *ResourcesOverview, mapDM *Map, graph Graph) error {
http.Handle("/", frontendFS)
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
// simple healthcheck
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"alive": true}`)

})
http.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
fileType := strings.Replace(r.URL.Path, "/api/", "", 1)

var j []byte
var err error

enableCors(&w)

switch fileType {
case "plan":
j, err = json.Marshal(plan)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error producing plan JSON: %s\n", err))
}
case "rso":
j, err = json.Marshal(rso)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error producing rso JSON: %s\n", err))
}
case "map":
j, err = json.Marshal(mapDM)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error producing map JSON: %s\n", err))
}
case "graph":
j, err = json.Marshal(graph)
if err != nil {
io.WriteString(w, fmt.Sprintf("Error producing graph JSON: %s\n", err))
}
default:
io.WriteString(w, "Please enter a valid file type: plan, rso, map, graph\n")
}

w.Header().Set("Content-Type", "application/json")
io.Copy(w, bytes.NewReader(j))
})

log.Println("Rover is running on localhost:9000")

return http.ListenAndServe(":9000", nil)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ui/dist/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html lang=""><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="/favicon.ico"><title>ui</title><link rel="stylesheet" href="/chota.min.css"><link rel="stylesheet" href="/style.css"><link href="/css/app.22910c3c.css" rel="preload" as="style"><link href="/js/app.20524777.js" rel="preload" as="script"><link href="/js/chunk-vendors.bd6ebaf4.js" rel="preload" as="script"><link href="/css/app.22910c3c.css" rel="stylesheet"></head><body><noscript><strong>We're sorry but ui doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div><script src="/js/chunk-vendors.bd6ebaf4.js"></script><script src="/js/app.20524777.js"></script></body></html>
<!DOCTYPE html><html lang=""><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="/favicon.ico"><title>ui</title><link rel="stylesheet" href="/chota.min.css"><link rel="stylesheet" href="/style.css"><link href="/css/app.f05e9f06.css" rel="preload" as="style"><link href="/js/app.8888d743.js" rel="preload" as="script"><link href="/js/chunk-vendors.bd6ebaf4.js" rel="preload" as="script"><link href="/css/app.f05e9f06.css" rel="stylesheet"></head><body><noscript><strong>We're sorry but ui doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div><script src="/js/chunk-vendors.bd6ebaf4.js"></script><script src="/js/app.8888d743.js"></script></body></html>
2 changes: 0 additions & 2 deletions ui/dist/js/app.20524777.js

This file was deleted.

1 change: 0 additions & 1 deletion ui/dist/js/app.20524777.js.map

This file was deleted.

2 changes: 2 additions & 0 deletions ui/dist/js/app.8888d743.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions ui/dist/js/app.8888d743.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit afa64cc

Please sign in to comment.