forked from carimura/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
304 lines (271 loc) · 6.91 KB
/
common.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"unicode"
"github.com/coreos/go-semver/semver"
"github.com/fnproject/cli/langs"
)
const (
functionsDockerImage = "fnproject/functions"
minRequiredDockerVersion = "17.5.0"
envFnRegistry = "FN_REGISTRY"
)
type HasRegistry interface {
Registry() string
}
func setRegistryEnv(hr HasRegistry) {
if hr.Registry() != "" {
err := os.Setenv(envFnRegistry, hr.Registry())
if err != nil {
log.Fatalf("Couldn't set %s env var: %v\n", envFnRegistry, err)
}
}
}
func buildfunc(fn string, noCache bool) (*funcfile, error) {
funcfile, err := parsefuncfile(fn)
if err != nil {
return nil, err
}
if funcfile.Version == "" {
funcfile, err = bumpversion(*funcfile)
if err != nil {
return nil, err
}
if err := storefuncfile(fn, funcfile); err != nil {
return nil, err
}
funcfile, err = parsefuncfile(fn)
if err != nil {
return nil, err
}
}
if err := localbuild(fn, funcfile.Build); err != nil {
return nil, err
}
if err := dockerbuild(fn, funcfile, noCache); err != nil {
return nil, err
}
return funcfile, nil
}
func localbuild(path string, steps []string) error {
for _, cmd := range steps {
exe := exec.Command("/bin/sh", "-c", cmd)
exe.Dir = filepath.Dir(path)
if err := exe.Run(); err != nil {
return fmt.Errorf("error running command %v (%v)", cmd, err)
}
}
return nil
}
func dockerbuild(path string, ff *funcfile, noCache bool) error {
err := dockerVersionCheck()
if err != nil {
return err
}
dir := filepath.Dir(path)
var helper langs.LangHelper
dockerfile := filepath.Join(dir, "Dockerfile")
if !exists(dockerfile) {
helper = langs.GetLangHelper(ff.Runtime)
if helper == nil {
return fmt.Errorf("Cannot build, no language helper found for %v", ff.Runtime)
}
dockerfile, err = writeTmpDockerfile(helper, dir, ff)
if err != nil {
return err
}
defer os.Remove(dockerfile)
if helper.HasPreBuild() {
err := helper.PreBuild()
if err != nil {
return err
}
}
}
fmt.Printf("Building image %v\n", ff.ImageName())
cancel := make(chan os.Signal, 3)
signal.Notify(cancel, os.Interrupt) // and others perhaps
defer signal.Stop(cancel)
result := make(chan error, 1)
go func(done chan<- error) {
args := []string{
"build",
"-t", ff.ImageName(),
"-f", dockerfile,
}
if noCache {
args = append(args, "--no-cache")
}
args = append(args,
"--build-arg", "HTTP_PROXY",
"--build-arg", "HTTPS_PROXY",
".")
cmd := exec.Command("docker", args...)
cmd.Dir = dir
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
done <- cmd.Run()
}(result)
select {
case err := <-result:
if err != nil {
return fmt.Errorf("error running docker build: %v", err)
}
case signal := <-cancel:
return fmt.Errorf("build cancelled on signal %v", signal)
}
if helper != nil {
err := helper.AfterBuild()
if err != nil {
return err
}
}
return nil
}
func dockerVersionCheck() error {
out, err := exec.Command("docker", "version", "--format", "{{.Server.Version}}").Output()
if err != nil {
return fmt.Errorf("could not check Docker version: %v", err)
}
// dev / test builds append '-ce', trim this
trimmed := strings.TrimRightFunc(string(out), func(r rune) bool { return r != '.' && !unicode.IsDigit(r) })
v, err := semver.NewVersion(trimmed)
if err != nil {
return fmt.Errorf("could not check Docker version: %v", err)
}
vMin, err := semver.NewVersion(minRequiredDockerVersion)
if err != nil {
return fmt.Errorf("our bad, sorry... please make an issue.", err)
}
if v.LessThan(*vMin) {
return fmt.Errorf("please upgrade your version of Docker to %s or greater", minRequiredDockerVersion)
}
return nil
}
func exists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func writeTmpDockerfile(helper langs.LangHelper, dir string, ff *funcfile) (string, error) {
if ff.Entrypoint == "" && ff.Cmd == "" {
return "", errors.New("entrypoint and cmd are missing, you must provide one or the other")
}
fd, err := ioutil.TempFile(dir, "Dockerfile")
if err != nil {
return "", err
}
defer fd.Close()
// multi-stage build: https://medium.com/travis-on-docker/multi-stage-docker-builds-for-creating-tiny-go-images-e0e1867efe5a
dfLines := []string{}
if helper.IsMultiStage() {
// build stage
dfLines = append(dfLines, fmt.Sprintf("FROM %s as build-stage", helper.BuildFromImage()))
} else {
dfLines = append(dfLines, fmt.Sprintf("FROM %s", helper.BuildFromImage()))
}
dfLines = append(dfLines, "WORKDIR /function")
dfLines = append(dfLines, helper.DockerfileBuildCmds()...)
if helper.IsMultiStage() {
// final stage
dfLines = append(dfLines, fmt.Sprintf("FROM %s", helper.RunFromImage()))
dfLines = append(dfLines, "WORKDIR /function")
dfLines = append(dfLines, helper.DockerfileCopyCmds()...)
}
if ff.Entrypoint != "" {
dfLines = append(dfLines, fmt.Sprintf("ENTRYPOINT [%s]", stringToSlice(ff.Entrypoint)))
}
if ff.Cmd != "" {
dfLines = append(dfLines, fmt.Sprintf("CMD [%s]", stringToSlice(ff.Cmd)))
}
err = writeLines(fd, dfLines)
if err != nil {
return "", err
}
return fd.Name(), err
}
func writeLines(w io.Writer, lines []string) error {
writer := bufio.NewWriter(w)
for _, l := range lines {
_, err := writer.WriteString(l + "\n")
if err != nil {
return err
}
}
writer.Flush()
return nil
}
func stringToSlice(in string) string {
epvals := strings.Fields(in)
var buffer bytes.Buffer
for i, s := range epvals {
if i > 0 {
buffer.WriteString(", ")
}
buffer.WriteString("\"")
buffer.WriteString(s)
buffer.WriteString("\"")
}
return buffer.String()
}
func extractEnvConfig(configs []string) map[string]string {
c := make(map[string]string)
for _, v := range configs {
kv := strings.SplitN(v, "=", 2)
if len(kv) == 2 {
c[kv[0]] = os.ExpandEnv(kv[1])
}
}
return c
}
func dockerpush(ff *funcfile) error {
err := validImageName(ff.ImageName())
if err != nil {
return err
}
fmt.Printf("Pushing %v to docker registry...", ff.ImageName())
cmd := exec.Command("docker", "push", ff.ImageName())
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
return fmt.Errorf("error running docker push: %v", err)
}
return nil
}
func validImageName(n string) error {
// must have at least owner name and a tag
split := strings.Split(n, ":")
if len(split) < 2 {
return errors.New("image name must have a tag")
}
split2 := strings.Split(split[0], "/")
if len(split2) < 2 {
return errors.New("image name must have an owner and name, eg: username/myfunc. Be sure to set FN_REGISTRY env var or pass in --registry.")
}
return nil
}
func appNamePath(img string) (string, string) {
sep := strings.Index(img, "/")
if sep < 0 {
return "", ""
}
tag := strings.Index(img[sep:], ":")
if tag < 0 {
tag = len(img[sep:])
}
return img[:sep], img[sep : sep+tag]
}