create and start work again

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-11-16 05:21:44 -06:00
parent 5ea2e5999b
commit 70cc9944ad
3 changed files with 48 additions and 1 deletions

35
http.go
View File

@ -2,10 +2,12 @@ package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"go.wit.com/lib/protobuf/virtbuf"
"go.wit.com/lib/virtigolib"
"go.wit.com/log"
)
@ -22,6 +24,11 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
log.HttpMode(w)
defer log.HttpMode(nil)
msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
if err != nil {
log.Info("ReadAll() error =", err)
return
}
if route == "/uptime" {
ok, s := uptimeCheck()
log.Info(s)
@ -33,6 +40,34 @@ func okHandler(w http.ResponseWriter, r *http.Request) {
return
}
if route == "/create" {
var d *virtbuf.Droplet
d = new(virtbuf.Droplet)
if err := d.Unmarshal(msg); err != nil {
log.Info("proto.Unmarshal() failed on wire message len", len(msg))
log.Info("error =", err)
return
}
log.Info("proto.Unmarshal() worked on msg len", len(msg), "hostname =", d.Hostname)
found := me.cluster.FindDropletByName(d.Hostname)
if found != nil {
log.Info("already have hostname ", d.Hostname)
return
}
log.Info("new hostname ", d.Hostname)
if !me.cluster.AddDroplet(d) {
log.Info("new hostname added ok ", d.Hostname)
} else {
log.Info("hostname add failed for ", d.Hostname)
}
if err := me.cluster.ConfigSave(); err != nil {
log.Info("configsave error", err)
os.Exit(-1)
}
log.Info("config file saved")
return
}
if route == "/start" {
hostname := r.URL.Query().Get("hostname")
if hostname == "" {

View File

@ -11,6 +11,7 @@ import (
"time"
pb "go.wit.com/lib/protobuf/virtbuf"
"go.wit.com/log"
)
func isClusterStable() (string, error) {
@ -45,6 +46,7 @@ func Start(name string) (string, error) {
// validate the droplet
if err := ValidateDroplet(d); err != nil {
log.Info("ValidateDroplet() failed", err)
result = "ValidateDroplet() failed droplet " + d.Hostname
return result, err
}

View File

@ -198,8 +198,10 @@ func getNewMac() string {
return ""
}
// consistancy check. run on a regular basis
//
// runs on startup. dies if there are duplicates
// the config file must then be edited by hand
// the config file must then be edited by hand for now
func ValidateDroplets() (map[string]string, map[string]string, error) {
// uuid map to check for duplicates
var umap map[string]string
@ -301,6 +303,14 @@ func ValidateDroplet(check *pb.Droplet) error {
// check for duplicate mac addresses
for _, checkn := range check.Networks {
log.Info("found mac = ", checkn.Mac, check.Hostname)
if checkn.Mac == "" {
checkn.Mac = getNewMac()
if err := me.cluster.ConfigSave(); err != nil {
log.Info("configsave error", err)
os.Exit(-1)
}
}
loop := me.cluster.DropletsAll() // get the list of droplets
for loop.Scan() {
d := loop.Droplet()