Skip to content

Commit

Permalink
Add a docker-spk build subcommand.
Browse files Browse the repository at this point in the history
  • Loading branch information
zenhack committed Oct 9, 2018
1 parent 332150c commit fca27ea
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <image-name>
Expand All @@ -52,8 +64,7 @@ docker-spk pack -image <image-name>
...to use the image `<image-name>`, 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`:
Expand All @@ -70,7 +81,6 @@ seeing how to package apps with `docker-spk`.

# Reference


See `docker-spk -h`.

# License
Expand Down
38 changes: 38 additions & 0 deletions build.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package main

import (
"bufio"
"flag"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
)

Expand Down Expand Up @@ -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,
})
}
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
3 changes: 3 additions & 0 deletions pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fca27ea

Please sign in to comment.