/
main.go
366 lines (325 loc) · 9.91 KB
/
main.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright 2015 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package apigen
import (
"bytes"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/exec"
"os/signal"
"strings"
"text/template"
"time"
"github.com/TriggerMail/luci-go/common/clock"
log "github.com/TriggerMail/luci-go/common/logging"
"github.com/TriggerMail/luci-go/common/retry"
"github.com/TriggerMail/luci-go/common/sync/parallel"
)
const (
defaultPackageBase = "github.com/TriggerMail/luci-go/common/api"
// chromiumLicence is the standard Chromium license header.
chromiumLicense = `` +
"// Copyright {{.Year}} The LUCI Authors.\n" +
"//\n" +
"// Licensed under the Apache License, Version 2.0 (the \"License\");\n" +
"// you may not use this file except in compliance with the License.\n" +
"// You may obtain a copy of the License at\n" +
"//\n" +
"// http://www.apache.org/licenses/LICENSE-2.0\n" +
"//\n" +
"// Unless required by applicable law or agreed to in writing, software\n" +
"// distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
"// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
"// See the License for the specific language governing permissions and\n" +
"// limitations under the License.\n" +
"\n"
)
var (
// chromiumLicenseTemplate is the compiled Chromium license template text.
chromiumLicenseTemplate *template.Template
)
func init() {
chromiumLicenseTemplate = template.Must(template.New("chromium license").Parse(chromiumLicense))
}
func compileChromiumLicense(c context.Context) (string, error) {
buf := bytes.Buffer{}
err := chromiumLicenseTemplate.Execute(&buf, map[string]interface{}{
"Year": clock.Now(c).Year(),
})
if err != nil {
return "", err
}
return buf.String(), nil
}
// Application is the main apigen application instance.
type Application struct {
servicePath string
serviceAPIRoot string
genPath string
apiPackage string
apiSubproject string
apiWhitelist apiWhitelist
baseURL string
license string
}
// AddToFlagSet adds application-level flags to the supplied FlagSet.
func (a *Application) AddToFlagSet(fs *flag.FlagSet) {
flag.StringVar(&a.servicePath, "service", ".",
"Path to the AppEngine service to generate from.")
flag.StringVar(&a.serviceAPIRoot, "service-api-root", "/_ah/api/",
"The service's API root path.")
flag.StringVar(&a.genPath, "generator", "google-api-go-generator",
"Path to the `google-api-go-generator` binary to use.")
flag.StringVar(&a.apiPackage, "api-package", defaultPackageBase,
"Name of the root API package on GOPATH.")
flag.StringVar(&a.apiSubproject, "api-subproject", "",
"If supplied, place APIs in an additional subdirectory under -api-package.")
flag.Var(&a.apiWhitelist, "api",
"If supplied, limit the emitted APIs to those named. Can be specified "+
"multiple times.")
flag.StringVar(&a.baseURL, "base-url", "http://localhost:8080",
"Use this as the default base service client URL.")
}
func resolveExecutable(path *string) error {
if path == nil || *path == "" {
return errors.New("empty path")
}
lpath, err := exec.LookPath(*path)
if err != nil {
return fmt.Errorf("could not find [%s]: %s", *path, err)
}
st, err := os.Stat(lpath)
if err != nil {
return err
}
if st.Mode().Perm()&0111 == 0 {
return errors.New("file is not executable")
}
*path = lpath
return nil
}
// retryHTTP executes an HTTP call to the specified URL, retrying if it fails.
//
// It will return an error if no successful HTTP results were returned.
// Otherwise, it will return the body of the successful HTTP response.
func retryHTTP(c context.Context, u url.URL, method, body string) ([]byte, error) {
client := http.Client{}
gen := func() retry.Iterator {
return &retry.Limited{
Delay: 2 * time.Second,
Retries: 20,
}
}
output := []byte(nil)
err := retry.Retry(c, gen, func() error {
req := http.Request{
Method: method,
URL: &u,
Header: http.Header{},
}
if len(body) > 0 {
req.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(body)))
req.ContentLength = int64(len(body))
req.Header.Add("Content-Type", "application/json")
}
resp, err := client.Do(&req)
if err != nil {
return err
}
if resp.Body != nil {
defer resp.Body.Close()
output, err = ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
}
switch resp.StatusCode {
case http.StatusOK, http.StatusNoContent:
return nil
default:
return fmt.Errorf("unsuccessful status code (%d): %s", resp.StatusCode, resp.Status)
}
}, func(err error, d time.Duration) {
log.Fields{
log.ErrorKey: err,
"url": u.String(),
"delay": d,
}.Infof(c, "Service is not up yet; retrying.")
})
if err != nil {
return nil, err
}
log.Fields{
"url": u.String(),
}.Infof(c, "Service is alive!")
return output, nil
}
// Run executes the application using the supplied context.
//
// Note that this intentionally consumes the Application by value, as we may
// modify its configuration as parameters become resolved.
func (a Application) Run(c context.Context) error {
if err := resolveExecutable(&a.genPath); err != nil {
return fmt.Errorf("invalid API generator path (-google-api-go-generator): %s", err)
}
apiDst, err := getPackagePath(a.apiPackage)
if err != nil {
return fmt.Errorf("failed to find package path for [%s]: %s", a.apiPackage, err)
}
if a.apiSubproject != "" {
apiDst = augPath(apiDst, a.apiSubproject)
a.apiPackage = strings.Join([]string{a.apiPackage, a.apiSubproject}, "/")
}
log.Fields{
"package": a.apiPackage,
"path": apiDst,
}.Debugf(c, "Identified API destination package path.")
// Compile our Chromium license.
a.license, err = compileChromiumLicense(c)
if err != nil {
return fmt.Errorf("failed to compile Chromium license: %s", err)
}
c, cancelFunc := context.WithCancel(c)
sigC := make(chan os.Signal, 1)
signal.Notify(sigC, os.Interrupt)
go func() {
for range sigC {
cancelFunc()
}
}()
defer signal.Stop(sigC)
// (1) Execute our service. Capture its discovery API.
svc, err := loadService(c, a.servicePath)
if err != nil {
return fmt.Errorf("failed to load service [%s]: %s", a.servicePath, err)
}
err = svc.run(c, func(c context.Context, discoveryURL url.URL) error {
discoveryURL.Path = safeURLPathJoin(discoveryURL.Path, a.serviceAPIRoot, "discovery", "v1", "apis")
data, err := retryHTTP(c, discoveryURL, "GET", "")
if err != nil {
return fmt.Errorf("discovery server did not come online: %s", err)
}
dir := directoryList{}
if err := json.Unmarshal(data, &dir); err != nil {
return fmt.Errorf("failed to load directory list: %s", err)
}
// Ensure that our target API base directory exists.
if err := ensureDirectory(apiDst); err != nil {
return fmt.Errorf("failed to create destination directory: %s", err)
}
// Run "google-api-go-generator" against the hosted service.
err = parallel.FanOutIn(func(taskC chan<- func() error) {
for i, item := range dir.Items {
item := item
c := log.SetFields(c, log.Fields{
"index": i,
"api": item.ID,
})
if !a.isWhitelisted(item.ID) {
log.Infof(c, "API is not whitelisted; skipping.")
continue
}
taskC <- func() error {
return a.generateAPI(c, item, &discoveryURL, apiDst)
}
}
})
if err != nil {
return err
}
return nil
})
if err != nil {
log.Fields{
log.ErrorKey: err,
}.Errorf(c, "Failed to extract APIs.")
}
return nil
}
// generateAPI generates and installs a single directory item's API.
func (a *Application) generateAPI(c context.Context, item *directoryItem, discoveryURL *url.URL, dst string) error {
tmpdir, err := ioutil.TempDir(os.TempDir(), "apigen")
if err != nil {
return err
}
defer func() {
os.RemoveAll(tmpdir)
}()
gendir := augPath(tmpdir, "gen")
headerPath := augPath(tmpdir, "header.txt")
if err := ioutil.WriteFile(headerPath, []byte(a.license), 0644); err != nil {
return err
}
args := []string{
"-cache=false", // Apparently the form {"-cache", "false"} is ignored.
"-discoveryurl", discoveryURL.String(),
"-api", item.ID,
"-gendir", gendir,
"-api_pkg_base", a.apiPackage,
"-base_url", a.baseURL,
"-header_path", headerPath,
}
log.Fields{
"command": a.genPath,
"args": args,
}.Debugf(c, "Executing google-api-go-generator.")
out, err := exec.Command(a.genPath, args...).CombinedOutput()
log.Infof(c, "Output:\n%s", out)
if err != nil {
return fmt.Errorf("error executing google-api-go-generator: %s", err)
}
err = installSource(gendir, dst, func(relpath string, data []byte) ([]byte, error) {
// Skip the root "api-list.json" file. This is generated only for the subset
// of APIs that this installation is handling, and is not representative of
// the full discovery (much less installation) API set.
if relpath == "api-list.json" {
return nil, nil
}
if !strings.HasSuffix(relpath, "-gen.go") {
return data, nil
}
log.Fields{
"relpath": relpath,
}.Infof(c, "Fixing up generated Go file.")
return data, nil
})
if err != nil {
return fmt.Errorf("failed to install [%s]: %s", item.ID, err)
}
return nil
}
func (a *Application) isWhitelisted(id string) bool {
if len(a.apiWhitelist) == 0 {
return true
}
for _, w := range a.apiWhitelist {
if w == id {
return true
}
}
return false
}
func safeURLPathJoin(p ...string) string {
for i, v := range p {
p[i] = strings.Trim(v, "/")
}
return strings.Join(p, "/")
}