From 6f80f664ceb92ad8abcac7b99a101c089fae18d3 Mon Sep 17 00:00:00 2001 From: Justin Cormack Date: Mon, 24 Apr 2017 18:06:35 +0100 Subject: [PATCH] Split cli into moby and linuxkit moby just does the simple `build` cases, while `linuxkit` does `push` and `run`. Signed-off-by: Justin Cormack --- Makefile | 37 +++++---- README.md | 24 +++--- src/cmd/{moby => linuxkit}/gcp.go | 0 src/cmd/linuxkit/main.go | 95 ++++++++++++++++++++++ src/cmd/{moby => linuxkit}/push.go | 0 src/cmd/{moby => linuxkit}/push_gcp.go | 0 src/cmd/{moby => linuxkit}/run.go | 0 src/cmd/{moby => linuxkit}/run_gcp.go | 0 src/cmd/{moby => linuxkit}/run_hyperkit.go | 0 src/cmd/{moby => linuxkit}/run_packet.go | 0 src/cmd/{moby => linuxkit}/run_qemu.go | 0 src/cmd/{moby => linuxkit}/run_vmware.go | 0 src/cmd/{moby => linuxkit}/util.go | 0 src/cmd/moby/main.go | 6 -- 14 files changed, 129 insertions(+), 33 deletions(-) rename src/cmd/{moby => linuxkit}/gcp.go (100%) create mode 100644 src/cmd/linuxkit/main.go rename src/cmd/{moby => linuxkit}/push.go (100%) rename src/cmd/{moby => linuxkit}/push_gcp.go (100%) rename src/cmd/{moby => linuxkit}/run.go (100%) rename src/cmd/{moby => linuxkit}/run_gcp.go (100%) rename src/cmd/{moby => linuxkit}/run_hyperkit.go (100%) rename src/cmd/{moby => linuxkit}/run_packet.go (100%) rename src/cmd/{moby => linuxkit}/run_qemu.go (100%) rename src/cmd/{moby => linuxkit}/run_vmware.go (100%) rename src/cmd/{moby => linuxkit}/util.go (100%) diff --git a/Makefile b/Makefile index 5955be38e..da7f98281 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ .PHONY: default all -default: bin/moby +default: bin/moby bin/linuxkit all: default VERSION="0.0" # dummy for now @@ -8,6 +8,7 @@ GIT_COMMIT=$(shell git rev-list -1 HEAD) GO_COMPILE=linuxkit/go-compile:4513068d9a7e919e4ec42e2d7ee879ff5b95b7f5@sha256:bdfadbe3e4ec699ca45b67453662321ec270f2d1a1dbdbf09625776d3ebd68c5 MOBY?=bin/moby +LINUXKIT?=bin/linuxkit GOOS=$(shell uname -s | tr '[:upper:]' '[:lower:]') GOARCH=amd64 ifneq ($(GOOS),linux) @@ -27,6 +28,13 @@ bin/moby: $(MOBY_DEPS) | bin rm tmp_moby_bin.tar touch $@ +LINUXKIT_DEPS=$(wildcard src/cmd/linuxkit/*.go) Makefile vendor.conf +bin/linuxkit: $(LINUXKIT_DEPS) | bin + tar cf - vendor -C src/cmd/linuxkit . | docker run --rm --net=none --log-driver=none -i $(CROSS) $(GO_COMPILE) --package github.com/linuxkit/linuxkit --ldflags "-X main.GitCommit=$(GIT_COMMIT) -X main.Version=$(VERSION)" -o $@ > tmp_linuxkit_bin.tar + tar xf tmp_linuxkit_bin.tar > $@ + rm tmp_linuxkit_bin.tar + touch $@ + INFRAKIT_DEPS=$(wildcard src/cmd/infrakit-instance-hyperkit/*.go) Makefile vendor.conf bin/infrakit-instance-hyperkit: $(INFRAKIT_DEPS) | bin tar cf - vendor -C src/cmd/infrakit-instance-hyperkit . | docker run --rm --net=none --log-driver=none -i $(CROSS) $(GO_COMPILE) --package github.com/linuxkit/linuxkit -o $@ > tmp_infrakit_instance_hyperkit_bin.tar @@ -35,14 +43,13 @@ bin/infrakit-instance-hyperkit: $(INFRAKIT_DEPS) | bin touch $@ test-initrd.img: $(MOBY) test/test.yml - bin/moby build test/test.yml + $(MOBY) build test/test.yml test-bzImage: test-initrd.img -# interactive versions need to use volume mounts .PHONY: test-qemu-efi -test-qemu-efi: test-efi.iso - $(MOBY) run $^ | tee test-efi.log +test-qemu-efi: $(LINUXKIT) test-efi.iso + $(LINUXKIT) run $^ | tee test-efi.log $(call check_test_log, test-efi.log) bin: @@ -56,29 +63,29 @@ define check_test_log endef .PHONY: test-hyperkit -test-hyperkit: $(MOBY) test-initrd.img test-bzImage test-cmdline +test-hyperkit: $(LINUXKIT) test-initrd.img test-bzImage test-cmdline rm -f disk.img - $(MOBY) run test | tee test.log + $(LINUXKIT) run test | tee test.log $(call check_test_log, test.log) .PHONY: test-gcp -test-gcp: $(MOBY) test.img.tar.gz - $(MOBY) push gcp test.img.tar.gz - $(MOBY) run gcp test | tee test-gcp.log +test-gcp: $(LINUXKIT) test.img.tar.gz + $(LINUXKIT) push gcp test.img.tar.gz + $(LINUXKIT) run gcp test | tee test-gcp.log $(call check_test_log, test-gcp.log) .PHONY: test -test: test-initrd.img test-bzImage test-cmdline - $(MOBY) run test | tee test.log +test: $(LINUXKIT) test-initrd.img test-bzImage test-cmdline + $(LINUXKIT) run test | tee test.log $(call check_test_log, test.log) test-ltp.img.tar.gz: $(MOBY) test/ltp/test-ltp.yml $(MOBY) build test/ltp/test-ltp.yml .PHONY: test-ltp -test-ltp: $(MOBY) test-ltp.img.tar.gz - $(MOBY) push gcp test-ltp.img.tar.gz - $(MOBY) run gcp -skip-cleanup -machine n1-highcpu-4 test-ltp | tee test-ltp.log +test-ltp: $(LINUXKIT) test-ltp.img.tar.gz + $(LINUXKIT) push gcp test-ltp.img.tar.gz + $(LINUXKIT) run gcp -skip-cleanup -machine n1-highcpu-4 test-ltp | tee test-ltp.log $(call check_test_log, test-ltp.log) .PHONY: ci ci-tag ci-pr diff --git a/README.md b/README.md index da9a7f69d..c9223f620 100644 --- a/README.md +++ b/README.md @@ -15,17 +15,20 @@ LinuxKit, a toolkit for building custom minimal, immutable Linux distributions. ## Getting Started -### Build the `moby` tool +### Build the `moby` and `linuxkit` tools -Simple build instructions: use `make` to build. This will build the customisation tool in `bin/`. Add this -to your `PATH` or copy it to somewhere in your `PATH` eg `sudo cp bin/moby /usr/local/bin/`. Or you can use `sudo make install`. +LinuxKit uses the `moby` tool for image builds, and the `linuxkit` tool for pushing and running VM images. + +Simple build instructions: use `make` to build. This will build the tools in `bin/`. Add this +to your `PATH` or copy it to somewhere in your `PATH` eg `sudo cp bin/* /usr/local/bin/`. Or you can use `sudo make install`. If you already have `go` installed you can use `go get -u github.com/linuxkit/linuxkit/src/cmd/moby` to install -the `moby` tool. You can use `go get -u github.com/linuxkit/linuxkit/src/cmd/infrakit-instance-hyperkit` +the `moby` build tool, and `go get -u github.com/linuxkit/linuxkit/src/cmd/linuxkit` to install the `linuxkit` tool. +You can use `go get -u github.com/linuxkit/linuxkit/src/cmd/infrakit-instance-hyperkit` to get the hyperkit infrakit tool. Once you have built the tool, use `moby build linuxkit.yml` to build the example configuration, -and `bin/moby run linuxkit` to run locally. Use `halt` to terminate on the console. +and `linuxkit run linuxkit` to run locally. Use `halt` to terminate on the console. Build requirements: - GNU `make` @@ -34,16 +37,13 @@ Build requirements: ### Booting and Testing -You can use `moby run ` to execute the image you created with `moby build .yml`. +You can use `linuxkit run ` to execute the image you created with `moby build .yml`. This will use a suitable backend for your platform or you can choose one, for example VMWare. -See `moby run --help`. - -Some platforms do not yet have `moby run` support, so you can use `./scripts/qemu.sh moby-initrd.img moby-bzImage moby-cmdline` -or `./scripts/qemu.sh mobylinux-bios.iso` which runs qemu in a Docker container. +See `linuxkit run --help`. `make test` or `make test-hyperkit` will run the test suite -There are also docs for booting on [Google Cloud](docs/gcp.md); `./bin/moby run --gcp .yml` should +There are also docs for booting on [Google Cloud](docs/gcp.md); `linuxkit push gcp && linuxkit run gcp .yml` should work if you specified a GCP image to be built in the config. More detailed docs will be available shortly, for running both single hosts and clusters. @@ -51,7 +51,7 @@ More detailed docs will be available shortly, for running both single hosts and ## Building your own customised image To customise, copy or modify the [`linuxkit.yml`](linuxkit.yml) to your own `file.yml` or use one of the [examples](examples/) and then run `moby build file.yml` to -generate its specified output. You can run the output with `moby run file`. +generate its specified output. You can run the output with `linuxkit run file`. The yaml file specifies a kernel and base init system, a set of containers that are built into the generated image and started at boot time. It also specifies what formats to output, such as bootable ISOs and images for various platforms. diff --git a/src/cmd/moby/gcp.go b/src/cmd/linuxkit/gcp.go similarity index 100% rename from src/cmd/moby/gcp.go rename to src/cmd/linuxkit/gcp.go diff --git a/src/cmd/linuxkit/main.go b/src/cmd/linuxkit/main.go new file mode 100644 index 000000000..76cba44e7 --- /dev/null +++ b/src/cmd/linuxkit/main.go @@ -0,0 +1,95 @@ +package main + +import ( + "flag" + "fmt" + "os" + "path/filepath" + + log "github.com/Sirupsen/logrus" +) + +var ( + defaultLogFormatter = &log.TextFormatter{} + + // Version is the human-readable version + Version = "unknown" + + // GitCommit hash, set at compile time + GitCommit = "unknown" +) + +// infoFormatter overrides the default format for Info() log events to +// provide an easier to read output +type infoFormatter struct { +} + +func (f *infoFormatter) Format(entry *log.Entry) ([]byte, error) { + if entry.Level == log.InfoLevel { + return append([]byte(entry.Message), '\n'), nil + } + return defaultLogFormatter.Format(entry) +} + +func version() { + fmt.Printf("%s version %s\n", filepath.Base(os.Args[0]), Version) + fmt.Printf("commit: %s\n", GitCommit) + os.Exit(0) +} + +func main() { + flag.Usage = func() { + fmt.Printf("USAGE: %s [options] COMMAND\n\n", filepath.Base(os.Args[0])) + fmt.Printf("Commands:\n") + fmt.Printf(" push Push a VM image to a cloud or image store\n") + fmt.Printf(" run Run a VM image on a local hypervisor or remote cloud\n") + fmt.Printf(" version Print version information\n") + fmt.Printf(" help Print this message\n") + fmt.Printf("\n") + fmt.Printf("Run '%s COMMAND --help' for more information on the command\n", filepath.Base(os.Args[0])) + fmt.Printf("\n") + fmt.Printf("Options:\n") + flag.PrintDefaults() + } + flagQuiet := flag.Bool("q", false, "Quiet execution") + flagVerbose := flag.Bool("v", false, "Verbose execution") + + // Set up logging + log.SetFormatter(new(infoFormatter)) + log.SetLevel(log.InfoLevel) + flag.Parse() + if *flagQuiet && *flagVerbose { + fmt.Printf("Can't set quiet and verbose flag at the same time\n") + os.Exit(1) + } + if *flagQuiet { + log.SetLevel(log.ErrorLevel) + } + if *flagVerbose { + // Switch back to the standard formatter + log.SetFormatter(defaultLogFormatter) + log.SetLevel(log.DebugLevel) + } + + args := flag.Args() + if len(args) < 1 { + fmt.Printf("Please specify a command.\n\n") + flag.Usage() + os.Exit(1) + } + + switch args[0] { + case "push": + push(args[1:]) + case "run": + run(args[1:]) + case "version": + version() + case "help": + flag.Usage() + default: + fmt.Printf("%q is not valid command.\n\n", args[0]) + flag.Usage() + os.Exit(1) + } +} diff --git a/src/cmd/moby/push.go b/src/cmd/linuxkit/push.go similarity index 100% rename from src/cmd/moby/push.go rename to src/cmd/linuxkit/push.go diff --git a/src/cmd/moby/push_gcp.go b/src/cmd/linuxkit/push_gcp.go similarity index 100% rename from src/cmd/moby/push_gcp.go rename to src/cmd/linuxkit/push_gcp.go diff --git a/src/cmd/moby/run.go b/src/cmd/linuxkit/run.go similarity index 100% rename from src/cmd/moby/run.go rename to src/cmd/linuxkit/run.go diff --git a/src/cmd/moby/run_gcp.go b/src/cmd/linuxkit/run_gcp.go similarity index 100% rename from src/cmd/moby/run_gcp.go rename to src/cmd/linuxkit/run_gcp.go diff --git a/src/cmd/moby/run_hyperkit.go b/src/cmd/linuxkit/run_hyperkit.go similarity index 100% rename from src/cmd/moby/run_hyperkit.go rename to src/cmd/linuxkit/run_hyperkit.go diff --git a/src/cmd/moby/run_packet.go b/src/cmd/linuxkit/run_packet.go similarity index 100% rename from src/cmd/moby/run_packet.go rename to src/cmd/linuxkit/run_packet.go diff --git a/src/cmd/moby/run_qemu.go b/src/cmd/linuxkit/run_qemu.go similarity index 100% rename from src/cmd/moby/run_qemu.go rename to src/cmd/linuxkit/run_qemu.go diff --git a/src/cmd/moby/run_vmware.go b/src/cmd/linuxkit/run_vmware.go similarity index 100% rename from src/cmd/moby/run_vmware.go rename to src/cmd/linuxkit/run_vmware.go diff --git a/src/cmd/moby/util.go b/src/cmd/linuxkit/util.go similarity index 100% rename from src/cmd/moby/util.go rename to src/cmd/linuxkit/util.go diff --git a/src/cmd/moby/main.go b/src/cmd/moby/main.go index faf7b09b2..33a262585 100644 --- a/src/cmd/moby/main.go +++ b/src/cmd/moby/main.go @@ -42,8 +42,6 @@ func main() { fmt.Printf("USAGE: %s [options] COMMAND\n\n", filepath.Base(os.Args[0])) fmt.Printf("Commands:\n") fmt.Printf(" build Build a Moby image from a YAML file\n") - fmt.Printf(" push Push a VM image to a cloud or image store\n") - fmt.Printf(" run Run a VM image on a local hypervisor or remote cloud\n") fmt.Printf(" version Print version information\n") fmt.Printf(" help Print this message\n") fmt.Printf("\n") @@ -82,10 +80,6 @@ func main() { switch args[0] { case "build": build(args[1:]) - case "push": - push(args[1:]) - case "run": - run(args[1:]) case "version": version() case "help":