X Tutup
Skip to content

Commit ae2d344

Browse files
author
Olivier Gambier
committed
Carry on commits from docker-archive-public#2033
A couple of small cleanup and enhancements that were dropped after the revert. Signed-off-by: Olivier Gambier <olivier@docker.com>
1 parent d855c35 commit ae2d344

File tree

5 files changed

+89
-68
lines changed

5 files changed

+89
-68
lines changed

drivers/digitalocean/digitalocean.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,6 @@ func (d *Driver) GetURL() (string, error) {
230230
return fmt.Sprintf("tcp://%s:2376", ip), nil
231231
}
232232

233-
func (d *Driver) GetIP() (string, error) {
234-
if d.IPAddress == "" {
235-
return "", fmt.Errorf("IP address is not set")
236-
}
237-
return d.IPAddress, nil
238-
}
239-
240233
func (d *Driver) GetState() (state.State, error) {
241234
droplet, _, err := d.getClient().Droplets.Get(d.DropletID)
242235
if err != nil {

drivers/exoscale/exoscale.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,6 @@ func (d *Driver) GetURL() (string, error) {
150150
return fmt.Sprintf("tcp://%s:2376", ip), nil
151151
}
152152

153-
func (d *Driver) GetIP() (string, error) {
154-
if d.IPAddress == "" {
155-
return "", fmt.Errorf("IP address is not set")
156-
}
157-
return d.IPAddress, nil
158-
}
159-
160153
func (d *Driver) GetState() (state.State, error) {
161154
client := egoscale.NewClient(d.URL, d.ApiKey, d.ApiSecretKey)
162155
vm, err := client.GetVirtualMachine(d.Id)

drivers/generic/generic.go

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package generic
22

33
import (
4+
"errors"
45
"fmt"
56
"net"
67
"os"
78
"path/filepath"
9+
"strconv"
810
"time"
911

1012
"github.com/docker/machine/libmachine/drivers"
@@ -20,13 +22,11 @@ type Driver struct {
2022
}
2123

2224
const (
23-
defaultSSHUser = "root"
24-
defaultSSHPort = 22
2525
defaultTimeout = 1 * time.Second
2626
)
2727

2828
var (
29-
defaultSSHKey = filepath.Join(mcnutils.GetHomeDir(), ".ssh", "id_rsa")
29+
defaultSourceSSHKey = filepath.Join(mcnutils.GetHomeDir(), ".ssh", "id_rsa")
3030
)
3131

3232
// GetCreateFlags registers the flags this driver adds to
@@ -40,31 +40,29 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
4040
mcnflag.StringFlag{
4141
Name: "generic-ssh-user",
4242
Usage: "SSH user",
43-
Value: defaultSSHUser,
43+
Value: drivers.DefaultSSHUser,
4444
},
4545
mcnflag.StringFlag{
4646
Name: "generic-ssh-key",
4747
Usage: "SSH private key path",
48-
Value: defaultSSHKey,
48+
Value: defaultSourceSSHKey,
4949
},
5050
mcnflag.IntFlag{
5151
Name: "generic-ssh-port",
5252
Usage: "SSH port",
53-
Value: defaultSSHPort,
53+
Value: drivers.DefaultSSHPort,
5454
},
5555
}
5656
}
5757

5858
// NewDriver creates and returns a new instance of the driver
5959
func NewDriver(hostName, storePath string) drivers.Driver {
6060
return &Driver{
61-
SSHKey: defaultSSHKey,
6261
BaseDriver: &drivers.BaseDriver{
63-
SSHUser: defaultSSHUser,
64-
SSHPort: defaultSSHPort,
6562
MachineName: hostName,
6663
StorePath: storePath,
6764
},
65+
SSHKey: defaultSourceSSHKey,
6866
}
6967
}
7068

@@ -87,29 +85,26 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
8785
d.SSHPort = flags.Int("generic-ssh-port")
8886

8987
if d.IPAddress == "" {
90-
return fmt.Errorf("generic driver requires the --generic-ip-address option")
88+
return errors.New("generic driver requires the --generic-ip-address option")
9189
}
9290

9391
if d.SSHKey == "" {
94-
return fmt.Errorf("generic driver requires the --generic-ssh-key option")
92+
return errors.New("generic driver requires the --generic-ssh-key option")
9593
}
9694

9795
return nil
9896
}
9997

100-
func (d *Driver) PreCreateCheck() error {
101-
return nil
102-
}
103-
10498
func (d *Driver) Create() error {
105-
log.Infof("Importing SSH key...")
99+
log.Info("Importing SSH key...")
106100

101+
// TODO: validate the key is a valid key
107102
if err := mcnutils.CopyFile(d.SSHKey, d.GetSSHKeyPath()); err != nil {
108103
return fmt.Errorf("unable to copy ssh key: %s", err)
109104
}
110105

111106
if err := os.Chmod(d.GetSSHKeyPath(), 0600); err != nil {
112-
return err
107+
return fmt.Errorf("unable to set permissions on the ssh key: %s", err)
113108
}
114109

115110
log.Debugf("IP: %s", d.IPAddress)
@@ -125,16 +120,10 @@ func (d *Driver) GetURL() (string, error) {
125120
return fmt.Sprintf("tcp://%s:2376", ip), nil
126121
}
127122

128-
func (d *Driver) GetIP() (string, error) {
129-
if d.IPAddress == "" {
130-
return "", fmt.Errorf("IP address is not set")
131-
}
132-
return d.IPAddress, nil
133-
}
134-
135123
func (d *Driver) GetState() (state.State, error) {
136-
addr := fmt.Sprintf("%s:%d", d.IPAddress, d.SSHPort)
137-
_, err := net.DialTimeout("tcp", addr, defaultTimeout)
124+
125+
address := net.JoinHostPort(d.IPAddress, strconv.Itoa(d.SSHPort))
126+
_, err := net.DialTimeout("tcp", address, defaultTimeout)
138127
var st state.State
139128
if err != nil {
140129
st = state.Stopped
@@ -145,11 +134,11 @@ func (d *Driver) GetState() (state.State, error) {
145134
}
146135

147136
func (d *Driver) Start() error {
148-
return fmt.Errorf("generic driver does not support start")
137+
return errors.New("generic driver does not support start")
149138
}
150139

151140
func (d *Driver) Stop() error {
152-
return fmt.Errorf("generic driver does not support stop")
141+
return errors.New("generic driver does not support stop")
153142
}
154143

155144
func (d *Driver) Remove() error {
@@ -158,22 +147,15 @@ func (d *Driver) Remove() error {
158147

159148
func (d *Driver) Restart() error {
160149
log.Debug("Restarting...")
161-
162-
if _, err := drivers.RunSSHCommandFromDriver(d, "sudo shutdown -r now"); err != nil {
163-
return err
164-
}
165-
166-
return nil
150+
_, err := drivers.RunSSHCommandFromDriver(d, "sudo shutdown -r now")
151+
return err
167152
}
168153

169154
func (d *Driver) Kill() error {
170155
log.Debug("Killing...")
171156

172-
if _, err := drivers.RunSSHCommandFromDriver(d, "sudo shutdown -P now"); err != nil {
173-
return err
174-
}
175-
176-
return nil
157+
_, err := drivers.RunSSHCommandFromDriver(d, "sudo shutdown -P now")
158+
return err
177159
}
178160

179161
func (d *Driver) publicSSHKeyPath() string {

libmachine/drivers/base.go

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,65 @@
11
package drivers
22

3-
import "path/filepath"
3+
import (
4+
"errors"
5+
"fmt"
6+
"net"
7+
"path/filepath"
8+
)
9+
10+
const (
11+
DefaultSSHUser = "root"
12+
DefaultSSHPort = 22
13+
)
414

515
// BaseDriver - Embed this struct into drivers to provide the common set
616
// of fields and functions.
717
type BaseDriver struct {
818
IPAddress string
19+
MachineName string
920
SSHUser string
1021
SSHPort int
11-
MachineName string
22+
SSHKeyPath string
23+
StorePath string
1224
SwarmMaster bool
1325
SwarmHost string
1426
SwarmDiscovery string
15-
StorePath string
16-
}
17-
18-
// GetSSHKeyPath -
19-
func (d *BaseDriver) GetSSHKeyPath() string {
20-
return filepath.Join(d.StorePath, "machines", d.MachineName, "id_rsa")
2127
}
2228

2329
// DriverName returns the name of the driver
2430
func (d *BaseDriver) DriverName() string {
2531
return "unknown"
2632
}
2733

28-
// GetIP returns the ip
34+
// GetMachineName returns the machine name
2935
func (d *BaseDriver) GetMachineName() string {
3036
return d.MachineName
3137
}
3238

33-
// ResolveStorePath -
34-
func (d *BaseDriver) ResolveStorePath(file string) string {
35-
return filepath.Join(d.StorePath, "machines", d.MachineName, file)
39+
// GetIP returns the ip
40+
func (d *BaseDriver) GetIP() (string, error) {
41+
if d.IPAddress == "" {
42+
return "", errors.New("IP address is not set")
43+
}
44+
ip := net.ParseIP(d.IPAddress)
45+
if ip == nil {
46+
return "", fmt.Errorf("IP address is invalid: %s", d.IPAddress)
47+
}
48+
return d.IPAddress, nil
49+
}
50+
51+
// GetSSHKeyPath returns the ssh key path
52+
func (d *BaseDriver) GetSSHKeyPath() string {
53+
if d.SSHKeyPath == "" {
54+
d.SSHKeyPath = d.ResolveStorePath("id_rsa")
55+
}
56+
return d.SSHKeyPath
3657
}
3758

3859
// GetSSHPort returns the ssh port, 22 if not specified
3960
func (d *BaseDriver) GetSSHPort() (int, error) {
4061
if d.SSHPort == 0 {
41-
d.SSHPort = 22
62+
d.SSHPort = DefaultSSHPort
4263
}
4364

4465
return d.SSHPort, nil
@@ -47,13 +68,17 @@ func (d *BaseDriver) GetSSHPort() (int, error) {
4768
// GetSSHUsername returns the ssh user name, root if not specified
4869
func (d *BaseDriver) GetSSHUsername() string {
4970
if d.SSHUser == "" {
50-
d.SSHUser = "root"
71+
d.SSHUser = DefaultSSHUser
5172
}
52-
5373
return d.SSHUser
5474
}
5575

5676
// PreCreateCheck is called to enforce pre-creation steps
5777
func (d *BaseDriver) PreCreateCheck() error {
5878
return nil
5979
}
80+
81+
// ResolveStorePath returns the store path where the machine is
82+
func (d *BaseDriver) ResolveStorePath(file string) string {
83+
return filepath.Join(d.StorePath, "machines", d.MachineName, file)
84+
}

libmachine/drivers/base_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package drivers
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"github.com/stretchr/testify/assert"
7+
"testing"
8+
)
9+
10+
func TestIP(t *testing.T) {
11+
cases := []struct {
12+
baseDriver *BaseDriver
13+
expectedIp string
14+
expectedErr error
15+
}{
16+
{&BaseDriver{}, "", errors.New("IP address is not set")},
17+
{&BaseDriver{IPAddress: "2001:4860:0:2001::68"}, "2001:4860:0:2001::68", nil},
18+
{&BaseDriver{IPAddress: "192.168.0.1"}, "192.168.0.1", nil},
19+
{&BaseDriver{IPAddress: "::1"}, "::1", nil},
20+
{&BaseDriver{IPAddress: "whatever"}, "", fmt.Errorf("IP address is invalid: %s", "whatever")},
21+
}
22+
23+
for _, c := range cases {
24+
ip, err := c.baseDriver.GetIP()
25+
assert.Equal(t, c.expectedIp, ip)
26+
assert.Equal(t, c.expectedErr, err)
27+
}
28+
}

0 commit comments

Comments
 (0)
X Tutup