Put all core drivers in docker-machine binary

Signed-off-by: David Gageot <david@gageot.net>
This commit is contained in:
David Gageot 2015-11-28 11:02:01 +01:00
parent e35b169139
commit 92ce49b367
20 changed files with 99 additions and 196 deletions

View File

@ -99,21 +99,13 @@ This will generate and open the report file:
### Advanced build targets
Just build the machine binary itself (native):
Build for all supported OSes and architectures (binaries will be in the `bin` project subfolder):
make machine
make build-x
Just build the plugins (native):
Build for a specific list of OSes and architectures:
make plugins
Build for all supported oses and architectures (binaries will be in the `bin` project subfolder):
make cross
Build for a specific list of oses and architectures:
TARGET_OS=linux TARGET_ARCH="amd64 arm" make cross
TARGET_OS=linux TARGET_ARCH="amd64 arm" make build-x
You can further control build options through the following environment variables:

View File

@ -1,10 +0,0 @@
package main
import (
"github.com/docker/machine/drivers/amazonec2"
"github.com/docker/machine/libmachine/drivers/plugin"
)
func main() {
plugin.RegisterDriver(amazonec2.NewDriver("", ""))
}

View File

@ -1,10 +0,0 @@
package main
import (
"github.com/docker/machine/drivers/azure"
"github.com/docker/machine/libmachine/drivers/plugin"
)
func main() {
plugin.RegisterDriver(azure.NewDriver("", ""))
}

View File

@ -1,10 +0,0 @@
package main
import (
"github.com/docker/machine/drivers/digitalocean"
"github.com/docker/machine/libmachine/drivers/plugin"
)
func main() {
plugin.RegisterDriver(digitalocean.NewDriver("", ""))
}

View File

@ -1,10 +0,0 @@
package main
import (
"github.com/docker/machine/drivers/exoscale"
"github.com/docker/machine/libmachine/drivers/plugin"
)
func main() {
plugin.RegisterDriver(exoscale.NewDriver("", ""))
}

View File

@ -1,10 +0,0 @@
package main
import (
"github.com/docker/machine/drivers/generic"
"github.com/docker/machine/libmachine/drivers/plugin"
)
func main() {
plugin.RegisterDriver(generic.NewDriver("", ""))
}

View File

@ -1,10 +0,0 @@
package main
import (
"github.com/docker/machine/drivers/google"
"github.com/docker/machine/libmachine/drivers/plugin"
)
func main() {
plugin.RegisterDriver(google.NewDriver("", ""))
}

View File

@ -1,10 +0,0 @@
package main
import (
"github.com/docker/machine/drivers/hyperv"
"github.com/docker/machine/libmachine/drivers/plugin"
)
func main() {
plugin.RegisterDriver(hyperv.NewDriver("", ""))
}

View File

@ -1,10 +0,0 @@
package main
import (
"github.com/docker/machine/drivers/none"
"github.com/docker/machine/libmachine/drivers/plugin"
)
func main() {
plugin.RegisterDriver(none.NewDriver("", ""))
}

View File

@ -1,10 +0,0 @@
package main
import (
"github.com/docker/machine/drivers/openstack"
"github.com/docker/machine/libmachine/drivers/plugin"
)
func main() {
plugin.RegisterDriver(openstack.NewDriver("", ""))
}

View File

@ -1,10 +0,0 @@
package main
import (
"github.com/docker/machine/drivers/rackspace"
"github.com/docker/machine/libmachine/drivers/plugin"
)
func main() {
plugin.RegisterDriver(rackspace.NewDriver("", ""))
}

View File

@ -1,10 +0,0 @@
package main
import (
"github.com/docker/machine/drivers/softlayer"
"github.com/docker/machine/libmachine/drivers/plugin"
)
func main() {
plugin.RegisterDriver(softlayer.NewDriver("", ""))
}

View File

@ -1,10 +0,0 @@
package main
import (
"github.com/docker/machine/drivers/virtualbox"
"github.com/docker/machine/libmachine/drivers/plugin"
)
func main() {
plugin.RegisterDriver(virtualbox.NewDriver("", ""))
}

View File

@ -1,10 +0,0 @@
package main
import (
"github.com/docker/machine/drivers/vmwarefusion"
"github.com/docker/machine/libmachine/drivers/plugin"
)
func main() {
plugin.RegisterDriver(vmwarefusion.NewDriver("", ""))
}

View File

@ -1,10 +0,0 @@
package main
import (
"github.com/docker/machine/drivers/vmwarevcloudair"
"github.com/docker/machine/libmachine/drivers/plugin"
)
func main() {
plugin.RegisterDriver(vmwarevcloudair.NewDriver("", ""))
}

View File

@ -1,10 +0,0 @@
package main
import (
"github.com/docker/machine/drivers/vmwarevsphere"
"github.com/docker/machine/libmachine/drivers/plugin"
)
func main() {
plugin.RegisterDriver(vmwarevsphere.NewDriver("", ""))
}

View File

@ -9,6 +9,23 @@ import (
"github.com/codegangsta/cli"
"github.com/docker/machine/commands"
"github.com/docker/machine/commands/mcndirs"
"github.com/docker/machine/drivers/amazonec2"
"github.com/docker/machine/drivers/azure"
"github.com/docker/machine/drivers/digitalocean"
"github.com/docker/machine/drivers/exoscale"
"github.com/docker/machine/drivers/generic"
"github.com/docker/machine/drivers/google"
"github.com/docker/machine/drivers/hyperv"
"github.com/docker/machine/drivers/none"
"github.com/docker/machine/drivers/openstack"
"github.com/docker/machine/drivers/rackspace"
"github.com/docker/machine/drivers/softlayer"
"github.com/docker/machine/drivers/virtualbox"
"github.com/docker/machine/drivers/vmwarefusion"
"github.com/docker/machine/drivers/vmwarevcloudair"
"github.com/docker/machine/drivers/vmwarevsphere"
"github.com/docker/machine/libmachine/drivers/plugin"
"github.com/docker/machine/libmachine/drivers/plugin/localbinary"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/version"
)
@ -65,6 +82,12 @@ func setDebugOutputLevel() {
}
func main() {
if os.Getenv(localbinary.PluginEnvKey) == localbinary.PluginEnvVal {
driverName := os.Getenv(localbinary.PluginEnvDriverName)
runDriver(driverName)
return
}
setDebugOutputLevel()
cli.AppHelpTemplate = AppHelpTemplate
cli.CommandHelpTemplate = CommandHelpTemplate
@ -133,6 +156,44 @@ func main() {
}
}
func runDriver(driverName string) {
switch driverName {
case "amazonec2":
plugin.RegisterDriver(amazonec2.NewDriver("", ""))
case "azure":
plugin.RegisterDriver(azure.NewDriver("", ""))
case "digitalocean":
plugin.RegisterDriver(digitalocean.NewDriver("", ""))
case "exoscale":
plugin.RegisterDriver(exoscale.NewDriver("", ""))
case "generic":
plugin.RegisterDriver(generic.NewDriver("", ""))
case "google":
plugin.RegisterDriver(google.NewDriver("", ""))
case "hyperv":
plugin.RegisterDriver(hyperv.NewDriver("", ""))
case "none":
plugin.RegisterDriver(none.NewDriver("", ""))
case "openstack":
plugin.RegisterDriver(openstack.NewDriver("", ""))
case "rackspace":
plugin.RegisterDriver(rackspace.NewDriver("", ""))
case "softlayer":
plugin.RegisterDriver(softlayer.NewDriver("", ""))
case "virtualbox":
plugin.RegisterDriver(virtualbox.NewDriver("", ""))
case "vmwarefusion":
plugin.RegisterDriver(vmwarefusion.NewDriver("", ""))
case "vmwarevcloudair":
plugin.RegisterDriver(vmwarevcloudair.NewDriver("", ""))
case "vmwarevsphere":
plugin.RegisterDriver(vmwarevsphere.NewDriver("", ""))
default:
fmt.Fprintf(os.Stderr, "Unsupported driver: %s\n", driverName)
os.Exit(1)
}
}
func cmdNotFound(c *cli.Context, command string) {
log.Fatalf(
"%s: '%s' is not a %s command. See '%s --help'.",

View File

@ -16,13 +16,18 @@ var (
// Timeout where we will bail if we're not able to properly contact the
// plugin server.
defaultTimeout = 10 * time.Second
CoreDrivers = [...]string{"amazonec2", "azure", "digitalocean",
"exoscale", "generic", "google", "hyperv", "none", "openstack",
"rackspace", "softlayer", "virtualbox", "vmwarefusion",
"vmwarevcloudair", "vmwarevsphere"}
)
const (
pluginOutPrefix = "(%s) "
pluginErrPrefix = "(%s) DBG | "
PluginEnvKey = "MACHINE_PLUGIN_TOKEN"
PluginEnvVal = "42"
pluginOutPrefix = "(%s) "
pluginErrPrefix = "(%s) DBG | "
PluginEnvKey = "MACHINE_PLUGIN_TOKEN"
PluginEnvVal = "42"
PluginEnvDriverName = "MACHINE_PLUGIN_DRIVER_NAME"
)
type PluginStreamer interface {
@ -84,8 +89,19 @@ func (e ErrPluginBinaryNotFound) Error() string {
return fmt.Sprintf("Driver %q not found. Do you have the plugin binary accessible in your PATH?", e.driverName)
}
func driverPath(driverName string) string {
for _, coreDriver := range CoreDrivers {
if coreDriver == driverName {
return os.Args[0] // "docker-machine"
}
}
return fmt.Sprintf("docker-machine-driver-%s", driverName)
}
func NewPlugin(driverName string) (*Plugin, error) {
binaryPath, err := exec.LookPath(fmt.Sprintf("docker-machine-driver-%s", driverName))
driverPath := driverPath(driverName)
binaryPath, err := exec.LookPath(driverPath)
if err != nil {
return nil, ErrPluginBinaryNotFound{driverName}
}
@ -123,6 +139,7 @@ func (lbe *Executor) Start() (*bufio.Scanner, *bufio.Scanner, error) {
errScanner := bufio.NewScanner(lbe.pluginStderr)
os.Setenv(PluginEnvKey, PluginEnvVal)
os.Setenv(PluginEnvDriverName, lbe.DriverName)
if err := cmd.Start(); err != nil {
return nil, nil, fmt.Errorf("Error starting plugin binary: %s", err)

View File

@ -1,31 +1,22 @@
build-clean:
rm -Rf $(PREFIX)/bin/*
extension = $(patsubst windows,.exe,$(filter windows,$(1)))
# Cross builder helper
define gocross
GOOS=$(1) GOARCH=$(2) CGO_ENABLED=0 go build \
GOOS=$(1) GOARCH=$(2) CGO_ENABLED=0 $(GO) build \
-o $(PREFIX)/bin/docker-machine_$(1)-$(2)/docker-$(patsubst cmd/%.go,%,$3)$(call extension,$(GOOS)) \
-a $(VERBOSE_GO) -tags "static_build netgo $(BUILDTAGS)" -installsuffix netgo \
-ldflags "$(GO_LDFLAGS) -extldflags -static" $(GO_GCFLAGS) $(3);
endef
# XXX building with -a fails in debug (with -N -l) ????
build-clean:
rm -Rf $(PREFIX)/bin/*
# Independent targets for every bin
$(PREFIX)/bin/docker-%: ./cmd/%.go $(shell find . -type f -name '*.go')
$(GO) build -o $@$(call extension,$(GOOS)) $(VERBOSE_GO) -tags "$(BUILDTAGS)" -ldflags "$(GO_LDFLAGS)" $(GO_GCFLAGS) $<
# Cross-compilation targets
build-x-%: ./cmd/%.go $(shell find . -type f -name '*.go')
build-x: ./cmd/machine.go
$(foreach GOARCH,$(TARGET_ARCH),$(foreach GOOS,$(TARGET_OS),$(call gocross,$(GOOS),$(GOARCH),$<)))
# Build just machine
build-machine: $(PREFIX)/bin/docker-machine
$(PREFIX)/bin/docker-machine$(call extension,$(GOOS)): ./cmd/machine.go
$(GO) build \
-o $@ \
$(VERBOSE_GO) -tags "$(BUILDTAGS)" \
-ldflags "$(GO_LDFLAGS)" $(GO_GCFLAGS) $<
# Build all plugins
build-plugins: $(patsubst ./cmd/%.go,$(PREFIX)/bin/docker-%,$(filter-out %_test.go, $(wildcard ./cmd/machine-driver-*.go)))
# Overall cross-build
build-x: $(patsubst ./cmd/%.go,build-x-%,$(filter-out %_test.go, $(wildcard ./cmd/*.go)))
build: $(PREFIX)/bin/docker-machine

View File

@ -44,24 +44,16 @@ include mk/release.mk
include mk/test.mk
include mk/validate.mk
.all_build: build build-clean build-x build-machine build-plugins
.all_build: build build-clean build-x
.all_coverage: coverage-generate coverage-html coverage-send coverage-serve coverage-clean
.all_release: release-checksum release
.all_test: test-short test-long test-integration
.all_validate: dco fmt vet lint
default: build
# Build native machine and all drivers
build: build-machine build-plugins
# Just build native machine itself
machine: build-machine
# Just build the native plugins
plugins: build-plugins
# Build all, cross platform
cross: build-x
install:
cp $(PREFIX)/bin/docker-machine $(PREFIX)/bin/docker-machine-driver* /usr/local/bin
cp $(PREFIX)/bin/docker-machine /usr/local/bin
clean: coverage-clean build-clean
test: dco fmt test-short lint vet