1package drivers 2 3import ( 4 "errors" 5 "path/filepath" 6) 7 8const ( 9 DefaultSSHUser = "root" 10 DefaultSSHPort = 22 11 DefaultEngineInstallURL = "https://get.docker.com" 12) 13 14// BaseDriver - Embed this struct into drivers to provide the common set 15// of fields and functions. 16type BaseDriver struct { 17 IPAddress string 18 MachineName string 19 SSHUser string 20 SSHPort int 21 SSHKeyPath string 22 StorePath string 23 SwarmMaster bool 24 SwarmHost string 25 SwarmDiscovery string 26} 27 28// DriverName returns the name of the driver 29func (d *BaseDriver) DriverName() string { 30 return "unknown" 31} 32 33// GetMachineName returns the machine name 34func (d *BaseDriver) GetMachineName() string { 35 return d.MachineName 36} 37 38// GetIP returns the ip 39func (d *BaseDriver) GetIP() (string, error) { 40 if d.IPAddress == "" { 41 return "", errors.New("IP address is not set") 42 } 43 return d.IPAddress, nil 44} 45 46// GetSSHKeyPath returns the ssh key path 47func (d *BaseDriver) GetSSHKeyPath() string { 48 if d.SSHKeyPath == "" { 49 d.SSHKeyPath = d.ResolveStorePath("id_rsa") 50 } 51 return d.SSHKeyPath 52} 53 54// GetSSHPort returns the ssh port, 22 if not specified 55func (d *BaseDriver) GetSSHPort() (int, error) { 56 if d.SSHPort == 0 { 57 d.SSHPort = DefaultSSHPort 58 } 59 60 return d.SSHPort, nil 61} 62 63// GetSSHUsername returns the ssh user name, root if not specified 64func (d *BaseDriver) GetSSHUsername() string { 65 if d.SSHUser == "" { 66 d.SSHUser = DefaultSSHUser 67 } 68 return d.SSHUser 69} 70 71// PreCreateCheck is called to enforce pre-creation steps 72func (d *BaseDriver) PreCreateCheck() error { 73 return nil 74} 75 76// ResolveStorePath returns the store path where the machine is 77func (d *BaseDriver) ResolveStorePath(file string) string { 78 return filepath.Join(d.StorePath, "machines", d.MachineName, file) 79} 80 81// SetSwarmConfigFromFlags configures the driver for swarm 82func (d *BaseDriver) SetSwarmConfigFromFlags(flags DriverOptions) { 83 d.SwarmMaster = flags.Bool("swarm-master") 84 d.SwarmHost = flags.String("swarm-host") 85 d.SwarmDiscovery = flags.String("swarm-discovery") 86} 87 88func EngineInstallURLFlagSet(flags DriverOptions) bool { 89 engineInstallURLFlag := flags.String("engine-install-url") 90 return engineInstallURLFlag != DefaultEngineInstallURL && engineInstallURLFlag != "" 91} 92