Skip to content

Commit fca27ea

Browse files
committed
Add a docker-spk build subcommand.
1 parent 332150c commit fca27ea

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

README.md

+14-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,19 @@ overridden with the `-keyring` flag).
4343
Edit the file to match your app. In particular, you will want to change
4444
the command used to launch the app, near the bottom of the file.
4545

46-
Then, get a Docker image to convert. Invoke:
46+
Then, create a `Dockerfile` in the current directory, which will be
47+
responsible for building the filesystem for your app. Finally, from the
48+
directory containing `Dockerfile` and `sandstorm-pkgdef.capnp`, run:
49+
50+
```
51+
docker-spk build
52+
```
53+
54+
This will build the docker image and then package it into a `.spk` file.
55+
with the name derived from the app name and version defined in
56+
`sandstorm-manifest.capnp`.
57+
58+
Alternatively, you can package an already-built docker image:
4759

4860
```
4961
docker-spk pack -image <image-name>
@@ -52,8 +64,7 @@ docker-spk pack -image <image-name>
5264
...to use the image `<image-name>`, fetched from a running Docker
5365
daemon.
5466

55-
This will create an `.spk` file, with the name derived from the app name
56-
and version defined in `sandstorm-manifest.capnp`.
67+
This will skip the build step and just create the `.spk`.
5768

5869
You can also use `docker save` to fetch the image manually and specify
5970
the file name via `-imagefile`:
@@ -70,7 +81,6 @@ seeing how to package apps with `docker-spk`.
7081

7182
# Reference
7283

73-
7484
See `docker-spk -h`.
7585

7686
# License

build.go

+38
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package main
22

33
import (
4+
"bufio"
45
"flag"
6+
"fmt"
7+
"os"
8+
"os/exec"
9+
"regexp"
510
"strings"
611
)
712

@@ -44,6 +49,39 @@ func (f *buildFlags) Parse() {
4449
f.pkgDefVar = pkgDefParts[1]
4550
}
4651

52+
var buildOkRegexp = regexp.MustCompile("Successfully built ([0-9a-fA-F]+)")
53+
4754
func buildCmd() {
55+
bFlags := &buildFlags{}
56+
bFlags.Register()
57+
bFlags.Parse()
58+
59+
cmd := exec.Command("docker", "build", ".")
60+
cmd.Stderr = os.Stderr
61+
out, err := cmd.StdoutPipe()
62+
chkfatal("Creating pipe for docker build", err)
63+
chkfatal("Starting docker build", cmd.Start())
64+
r := bufio.NewScanner(out)
65+
66+
image := ""
67+
for r.Scan() {
68+
line := r.Text()
69+
fmt.Println(line)
70+
subs := buildOkRegexp.FindStringSubmatch(line)
71+
if subs != nil && len(subs) == 2 {
72+
image = subs[1]
73+
}
74+
}
75+
chkfatal("Parsing output from docker build", err)
76+
chkfatal("Problem invoking docker build", cmd.Wait())
77+
if image == "" {
78+
fmt.Fprintln(os.Stderr,
79+
"Could not determine image id built by docker build.")
80+
os.Exit(1)
81+
}
4882

83+
doPack(&packFlags{
84+
buildFlags: *bFlags,
85+
image: image,
86+
})
4987
}

main.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ func usageErr(info string) {
5959

6060
func main() {
6161
subCommands := map[string]func(){
62-
"pack": packCmd,
63-
"init": initCmd,
62+
"pack": packCmd,
63+
"init": initCmd,
64+
"build": buildCmd,
6465
}
6566
flag.Usage = func() {
6667
keys := []string{}

pack.go

+3
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ func packCmd() {
112112
pFlags := &packFlags{}
113113
pFlags.Register()
114114
pFlags.Parse()
115+
doPack(pFlags)
116+
}
115117

118+
func doPack(pFlags *packFlags) {
116119
metadata := getPkgMetadata(pFlags.pkgDefFile, pFlags.pkgDefVar)
117120

118121
keyring, err := loadKeyring(*keyringPath)

0 commit comments

Comments
 (0)