-
Notifications
You must be signed in to change notification settings - Fork 74
/
build.go
174 lines (144 loc) 路 4.57 KB
/
build.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
package daemon
import (
"encoding/json"
"errors"
"fmt"
"io"
"mime"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/mholt/archiver"
"github.com/testground/testground/pkg/api"
"github.com/testground/testground/pkg/logging"
"github.com/testground/testground/pkg/rpc"
)
func (d *Daemon) buildHandler(engine api.Engine) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
ruid := r.Header.Get("X-Request-ID")
log := logging.S().With("req_id", ruid)
log.Infow("handle request", "command", "build")
defer log.Infow("request handled", "command", "build")
tgw := rpc.NewOutputWriter(w, r)
// Create a packing directory under the workdir.
dir := filepath.Join(engine.EnvConfig().Dirs().Work(), "requests", ruid)
if err := os.MkdirAll(dir, 0755); err != nil {
tgw.WriteError("failed to create temp directory to unpack request", "err", err)
return
}
var request *api.BuildRequest
sources, err := consumeRunBuildRequest(r, &request, dir)
if err != nil {
tgw.WriteError("failed to consume request", "err", err)
return
}
if sources == nil || sources.PlanDir == "" {
tgw.WriteError("bad request", "err", errors.New("plan directory not present"))
return
}
id, err := engine.QueueBuild(request, sources)
if err != nil {
tgw.WriteError(fmt.Sprintf("engine build error: %s", err))
return
}
tgw.WriteResult(id)
}
}
func (d *Daemon) buildPurgeHandler(engine api.Engine) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log := logging.S().With("req_id", r.Header.Get("X-Request-ID"))
log.Debugw("handle request", "command", "build/purge")
defer log.Debugw("request handled", "command", "build/purge")
tgw := rpc.NewOutputWriter(w, r)
var req api.BuildPurgeRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
tgw.WriteError("build parge json decode", "err", err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}
err = engine.DoBuildPurge(r.Context(), req.Builder, req.Testplan, tgw)
if err != nil {
tgw.WriteError("build purge error", "err", err.Error())
return
}
tgw.WriteResult("build purge succeeded")
}
}
func consumeRunBuildRequest(r *http.Request, body interface{}, dir string) (*api.UnpackedSources, error) {
var (
p *multipart.Part
err error
)
if r.Body == nil {
return nil, fmt.Errorf("failed to parse request: nil body")
}
defer r.Body.Close()
// Validate the incoming multipart request.
ct, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil {
return nil, fmt.Errorf("failed to parse request: %w", err)
}
if !strings.HasPrefix(ct, "multipart/") {
ct := r.Header.Get("Content-Type")
return nil, fmt.Errorf("not a multipart request; Content-Type: %s", ct)
}
rd := multipart.NewReader(r.Body, params["boundary"])
// 1. Read and decode the request payload.
if p, err = rd.NextPart(); err != nil {
return nil, fmt.Errorf("unexpected error when reading composition: %w", err)
}
if err = json.NewDecoder(p).Decode(body); err != nil {
return nil, fmt.Errorf("failed to json decode request body: %w", err)
}
var unpacked *api.UnpackedSources
Outer:
for {
switch p, err = rd.NextPart(); err {
case io.EOF:
// we're done.
break Outer
case nil:
if unpacked == nil {
unpacked = new(api.UnpackedSources)
unpacked.BaseDir = dir
}
var (
filename = p.FileName() // can be sdk.zip or extra.zip
kind = strings.TrimSuffix(filename, ".zip")
)
// Read the archive.
targetzip, err := os.Create(filepath.Join(dir, filename))
if err != nil {
return nil, fmt.Errorf("failed to create file for %s: %w", kind, err)
}
if _, err = io.Copy(targetzip, p); err != nil {
return nil, fmt.Errorf("unexpected error when copying %s: %w", kind, err)
}
// Inflate the archive.
destdir := filepath.Join(dir, kind)
if err := os.Mkdir(destdir, 0755); err != nil {
return nil, fmt.Errorf("failed to create directory for sdk: %w", err)
}
logging.S().Infof("extracting %s to %s", filename, destdir)
if err := archiver.NewZip().Unarchive(targetzip.Name(), destdir); err != nil {
return nil, fmt.Errorf("failed to decompress sdk: %w", err)
}
// Set the right directory.
switch kind {
case "sdk":
unpacked.SDKDir = destdir
case "extra":
unpacked.ExtraDir = destdir
case "plan":
unpacked.PlanDir = destdir
}
default:
// an error occurred.
return nil, fmt.Errorf("unexpected error when reading optional parts: %w", err)
}
}
return unpacked, nil
}