cli: Add support for multiple backends for "moby run"

- Move HyperKit code into a separate file. It should be compilable
  on all supported OSes now.
- Add a (optional) subcommand to "moby run" to select a backend
  i.e., "moby run hyperkit [options] [prefix]"
- On macOS the default is "hyperkit" so that:
  "moby run [options] [prefix]"
  just works
- Add enough command line parsing to make it easy to add new
  backends to the run command
Update help messages.

Signed-off-by: Rolf Neugebauer <rolf.neugebauer@docker.com>
This commit is contained in:
Rolf Neugebauer 2017-03-30 18:59:07 +01:00
parent 024b1d6d98
commit 4423d3e5de
3 changed files with 58 additions and 26 deletions

45
src/cmd/moby/run.go Normal file
View File

@ -0,0 +1,45 @@
package main
import (
"fmt"
"os"
"runtime"
log "github.com/Sirupsen/logrus"
)
func runUsage() {
fmt.Printf("USAGE: %s run [backend] [options] [prefix]\n\n", os.Args[0])
fmt.Printf("'backend' specifies the run backend.\n")
fmt.Printf("If not specified the platform specific default will be used\n")
fmt.Printf("Supported backends are (default platform in brackets):\n")
fmt.Printf(" hyperkit [macOS]\n")
fmt.Printf("\n")
fmt.Printf("'options' are the backend specific options.\n")
fmt.Printf("See 'moby run [backend] --help' for details.\n\n")
fmt.Printf("'prefix' specifies the path to the VM image.\n")
fmt.Printf("It defaults to './moby'.\n")
}
func run(args []string) {
if len(args) < 1 {
runUsage()
os.Exit(1)
}
switch args[0] {
case "help", "-h", "-help", "--help":
runUsage()
os.Exit(0)
case "hyperkit":
runHyperKit(args[1:])
default:
switch runtime.GOOS {
case "darwin":
runHyperKit(args)
default:
log.Errorf("There currently is no default 'run' backend for your platform.")
}
}
}

View File

@ -1,11 +0,0 @@
// +build !darwin
package main
import (
log "github.com/Sirupsen/logrus"
)
func run(args []string) {
log.Fatalf("'run' is not supported yet on your OS")
}

View File

@ -1,5 +1,3 @@
// +build darwin
package main
import (
@ -13,33 +11,33 @@ import (
)
// Process the run arguments and execute run
func run(args []string) {
runCmd := flag.NewFlagSet("run", flag.ExitOnError)
runCmd.Usage = func() {
fmt.Printf("USAGE: %s run [options] [prefix]\n\n", os.Args[0])
func runHyperKit(args []string) {
hyperkitCmd := flag.NewFlagSet("hyperkit", flag.ExitOnError)
hyperkitCmd.Usage = func() {
fmt.Printf("USAGE: %s run hyperkit [options] [prefix]\n\n", os.Args[0])
fmt.Printf("'prefix' specifies the path to the VM image.\n")
fmt.Printf("It defaults to './moby'.\n")
fmt.Printf("\n")
fmt.Printf("Options:\n")
runCmd.PrintDefaults()
hyperkitCmd.PrintDefaults()
}
runCPUs := runCmd.Int("cpus", 1, "Number of CPUs")
runMem := runCmd.Int("mem", 1024, "Amount of memory in MB")
runDiskSz := runCmd.Int("disk-size", 0, "Size of Disk in MB")
runDisk := runCmd.String("disk", "", "Path to disk image to used")
runCPUs := hyperkitCmd.Int("cpus", 1, "Number of CPUs")
runMem := hyperkitCmd.Int("mem", 1024, "Amount of memory in MB")
runDiskSz := hyperkitCmd.Int("disk-size", 0, "Size of Disk in MB")
runDisk := hyperkitCmd.String("disk", "", "Path to disk image to used")
runCmd.Parse(args)
remArgs := runCmd.Args()
hyperkitCmd.Parse(args)
remArgs := hyperkitCmd.Args()
prefix := "moby"
if len(remArgs) > 0 {
prefix = remArgs[0]
}
runInternal(*runCPUs, *runMem, *runDiskSz, *runDisk, prefix)
runHyperKitInternal(*runCPUs, *runMem, *runDiskSz, *runDisk, prefix)
}
func runInternal(cpus, mem, diskSz int, disk, prefix string) {
func runHyperKitInternal(cpus, mem, diskSz int, disk, prefix string) {
cmdline, err := ioutil.ReadFile(prefix + "-cmdline")
if err != nil {
log.Fatalf("Cannot open cmdline file: %v", err)