forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
host.go
221 lines (180 loc) · 5.36 KB
/
host.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
217
218
219
220
221
package host
import (
"errors"
"fmt"
"regexp"
"strings"
"github.com/docker/machine/libmachine/auth"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/provision"
"github.com/docker/machine/libmachine/provision/pkgaction"
"github.com/docker/machine/libmachine/provision/serviceaction"
"github.com/docker/machine/libmachine/ssh"
"github.com/docker/machine/libmachine/state"
"github.com/docker/machine/libmachine/swarm"
)
var (
validHostNamePattern = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9\-\.]*$`)
errMachineMustBeRunningForUpgrade = errors.New("Error: machine must be running to upgrade.")
stdSSHClientCreator SSHClientCreator = &StandardSSHClientCreator{}
)
type SSHClientCreator interface {
CreateSSHClient(d drivers.Driver) (ssh.Client, error)
}
type StandardSSHClientCreator struct {
drivers.Driver
}
func SetSSHClientCreator(creator SSHClientCreator) {
stdSSHClientCreator = creator
}
type Host struct {
ConfigVersion int
Driver drivers.Driver
DriverName string
HostOptions *Options
Name string
RawDriver []byte `json:"-"`
}
type Options struct {
Driver string
Memory int
Disk int
EngineOptions *engine.Options
SwarmOptions *swarm.Options
AuthOptions *auth.Options
}
type Metadata struct {
ConfigVersion int
DriverName string
HostOptions Options
}
func ValidateHostName(name string) bool {
return validHostNamePattern.MatchString(name)
}
func (h *Host) RunSSHCommand(command string) (string, error) {
return drivers.RunSSHCommandFromDriver(h.Driver, command)
}
func (h *Host) CreateSSHClient() (ssh.Client, error) {
return stdSSHClientCreator.CreateSSHClient(h.Driver)
}
func (creator *StandardSSHClientCreator) CreateSSHClient(d drivers.Driver) (ssh.Client, error) {
addr, err := d.GetSSHHostname()
if err != nil {
return ssh.ExternalClient{}, err
}
port, err := d.GetSSHPort()
if err != nil {
return ssh.ExternalClient{}, err
}
auth := &ssh.Auth{}
if d.GetSSHKeyPath() != "" {
auth.Keys = []string{d.GetSSHKeyPath()}
}
return ssh.NewClient(d.GetSSHUsername(), addr, port, auth)
}
func (h *Host) runActionForState(action func() error, desiredState state.State) error {
if drivers.MachineInState(h.Driver, desiredState)() {
return fmt.Errorf("Machine %q is already %s.", h.Name, strings.ToLower(desiredState.String()))
}
if err := action(); err != nil {
return err
}
return mcnutils.WaitFor(drivers.MachineInState(h.Driver, desiredState))
}
func (h *Host) WaitForDocker() error {
provisioner, err := provision.DetectProvisioner(h.Driver)
if err != nil {
return err
}
return provision.WaitForDocker(provisioner, engine.DefaultPort)
}
func (h *Host) Start() error {
log.Infof("Starting %q...", h.Name)
if err := h.runActionForState(h.Driver.Start, state.Running); err != nil {
return err
}
log.Infof("Machine %q was started.", h.Name)
return h.WaitForDocker()
}
func (h *Host) Stop() error {
log.Infof("Stopping %q...", h.Name)
if err := h.runActionForState(h.Driver.Stop, state.Stopped); err != nil {
return err
}
log.Infof("Machine %q was stopped.", h.Name)
return nil
}
func (h *Host) Kill() error {
log.Infof("Killing %q...", h.Name)
if err := h.runActionForState(h.Driver.Kill, state.Stopped); err != nil {
return err
}
log.Infof("Machine %q was killed.", h.Name)
return nil
}
func (h *Host) Restart() error {
log.Infof("Restarting %q...", h.Name)
if drivers.MachineInState(h.Driver, state.Stopped)() {
if err := h.Start(); err != nil {
return err
}
} else if drivers.MachineInState(h.Driver, state.Running)() {
if err := h.Driver.Restart(); err != nil {
return err
}
if err := mcnutils.WaitFor(drivers.MachineInState(h.Driver, state.Running)); err != nil {
return err
}
}
return h.WaitForDocker()
}
func (h *Host) Upgrade() error {
machineState, err := h.Driver.GetState()
if err != nil {
return err
}
if machineState != state.Running {
return errMachineMustBeRunningForUpgrade
}
provisioner, err := provision.DetectProvisioner(h.Driver)
if err != nil {
return err
}
log.Info("Upgrading docker...")
if err := provisioner.Package("docker", pkgaction.Upgrade); err != nil {
return err
}
log.Info("Restarting docker...")
return provisioner.Service("docker", serviceaction.Restart)
}
func (h *Host) URL() (string, error) {
return h.Driver.GetURL()
}
func (h *Host) AuthOptions() *auth.Options {
if h.HostOptions == nil {
return nil
}
return h.HostOptions.AuthOptions
}
func (h *Host) ConfigureAuth() error {
provisioner, err := provision.DetectProvisioner(h.Driver)
if err != nil {
return err
}
// TODO: This is kind of a hack (or is it? I'm not really sure until
// we have more clearly defined outlook on what the responsibilities
// and modularity of the provisioners should be).
//
// Call provision to re-provision the certs properly.
return provisioner.Provision(swarm.Options{}, *h.HostOptions.AuthOptions, *h.HostOptions.EngineOptions)
}
func (h *Host) Provision() error {
provisioner, err := provision.DetectProvisioner(h.Driver)
if err != nil {
return err
}
return provisioner.Provision(*h.HostOptions.SwarmOptions, *h.HostOptions.AuthOptions, *h.HostOptions.EngineOptions)
}