diff --git a/Makefile b/Makefile index 98c75d6..ce95f92 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ REDOMOD = $(shell if [ -e go.sum ]; then echo go.sum exists; else GO111MODULE= all: GO111MODULE=off go build -v -ldflags "-X main.Version=${VERSION} -X gui.GUIVERSION=${VERSION}" ./virtigo --version - ./virtigo --hosts farm01,farm02,farm03 + ./virtigo --hosts farm01 farm02 farm03 # this is for release builds using the go.mod files release-build: @@ -33,4 +33,5 @@ clean: git-clone: go-clone --recursive --go-src --no-work go.wit.com/apps/go-clone go-clone --recursive --go-src --no-work go.wit.com/apps/virtigo + go-clone --recursive --go-src --no-work go.wit.com/apps/gowebd go-clone --recursive --go-src --no-work go.wit.com/lib/daemons/virtigod diff --git a/argv.go b/argv.go index 101caa1..7221523 100644 --- a/argv.go +++ b/argv.go @@ -6,21 +6,19 @@ package main this enables command line options from other packages like 'gui' and 'log' */ +var argv args + type args struct { - DryRun bool `arg:"--dry-run" help:"show what would be run"` - Uptime bool `arg:"--uptime" help:"enable port 8080 for use with uptime checks like Kuma"` - Hosts []string `arg:"--hosts" help:"hosts to connect to"` + Hosts []string `arg:"--hosts" help:"hosts to connect to"` + Uptime bool `arg:"--uptime" default:"true" help:"allow uptime checks for things like Kuma"` + Port int `arg:"--port" default:"8080" help:"specify a different default port"` } func (a args) Description() string { return ` virtigo will help control your cluster of hypervisiors -Install: - apt install virtigo - -Sources: - go-clone --recursive --no-work -go-src go.wit.com/apps/virtigo +go install go.wit.com/apps/virtigo@latest ` } diff --git a/http.go b/http.go new file mode 100644 index 0000000..bdb11b5 --- /dev/null +++ b/http.go @@ -0,0 +1,90 @@ +package main + +import ( + "fmt" + "net/http" + "strings" + + "go.wit.com/lib/gui/shell" + "go.wit.com/log" +) + +// remove '?' part and trailing '/' +func cleanURL(url string) string { + url = "/" + strings.Trim(url, "/") + return url +} + +func okHandler(w http.ResponseWriter, r *http.Request) { + var tmp string + tmp = cleanURL(r.URL.Path) + + log.Info("Got URL:", tmp) + if tmp == "/" { + fmt.Fprintln(w, "OK") + return + } + if tmp == "/me" { + fmt.Fprintln(w, "OK") + return + } + + if tmp == "/vms" { + s := shell.Wget("http://wilddog:2520/vms") + var bytesSplice []byte + bytesSplice = s.Bytes() + fmt.Fprintln(w, string(bytesSplice)) + return + } + + if tmp == "/favicon.ico" { + writeFile(w, "ipv6.png") + return + } + if tmp == "/uptime" { + writeFile(w, "uptime.html") + return + } + + // used for uptime monitor checking (like Kuma) + if tmp == "/uptime" { + writeFile(w, "uptime.html") + return + } + log.Warn("BAD URL =", tmp) + fmt.Fprintln(w, "BAD ZOOT") + // badurl(w, r.URL.String()) + // fmt.Fprintln(w, "BAD", tmp) +} + +func writeFile(w http.ResponseWriter, filename string) { + // fmt.Fprintln(w, "GOT TEST?") + fullname := "resources/" + filename + pfile, err := resources.ReadFile(fullname) + if err != nil { + log.Println("ERROR:", err) + // w.Write(pfile) + return + } + + var repohtml string + repohtml = string(pfile) + if filename == "goReference.svg" { + w.Header().Set("Content-Type", "image/svg+xml") + } + fmt.Fprintln(w, repohtml) + log.Println("writeFile() found internal file:", filename) +} + +// starts and sits waiting for HTTP requests +func startHTTP() { + http.HandleFunc("/", okHandler) + + p := fmt.Sprintf(":%d", argv.Port) + log.Println("Running on port", p) + + err := http.ListenAndServe(p, nil) + if err != nil { + log.Println("Error starting server:", err) + } +} diff --git a/main.go b/main.go index 96b5223..bf56a04 100644 --- a/main.go +++ b/main.go @@ -3,23 +3,31 @@ package main import ( -// "log" + // "log" + "embed" "os" - "go.wit.com/log" "go.wit.com/dev/alexflint/arg" + "go.wit.com/log" ) var Version string -var myargs args + +//go:embed resources/* +var resources embed.FS func main() { - pp := arg.MustParse(&myargs) + pp := arg.MustParse(&argv) - if myargs.Uptime { + if ! argv.Uptime { pp.WriteHelp(os.Stdout) os.Exit(0) } - log.Info("connect to cluser here", myargs.Hosts) + log.Info("connect to cluser here", argv.Hosts) + for i, s := range argv.Hosts { + log.Info("i, s =", i, s) + } + + startHTTP() } diff --git a/resources/goReference.svg b/resources/goReference.svg new file mode 100644 index 0000000..99e2d73 --- /dev/null +++ b/resources/goReference.svg @@ -0,0 +1 @@ + \ No newline at end of file