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 547a3e9380
commit 7a6bc781de
2 changed files with 57 additions and 30 deletions

View File

@ -5,7 +5,9 @@ import (
"bytes" "bytes"
"errors" "errors"
"io" "io"
"io/ioutil"
"log" "log"
"path/filepath"
"github.com/docker/moby/src/initrd" "github.com/docker/moby/src/initrd"
) )
@ -66,7 +68,30 @@ func containersInitrd(containers []*bytes.Buffer) (*bytes.Buffer, error) {
return w, nil 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{} containers := []*bytes.Buffer{}
// get kernel bzImage and initrd tarball from container // get kernel bzImage and initrd tarball from container

View File

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