summaryrefslogtreecommitdiff
path: root/add.go
blob: 6356edeca208c1727e5b2868742bf6ead1c4ec91 (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package virtpb

import (
	"fmt"
	"time"

	"github.com/google/uuid"
	"go.wit.com/log"
)

/*
func (c *OldCluster) InitDroplet(hostname string) (*Droplet, error) {
	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
}

func (c *Cluster) appendDroplet(d *Droplet) {
	c.Lock()
	defer c.Unlock()

	c.d.Droplets = append(c.d.Droplets, d)
}
*/

// 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)
}

func (x *Hypervisor) SetMemoryGB(gb int) {
	x.Memory = int64(gb * 1024 * 1024 * 1024)
}

func (c *OldCluster) FindDropletByName(name string) *Droplet {
	loop := c.d.All() // get the list of droplets
	for loop.Scan() {
		d := loop.Next()
		if d.Hostname == name {
			return d
		}
	}
	return nil
}

func (c *OldCluster) FindDropletByUuid(id string) *Droplet {
	log.Info("START FIND", id)
	loop := c.d.All() // get the list of droplets
	for loop.Scan() {
		d := loop.Next()
		log.Info("droplet:", d.Hostname, d.Uuid)
	}
	log.Info("END FIND", id)
	return c.d.FindByUuid(id)
}

func (c *OldCluster) FindHypervisorByName(name string) *Hypervisor {
	for _, h := range c.H.Hypervisors {
		if h.Hostname == name {
			return h
		}
	}
	return nil
}

func (c *OldCluster) AddHypervisor(hostname string, cpus int, mem int) *Hypervisor {
	h := c.FindHypervisorByName(hostname)
	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)
	c.H.Hypervisors = append(c.H.Hypervisors, h)
	return h
}

func (c *OldCluster) AddEvent(e *Event) {
	c.e.Events = append(c.e.Events, e)
}

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

func (c *OldCluster) AddDropletSimple(uuid string, hostname string, cpus int, mem int) *Droplet {
	d := c.FindDropletByName(hostname)
	if d != nil {
		return d
	}

	d = &Droplet{
		Uuid:     uuid,
		Hostname: hostname,
		Cpus:     int64(cpus),
	}

	if cpus < 0 {
		d.Cpus = 1
	}
	d.Memory = SetGB(mem * 32)
	d.StartState = DropletState_OFF
	c.AddDroplet(d)
	return d
}

// This isn't for the marketing department
func (c *OldCluster) AddDropletLocal(name string, hypername string) *Droplet {
	d := &Droplet{
		Hostname: name,
	}
	d.LocalOnly = "yes on: " + hypername

	// by default, on locally imported domains, set the preferred hypervisor!
	d.PreferredHypervisor = hypername

	d.Current = new(Current)
	d.Current.Hypervisor = hypername
	d.StartState = DropletState_OFF
	d.Current.State = DropletState_OFF

	c.AddDroplet(d)
	return d
}

func (c *OldCluster) BlankFields() {
	loop := c.d.All() // get the list of droplets
	for loop.Scan() {
		d := loop.Next()
		d.Current = nil
	}
}

func (epb *Events) AppendEvent(e *Event) {
	epb.Events = append(epb.Events, e)
}

func (c *OldCluster) ClusterStable() (bool, string) {
	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)
}

// check the cluster and droplet to make sure it's ready to start
func (c *OldCluster) DropletReady(d *Droplet) (bool, string) {
	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, ""
}