2024-10-30 12:16:08 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
pb "go.wit.com/lib/protobuf/virtbuf"
|
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// attempts to create a new virtual machine
|
|
|
|
|
2024-10-30 18:09:54 -05:00
|
|
|
func create(w http.ResponseWriter, r *http.Request) (string, error) {
|
2024-10-30 12:16:08 -05:00
|
|
|
msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
|
|
|
|
if err != nil {
|
2024-10-30 18:09:54 -05:00
|
|
|
result := fmt.Sprintf("ReadAll() error =", err)
|
|
|
|
log.Info(result)
|
|
|
|
fmt.Fprintln(w, result)
|
|
|
|
return result, err
|
2024-10-30 12:16:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var d *pb.Droplet
|
|
|
|
d = new(pb.Droplet)
|
|
|
|
if err := d.UnmarshalJSON(msg); err != nil {
|
2024-10-30 18:09:54 -05:00
|
|
|
log.Info("UnmarshalJSON() failed", err)
|
|
|
|
if err := d.Unmarshal(msg); err != nil {
|
|
|
|
log.Info("droplet protobuf.Unmarshal() failed", err)
|
|
|
|
return "", err
|
|
|
|
}
|
2024-10-30 12:16:08 -05:00
|
|
|
}
|
2024-10-30 18:09:54 -05:00
|
|
|
d.StartState = pb.DropletState_OFF
|
2024-11-01 00:41:00 -05:00
|
|
|
d.SetState(pb.DropletState_OFF)
|
2024-10-30 18:09:54 -05:00
|
|
|
d.Memory = 2048 * 1024 * 1024
|
|
|
|
d.Cpus = 2
|
2024-10-30 12:16:08 -05:00
|
|
|
|
|
|
|
log.Info("Got msg:", string(msg))
|
|
|
|
log.Info("hostname =", d.Hostname)
|
|
|
|
name := d.Hostname
|
2024-10-30 19:38:12 -05:00
|
|
|
|
|
|
|
// don't continue past here if the grid is unstable anyway
|
|
|
|
// because this will add the droplet to cluster.Droplets
|
|
|
|
if s, err := isClusterStable(); err != nil {
|
|
|
|
log.Info(s)
|
|
|
|
fmt.Fprintln(w, s)
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
2024-10-31 06:41:30 -05:00
|
|
|
tmpd := me.cluster.FindDropletByName(name)
|
2024-10-30 12:16:08 -05:00
|
|
|
if tmpd != nil {
|
|
|
|
result := "create error: Droplet " + name + " is already defined"
|
2024-10-30 18:09:54 -05:00
|
|
|
log.Info(result)
|
2024-10-30 12:16:08 -05:00
|
|
|
fmt.Fprintln(w, result)
|
2024-10-30 18:09:54 -05:00
|
|
|
return result, errors.New(result)
|
2024-10-30 12:16:08 -05:00
|
|
|
}
|
2024-10-30 12:30:34 -05:00
|
|
|
|
2024-10-30 12:16:08 -05:00
|
|
|
if d.Uuid == "" {
|
|
|
|
u := uuid.New()
|
|
|
|
d.Uuid = u.String()
|
|
|
|
}
|
2024-10-30 12:30:34 -05:00
|
|
|
|
2024-10-30 12:16:08 -05:00
|
|
|
if len(d.Networks) == 0 {
|
|
|
|
var newNet *pb.Network
|
|
|
|
newNet = new(pb.Network)
|
2024-10-30 12:30:34 -05:00
|
|
|
newNet.Mac = getNewMac()
|
2024-10-30 12:16:08 -05:00
|
|
|
d.Networks = append(d.Networks, newNet)
|
|
|
|
// d.AddDefaultNetwork(mac)
|
|
|
|
}
|
2024-10-31 13:37:00 -05:00
|
|
|
me.cluster.AddDroplet(d)
|
2024-10-30 12:16:08 -05:00
|
|
|
|
|
|
|
result, err := startDroplet(d)
|
|
|
|
if err != nil {
|
2024-10-30 18:09:54 -05:00
|
|
|
log.Info(result)
|
|
|
|
log.Info("startDroplet(d) failed:", err)
|
2024-10-30 12:16:08 -05:00
|
|
|
fmt.Fprintln(w, result)
|
|
|
|
fmt.Fprintln(w, "startDroplet(d) failed:", err)
|
2024-10-30 18:09:54 -05:00
|
|
|
return result, err
|
2024-10-30 12:16:08 -05:00
|
|
|
}
|
|
|
|
fmt.Fprintln(w, result)
|
|
|
|
fmt.Fprintln(w, "START=OK")
|
2024-10-30 18:09:54 -05:00
|
|
|
return result, nil
|
2024-10-30 12:16:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// for now, because sometimes this should write to stdout and
|
|
|
|
// sometimes to http socket, it returns a string
|
|
|
|
func startDroplet(d *pb.Droplet) (string, error) {
|
|
|
|
var result string
|
|
|
|
name := d.Hostname
|
|
|
|
// validate the droplet
|
|
|
|
if err := ValidateDroplet(d); err != nil {
|
|
|
|
result = "ValidateDroplet() failed droplet " + d.Hostname
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// is the droplet already on?
|
2024-10-31 06:41:30 -05:00
|
|
|
if d.Current.State == pb.DropletState_ON {
|
2024-10-30 12:16:08 -05:00
|
|
|
result = "EVENT start droplet " + d.Hostname + " is already ON"
|
|
|
|
return result, errors.New(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// how long has the cluster been stable?
|
|
|
|
// wait until it is stable. use this to throttle droplet starts
|
|
|
|
dur := time.Since(me.unstable)
|
2024-11-01 00:41:00 -05:00
|
|
|
result = fmt.Sprintln("should start droplet", name, "here. grid stable for:", pb.FormatDuration(dur))
|
2024-10-30 12:16:08 -05:00
|
|
|
if dur < me.unstableTimeout {
|
2024-11-01 00:41:00 -05:00
|
|
|
tmp := pb.FormatDuration(me.unstableTimeout)
|
2024-10-30 12:16:08 -05:00
|
|
|
result += "grid is still too unstable (unstable timeout = " + tmp + ")"
|
|
|
|
return result, errors.New("grid is still unstable")
|
|
|
|
}
|
|
|
|
|
|
|
|
// make the list of hypervisors that are active and can start new droplets
|
|
|
|
var pool []*HyperT
|
|
|
|
for _, h := range me.hypers {
|
|
|
|
// this droplet is set to use this and only this hypervisor
|
|
|
|
if d.ForceHypervisor == h.pb.Hostname {
|
|
|
|
ok, b := h.start(d)
|
|
|
|
if ok {
|
|
|
|
return result + b, nil
|
|
|
|
}
|
|
|
|
return result + b, errors.New("start " + name + " on hypervisor " + h.pb.Hostname)
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip hypervisors marked inactive
|
|
|
|
if h.pb.Active != true {
|
|
|
|
result += fmt.Sprintln("hypervisor is inactive:", name, "for", h.pb.Hostname, h.pb.Active)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// the config file says this droplet should run on this hypervisor
|
|
|
|
// attempt to start the droplet here. use this even if the hypervisor is inactive?
|
|
|
|
if d.PreferredHypervisor == h.pb.Hostname {
|
|
|
|
ok, b := h.start(d)
|
|
|
|
if ok {
|
|
|
|
return result + b, nil
|
|
|
|
}
|
|
|
|
return result + b, errors.New("start " + name + " on hypervisor " + h.pb.Hostname)
|
|
|
|
}
|
|
|
|
|
|
|
|
result += fmt.Sprintln("hypervisor ready:", name, "for", h.pb.Hostname, h.pb.Active)
|
|
|
|
pool = append(pool, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
// left here as an example of how to actually do random numbers
|
|
|
|
// it's complete mathematical chaos. Randomness is simple when
|
|
|
|
// human interaction occurs -- which is exactly what happens most
|
|
|
|
// of the time. most random shit is bullshit. all you really need
|
|
|
|
// is exactly this to make sure the random functions work as they
|
|
|
|
// should. Probably, just use this everywhere in all cases. --jcarr
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
a := 0
|
|
|
|
b := len(pool)
|
|
|
|
n := a + rand.Intn(b-a)
|
|
|
|
result += fmt.Sprintln("pool has", len(pool), "members", "rand =", n)
|
|
|
|
h := pool[n]
|
|
|
|
|
|
|
|
ok, output := h.start(d)
|
|
|
|
if ok {
|
|
|
|
return result + output, nil
|
|
|
|
}
|
|
|
|
return result + output, errors.New("start " + name + " on hypervisor " + h.pb.Hostname)
|
|
|
|
}
|