forked from appc/goaci
/
goaci.go
201 lines (177 loc) · 4.52 KB
/
goaci.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
// TODO(jonboulle): at a bare minimum, allow user to specify arguments to exec
// TODO(jonboulle): allow user to add assets to the ACI
// TODO(jonboulle): support user-specified GOPATHs/local packages. Right now we pull down a fresh copy of the specified package every time. This is better in terms of isolation and reproducibility, but inconvenient.
// TODO(jonboulle): add git SHA as a label in the image manifest
// TODO(jonboulle): support passing user-supplied arguments to `go get`? this might be tricky as we need to set a lot ourselves, and what if they conflict?
// TODO(jonboulle): support multiple executables?
import (
"archive/tar"
"bytes"
"compress/gzip"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/appc/spec/aci"
"github.com/appc/spec/schema"
"github.com/appc/spec/schema/types"
)
var Debug bool
func die(s string, i ...interface{}) {
s = fmt.Sprintf(s, i...)
fmt.Fprintln(os.Stderr, strings.TrimSuffix(s, "\n"))
os.Exit(1)
}
func debug(i ...interface{}) {
if Debug {
s := fmt.Sprint(i...)
fmt.Fprintln(os.Stderr, strings.TrimSuffix(s, "\n"))
}
}
func main() {
if os.Getenv("GOPATH") != "" {
die("to avoid confusion GOPATH must not be set")
}
goroot := os.Getenv("GOROOT")
if goroot == "" {
die("GOROOT must be set")
}
if os.Getenv("GOACI_DEBUG") != "" {
Debug = true
}
// Set up a temporary directory for everything (gopath and builds)
tmpdir, err := ioutil.TempDir("", "goaci")
if err != nil {
die("error setting up temporary directory: %v", err)
}
defer os.RemoveAll(tmpdir)
// Scratch build dir for aci
acidir := filepath.Join(tmpdir, "aci")
// Be explicit with gobin
gobin := filepath.Join(tmpdir, "bin")
// Find the go binary
gocmd, err := exec.LookPath("go")
if err != nil {
die("could not find `go` in path")
}
// Construct args for a go get that does a static build
// TODO(jonboulle): go version 1.4
args := []string{
gocmd,
"get",
"-a",
"-tags", "netgo",
"-ldflags", "'-w'",
}
// Extract the package name (which is the last arg).
var ns string
for _, arg := range os.Args[1:] {
// TODO(jonboulle): try to pass the other args on to go get?
// args = append(args, arg)
ns = arg
}
name, err := types.NewACName(ns)
// TODO(jonboulle): could this ever actually happen?
if err != nil {
die("bad app name: %v", err)
}
// Use the last component, e.g. example.com/my/app --> app
ofn := filepath.Base(ns) + ".aci"
mode := os.O_CREATE | os.O_WRONLY | os.O_TRUNC
of, err := os.OpenFile(ofn, mode, 0644)
if err != nil {
die("error opening output file: %v", err)
}
cmd := exec.Cmd{
Env: []string{
"GOPATH=" + tmpdir,
"GOBIN=" + gobin,
"GOROOT=" + goroot,
"CGO_ENABLED=0",
"PATH=" + os.Getenv("PATH"),
},
Path: gocmd,
Args: args,
Stderr: os.Stderr,
Stdout: os.Stdout,
}
debug("env:", cmd.Env)
debug("running command:", strings.Join(cmd.Args, " "))
if err := cmd.Run(); err != nil {
die("error running go: %v", err)
}
// Check that we got 1 binary from the go get command
fi, err := ioutil.ReadDir(gobin)
if err != nil {
die(err.Error())
}
switch {
case len(fi) < 1:
die("no binaries found in gobin")
case len(fi) > 1:
debug(fmt.Sprint(fi))
die("can't handle multiple binaries")
}
fn := fi[0].Name()
debug("found binary: ", fn)
// Set up rootfs for ACI layout
rfs := filepath.Join(acidir, "rootfs")
err = os.MkdirAll(rfs, 0755)
if err != nil {
die(err.Error())
}
// Move the binary into the rootfs
ep := filepath.Join(rfs, fn)
err = os.Rename(filepath.Join(gobin, fn), ep)
if err != nil {
die(err.Error())
}
debug("moved binary to:", ep)
// Build the ACI
im := schema.ImageManifest{
ACKind: types.ACKind("ImageManifest"),
ACVersion: schema.AppContainerVersion,
Name: *name,
App: &types.App{
Exec: types.Exec{
filepath.Join("/", fn),
},
User: "0",
Group: "0",
},
}
debug(im)
gw := gzip.NewWriter(of)
tr := tar.NewWriter(gw)
defer func() {
tr.Close()
gw.Close()
of.Close()
}()
iw := aci.NewImageWriter(im, tr)
err = filepath.Walk(acidir, aci.BuildWalker(acidir, iw))
if err != nil {
die(err.Error())
}
err = iw.Close()
if err != nil {
die(err.Error())
}
fmt.Println("Wrote", of.Name())
}
// strip replaces all characters that are not [a-Z_] with _
func strip(in string) string {
out := bytes.Buffer{}
for _, c := range in {
if !strings.ContainsRune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTVWXYZ0123456789_", c) {
c = '_'
}
if _, err := out.WriteRune(c); err != nil {
panic(err)
}
}
return out.String()
}