This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
build.go
338 lines (290 loc) · 9.55 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
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
//+build mage
/*
Copyright 2019 Gravitational, Inc.
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 main
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/gravitational/trace"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
var (
// buildContainer is a docker container used to build go binaries
buildContainer = "golang:1.14.3"
// golangciVersion is the version of golangci-lint to use for linting
// https://github.com/golangci/golangci-lint/releases
golangciVersion = "v1.27.0"
// nethealthRegistryImage is the docker tag to use to push the nethealth container to the requested registry
nethealthRegistryImage = env("NETHEALTH_REGISTRY_IMAGE", "quay.io/gravitational/nethealth-dev")
// baseImage is the base OS image to use for containers
baseImage = "gcr.io/distroless/base-debian10"
// buildVersion allows override of the version string from env variable
buildVersion = env("BUILD_VERSION", "")
grpcProtocVersion = env("GRPC_PROTOC_VER", "3.14.0") // version of protoc
grpcGogoProtoTag = env("GRPC_GOGO_PROTO_TAG", "v1.3.1") // gogo version tag
)
type Build mg.Namespace
// All builds all binaries
func (Build) All() error {
mg.Deps(Build.Nethealth, Build.Satellite, Build.Healthz)
return nil
}
// BuildContainer creates a docker container as a consistent golang environment to use for software builds and tests
func (Build) BuildContainer() error {
fmt.Println("\n=====> Creating build container...\n")
return trace.Wrap(sh.RunV(
"docker", "build", "--pull",
"--tag", fmt.Sprint("satellite-build:", version()),
"--build-arg", fmt.Sprint("BUILD_IMAGE=", buildContainer),
"--build-arg", fmt.Sprint("GOLANGCI_VER=", golangciVersion),
"-f", "Dockerfile.build",
".",
))
}
// Nethealth builds the nethealth binary
func (Build) Nethealth() error {
fmt.Println("\n=====> Building nethealth binary...\n")
return trace.Wrap(sh.RunV(
"go", "build",
"-ldflags", flags(),
"-o", outPath("nethealth"),
"github.com/gravitational/satellite/cmd/nethealth",
))
}
// Satellite builds the satellite binary
func (Build) Satellite() error {
fmt.Println("\n=====> Building satellite binary...\n")
mg.Deps(Codegen.Grpc)
return trace.Wrap(sh.RunV(
"go", "build",
"-o", outPath("satellite"),
"-ldflags", flags(),
"github.com/gravitational/satellite/cmd/agent",
))
}
// Healthz builds the healthz binary
func (Build) Healthz() error {
fmt.Println("\n=====> Building healthz binary...\n")
return trace.Wrap(sh.RunV(
"go", "build",
"-o", outPath("healthz"),
"-ldflags", flags(),
"github.com/gravitational/satellite/cmd/healthz",
))
}
type Publish mg.Namespace
// Nethealth tags and publishes the nethealth docker container to the configured registry
func (Publish) Nethealth() error {
mg.Deps(Docker.Nethealth)
fmt.Println("\n=====> Publishing nethealth docker image...\n")
err := sh.RunV(
"docker", "tag",
fmt.Sprint("nethealth:", version()),
fmt.Sprint(nethealthRegistryImage, ":", version()),
)
if err != nil {
return trace.Wrap(err)
}
return trace.Wrap(sh.RunV(
"docker", "push", fmt.Sprint(nethealthRegistryImage, ":", version()),
))
}
type Docker mg.Namespace
// Nethealth builds nethealth docker image
func (Docker) Nethealth() error {
mg.Deps(Build.BuildContainer)
fmt.Println("\n=====> Building nethealth docker image...\n")
// Force pull upstream images to refresh any local caches
err := sh.RunV("docker", "pull", baseImage)
if err != nil {
return trace.Wrap(err)
}
return trace.Wrap(sh.RunV(
"docker", "build",
"--tag", fmt.Sprint("nethealth:", version()),
"--build-arg", fmt.Sprint("BUILD_VERSION=", version()),
"--build-arg", fmt.Sprint("BASE_IMAGE=", baseImage),
"--build-arg", fmt.Sprint("BUILD_IMAGE=satellite-build:", version()),
"-f", "Dockerfile.nethealth",
".",
))
}
// Healthz builds the healthz container
func (Docker) Healthz() error {
mg.Deps(Build.BuildContainer)
fmt.Println("\n=====> Building Satellite Docker Image...\n")
return trace.Wrap(sh.RunV(
"docker", "build",
"--tag", fmt.Sprint("satellite:", version()),
"--build-arg", fmt.Sprint("BUILD_IMAGE=satellite-build:", version()),
"--build-arg", fmt.Sprintf("BUILD_FLAGS=%v", flags()),
".",
))
}
type Test mg.Namespace
// All runs all test targets
func (Test) All() error {
mg.Deps(Test.Unit, Test.Lint, Test.Style)
return nil
}
// Unit runs unit tests with the race detector enabled
func (Test) Unit() error {
mg.Deps(Build.BuildContainer)
fmt.Println("\n=====> Running Satellite Unit Tests...\n")
return trace.Wrap(sh.RunV(
"docker", "run", "--rm",
hostVolume(),
`--env="GOCACHE=/go/src/github.com/gravitational/satellite/build/cache/go"`,
`-w=/go/src/github.com/gravitational/satellite/`,
fmt.Sprint("satellite-build:", version()),
"go", "--", "test", "-mod", "vendor", "./...", "-race",
))
}
// Lint runs lints against the repo (golangci)
func (Test) Lint() error {
mg.Deps(Build.BuildContainer)
fmt.Println("\n=====> Linting Satellite...\n")
return trace.Wrap(sh.RunV(
"docker", "run", "--rm",
hostVolume(),
`--env="GOCACHE=/go/src/github.com/gravitational/satellite/build/cache/go"`,
fmt.Sprint("satellite-build:", version()),
"golangci-lint", "run", "--new",
"--deadline=30m",
"--enable-all",
"--disable=gochecknoglobals",
"--disable=gochecknoinits",
"--disable=gomodguard",
))
}
// Style validates that licenses exist on each source file
func (Test) Style() error {
fmt.Println("\n=====> Running scripts/validate-license.sh...\n")
return trace.Wrap(sh.RunV("scripts/validate-license.sh"))
}
type Codegen mg.Namespace
// Buildbox creates a docker container for gRPC code generation
func (Codegen) Buildbox() error {
fmt.Println("\n=====> Building GRPC Buildbox...\n")
return trace.Wrap(sh.RunV(
"docker", "build",
"--tag", fmt.Sprint("satellite-grpc-buildbox:", version()),
"--build-arg", fmt.Sprint("GRPC_PROTOC_VER=", grpcProtocVersion),
"--build-arg", fmt.Sprint("GRPC_GOGO_PROTO_TAG=", grpcGogoProtoTag),
"-f", "build.assets/grpc/Dockerfile",
".",
))
}
// Grpc runs the gRPC code generator
func (Codegen) Grpc() error {
mg.Deps(Codegen.Buildbox)
fmt.Println("\n=====> Running GRPC Codegen inside docker...\n")
return trace.Wrap(sh.RunV(
"docker", "run",
hostVolume("delegated"),
fmt.Sprint("satellite-grpc-buildbox:", version()),
"sh", "-c",
"cd /go/src/github.com/gravitational/satellite/ && go run mage.go internal:grpcAgent internal:grpcDebug",
))
}
type Internal mg.Namespace
// GrpcAgent (Internal) generates protobuf stubs for Agent server
// The task is called from codeGen:grpc target inside docker
func (Internal) GrpcAgent() error {
fmt.Println("\n=====> Running protoc...\n")
err := os.Chdir(filepath.Join(srcDir(), "agent/proto/agentpb"))
if err != nil {
return trace.ConvertSystemError(err)
}
protoFiles, err := filepath.Glob("*.proto")
if err != nil {
return trace.Wrap(err)
}
args := []string{fmt.Sprint("-I=.:", os.Getenv("PROTO_INCLUDE")), "--gofast_out=plugins=grpc:."}
return trace.Wrap(sh.RunV(
"protoc",
append(args, protoFiles...)...,
))
}
// GrpcDebug (Internal) generates protobuf stubs for Debug server
func (Internal) GrpcDebug() error {
fmt.Println("\n=====> Running protoc...\n")
err := os.Chdir(filepath.Join(srcDir(), "agent/proto"))
if err != nil {
return trace.ConvertSystemError(err)
}
protoFiles, err := filepath.Glob("debug/*.proto")
if err != nil {
return trace.Wrap(err)
}
args := []string{fmt.Sprint("-I=.:", os.Getenv("PROTO_INCLUDE")), "--gofast_out=plugins=grpc,Mgogo.proto=github.com/gogo/protobuf/gogoproto:."}
return trace.Wrap(sh.RunV(
"protoc",
append(args, protoFiles...)...,
))
}
// Clean removes all build artifacts
func Clean() error {
buildDir := filepath.Join(srcDir(), "build")
fmt.Println("\n=====> Cleaning: ", buildDir)
return trace.ConvertSystemError(os.RemoveAll(buildDir))
}
func hostVolume(opts ...string) string {
wd, _ := os.Getwd()
if len(opts) == 0 {
return fmt.Sprintf("--volume=%v:/go/src/github.com/gravitational/satellite", wd)
}
return fmt.Sprintf("--volume=%v:/go/src/github.com/gravitational/satellite:%v", wd, opts[0])
}
func srcDir() string {
return filepath.Join(os.Getenv("GOPATH"), "src/github.com/gravitational/satellite/")
}
func flags() string {
timestamp := time.Now().Format(time.RFC3339)
hash := hash()
version := version()
flags := []string{
fmt.Sprint(`-X main.timestamp=`, timestamp),
fmt.Sprint(`-X main.commitHash=`, hash),
fmt.Sprint(`-X main.gitTag=`, version),
"-s -w", // shrink the binary
}
return strings.Join(flags, " ")
}
// hash returns the git hash for the current repository or "" if none.
func hash() string {
hash, _ := sh.Output("git", "rev-parse", "--short", "HEAD")
return hash
}
// version returns the git tag for the current branch or "" if none.
func version() string {
if buildVersion != "" {
return buildVersion
}
longTag, _ := sh.Output("git", "describe", "--tags", "--dirty")
return longTag
}
// env, loads a variable from the environment, or uses the provided default
func env(env, d string) string {
if os.Getenv(env) != "" {
return os.Getenv(env)
}
return d
}
func outPath(binaryName string) string {
return filepath.Join(srcDir(), "build", binaryName)
}