forked from rancher/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic.go
196 lines (161 loc) · 4.35 KB
/
generic.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
package generic
import (
"errors"
"fmt"
"net"
"os"
"path"
"strconv"
"time"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/state"
)
type Driver struct {
*drivers.BaseDriver
EnginePort int
SSHKey string
}
const (
defaultTimeout = 15 * time.Second
)
// GetCreateFlags registers the flags this driver adds to
// "docker hosts create"
func (d *Driver) GetCreateFlags() []mcnflag.Flag {
return []mcnflag.Flag{
mcnflag.IntFlag{
Name: "generic-engine-port",
Usage: "Docker engine port",
Value: engine.DefaultPort,
EnvVar: "GENERIC_ENGINE_PORT",
},
mcnflag.StringFlag{
Name: "generic-ip-address",
Usage: "IP Address of machine",
EnvVar: "GENERIC_IP_ADDRESS",
},
mcnflag.StringFlag{
Name: "generic-ssh-user",
Usage: "SSH user",
Value: drivers.DefaultSSHUser,
EnvVar: "GENERIC_SSH_USER",
},
mcnflag.StringFlag{
Name: "generic-ssh-key",
Usage: "SSH private key path (if not provided, default SSH key will be used)",
Value: "",
EnvVar: "GENERIC_SSH_KEY",
},
mcnflag.IntFlag{
Name: "generic-ssh-port",
Usage: "SSH port",
Value: drivers.DefaultSSHPort,
EnvVar: "GENERIC_SSH_PORT",
},
}
}
// NewDriver creates and returns a new instance of the driver
func NewDriver(hostName, storePath string) drivers.Driver {
return &Driver{
EnginePort: engine.DefaultPort,
BaseDriver: &drivers.BaseDriver{
MachineName: hostName,
StorePath: storePath,
},
}
}
// DriverName returns the name of the driver
func (d *Driver) DriverName() string {
return "generic"
}
func (d *Driver) GetSSHHostname() (string, error) {
return d.GetIP()
}
func (d *Driver) GetSSHUsername() string {
return d.SSHUser
}
func (d *Driver) GetSSHKeyPath() string {
return d.SSHKeyPath
}
func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.EnginePort = flags.Int("generic-engine-port")
d.IPAddress = flags.String("generic-ip-address")
d.SSHUser = flags.String("generic-ssh-user")
d.SSHKey = flags.String("generic-ssh-key")
d.SSHPort = flags.Int("generic-ssh-port")
if d.IPAddress == "" {
return errors.New("generic driver requires the --generic-ip-address option")
}
return nil
}
func (d *Driver) PreCreateCheck() error {
if d.SSHKey != "" {
if _, err := os.Stat(d.SSHKey); os.IsNotExist(err) {
return fmt.Errorf("SSH key does not exist: %q", d.SSHKey)
}
// TODO: validate the key is a valid key
}
return nil
}
func (d *Driver) Create() error {
if d.SSHKey == "" {
log.Info("No SSH key specified. Assuming an existing key at the default location.")
} else {
log.Info("Importing SSH key...")
d.SSHKeyPath = d.ResolveStorePath(path.Base(d.SSHKey))
if err := copySSHKey(d.SSHKey, d.SSHKeyPath); err != nil {
return err
}
if err := copySSHKey(d.SSHKey+".pub", d.SSHKeyPath+".pub"); err != nil {
log.Infof("Couldn't copy SSH public key : %s", err)
}
}
log.Debugf("IP: %s", d.IPAddress)
return nil
}
func (d *Driver) GetURL() (string, error) {
if err := drivers.MustBeRunning(d); err != nil {
return "", err
}
ip, err := d.GetIP()
if err != nil {
return "", err
}
return fmt.Sprintf("tcp://%s", net.JoinHostPort(ip, strconv.Itoa(d.EnginePort))), nil
}
func (d *Driver) GetState() (state.State, error) {
address := net.JoinHostPort(d.IPAddress, strconv.Itoa(d.SSHPort))
_, err := net.DialTimeout("tcp", address, defaultTimeout)
if err != nil {
return state.Stopped, nil
}
return state.Running, nil
}
func (d *Driver) Start() error {
return errors.New("generic driver does not support start")
}
func (d *Driver) Stop() error {
return errors.New("generic driver does not support stop")
}
func (d *Driver) Restart() error {
_, err := drivers.RunSSHCommandFromDriver(d, "sudo shutdown -r now")
return err
}
func (d *Driver) Kill() error {
return errors.New("generic driver does not support kill")
}
func (d *Driver) Remove() error {
return nil
}
func copySSHKey(src, dst string) error {
if err := mcnutils.CopyFile(src, dst); err != nil {
return fmt.Errorf("unable to copy ssh key: %s", err)
}
if err := os.Chmod(dst, 0600); err != nil {
return fmt.Errorf("unable to set permissions on the ssh key: %s", err)
}
return nil
}