package main import ( "fmt" "io/ioutil" "net/http" "os" "strings" "go.wit.com/lib/gui/shell" pb "go.wit.com/lib/protobuf/virtbuf" "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 log.Info("Got URL Path: ", r.URL.Path) log.Info("Got URL Query:", r.URL.Query().Get("start")) tmp = cleanURL(r.URL.Path) msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte if err != nil { fmt.Fprintln(w, "ReadAll() error =", err) return } log.Info("Got URL msg:", string(msg)) log.Info("Got URL jcarr2 tmp:", tmp) if tmp == "/" { fmt.Fprintln(w, "OK") return } if tmp == "/me" { j, err := dumpJsonClient(r) if err != nil { fmt.Fprintln(w, "BAD ZOOT") return } fmt.Fprintln(w, j) return } // exit the virtigo daemon & have systemd restart it // this can happen & when it does, access to // to libvirtd will hang (aka: virsh list will hang) // One way to trigger this is to not properly close // domain sockets opened from go-qemu/hypervisor // it's a good idea in any case so leave it here if tmp == "/kill" { log.Warn("KILLED") fmt.Fprintln(w, "KILLED") os.Exit(-1) return } if tmp == "/vms" { s := poolHypervisor(hv) fmt.Fprint(w, s) return } if tmp == "/cluster" { log.Info("/cluster jcarr actually doing START") fmt.Fprintln(w, "/cluster jcarr actually doing START") var c *pb.Cluster c = new(pb.Cluster) fmt.Fprintln(w, "START len(msg) =", len(msg)) err = c.UnmarshalJSON(msg) if err != nil { fmt.Fprintln(w, "START FAILED") fmt.Fprintln(w, "error =", err) } for _, dir := range c.Dirs { log.Info("append dir", dir) fmt.Fprintln(w, "append dir", dir) me.dirs = append(me.dirs, dir) } } if tmp == "/start" { log.Info("/start jcarr actually doing START") fmt.Fprintln(w, "/start jcarr actually doing START") start := r.URL.Query().Get("start") xml := "/tmp/" + start + ".xml" if shell.Unlink(xml) { fmt.Fprintln(w, "START FAILED") fmt.Fprintln(w, "error =", "could not remove existing xml file", xml) fmt.Fprintln(w, "error =", "file still exists after unlink") return } // fmt.Fprintln(w, "HTTP:", r.Body) var d *pb.Droplet d = new(pb.Droplet) // msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte fmt.Fprintln(w, "/start ReadAll() START") fmt.Fprintln(w, "msg =", string(msg)) fmt.Fprintln(w, "/start ReadAll() END") if err != nil { fmt.Fprintln(w, "START FAILED") fmt.Fprintln(w, "error =", err) return } fmt.Fprintln(w, "START len(msg) =", len(msg)) err = d.UnmarshalJSON(msg) if d.Hostname == "" { fmt.Fprintln(w, "START hostname is blank") log.Info("START hostname is blank") return } if err != nil { fmt.Fprintln(w, "START FAILED") fmt.Fprintln(w, "error =", err) } if err := newStart(start, d); err != nil { fmt.Fprintln(w, "START FAILED") fmt.Fprintln(w, "error =", err) return } cmd := []string{"virsh", "create", xml} fmt.Fprintln(w, "Handling URL:", tmp, "start droplet") log.Warn("cmd :", cmd) fmt.Fprintln(w, "Handling URL:", tmp, "start droplet") fmt.Fprintln(w, "cmd: ", cmd) err, ok, output := shell.RunCmd("/home/", cmd) shell.Run(cmd) if ok { fmt.Fprintln(w, "START OK") fmt.Fprintln(w, output) } else { fmt.Fprintln(w, "START FAILED") fmt.Fprintln(w, "error =", err) fmt.Fprintln(w, "output =", output) } 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) } }