From fca27eaad0ecd5d55349b0af84c6618fe70f1810 Mon Sep 17 00:00:00 2001 From: Ian Denhardt Date: Tue, 9 Oct 2018 16:30:02 -0400 Subject: [PATCH] Add a docker-spk build subcommand. --- README.md | 18 ++++++++++++++---- build.go | 38 ++++++++++++++++++++++++++++++++++++++ main.go | 5 +++-- pack.go | 3 +++ 4 files changed, 58 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1661d35..b2d6057 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,19 @@ overridden with the `-keyring` flag). Edit the file to match your app. In particular, you will want to change the command used to launch the app, near the bottom of the file. -Then, get a Docker image to convert. Invoke: +Then, create a `Dockerfile` in the current directory, which will be +responsible for building the filesystem for your app. Finally, from the +directory containing `Dockerfile` and `sandstorm-pkgdef.capnp`, run: + +``` +docker-spk build +``` + +This will build the docker image and then package it into a `.spk` file. +with the name derived from the app name and version defined in +`sandstorm-manifest.capnp`. + +Alternatively, you can package an already-built docker image: ``` docker-spk pack -image @@ -52,8 +64,7 @@ docker-spk pack -image ...to use the image ``, fetched from a running Docker daemon. -This will create an `.spk` file, with the name derived from the app name -and version defined in `sandstorm-manifest.capnp`. +This will skip the build step and just create the `.spk`. You can also use `docker save` to fetch the image manually and specify the file name via `-imagefile`: @@ -70,7 +81,6 @@ seeing how to package apps with `docker-spk`. # Reference - See `docker-spk -h`. # License diff --git a/build.go b/build.go index 72d00b4..d5c854f 100644 --- a/build.go +++ b/build.go @@ -1,7 +1,12 @@ package main import ( + "bufio" "flag" + "fmt" + "os" + "os/exec" + "regexp" "strings" ) @@ -44,6 +49,39 @@ func (f *buildFlags) Parse() { f.pkgDefVar = pkgDefParts[1] } +var buildOkRegexp = regexp.MustCompile("Successfully built ([0-9a-fA-F]+)") + func buildCmd() { + bFlags := &buildFlags{} + bFlags.Register() + bFlags.Parse() + + cmd := exec.Command("docker", "build", ".") + cmd.Stderr = os.Stderr + out, err := cmd.StdoutPipe() + chkfatal("Creating pipe for docker build", err) + chkfatal("Starting docker build", cmd.Start()) + r := bufio.NewScanner(out) + + image := "" + for r.Scan() { + line := r.Text() + fmt.Println(line) + subs := buildOkRegexp.FindStringSubmatch(line) + if subs != nil && len(subs) == 2 { + image = subs[1] + } + } + chkfatal("Parsing output from docker build", err) + chkfatal("Problem invoking docker build", cmd.Wait()) + if image == "" { + fmt.Fprintln(os.Stderr, + "Could not determine image id built by docker build.") + os.Exit(1) + } + doPack(&packFlags{ + buildFlags: *bFlags, + image: image, + }) } diff --git a/main.go b/main.go index 94399b4..57c8f76 100644 --- a/main.go +++ b/main.go @@ -59,8 +59,9 @@ func usageErr(info string) { func main() { subCommands := map[string]func(){ - "pack": packCmd, - "init": initCmd, + "pack": packCmd, + "init": initCmd, + "build": buildCmd, } flag.Usage = func() { keys := []string{} diff --git a/pack.go b/pack.go index 150e312..437c198 100644 --- a/pack.go +++ b/pack.go @@ -112,7 +112,10 @@ func packCmd() { pFlags := &packFlags{} pFlags.Register() pFlags.Parse() + doPack(pFlags) +} +func doPack(pFlags *packFlags) { metadata := getPkgMetadata(pFlags.pkgDefFile, pFlags.pkgDefVar) keyring, err := loadKeyring(*keyringPath)