2024-10-22 04:18:40 -05:00
|
|
|
package virtbuf
|
|
|
|
|
2024-10-22 04:37:28 -05:00
|
|
|
import (
|
2024-10-22 06:19:24 -05:00
|
|
|
"fmt"
|
2024-10-31 04:52:14 -05:00
|
|
|
"time"
|
2024-10-22 06:19:24 -05:00
|
|
|
|
2024-10-22 15:58:28 -05:00
|
|
|
"github.com/google/uuid"
|
2024-10-31 04:52:14 -05:00
|
|
|
"go.wit.com/log"
|
2024-10-22 04:37:28 -05:00
|
|
|
)
|
2024-10-22 04:26:29 -05:00
|
|
|
|
2024-11-16 05:22:11 -06:00
|
|
|
/*
|
2024-11-07 05:04:11 -06:00
|
|
|
func (c *Cluster) InitDroplet(hostname string) (*Droplet, error) {
|
2024-11-01 08:31:25 -05:00
|
|
|
var d *Droplet
|
|
|
|
d = new(Droplet)
|
|
|
|
d.Current = new(Current)
|
|
|
|
|
|
|
|
d = c.FindDropletByName(hostname)
|
|
|
|
if d != nil {
|
|
|
|
return d, errors.New("duplicate hostname: " + hostname)
|
|
|
|
}
|
|
|
|
d.Hostname = hostname
|
|
|
|
// d.Uuid = uuid.New() // not appropriate here
|
|
|
|
|
|
|
|
// set some defaults
|
|
|
|
d.StartState = DropletState_OFF
|
|
|
|
d.Current.State = DropletState_UNKNOWN
|
|
|
|
c.appendDroplet(d)
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
2024-11-07 05:04:11 -06:00
|
|
|
func (c *Cluster) appendDroplet(d *Droplet) {
|
2024-11-01 08:31:25 -05:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
c.d.Droplets = append(c.d.Droplets, d)
|
|
|
|
}
|
2024-11-16 05:22:11 -06:00
|
|
|
*/
|
2024-11-01 08:31:25 -05:00
|
|
|
|
2024-10-22 16:33:36 -05:00
|
|
|
// can the json protobuf output use a string and have a type handler
|
|
|
|
// to convert it back to int64?
|
|
|
|
func SetGB(gb int) int64 {
|
|
|
|
return int64(gb * 1024 * 1024 * 1024)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetMB(mb int) int64 {
|
|
|
|
return int64(mb * 1024 * 1024)
|
|
|
|
}
|
|
|
|
|
2024-10-22 04:26:29 -05:00
|
|
|
func (x *Hypervisor) SetMemoryGB(gb int) {
|
2024-10-22 04:18:40 -05:00
|
|
|
x.Memory = int64(gb * 1024 * 1024 * 1024)
|
|
|
|
}
|
2024-10-22 04:26:29 -05:00
|
|
|
|
2024-11-07 05:04:11 -06:00
|
|
|
func (c *Cluster) FindDropletByName(name string) *Droplet {
|
2024-10-31 13:07:24 -05:00
|
|
|
loop := c.DropletsAll() // get the list of droplets
|
|
|
|
for loop.Scan() {
|
|
|
|
d := loop.Droplet()
|
2024-10-22 15:58:28 -05:00
|
|
|
if d.Hostname == name {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-07 05:04:11 -06:00
|
|
|
func (c *Cluster) FindHypervisorByName(name string) *Hypervisor {
|
2024-10-31 13:21:10 -05:00
|
|
|
for _, h := range c.H.Hypervisors {
|
2024-10-22 15:58:28 -05:00
|
|
|
if h.Hostname == name {
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-10-22 16:33:36 -05:00
|
|
|
|
2024-11-07 05:04:11 -06:00
|
|
|
func (c *Cluster) AddHypervisor(hostname string, cpus int, mem int) *Hypervisor {
|
2024-10-31 09:19:15 -05:00
|
|
|
h := c.FindHypervisorByName(hostname)
|
2024-10-22 16:33:36 -05:00
|
|
|
if h != nil {
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
// Generate a new UUID
|
|
|
|
id := uuid.New()
|
|
|
|
h = &Hypervisor{
|
|
|
|
Uuid: id.String(),
|
|
|
|
Hostname: hostname,
|
|
|
|
Cpus: int64(cpus),
|
|
|
|
Comment: "this is a fake hypervisor",
|
|
|
|
}
|
|
|
|
if cpus < 0 {
|
|
|
|
h.Cpus = 1
|
|
|
|
}
|
|
|
|
h.SetMemoryGB(mem * 32)
|
2024-10-31 13:21:10 -05:00
|
|
|
c.H.Hypervisors = append(c.H.Hypervisors, h)
|
2024-10-22 16:33:36 -05:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2024-11-07 05:04:11 -06:00
|
|
|
func (c *Cluster) AddEvent(e *Event) {
|
2024-10-31 13:38:58 -05:00
|
|
|
c.e.Events = append(c.e.Events, e)
|
|
|
|
}
|
|
|
|
|
2024-11-16 05:22:11 -06:00
|
|
|
// creates a new droplet with default values
|
|
|
|
func NewDefaultDroplet(hostname string) *Droplet {
|
|
|
|
id := uuid.New() // Generate a new UUID
|
|
|
|
d := &Droplet{
|
|
|
|
Uuid: id.String(),
|
|
|
|
Hostname: hostname,
|
|
|
|
Cpus: int64(2),
|
|
|
|
}
|
|
|
|
d.Memory = SetGB(2)
|
|
|
|
d.StartState = DropletState_OFF
|
|
|
|
|
|
|
|
return d
|
2024-10-31 13:38:58 -05:00
|
|
|
}
|
|
|
|
|
2024-11-07 05:04:11 -06:00
|
|
|
func (c *Cluster) AddDropletSimple(uuid string, hostname string, cpus int, mem int) *Droplet {
|
2024-10-31 06:49:05 -05:00
|
|
|
d := c.FindDropletByName(hostname)
|
2024-10-22 16:33:36 -05:00
|
|
|
if d != nil {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
d = &Droplet{
|
2024-10-23 06:37:08 -05:00
|
|
|
Uuid: uuid,
|
2024-10-22 16:33:36 -05:00
|
|
|
Hostname: hostname,
|
|
|
|
Cpus: int64(cpus),
|
|
|
|
}
|
|
|
|
|
|
|
|
if cpus < 0 {
|
|
|
|
d.Cpus = 1
|
|
|
|
}
|
|
|
|
d.Memory = SetGB(mem * 32)
|
2024-10-31 22:14:52 -05:00
|
|
|
d.StartState = DropletState_OFF
|
2024-10-31 13:38:58 -05:00
|
|
|
c.AddDroplet(d)
|
2024-10-22 16:33:36 -05:00
|
|
|
return d
|
|
|
|
}
|
2024-10-23 07:18:20 -05:00
|
|
|
|
2024-10-31 15:44:29 -05:00
|
|
|
// This isn't for the marketing department
|
2024-11-07 05:04:11 -06:00
|
|
|
func (c *Cluster) AddDropletLocal(name string, hypername string) *Droplet {
|
2024-10-31 15:44:29 -05:00
|
|
|
d := &Droplet{
|
|
|
|
Hostname: name,
|
|
|
|
}
|
2024-11-01 00:41:34 -05:00
|
|
|
d.LocalOnly = "yes on: " + hypername
|
2024-11-01 08:45:52 -05:00
|
|
|
|
|
|
|
// by default, on locally imported domains, set the preferred hypervisor!
|
|
|
|
d.PreferredHypervisor = hypername
|
|
|
|
|
2024-10-31 15:44:29 -05:00
|
|
|
d.Current = new(Current)
|
|
|
|
d.Current.Hypervisor = hypername
|
2024-10-31 22:14:52 -05:00
|
|
|
d.StartState = DropletState_OFF
|
2024-10-31 15:44:29 -05:00
|
|
|
d.Current.State = DropletState_OFF
|
|
|
|
|
|
|
|
c.AddDroplet(d)
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2024-11-07 05:04:11 -06:00
|
|
|
func (c *Cluster) BlankFields() {
|
2024-10-31 13:07:24 -05:00
|
|
|
loop := c.DropletsAll() // get the list of droplets
|
|
|
|
for loop.Scan() {
|
|
|
|
d := loop.Droplet()
|
2024-10-31 04:21:46 -05:00
|
|
|
d.Current = nil
|
2024-10-26 12:33:13 -05:00
|
|
|
}
|
|
|
|
}
|
2024-10-27 05:43:30 -05:00
|
|
|
|
2024-10-30 18:10:40 -05:00
|
|
|
func (epb *Events) AppendEvent(e *Event) {
|
|
|
|
epb.Events = append(epb.Events, e)
|
2024-10-27 05:43:30 -05:00
|
|
|
}
|
2024-10-31 04:29:51 -05:00
|
|
|
|
2024-11-07 05:04:11 -06:00
|
|
|
func (c *Cluster) ClusterStable() (bool, string) {
|
2024-10-31 04:52:14 -05:00
|
|
|
last := time.Since(c.Unstable.AsTime())
|
|
|
|
if last > c.UnstableTimeout.AsDuration() {
|
|
|
|
// the cluster has not been stable for 133 seconds
|
|
|
|
log.Warn("clusterReady() is stable for ", FormatDuration(c.UnstableTimeout.AsDuration()), " secs")
|
|
|
|
return true, fmt.Sprintln("clusterReady() is stable ", FormatDuration(c.UnstableTimeout.AsDuration()), " secs")
|
|
|
|
}
|
|
|
|
log.Warn("clusterReady() is unstable for", FormatDuration(last))
|
|
|
|
return false, "clusterReady() is unstable for " + FormatDuration(last)
|
|
|
|
}
|
|
|
|
|
2024-10-31 04:29:51 -05:00
|
|
|
// check the cluster and droplet to make sure it's ready to start
|
2024-11-07 05:04:11 -06:00
|
|
|
func (c *Cluster) DropletReady(d *Droplet) (bool, string) {
|
2024-10-31 04:29:51 -05:00
|
|
|
if c == nil {
|
|
|
|
return false, "cluster == nil"
|
|
|
|
}
|
|
|
|
if d == nil {
|
|
|
|
return false, "droplet == nil"
|
|
|
|
}
|
|
|
|
if d.Current == nil {
|
|
|
|
return false, "droplet.Current == nil"
|
|
|
|
}
|
|
|
|
// can't start already started droplet
|
|
|
|
if d.Current.State == DropletState_ON {
|
|
|
|
return false, "EVENT start droplet is already ON"
|
|
|
|
}
|
|
|
|
if d.Current.State != DropletState_OFF {
|
|
|
|
return false, "EVENT start droplet is not OFF state = " + string(d.Current.State)
|
|
|
|
}
|
|
|
|
if d.Current.StartAttempts > 2 {
|
|
|
|
// reason := "EVENT start droplet has already been started " + d.starts + " times"
|
|
|
|
return false, fmt.Sprintln("EVENT start droplet has already been started ", d.Current.StartAttempts, " times")
|
|
|
|
}
|
|
|
|
return true, ""
|
|
|
|
}
|