Skip to content
This repository was archived by the owner on Dec 17, 2021. It is now read-only.

Commit 02ee328

Browse files
committed
cmd/machine: Add
1 parent efb4459 commit 02ee328

File tree

2 files changed

+232
-0
lines changed

2 files changed

+232
-0
lines changed

cmd/machine.go

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strconv"
7+
8+
"path/filepath"
9+
10+
"github.com/codegangsta/cli"
11+
"github.com/docker/machine/commands"
12+
"github.com/docker/machine/commands/mcndirs"
13+
"github.com/docker/machine/drivers/amazonec2"
14+
"github.com/docker/machine/drivers/azure"
15+
"github.com/docker/machine/drivers/digitalocean"
16+
"github.com/docker/machine/drivers/exoscale"
17+
"github.com/docker/machine/drivers/generic"
18+
"github.com/docker/machine/drivers/google"
19+
"github.com/docker/machine/drivers/hyperv"
20+
"github.com/docker/machine/drivers/none"
21+
"github.com/docker/machine/drivers/openstack"
22+
"github.com/docker/machine/drivers/rackspace"
23+
"github.com/docker/machine/drivers/softlayer"
24+
"github.com/docker/machine/drivers/virtualbox"
25+
"github.com/docker/machine/drivers/vmwarefusion"
26+
"github.com/docker/machine/drivers/vmwarevcloudair"
27+
"github.com/docker/machine/drivers/vmwarevsphere"
28+
"github.com/docker/machine/libmachine/drivers/plugin"
29+
"github.com/docker/machine/libmachine/drivers/plugin/localbinary"
30+
"github.com/docker/machine/libmachine/log"
31+
"github.com/docker/machine/version"
32+
)
33+
34+
var AppHelpTemplate = `Usage: {{.Name}} {{if .Flags}}[OPTIONS] {{end}}COMMAND [arg...]
35+
36+
{{.Usage}}
37+
38+
Version: {{.Version}}{{if or .Author .Email}}
39+
40+
Author:{{if .Author}}
41+
{{.Author}}{{if .Email}} - <{{.Email}}>{{end}}{{else}}
42+
{{.Email}}{{end}}{{end}}
43+
{{if .Flags}}
44+
Options:
45+
{{range .Flags}}{{.}}
46+
{{end}}{{end}}
47+
Commands:
48+
{{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
49+
{{end}}
50+
Run '{{.Name}} COMMAND --help' for more information on a command.
51+
`
52+
53+
var CommandHelpTemplate = `Usage: docker-machine {{.Name}}{{if .Flags}} [OPTIONS]{{end}} [arg...]
54+
55+
{{.Usage}}{{if .Description}}
56+
57+
Description:
58+
{{.Description}}{{end}}{{if .Flags}}
59+
60+
Options:
61+
{{range .Flags}}
62+
{{.}}{{end}}{{ end }}
63+
`
64+
65+
func setDebugOutputLevel() {
66+
// TODO: I'm not really a fan of this method and really would rather
67+
// use -v / --verbose TBQH
68+
for _, f := range os.Args {
69+
if f == "-D" || f == "--debug" || f == "-debug" {
70+
log.SetDebug(true)
71+
}
72+
}
73+
74+
debugEnv := os.Getenv("MACHINE_DEBUG")
75+
if debugEnv != "" {
76+
showDebug, err := strconv.ParseBool(debugEnv)
77+
if err != nil {
78+
fmt.Fprintf(os.Stderr, "Error parsing boolean value from MACHINE_DEBUG: %s\n", err)
79+
os.Exit(1)
80+
}
81+
log.SetDebug(showDebug)
82+
}
83+
}
84+
85+
func main() {
86+
if os.Getenv(localbinary.PluginEnvKey) == localbinary.PluginEnvVal {
87+
driverName := os.Getenv(localbinary.PluginEnvDriverName)
88+
runDriver(driverName)
89+
return
90+
}
91+
92+
localbinary.CurrentBinaryIsDockerMachine = true
93+
94+
setDebugOutputLevel()
95+
cli.AppHelpTemplate = AppHelpTemplate
96+
cli.CommandHelpTemplate = CommandHelpTemplate
97+
app := cli.NewApp()
98+
app.Name = filepath.Base(os.Args[0])
99+
app.Author = "Docker Machine Contributors"
100+
app.Email = "https://github.com/docker/machine"
101+
102+
app.Commands = commands.Commands
103+
app.CommandNotFound = cmdNotFound
104+
app.Usage = "Create and manage machines running Docker."
105+
app.Version = version.FullVersion()
106+
107+
log.Debug("Docker Machine Version: ", app.Version)
108+
109+
app.Flags = []cli.Flag{
110+
cli.BoolFlag{
111+
Name: "debug, D",
112+
Usage: "Enable debug mode",
113+
},
114+
cli.StringFlag{
115+
EnvVar: "MACHINE_STORAGE_PATH",
116+
Name: "storage-path, s",
117+
Value: mcndirs.GetBaseDir(),
118+
Usage: "Configures storage path",
119+
},
120+
cli.StringFlag{
121+
EnvVar: "MACHINE_TLS_CA_CERT",
122+
Name: "tls-ca-cert",
123+
Usage: "CA to verify remotes against",
124+
Value: "",
125+
},
126+
cli.StringFlag{
127+
EnvVar: "MACHINE_TLS_CA_KEY",
128+
Name: "tls-ca-key",
129+
Usage: "Private key to generate certificates",
130+
Value: "",
131+
},
132+
cli.StringFlag{
133+
EnvVar: "MACHINE_TLS_CLIENT_CERT",
134+
Name: "tls-client-cert",
135+
Usage: "Client cert to use for TLS",
136+
Value: "",
137+
},
138+
cli.StringFlag{
139+
EnvVar: "MACHINE_TLS_CLIENT_KEY",
140+
Name: "tls-client-key",
141+
Usage: "Private key used in client TLS auth",
142+
Value: "",
143+
},
144+
cli.StringFlag{
145+
EnvVar: "MACHINE_GITHUB_API_TOKEN",
146+
Name: "github-api-token",
147+
Usage: "Token to use for requests to the Github API",
148+
Value: "",
149+
},
150+
cli.BoolFlag{
151+
EnvVar: "MACHINE_NATIVE_SSH",
152+
Name: "native-ssh",
153+
Usage: "Use the native (Go-based) SSH implementation.",
154+
},
155+
cli.StringFlag{
156+
EnvVar: "MACHINE_BUGSNAG_API_TOKEN",
157+
Name: "bugsnag-api-token",
158+
Usage: "BugSnag API token for crash reporting",
159+
Value: "",
160+
},
161+
}
162+
163+
if err := app.Run(os.Args); err != nil {
164+
log.Error(err)
165+
}
166+
}
167+
168+
func runDriver(driverName string) {
169+
switch driverName {
170+
case "amazonec2":
171+
plugin.RegisterDriver(amazonec2.NewDriver("", ""))
172+
case "azure":
173+
plugin.RegisterDriver(azure.NewDriver("", ""))
174+
case "digitalocean":
175+
plugin.RegisterDriver(digitalocean.NewDriver("", ""))
176+
case "exoscale":
177+
plugin.RegisterDriver(exoscale.NewDriver("", ""))
178+
case "generic":
179+
plugin.RegisterDriver(generic.NewDriver("", ""))
180+
case "google":
181+
plugin.RegisterDriver(google.NewDriver("", ""))
182+
case "hyperv":
183+
plugin.RegisterDriver(hyperv.NewDriver("", ""))
184+
case "none":
185+
plugin.RegisterDriver(none.NewDriver("", ""))
186+
case "openstack":
187+
plugin.RegisterDriver(openstack.NewDriver("", ""))
188+
case "rackspace":
189+
plugin.RegisterDriver(rackspace.NewDriver("", ""))
190+
case "softlayer":
191+
plugin.RegisterDriver(softlayer.NewDriver("", ""))
192+
case "virtualbox":
193+
plugin.RegisterDriver(virtualbox.NewDriver("", ""))
194+
case "vmwarefusion":
195+
plugin.RegisterDriver(vmwarefusion.NewDriver("", ""))
196+
case "vmwarevcloudair":
197+
plugin.RegisterDriver(vmwarevcloudair.NewDriver("", ""))
198+
case "vmwarevsphere":
199+
plugin.RegisterDriver(vmwarevsphere.NewDriver("", ""))
200+
default:
201+
fmt.Fprintf(os.Stderr, "Unsupported driver: %s\n", driverName)
202+
os.Exit(1)
203+
}
204+
}
205+
206+
func cmdNotFound(c *cli.Context, command string) {
207+
log.Errorf(
208+
"%s: '%s' is not a %s command. See '%s --help'.",
209+
c.App.Name,
210+
command,
211+
c.App.Name,
212+
os.Args[0],
213+
)
214+
os.Exit(1)
215+
}

cmd/machine_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/docker/machine/commands/mcndirs"
8+
)
9+
10+
func TestStorePathSetCorrectly(t *testing.T) {
11+
mcndirs.BaseDir = ""
12+
os.Args = []string{"docker-machine", "--storage-path", "/tmp/foo"}
13+
main()
14+
if mcndirs.BaseDir != "/tmp/foo" {
15+
t.Fatal("Expected MACHINE_STORAGE_PATH environment variable to be /tmp/foo but was ", os.Getenv("MACHINE_STORAGE_PATH"))
16+
}
17+
}

0 commit comments

Comments
 (0)