moby: Introduce the "build" subcommand

Signed-off-by: Rolf Neugebauer <rolf.neugebauer@docker.com>
This commit is contained in:
Rolf Neugebauer 2017-03-21 21:27:47 +00:00
parent a188c21ddd
commit 759637b3f0
5 changed files with 63 additions and 34 deletions

View File

@ -15,12 +15,12 @@ bin/moby: $(MOBY_DEPS) | bin
tar cf - vendor src/initrd src/pad4 -C src/cmd/moby . | docker run --rm --net=none --log-driver=none -i $(CROSS) $(GO_COMPILE) --package github.com/docker/moby -o $@ | tar xf -
moby-initrd.img: bin/moby moby.yaml
$^
bin/moby build moby.yaml
moby-bzImage: moby-initrd.img
test-initrd.img: bin/moby test/test.yaml
$^
bin/moby build test/test.yaml
test-bzImage: test-initrd.img

View File

@ -22,7 +22,7 @@ This will build the Moby customisation tool and a Moby initrd image.
If you already have a Go build environment and installed the source in your `GOPATH`
you can do `go install github.com/docker/moby/cmd/moby` to install the `moby` tool
instead, and then use `moby moby.yaml` to build the example configuration.
instead, and then use `moby build moby.yaml` to build the example configuration.
#### Build requirements
@ -40,7 +40,7 @@ instead, and then use `moby moby.yaml` to build the example configuration.
## Customise
To customise, copy or modify the [`moby.yaml`](moby.yaml) to your own `file.yaml` or use on of the [examples](examples/) and then run `./bin/moby file.yaml` to
To customise, copy or modify the [`moby.yaml`](moby.yaml) to your own `file.yaml` or use on of the [examples](examples/) and then run `./bin/moby build file.yaml` to
generate its specified output. You can run the output with `./scripts/qemu.sh` or `./scripts/hyperkit.sh`, or on other
platforms.

View File

@ -5,7 +5,9 @@ import (
"bytes"
"errors"
"io"
"io/ioutil"
"log"
"path/filepath"
"github.com/docker/moby/src/initrd"
)
@ -66,7 +68,30 @@ func containersInitrd(containers []*bytes.Buffer) (*bytes.Buffer, error) {
return w, nil
}
func build(m *Moby, name string) {
func build(name string, args []string) {
conf := "moby.yaml"
if len(args) > 0 {
conf = args[0]
}
if name == "" {
name = filepath.Base(conf)
ext := filepath.Ext(conf)
if ext != "" {
name = name[:len(name)-len(ext)]
}
}
config, err := ioutil.ReadFile(conf)
if err != nil {
log.Fatalf("Cannot open config file: %v", err)
}
m, err := NewConfig(config)
if err != nil {
log.Fatalf("Invalid config: %v", err)
}
containers := []*bytes.Buffer{}
// get kernel bzImage and initrd tarball from container

View File

@ -6,9 +6,8 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
)
func dockerRun(args ...string) ([]byte, error) {
@ -96,37 +95,40 @@ func dockerRunInput(input io.Reader, args ...string) ([]byte, error) {
return stdout, nil
}
var (
conf string
name string
)
func main() {
flag.StringVar(&name, "name", "", "Name to use for output files")
flag.Parse()
conf = "moby.yaml"
if len(flag.Args()) > 0 {
conf = flag.Args()[0]
flag.Usage = func() {
fmt.Printf("USAGE: %s COMMAND\n\n", os.Args[0])
fmt.Printf("Commands:\n")
fmt.Printf(" build Build a Moby image from a YAML file\n")
fmt.Printf(" help Print this message\n")
fmt.Printf("\n")
fmt.Printf("Run '%s COMMAND --help' for more information on the command\n", os.Args[0])
}
if name == "" {
name = filepath.Base(conf)
ext := filepath.Ext(conf)
if ext != "" {
name = name[:len(name)-len(ext)]
}
buildCmd := flag.NewFlagSet("build", flag.ExitOnError)
buildCmd.Usage = func() {
fmt.Printf("USAGE: %s build [options] [file.yaml]\n\n", os.Args[0])
fmt.Printf("'file.yaml' defaults to 'moby.yaml' if not specified.\n\n")
fmt.Printf("Options:\n")
buildCmd.PrintDefaults()
}
buildName := buildCmd.String("name", "", "Name to use for output files")
if len(os.Args) < 2 {
fmt.Printf("Please specify a command.\n\n")
flag.Usage()
os.Exit(1)
}
config, err := ioutil.ReadFile(conf)
if err != nil {
log.Fatalf("Cannot open config file: %v", err)
switch os.Args[1] {
case "build":
buildCmd.Parse(os.Args[2:])
build(*buildName, buildCmd.Args())
case "help":
flag.Usage()
default:
fmt.Printf("%q is not valid command.\n\n", os.Args[1])
flag.Usage()
os.Exit(1)
}
m, err := NewConfig(config)
if err != nil {
log.Fatalf("Invalid config: %v", err)
}
build(m, name)
}

View File

@ -1,5 +1,7 @@
github.com/docker/hyperkit/go 9f093be2e131133a7ec63f859d75242f1db9116e
github.com/googleapis/gax-go 8c5154c0fe5bf18cf649634d4c6df50897a32751
github.com/golang/protobuf/proto c9c7427a2a70d2eb3bafa0ab2dc163e45f143317
github.com/rneugeba/iso9660wrap 9c7eaf5ac74b2416be8b7b8d1f35b9af44a6e4fa
github.com/surma/gocpio fcb68777e7dc4ea43ffce871b552c0d073c17495
cloud.google.com/go/storage 0bee953ffbf6fda3be3f56f1e9578f16a1e978a4
golang.org/x/net/context a6577fac2d73be281a500b310739095313165611