forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
91 lines (77 loc) · 2.21 KB
/
base.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
package drivers
import (
"errors"
"path/filepath"
)
const (
DefaultSSHUser = "root"
DefaultSSHPort = 22
DefaultEngineInstallURL = "https://get.docker.com"
)
// BaseDriver - Embed this struct into drivers to provide the common set
// of fields and functions.
type BaseDriver struct {
IPAddress string
MachineName string
SSHUser string
SSHPort int
SSHKeyPath string
StorePath string
SwarmMaster bool
SwarmHost string
SwarmDiscovery string
}
// DriverName returns the name of the driver
func (d *BaseDriver) DriverName() string {
return "unknown"
}
// GetMachineName returns the machine name
func (d *BaseDriver) GetMachineName() string {
return d.MachineName
}
// GetIP returns the ip
func (d *BaseDriver) GetIP() (string, error) {
if d.IPAddress == "" {
return "", errors.New("IP address is not set")
}
return d.IPAddress, nil
}
// GetSSHKeyPath returns the ssh key path
func (d *BaseDriver) GetSSHKeyPath() string {
if d.SSHKeyPath == "" {
d.SSHKeyPath = d.ResolveStorePath("id_rsa")
}
return d.SSHKeyPath
}
// GetSSHPort returns the ssh port, 22 if not specified
func (d *BaseDriver) GetSSHPort() (int, error) {
if d.SSHPort == 0 {
d.SSHPort = DefaultSSHPort
}
return d.SSHPort, nil
}
// GetSSHUsername returns the ssh user name, root if not specified
func (d *BaseDriver) GetSSHUsername() string {
if d.SSHUser == "" {
d.SSHUser = DefaultSSHUser
}
return d.SSHUser
}
// PreCreateCheck is called to enforce pre-creation steps
func (d *BaseDriver) PreCreateCheck() error {
return nil
}
// ResolveStorePath returns the store path where the machine is
func (d *BaseDriver) ResolveStorePath(file string) string {
return filepath.Join(d.StorePath, "machines", d.MachineName, file)
}
// SetSwarmConfigFromFlags configures the driver for swarm
func (d *BaseDriver) SetSwarmConfigFromFlags(flags DriverOptions) {
d.SwarmMaster = flags.Bool("swarm-master")
d.SwarmHost = flags.String("swarm-host")
d.SwarmDiscovery = flags.String("swarm-discovery")
}
func EngineInstallURLFlagSet(flags DriverOptions) bool {
engineInstallURLFlag := flags.String("engine-install-url")
return engineInstallURLFlag != DefaultEngineInstallURL && engineInstallURLFlag != ""
}