From 3637f0a5bd7838189c0e16e914cd09aa58211efe Mon Sep 17 00:00:00 2001 From: Justin Cormack Date: Mon, 6 Mar 2017 21:25:11 +0000 Subject: [PATCH] Out with the old, in with the new Moby - remove remainder of editions code - add a new check container to run tests without Docker - switch over `make test` to use new command to build tests Signed-off-by: Justin Cormack --- moby/config.go => config.go | 21 +++++++++++++++++---- moby/main.go => main.go | 34 ++++++++++++++++++++++++++++------ moby/output.go => output.go | 16 ++++++++-------- 3 files changed, 53 insertions(+), 18 deletions(-) rename moby/config.go => config.go (78%) rename moby/main.go => main.go (85%) rename moby/output.go => output.go (78%) diff --git a/moby/config.go b/config.go similarity index 78% rename from moby/config.go rename to config.go index 78383e393..b4d2096fa 100644 --- a/moby/config.go +++ b/config.go @@ -4,6 +4,7 @@ import ( "archive/tar" "bytes" "errors" + "fmt" "path" "strconv" "strings" @@ -11,10 +12,12 @@ import ( "gopkg.in/yaml.v2" ) +// Moby is the type of a Moby config file type Moby struct { Kernel string Init string System []MobyImage + Daemon []MobyImage Files []struct { Path string Contents string @@ -24,6 +27,7 @@ type Moby struct { } } +// MobyImage is the type of an image config, based on Compose type MobyImage struct { Name string Image string @@ -32,10 +36,12 @@ type MobyImage struct { OomScoreAdj int64 `yaml:"oom_score_adj"` Command []string NetworkMode string `yaml:"network_mode"` + Pid string } const riddler = "mobylinux/riddler:7d4545d8b8ac2700971a83f12a3446a76db28c14@sha256:11b7310df6482fc38aa52b419c2ef1065d7b9207c633d47554e13aa99f6c0b72" +// NewConfig parses a config file func NewConfig(config []byte) (*Moby, error) { m := Moby{} @@ -47,9 +53,11 @@ func NewConfig(config []byte) (*Moby, error) { return &m, nil } -func ConfigToRun(image *MobyImage) []string { +// ConfigToRun converts a config to a series of arguments for docker run +func ConfigToRun(order int, path string, image *MobyImage) []string { // riddler arguments - args := []string{"-v", "/var/run/docker.sock:/var/run/docker.sock", riddler, image.Image, "/containers/" + image.Name} + so := fmt.Sprintf("%03d", order) + args := []string{"-v", "/var/run/docker.sock:/var/run/docker.sock", riddler, image.Image, "/containers/" + path + "/" + so + "-" + image.Name} // docker arguments args = append(args, "--cap-drop", "all") for _, cap := range image.Capabilities { @@ -62,7 +70,12 @@ func ConfigToRun(image *MobyImage) []string { args = append(args, "--oom-score-adj", strconv.FormatInt(image.OomScoreAdj, 10)) } if image.NetworkMode != "" { - args = append(args, "--net", image.NetworkMode) + // TODO only "host" supported + args = append(args, "--net="+image.NetworkMode) + } + if image.Pid != "" { + // TODO only "host" supported + args = append(args, "--pid="+image.Pid) } for _, bind := range image.Binds { args = append(args, "-v", bind) @@ -75,7 +88,7 @@ func ConfigToRun(image *MobyImage) []string { return args } -func Filesystem(m *Moby) (*bytes.Buffer, error) { +func filesystem(m *Moby) (*bytes.Buffer, error) { buf := new(bytes.Buffer) tw := tar.NewWriter(buf) defer tw.Close() diff --git a/moby/main.go b/main.go similarity index 85% rename from moby/main.go rename to main.go index ec46cccc6..44b1075f3 100644 --- a/moby/main.go +++ b/main.go @@ -7,7 +7,9 @@ import ( "io" "io/ioutil" "log" + "os" "os/exec" + "path/filepath" "github.com/docker/moby/pkg/initrd" ) @@ -137,9 +139,18 @@ func build(configfile string) { buffer := bytes.NewBuffer(init) containers = append(containers, buffer) - for _, image := range m.System { - args := ConfigToRun(&image) - // get output tarball + for i, image := range m.System { + args := ConfigToRun(i, "system", &image) + out, err := dockerRun(args...) + if err != nil { + log.Fatalf("Failed to build container tarball: %v", err) + } + buffer := bytes.NewBuffer(out) + containers = append(containers, buffer) + } + + for i, image := range m.Daemon { + args := ConfigToRun(i, "daemon", &image) out, err := dockerRun(args...) if err != nil { log.Fatalf("Failed to build container tarball: %v", err) @@ -149,7 +160,7 @@ func build(configfile string) { } // add files - buffer, err = Filesystem(m) + buffer, err = filesystem(m) if err != nil { log.Fatalf("failed to add filesystem parts: %v", err) } @@ -160,12 +171,23 @@ func build(configfile string) { log.Fatalf("Failed to make initrd %v", err) } - err = outputs(m, bzimage.Bytes(), initrd.Bytes()) + base := filepath.Base(conf) + ext := filepath.Ext(conf) + if ext != "" { + base = base[:len(base)-len(ext)] + } + + err = outputs(m, base, bzimage.Bytes(), initrd.Bytes()) if err != nil { log.Fatalf("Error writing outputs: %v", err) } } +var conf = "moby.yaml" + func main() { - build("moby.yaml") + if len(os.Args) >= 2 { + conf = os.Args[1] + } + build(conf) } diff --git a/moby/output.go b/output.go similarity index 78% rename from moby/output.go rename to output.go index dcd5fd2ec..34f8e92c5 100644 --- a/moby/output.go +++ b/output.go @@ -13,21 +13,21 @@ const ( efi = "mobylinux/mkimage-iso-efi:40f35270037dae95584324427e56f829756ff145@sha256:ae5b37ae560a5e030342f3d493d4ad611f2694bcd54eba86bf42ca069da986a7" ) -func outputs(m *Moby, bzimage []byte, initrd []byte) error { +func outputs(m *Moby, base string, bzimage []byte, initrd []byte) error { for _, o := range m.Outputs { switch o.Format { case "kernel+initrd": - err := outputKernelInitrd(bzimage, initrd) + err := outputKernelInitrd(base, bzimage, initrd) if err != nil { return fmt.Errorf("Error writing %s output: %v", o.Format, err) } case "iso-bios": - err := outputISO(bios, "mobylinux.iso", bzimage, initrd) + err := outputISO(bios, base+".iso", bzimage, initrd) if err != nil { return fmt.Errorf("Error writing %s output: %v", o.Format, err) } case "iso-efi": - err := outputISO(efi, "mobylinux-efi.iso", bzimage, initrd) + err := outputISO(efi, base+"-efi.iso", bzimage, initrd) if err != nil { return fmt.Errorf("Error writing %s output: %v", o.Format, err) } @@ -87,15 +87,15 @@ func outputISO(image, filename string, bzimage []byte, initrd []byte) error { return nil } -func outputKernelInitrd(bzimage []byte, initrd []byte) error { - err := ioutil.WriteFile("initrd.img", initrd, os.FileMode(0644)) +func outputKernelInitrd(base string, bzimage []byte, initrd []byte) error { + err := ioutil.WriteFile(base+"-initrd.img", initrd, os.FileMode(0644)) if err != nil { return err } - err = ioutil.WriteFile("bzImage", bzimage, os.FileMode(0644)) + err = ioutil.WriteFile(base+"-bzImage", bzimage, os.FileMode(0644)) if err != nil { return err } - fmt.Println("bzImage initrd.img") + fmt.Println(base + "-bzImage " + base + "-initrd.img") return nil }