forked from weaveworks/footloose
/
machine.go
216 lines (183 loc) · 4.9 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
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
210
211
212
213
214
215
216
package cluster
import (
"fmt"
"strconv"
"strings"
"syscall"
"github.com/docker/docker/api/types/network"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/kingdonb/footloose/pkg/config"
"github.com/kingdonb/footloose/pkg/docker"
"github.com/kingdonb/footloose/pkg/exec"
"github.com/kingdonb/footloose/pkg/ignite"
)
// Machine is a single machine.
type Machine struct {
spec *config.Machine
// container name.
name string
// container hostname.
hostname string
// container ip.
ip string
runtimeNetworks []*RuntimeNetwork
// Fields that are cached from the docker daemon.
ports map[int]int
// maps containerPort -> hostPort.
}
// ContainerName is the name of the running container corresponding to this
// Machine.
func (m *Machine) ContainerName() string {
if m.IsIgnite() {
filter := fmt.Sprintf(`label=ignite.name=%s`, m.name)
cid, err := exec.ExecuteCommand("docker", "ps", "-q", "-f", filter)
if err != nil || len(cid) == 0 {
return m.name
}
return cid
}
return m.name
}
// Hostname is the machine hostname.
func (m *Machine) Hostname() string {
return m.hostname
}
// IsCreated returns if a machine is has been created. A created machine could
// either be running or stopped.
func (m *Machine) IsCreated() bool {
if m.IsIgnite() {
return ignite.IsCreated(m.name)
}
res, _ := docker.Inspect(m.name, "{{.Name}}")
if len(res) > 0 && len(res[0]) > 0 {
return true
}
return false
}
// IsStarted returns if a machine is currently started or not.
func (m *Machine) IsStarted() bool {
if m.IsIgnite() {
return ignite.IsStarted(m.name)
}
res, _ := docker.Inspect(m.name, "{{.State.Running}}")
parsed, _ := strconv.ParseBool(strings.Trim(res[0], `'`))
return parsed
}
// HostPort returns the host port corresponding to the given container port.
func (m *Machine) HostPort(containerPort int) (hostPort int, err error) {
// Use the cached version first.
if hostPort, ok := m.ports[containerPort]; ok {
return hostPort, nil
}
// retrieve the specific port mapping using docker inspect
lines, err := docker.Inspect(m.ContainerName(), fmt.Sprintf("{{(index (index .NetworkSettings.Ports \"%d/tcp\") 0).HostPort}}", containerPort))
if err != nil {
return -1, errors.Wrapf(err, "hostport: failed to inspect container: %v", lines)
}
if len(lines) != 1 {
return -1, errors.Errorf("hostport: should only be one line, got %d lines", len(lines))
}
if m.ports == nil {
m.ports = make(map[int]int)
}
port := strings.Replace(lines[0], "'", "", -1)
m.ports[containerPort], err = strconv.Atoi(port)
if err != nil {
return -1, errors.Wrap(err, "hostport: failed to parse string to int")
}
return m.ports[containerPort], nil
}
func (m *Machine) networks() ([]*RuntimeNetwork, error) {
if len(m.runtimeNetworks) != 0 {
return m.runtimeNetworks, nil
}
var networks map[string]*network.EndpointSettings
if err := docker.InspectObject(m.name, ".NetworkSettings.Networks", &networks); err != nil {
return nil, err
}
m.runtimeNetworks = NewRuntimeNetworks(networks)
return m.runtimeNetworks, nil
}
func (m *Machine) igniteStatus(s *MachineStatus) error {
vm, err := ignite.PopulateMachineDetails(m.name)
if err != nil {
return err
}
// Set Ports
var ports []port
for _, p := range vm.Spec.Network.Ports {
ports = append(ports, port{
Host: int(p.HostPort),
Guest: int(p.VMPort),
})
}
s.Ports = ports
if vm.Status.IpAddresses != nil && len(vm.Status.IpAddresses) > 0 {
m.ip = vm.Status.IpAddresses[0]
}
s.RuntimeNetworks = NewIgniteRuntimeNetwork(&vm.Status)
return nil
}
func (m *Machine) dockerStatus(s *MachineStatus) error {
var ports []port
if m.IsCreated() {
for _, v := range m.spec.PortMappings {
hPort, err := m.HostPort(int(v.ContainerPort))
if err != nil {
hPort = 0
}
p := port{
Host: hPort,
Guest: int(v.ContainerPort),
}
ports = append(ports, p)
}
}
if len(ports) < 1 {
for _, p := range m.spec.PortMappings {
ports = append(ports, port{Host: 0, Guest: int(p.ContainerPort)})
}
}
s.Ports = ports
s.RuntimeNetworks, _ = m.networks()
return nil
}
// Status returns the machine status.
func (m *Machine) Status() *MachineStatus {
s := MachineStatus{}
s.Container = m.ContainerName()
s.Image = m.spec.Image
s.Command = m.spec.Cmd
s.Spec = m.spec
s.Hostname = m.Hostname()
s.IP = m.ip
state := NotCreated
if m.IsCreated() {
state = Stopped
if m.IsStarted() {
state = Running
}
}
s.State = state
if m.IsIgnite() {
_ = m.igniteStatus(&s)
} else {
_ = m.dockerStatus(&s)
}
return &s
}
// Only check for Ignite prerequisites once
var igniteChecked bool
// IsIgnite returns if the backend is Ignite
func (m *Machine) IsIgnite() (b bool) {
b = m.spec.Backend == ignite.BackendName
if !igniteChecked && b {
if syscall.Getuid() != 0 {
log.Fatalf("Footloose needs to run as root to use the %q backend", ignite.BackendName)
}
ignite.CheckVersion()
igniteChecked = true
}
return
}