1package drivers 2 3import ( 4 "errors" 5 6 "github.com/docker/machine/libmachine/log" 7 "github.com/docker/machine/libmachine/mcnflag" 8 "github.com/docker/machine/libmachine/state" 9) 10 11// Driver defines how a host is created and controlled. Different types of 12// driver represent different ways hosts can be created (e.g. different 13// hypervisors, different cloud providers) 14type Driver interface { 15 // Create a host using the driver's config 16 Create() error 17 18 // DriverName returns the name of the driver 19 DriverName() string 20 21 // GetCreateFlags returns the mcnflag.Flag slice representing the flags 22 // that can be set, their descriptions and defaults. 23 GetCreateFlags() []mcnflag.Flag 24 25 // GetIP returns an IP or hostname that this host is available at 26 // e.g. 1.2.3.4 or docker-host-d60b70a14d3a.cloudapp.net 27 GetIP() (string, error) 28 29 // GetMachineName returns the name of the machine 30 GetMachineName() string 31 32 // GetSSHHostname returns hostname for use with ssh 33 GetSSHHostname() (string, error) 34 35 // GetSSHKeyPath returns key path for use with ssh 36 GetSSHKeyPath() string 37 38 // GetSSHPort returns port for use with ssh 39 GetSSHPort() (int, error) 40 41 // GetSSHUsername returns username for use with ssh 42 GetSSHUsername() string 43 44 // GetURL returns a Docker compatible host URL for connecting to this host 45 // e.g. tcp://1.2.3.4:2376 46 GetURL() (string, error) 47 48 // GetState returns the state that the host is in (running, stopped, etc) 49 GetState() (state.State, error) 50 51 // Kill stops a host forcefully 52 Kill() error 53 54 // PreCreateCheck allows for pre-create operations to make sure a driver is ready for creation 55 PreCreateCheck() error 56 57 // Remove a host 58 Remove() error 59 60 // Restart a host. This may just call Stop(); Start() if the provider does not 61 // have any special restart behaviour. 62 Restart() error 63 64 // SetConfigFromFlags configures the driver with the object that was returned 65 // by RegisterCreateFlags 66 SetConfigFromFlags(opts DriverOptions) error 67 68 // Start a host 69 Start() error 70 71 // Stop a host gracefully 72 Stop() error 73} 74 75var ErrHostIsNotRunning = errors.New("Host is not running") 76 77type DriverOptions interface { 78 String(key string) string 79 StringSlice(key string) []string 80 Int(key string) int 81 Bool(key string) bool 82} 83 84func MachineInState(d Driver, desiredState state.State) func() bool { 85 return func() bool { 86 currentState, err := d.GetState() 87 if err != nil { 88 log.Debugf("Error getting machine state: %s", err) 89 } 90 if currentState == desiredState { 91 return true 92 } 93 return false 94 } 95} 96 97// MustBeRunning will return an error if the machine is not in a running state. 98func MustBeRunning(d Driver) error { 99 s, err := d.GetState() 100 if err != nil { 101 return err 102 } 103 104 if s != state.Running { 105 return ErrHostIsNotRunning 106 } 107 108 return nil 109} 110