X Tutup
Skip to content

Commit 19ce7b7

Browse files
Revise CommandLine interface to contain libmachine client and store
Signed-off-by: Nathan LeClaire <nathan.leclaire@gmail.com>
1 parent 3affe5e commit 19ce7b7

30 files changed

+356
-360
lines changed

cmd/machine.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"github.com/docker/machine/commands"
1111
"github.com/docker/machine/commands/mcndirs"
1212
"github.com/docker/machine/libmachine/log"
13-
"github.com/docker/machine/libmachine/mcnutils"
14-
"github.com/docker/machine/libmachine/ssh"
1513
"github.com/docker/machine/version"
1614
)
1715

@@ -74,16 +72,6 @@ func main() {
7472
app.Name = path.Base(os.Args[0])
7573
app.Author = "Docker Machine Contributors"
7674
app.Email = "https://github.com/docker/machine"
77-
app.Before = func(c *cli.Context) error {
78-
// TODO: Need better handling of config, everything is too
79-
// complected together right now.
80-
if c.GlobalBool("native-ssh") {
81-
ssh.SetDefaultClient(ssh.Native)
82-
}
83-
mcnutils.GithubAPIToken = c.GlobalString("github-api-token")
84-
mcndirs.BaseDir = c.GlobalString("storage-path")
85-
return nil
86-
}
8775

8876
app.Commands = commands.Commands
8977
app.CommandNotFound = cmdNotFound

commands/active.go

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,33 @@ import (
44
"errors"
55
"fmt"
66

7-
"github.com/docker/machine/libmachine/host"
7+
"github.com/docker/machine/libmachine"
88
"github.com/docker/machine/libmachine/persist"
99
)
1010

1111
var (
1212
errTooManyArguments = errors.New("Error: Too many arguments given")
13+
errNoActiveHost = errors.New("No active host found")
1314
)
1415

15-
func cmdActive(c CommandLine) error {
16+
func cmdActive(c CommandLine, api libmachine.API) error {
1617
if len(c.Args()) > 0 {
1718
return errTooManyArguments
1819
}
1920

20-
store := getStore(c)
21-
22-
host, err := getActiveHost(store)
21+
hosts, err := persist.LoadAllHosts(api)
2322
if err != nil {
2423
return fmt.Errorf("Error getting active host: %s", err)
2524
}
2625

27-
if host != nil {
28-
fmt.Println(host.Name)
29-
}
30-
31-
return nil
32-
}
33-
34-
func getActiveHost(store persist.Store) (*host.Host, error) {
35-
hosts, err := listHosts(store)
36-
if err != nil {
37-
return nil, err
38-
}
39-
40-
hostListItems := getHostListItems(hosts)
26+
items := getHostListItems(hosts)
4127

42-
for _, item := range hostListItems {
28+
for _, item := range items {
4329
if item.Active {
44-
return loadHost(store, item.Name)
30+
fmt.Println(item.Name)
31+
return nil
4532
}
4633
}
4734

48-
return nil, errors.New("Active host not found")
35+
return errNoActiveHost
4936
}

commands/commands.go

Lines changed: 46 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ import (
99

1010
"github.com/codegangsta/cli"
1111
"github.com/docker/machine/commands/mcndirs"
12-
"github.com/docker/machine/drivers/errdriver"
12+
"github.com/docker/machine/libmachine"
1313
"github.com/docker/machine/libmachine/cert"
14-
"github.com/docker/machine/libmachine/drivers"
15-
"github.com/docker/machine/libmachine/drivers/plugin/localbinary"
16-
"github.com/docker/machine/libmachine/drivers/rpc"
1714
"github.com/docker/machine/libmachine/host"
1815
"github.com/docker/machine/libmachine/log"
16+
"github.com/docker/machine/libmachine/mcnutils"
1917
"github.com/docker/machine/libmachine/persist"
18+
"github.com/docker/machine/libmachine/ssh"
2019
)
2120

2221
var (
@@ -64,26 +63,49 @@ func (c *contextCommandLine) Application() *cli.App {
6463
return c.App
6564
}
6665

67-
func newPluginDriver(driverName string, rawContent []byte) (drivers.Driver, error) {
68-
d, err := rpcdriver.NewRPCClientDriver(rawContent, driverName)
66+
func runAction(actionName string, c CommandLine, api libmachine.API) error {
67+
hosts, err := persist.LoadHosts(api, c.Args())
6968
if err != nil {
70-
// Not being able to find a driver binary is a "known error"
71-
if _, ok := err.(localbinary.ErrPluginBinaryNotFound); ok {
72-
return errdriver.NewDriver(driverName), nil
73-
}
74-
return nil, err
69+
return err
70+
}
71+
72+
if len(hosts) == 0 {
73+
return ErrNoMachineSpecified
7574
}
7675

77-
if driverName == "virtualbox" {
78-
return drivers.NewSerialDriver(d), nil
76+
if errs := runActionForeachMachine(actionName, hosts); len(errs) > 0 {
77+
return consolidateErrs(errs)
78+
}
79+
80+
for _, h := range hosts {
81+
if err := api.Save(h); err != nil {
82+
return fmt.Errorf("Error saving host to store: %s", err)
83+
}
7984
}
8085

81-
return d, nil
86+
return nil
8287
}
8388

84-
func fatalOnError(command func(commandLine CommandLine) error) func(context *cli.Context) {
89+
func fatalOnError(command func(commandLine CommandLine, api libmachine.API) error) func(context *cli.Context) {
8590
return func(context *cli.Context) {
86-
if err := command(&contextCommandLine{context}); err != nil {
91+
api := libmachine.NewClient(mcndirs.GetBaseDir())
92+
93+
if context.GlobalBool("native-ssh") {
94+
api.SSHClientType = ssh.Native
95+
}
96+
api.GithubAPIToken = context.GlobalString("github-api-token")
97+
api.Filestore.Path = context.GlobalString("storage-path")
98+
99+
// TODO (nathanleclaire): These should ultimately be accessed
100+
// through the libmachine client by the rest of the code and
101+
// not through their respective modules. For now, however,
102+
// they are also being set the way that they originally were
103+
// set to preserve backwards compatibility.
104+
mcndirs.BaseDir = api.Filestore.Path
105+
mcnutils.GithubAPIToken = api.GithubAPIToken
106+
ssh.SetDefaultClient(api.SSHClientType)
107+
108+
if err := command(&contextCommandLine{context}, api); err != nil {
87109
log.Fatal(err)
88110
}
89111
}
@@ -102,88 +124,6 @@ func confirmInput(msg string) (bool, error) {
102124
return confirmed, nil
103125
}
104126

105-
func getStore(c CommandLine) persist.Store {
106-
certInfo := getCertPathInfoFromContext(c)
107-
return &persist.Filestore{
108-
Path: c.GlobalString("storage-path"),
109-
CaCertPath: certInfo.CaCertPath,
110-
CaPrivateKeyPath: certInfo.CaPrivateKeyPath,
111-
}
112-
}
113-
114-
func listHosts(store persist.Store) ([]*host.Host, error) {
115-
cliHosts := []*host.Host{}
116-
117-
hosts, err := store.List()
118-
if err != nil {
119-
return nil, fmt.Errorf("Error attempting to list hosts from store: %s", err)
120-
}
121-
122-
for _, h := range hosts {
123-
d, err := newPluginDriver(h.DriverName, h.RawDriver)
124-
if err != nil {
125-
return nil, fmt.Errorf("Error attempting to invoke binary for plugin '%s': %s", h.DriverName, err)
126-
}
127-
128-
h.Driver = d
129-
130-
cliHosts = append(cliHosts, h)
131-
}
132-
133-
return cliHosts, nil
134-
}
135-
136-
func loadHost(store persist.Store, hostName string) (*host.Host, error) {
137-
h, err := store.Load(hostName)
138-
if err != nil {
139-
return nil, fmt.Errorf("Loading host from store failed: %s", err)
140-
}
141-
142-
d, err := newPluginDriver(h.DriverName, h.RawDriver)
143-
if err != nil {
144-
return nil, fmt.Errorf("Error attempting to invoke binary for plugin: %s", err)
145-
}
146-
147-
h.Driver = d
148-
149-
return h, nil
150-
}
151-
152-
func saveHost(store persist.Store, h *host.Host) error {
153-
if err := store.Save(h); err != nil {
154-
return fmt.Errorf("Error attempting to save host to store: %s", err)
155-
}
156-
157-
return nil
158-
}
159-
160-
func getFirstArgHost(c CommandLine) (*host.Host, error) {
161-
store := getStore(c)
162-
hostName := c.Args().First()
163-
164-
h, err := loadHost(store, hostName)
165-
if err != nil {
166-
return nil, fmt.Errorf("Error trying to get host %q: %s", hostName, err)
167-
}
168-
169-
return h, nil
170-
}
171-
172-
func getHostsFromContext(c CommandLine) ([]*host.Host, error) {
173-
store := getStore(c)
174-
hosts := []*host.Host{}
175-
176-
for _, hostName := range c.Args() {
177-
h, err := loadHost(store, hostName)
178-
if err != nil {
179-
return nil, fmt.Errorf("Could not load host %q: %s", hostName, err)
180-
}
181-
hosts = append(hosts, h)
182-
}
183-
184-
return hosts, nil
185-
}
186-
187127
var Commands = []cli.Command{
188128
{
189129
Name: "active",
@@ -427,52 +367,27 @@ func consolidateErrs(errs []error) error {
427367
return errors.New(strings.TrimSpace(finalErr))
428368
}
429369

430-
func runActionWithContext(actionName string, c CommandLine) error {
431-
store := getStore(c)
432-
433-
hosts, err := getHostsFromContext(c)
434-
if err != nil {
435-
return err
436-
}
437-
438-
if len(hosts) == 0 {
439-
return ErrNoMachineSpecified
440-
}
441-
442-
if errs := runActionForeachMachine(actionName, hosts); len(errs) > 0 {
443-
return consolidateErrs(errs)
444-
}
445-
446-
for _, h := range hosts {
447-
if err := saveHost(store, h); err != nil {
448-
return fmt.Errorf("Error saving host to store: %s", err)
449-
}
450-
}
451-
452-
return nil
453-
}
454-
455-
// Returns the cert paths.
456-
// codegangsta/cli will not set the cert paths if the storage-path is set to
457-
// something different so we cannot use the paths in the global options. le
458-
// sigh.
459-
func getCertPathInfoFromContext(c CommandLine) cert.PathInfo {
370+
// Returns the cert paths. codegangsta/cli will not set the cert paths if the
371+
// storage-path is set to something different so we cannot use the paths in the
372+
// global options. le sigh.
373+
func getCertPathInfoFromCommandLine(c CommandLine) cert.PathInfo {
460374
caCertPath := c.GlobalString("tls-ca-cert")
375+
caKeyPath := c.GlobalString("tls-ca-key")
376+
clientCertPath := c.GlobalString("tls-client-cert")
377+
clientKeyPath := c.GlobalString("tls-client-key")
378+
461379
if caCertPath == "" {
462380
caCertPath = filepath.Join(mcndirs.GetMachineCertDir(), "ca.pem")
463381
}
464382

465-
caKeyPath := c.GlobalString("tls-ca-key")
466383
if caKeyPath == "" {
467384
caKeyPath = filepath.Join(mcndirs.GetMachineCertDir(), "ca-key.pem")
468385
}
469386

470-
clientCertPath := c.GlobalString("tls-client-cert")
471387
if clientCertPath == "" {
472388
clientCertPath = filepath.Join(mcndirs.GetMachineCertDir(), "cert.pem")
473389
}
474390

475-
clientKeyPath := c.GlobalString("tls-client-key")
476391
if clientKeyPath == "" {
477392
clientKeyPath = filepath.Join(mcndirs.GetMachineCertDir(), "key.pem")
478393
}

commands/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"strings"
88

9+
"github.com/docker/machine/libmachine"
910
"github.com/docker/machine/libmachine/auth"
1011
"github.com/docker/machine/libmachine/cert"
1112
"github.com/docker/machine/libmachine/host"
@@ -26,7 +27,7 @@ Be advised that this will trigger a Docker daemon restart which will stop runnin
2627
`, e.hostURL, e.wrappedErr)
2728
}
2829

29-
func cmdConfig(c CommandLine) error {
30+
func cmdConfig(c CommandLine, api libmachine.API) error {
3031
// Ensure that log messages always go to stderr when this command is
3132
// being run (it is intended to be run in a subshell)
3233
log.SetOutWriter(os.Stderr)
@@ -35,7 +36,7 @@ func cmdConfig(c CommandLine) error {
3536
return ErrExpectedOneMachine
3637
}
3738

38-
host, err := getFirstArgHost(c)
39+
host, err := api.Load(c.Args().First())
3940
if err != nil {
4041
return err
4142
}

0 commit comments

Comments
 (0)
X Tutup