1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
package main
// validates the droplet information
// finds a hypervisor
// attempts to start the virtual machine
import (
"errors"
"fmt"
"math/rand"
"time"
"go.wit.com/lib/protobuf/virtpb"
"go.wit.com/log"
)
func isClusterStable() (string, error) {
// how long has the cluster been stable?
// wait until it is stable. use this to throttle droplet starts
dur := time.Since(me.unstable)
good := fmt.Sprintln("trying to start droplet here. grid stable for: ", virtpb.FormatDuration(dur))
if dur < me.unstableTimeout {
tmp := virtpb.FormatDuration(me.unstableTimeout)
err := "grid is still too unstable (unstable timeout = " + tmp + ")\n"
return good + err, errors.New(err)
}
return good, nil
}
// for now, because sometimes this should write to stdout and
// sometimes to http socket, it returns a string
func Start(id string) (string, error) {
var result string
if s, err := isClusterStable(); err != nil {
result += s
return result, err
}
// lookup the droplet by name
d := me.cluster.FindDropletByName(id)
if d == nil {
result = "can't start unknown droplet: " + id
return result, errors.New(result)
}
// validate the droplet
if err := ValidateDroplet(d); err != nil {
log.Info("ValidateDroplet() failed", err)
result = "ValidateDroplet() failed droplet " + d.Hostname
return result, err
}
// is the droplet already on?
if d.Current.State == virtpb.DropletState_ON {
result = "EVENT start droplet " + d.Hostname + " is already ON"
return result, errors.New(result)
}
// 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 " + d.Hostname + " on hypervisor " + h.pb.Hostname)
}
// skip hypervisors marked inactive
if h.pb.Active != true {
result += fmt.Sprintln("hypervisor is inactive:", d.Hostname, "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 " + d.Hostname + " on hypervisor " + h.pb.Hostname)
}
result += fmt.Sprintln("hypervisor ready:", d.Hostname, "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 " + d.Hostname + " on hypervisor " + h.pb.Hostname)
}
|