@@ -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
2221var (
@@ -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-
187127var 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 }
0 commit comments