start worked by sending protobuf

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-26 20:09:59 -05:00
parent 7837182d53
commit 3c1efcba0e
5 changed files with 68 additions and 5 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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)

49
post.go Normal file
View File

@ -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
}

View File

@ -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
}