From 3c1efcba0e6d5757aea38b0c2067fdbb26be8105 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 26 Oct 2024 20:09:59 -0500 Subject: [PATCH] start worked by sending protobuf Signed-off-by: Jeff Carr --- event.go | 18 +++++++++++++++--- http.go | 4 ++-- main.go | 1 + post.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ structs.go | 1 + 5 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 post.go diff --git a/event.go b/event.go index 261c48b..0705595 100644 --- a/event.go +++ b/event.go @@ -51,6 +51,8 @@ func dropletReady(d *pb.Droplet) (bool, string) { return true, "" } +// this must be bool in string because accumulated output is sometimes +// written to STDOUT, sometimes to http func (h *HyperT) start(d *pb.Droplet) (bool, string) { ready, result := clusterReady() if !ready { @@ -62,9 +64,19 @@ func (h *HyperT) start(d *pb.Droplet) (bool, string) { } url := "http://" + h.pb.Hostname + ":2520/start?start=" + d.Hostname - s := shell.Wget(url) + var msg string + var data []byte + msg = d.FormatJSON() + data = []byte(msg) // Convert the string to []byte + req, err := httpPost(url, data) + if err != nil { + return false, fmt.Sprintln("error:", err) + } + log.Info("http post url:", url) + log.Info("http post data:", msg) + result = "EVENT start droplet url: " + url + "\n" - result += "EVENT start droplet response: " + s.String() + result += "EVENT start droplet response: " + string(req) // increment the counter for a start attempt working d.Starts += 1 @@ -99,7 +111,7 @@ func Start(name string) (bool, string) { dur := time.Since(me.unstable) // how long has the cluster been stable? result = fmt.Sprintln("should start droplet", name, "here. grid stable for:", shell.FormatDuration(dur)) - if dur < 17*time.Second { + if dur < me.unstableTimeout { result += "grid is still too unstable" return false, result } diff --git a/http.go b/http.go index cb3f35a..684c9a8 100644 --- a/http.go +++ b/http.go @@ -5,9 +5,9 @@ import ( "net/http" "strings" + pb "go.wit.com/lib/protobuf/virtbuf" "go.wit.com/lib/virtigoxml" "go.wit.com/log" - pb "go.wit.com/lib/protobuf/virtbuf" ) // remove '?' part and trailing '/' @@ -104,7 +104,7 @@ func okHandler(w http.ResponseWriter, r *http.Request) { } log.Warn("BAD URL =", tmp) - fmt.Fprintln(w, "BAD URL", tmp) + fmt.Fprintln(w, "BAD URL tmp =", tmp) } // write a file out to the http socket diff --git a/main.go b/main.go index c484e8a..db6449c 100644 --- a/main.go +++ b/main.go @@ -43,6 +43,7 @@ func main() { me.changed = false // me.dmap = make(map[*pb.Droplet]*DropletT) me.hmap = make(map[*pb.Hypervisor]*HyperT) + me.unstableTimeout = 17 * time.Second // read in the config file me.cluster = new(pb.Cluster) diff --git a/post.go b/post.go new file mode 100644 index 0000000..9537bc8 --- /dev/null +++ b/post.go @@ -0,0 +1,49 @@ +package main + +import ( + "bytes" + "io/ioutil" + "net/http" + "os" + "os/user" + "strings" + + "go.wit.com/log" +) + +func httpPost(url string, data []byte) ([]byte, error) { + var err error + var req *http.Request + + // data := []byte("some junk") + // url := "https://go.wit.com/register/" + + req, err = http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data)) + + usr, _ := user.Current() + req.Header.Set("author", usr.Username) + hostname, _ := os.Hostname() + req.Header.Set("hostname", hostname) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + log.Error(err) + return nil, err + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Error(err) + return body, err + } + + test := strings.TrimSpace(string(body)) + log.Info("go.wit.com returned body:", test) + if test == "OK" { + return body, nil + } + + return body, nil +} diff --git a/structs.go b/structs.go index a19c2ba..d2dd8cb 100644 --- a/structs.go +++ b/structs.go @@ -31,6 +31,7 @@ type virtigoT struct { killcount int unstable time.Time // the last time the cluster was incorrect changed bool + unstableTimeout time.Duration // dirs []string // all the paths too search for a qcow image }