forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coreos.go
182 lines (153 loc) · 3.85 KB
/
coreos.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
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
package machine
import (
"errors"
"io/ioutil"
"net"
"path/filepath"
"strings"
"sync"
"time"
"github.com/coreos/fleet/Godeps/_workspace/src/github.com/docker/libcontainer/netlink"
"github.com/coreos/fleet/log"
"github.com/coreos/fleet/unit"
)
const (
machineIDPath = "/etc/machine-id"
)
func NewCoreOSMachine(static MachineState, um unit.UnitManager) *CoreOSMachine {
log.V(1).Infof("Created CoreOSMachine with static state %s", static)
m := &CoreOSMachine{
staticState: static,
um: um,
}
return m
}
type CoreOSMachine struct {
sync.RWMutex
um unit.UnitManager
staticState MachineState
dynamicState *MachineState
}
func (m *CoreOSMachine) String() string {
return m.State().ID
}
// State returns a MachineState object representing the CoreOSMachine's
// static state overlaid on its dynamic state at the time of execution.
func (m *CoreOSMachine) State() (state MachineState) {
m.RLock()
defer m.RUnlock()
if m.dynamicState == nil {
state = MachineState(m.staticState)
} else {
state = stackState(m.staticState, *m.dynamicState)
}
return
}
// Refresh updates the current state of the CoreOSMachine.
func (m *CoreOSMachine) Refresh() {
m.RLock()
defer m.RUnlock()
cs := m.currentState()
if cs == nil {
log.Warning("Unable to refresh machine state")
} else {
m.dynamicState = cs
}
}
// PeriodicRefresh updates the current state of the CoreOSMachine at the
// interval indicated. Operation ceases when the provided channel is closed.
func (m *CoreOSMachine) PeriodicRefresh(interval time.Duration, stop chan bool) {
ticker := time.NewTicker(interval)
for {
select {
case <-stop:
log.V(1).Info("Halting CoreOSMachine.PeriodicRefresh")
ticker.Stop()
return
case <-ticker.C:
m.Refresh()
}
}
}
// currentState generates a MachineState object with the values read from
// the local system
func (m *CoreOSMachine) currentState() *MachineState {
id, err := readLocalMachineID("/")
if err != nil {
log.Errorf("Error retrieving machineID: %v\n", err)
return nil
}
publicIP := getLocalIP()
return &MachineState{
ID: id,
PublicIP: publicIP,
Metadata: make(map[string]string, 0),
}
}
// IsLocalMachineID returns whether the given machine ID is equal to that of the local machine
func IsLocalMachineID(mID string) bool {
m, err := readLocalMachineID("/")
return err == nil && m == mID
}
func readLocalMachineID(root string) (string, error) {
fullPath := filepath.Join(root, machineIDPath)
id, err := ioutil.ReadFile(fullPath)
if err != nil {
return "", err
}
mID := strings.TrimSpace(string(id))
if mID == "" {
return "", errors.New("found empty machineID")
}
return mID, nil
}
func getLocalIP() (got string) {
iface := getDefaultGatewayIface()
if iface == nil {
return
}
addrs, err := iface.Addrs()
if err != nil || len(addrs) == 0 {
return
}
for _, addr := range addrs {
// Attempt to parse the address in CIDR notation
// and assert that it is IPv4 and global unicast
ip, _, err := net.ParseCIDR(addr.String())
if err != nil {
continue
}
if !usableAddress(ip) {
continue
}
got = ip.String()
break
}
return
}
func usableAddress(ip net.IP) bool {
return ip.To4() != nil && ip.IsGlobalUnicast()
}
func getDefaultGatewayIface() *net.Interface {
log.V(1).Infof("Attempting to retrieve IP route info from netlink")
routes, err := netlink.NetworkGetRoutes()
if err != nil {
log.V(1).Infof("Unable to detect default interface: %v", err)
return nil
}
if len(routes) == 0 {
log.V(1).Infof("Netlink returned zero routes")
return nil
}
for _, route := range routes {
if route.Default {
if route.Iface == nil {
log.V(1).Infof("Found default route but could not determine interface")
}
log.V(1).Infof("Found default route with interface %v", route.Iface.Name)
return route.Iface
}
}
log.V(1).Infof("Unable to find default route")
return nil
}