Skip to content
Snippets Groups Projects
Commit 6d2b2375 authored by ale's avatar ale
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
main.go 0 → 100644
package main
import (
"bytes"
"compress/flate"
"encoding/base64"
"encoding/json"
"flag"
"html/template"
"io"
"log"
"net/http"
)
var (
addr = flag.String("addr", ":3499", "address to listen on")
hostname = flag.String("hostname", "localhost", "public hostname")
)
var (
dashTplSrc = `<!doctype html>
<html lang="en">
<head>
<title>VMine test cluster</title>
<style type="text/css">
body { background: white; }
.name { font-size: 120%; }
.attrs { color: #666; font-size: 90%; }
.table { border: 0; width: 100%; }
</style>
</head>
<body>
<h1>VMine test cluster</h1>
<h4>Hosts</h4>
<table class="table">
<tbody>
{{$hostname := .Hostname}}
{{range $index, $host := .Inventory}}
<tr>
<td>
<b class="name">{{$host.Name}}</b> {{$host.IP}}<br>
<span class="attrs">
{{$host.CPU}} cores, {{$host.RAM}}M ram
</span>
</td>
<td>
<pre>ssh -i ~/.vagrant.d/insecure_private_key -J {{$hostname}} vagrant@{{$host.IP}}</pre>
</td>
</tr>
{{end}}
</tbody>
</table>
<h4>SOCKS proxy connection</h4>
<pre>ssh -n -L 9051:{{.SocksIP}}:9051 {{.Hostname}} &
export https_proxy=localhost:9051
</pre>
</body>
</html>`
dashTpl = template.Must(template.New("").Parse(dashTplSrc))
)
type hostinfo struct {
Name string `json:"name"`
Image string `json:"image"`
CPU float64 `json:"cpu"`
RAM float64 `json:"ram"`
IP string `json:"ip"`
}
func decompress(input []byte) ([]byte, error) {
var out bytes.Buffer
r := flate.NewReader(bytes.NewReader(input))
defer r.Close()
_, err := io.Copy(&out, r)
return out.Bytes(), err
}
func decodePayload(encoded string) ([]*hostinfo, error) {
compressed, err := base64.URLEncoding.DecodeString(encoded)
if err != nil {
return nil, err
}
decompressed, err := decompress(compressed)
if err != nil {
return nil, err
}
var inventory []*hostinfo
err = json.Unmarshal(decompressed, &inventory)
return inventory, err
}
func handleRequest(w http.ResponseWriter, req *http.Request) {
inventory, err := decodePayload(req.URL.Path)
if err != nil {
log.Printf("error decoding payload: %s", err)
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
// nolint: errcheck
dashTpl.Execute(w, struct {
Inventory []*hostinfo
SocksIP string
Hostname string
}{
Inventory: inventory,
SocksIP: inventory[0].IP,
Hostname: *hostname,
})
}
func main() {
log.SetFlags(0)
flag.Parse()
http.Handle("/dash/", http.StripPrefix("/dash/", http.HandlerFunc(handleRequest)))
log.Fatal(http.ListenAndServe(*addr, nil))
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment