forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine.go
77 lines (64 loc) · 2.04 KB
/
machine.go
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
package machine
import (
log "github.com/coreos/fleet/third_party/github.com/golang/glog"
)
// Machine provides the means for a caller to access a relatively
// up-to-date MachineState object.
type Machine struct {
staticState MachineState
dynamicState *MachineState
}
// New creates a new Machine object. The provided parameters will override
// those that might be dynamically generated by the Machine on the fly.
func New(bootId string, publicIP string, metadata map[string]string) *Machine {
static := MachineState{bootId, publicIP, metadata}
log.V(2).Infof("Created Machine with static state %s", static)
m := &Machine{staticState: static}
return m
}
func (m *Machine) String() string {
return m.State().BootId
}
// State returns a MachineState object representing the Machine's
// static state overlaid on its dynamic state at the time of execution.
func (m *Machine) State() (state MachineState) {
if m.dynamicState == nil {
state = MachineState(m.staticState)
} else {
state = stackState(m.staticState, *m.dynamicState)
}
return
}
// RefreshState generates a new MachineState object based on the
// current state of the underlying host, storing it internally for
// future reference before returning it.
func (m *Machine) RefreshState() *MachineState {
state := CurrentState()
m.dynamicState = &state
return &state
}
// HasMetadata determine if a Machine fulfills the given requirements
// based on its current state.
func (m *Machine) HasMetadata(metadata map[string][]string) bool {
state := m.State()
for key, values := range metadata {
local, ok := state.Metadata[key]
if !ok {
log.V(1).Infof("No local values found for Metadata(%s)", key)
return false
}
log.V(2).Infof("Asserting local Metadata(%s) meets requirements", key)
var localMatch bool
for _, val := range values {
if local == val {
log.V(1).Infof("Local Metadata(%s) meets requirement", key)
localMatch = true
}
}
if !localMatch {
log.V(1).Infof("Local Metadata(%s) does not match requirement", key)
return false
}
}
return true
}