forked from tarantool/tt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagefile.go
377 lines (299 loc) · 8.59 KB
/
magefile.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
367
368
369
370
371
372
373
374
375
376
377
//go:build mage
// +build mage
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/apex/log"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
const (
buildTypeEnv = "TT_CLI_BUILD_SSL"
goPackageName = "github.com/tarantool/tt/cli"
asmflags = "all=-trimpath=${PWD}"
gcflags = "all=-trimpath=${PWD}"
packagePath = "./cli"
defaultLinuxConfigPath = "/etc/tarantool"
defaultDarwinConfigPath = "/usr/local/etc/tarantool"
cartridgePath = "cli/cartridge/third_party/cartridge-cli"
)
var (
ldflags = []string{
"-s", "-w",
"-X ${PACKAGE}/version.gitTag=${GIT_TAG}",
"-X ${PACKAGE}/version.gitCommit=${GIT_COMMIT}",
"-X ${PACKAGE}/version.versionLabel=${VERSION_LABEL}",
"-X ${PACKAGE}/configure.defaultConfigPath=${CONFIG_PATH}",
}
staticLdflags = []string{
"-linkmode=external", "-extldflags", "-static",
}
goExecutableName = "go"
pythonExecutableName = "python3"
ttExecutableName = "tt"
generateModePath = filepath.Join(packagePath, "codegen", "generate_code.go")
)
type BuildType string
const (
BuildTypeDefault BuildType = ""
BuildTypeNoCgo BuildType = "no"
BuildTypeShared BuildType = "shared"
BuildTypeStatic BuildType = "static"
)
func init() {
var err error
if specifiedGoExe := os.Getenv("GOEXE"); specifiedGoExe != "" {
goExecutableName = specifiedGoExe
}
if specifiedTTExe := os.Getenv("TTEXE"); specifiedTTExe != "" {
ttExecutableName = specifiedTTExe
} else {
if ttExecutableName, err = filepath.Abs(ttExecutableName); err != nil {
panic(err)
}
}
// We want to use Go 1.11 modules even if the source lives inside GOPATH.
// The default is "auto".
os.Setenv("GO111MODULE", "on")
}
// Generate cartridge-cli Go code.
// Cartridge-cli uses code generator for Go.
// Accordingly, before building tt, we must generate this code.
func GenCC() {
currDir, err := os.Getwd()
if err != nil {
panic(err)
}
err = os.Chdir(cartridgePath)
if err != nil {
panic(err)
}
err = sh.Run("go", "run", "cli/codegen/generate_code.go")
if err != nil {
panic(err)
}
err = os.Chdir(currDir)
if err != nil {
panic(err)
}
}
// applyPatch applies the patch if it hasn't already been applied.
func applyPatch(path string) error {
// Run the patch in dry run mode.
out, err := sh.Output(
"patch", "-d", cartridgePath, "-N", "-p1", "--dry-run", "-V", "none", "-i", path,
)
// If an error is returned, one of two things has happened:
// the patch has already been applied or an error has occurred.
if err != nil {
if strings.Contains(out, "previously applied") {
return nil
}
return err
}
err = sh.Run(
"patch", "-d", cartridgePath, "-N", "-p1", "-V", "none", "-i", path,
)
fmt.Printf("* %s [done]\n", filepath.Base(path))
return nil
}
// Patch cartridge-cli.
// Before building tt, we must apply patches for cartridge-cli.
// These patches contain code specific to cartridge-cli integration into tt
// and are not subject to commit to the cartridge's upstream.
func PatchCC() error {
mg.Deps(GenCC)
fmt.Printf("%s\n", "Apply cartridge-cli patches...")
patches_path := "../../extra/"
patches := []string{
"001_make_cmd_public.patch",
"002_fix_admin_param.patch",
"003_fix_work_paths.patch",
"004_fix_warning.patch",
"005_rename_tt_env.patch",
"006_consider_tt_run_dir.patch",
}
for _, patch := range patches {
err := applyPatch(patches_path + patch)
if err != nil {
return err
}
}
return nil
}
// Building tt executable. Supported environment variables:
// TT_CLI_BUILD_SSL=(no|static|shared)
func Build() error {
fmt.Println("Building tt...")
mg.Deps(PatchCC)
mg.Deps(GenerateGoCode)
buildLdflags := make([]string, len(ldflags))
copy(buildLdflags, ldflags)
tags := "-tags=netgo,osusergo"
buildType := os.Getenv(buildTypeEnv)
switch BuildType(buildType) {
case BuildTypeDefault:
fallthrough
case BuildTypeNoCgo:
tags = tags + ",go_tarantool_ssl_disable"
case BuildTypeStatic:
if runtime.GOOS != "darwin" {
buildLdflags = append(buildLdflags, staticLdflags...)
}
tags = tags + ",openssl_static"
case BuildTypeShared:
default:
return fmt.Errorf("Unsupported build type: %s, supported: "+
"%s, %s, %s\n",
buildType, BuildTypeNoCgo, BuildTypeStatic, BuildTypeShared)
}
err := sh.RunWith(
getBuildEnvironment(), goExecutableName, "build",
"-o", ttExecutableName,
tags,
"-ldflags", strings.Join(buildLdflags, " "),
"-asmflags", asmflags,
"-gcflags", gcflags,
packagePath,
)
if err != nil {
return fmt.Errorf("Failed to build tt executable: %s", err)
}
return nil
}
// Run license checker.
func CheckLicenses() error {
fmt.Println("Running license checker...")
home, err := os.UserHomeDir()
if err != nil {
return err
}
if err := sh.RunV(home+"/go/bin/lichen", "--config", ".lichen.yaml", "tt"); err != nil {
return err
}
return nil
}
// Run golang and python linters.
func Lint() error {
fmt.Println("Running golangci-lint...")
mg.Deps(PatchCC)
mg.Deps(GenerateGoCode)
if err := sh.RunV("golangci-lint", "run", "--config=golangci-lint.yml"); err != nil {
return err
}
fmt.Println("Running flake8...")
if err := sh.RunV(pythonExecutableName, "-m", "flake8", "test"); err != nil {
return err
}
return nil
}
// Run unit tests.
func Unit() error {
fmt.Println("Running unit tests...")
mg.Deps(GenerateGoCode)
if mg.Verbose() {
return sh.RunV(goExecutableName, "test", "-v",
fmt.Sprintf("%s/...", packagePath))
}
return sh.RunV(goExecutableName, "test", fmt.Sprintf("%s/...", packagePath))
}
// Run unit tests with a Tarantool instance integration.
func UnitFull() error {
fmt.Println("Running full unit tests...")
mg.Deps(GenerateGoCode)
if mg.Verbose() {
return sh.RunV(goExecutableName, "test", "-v", fmt.Sprintf("%s/...", packagePath),
"-tags", "integration")
}
return sh.RunV(goExecutableName, "test", fmt.Sprintf("%s/...", packagePath),
"-tags", "integration")
}
// Run integration tests, excluding slow tests.
func Integration() error {
fmt.Println("Running integration tests...")
return sh.RunV(pythonExecutableName, "-m", "pytest", "-m", "not slow and not slow_ee "+
"and not notarantool", "test/integration")
}
// Run full set of integration tests.
func IntegrationFull() error {
fmt.Println("Running all integration tests...")
return sh.RunV(pythonExecutableName, "-m", "pytest", "-m", "not slow_ee and not notarantool",
"test/integration")
}
// Run set of ee integration tests.
func IntegrationEE() error {
fmt.Println("Running all EE integration tests...")
return sh.RunV(pythonExecutableName, "-m", "pytest", "test/integration/ee")
}
// Run integration tests without system-wide installed Tarantool.
func IntegrationNoTarantool() error {
fmt.Println("Running integration tests without Tarantool...")
return sh.RunV(pythonExecutableName, "-m", "pytest", "-m", "notarantool",
"test/integration")
}
// Run codespell checks.
func Codespell() error {
fmt.Println("Running codespell tests...")
return sh.RunV("codespell", packagePath, "test", "README.md", "doc", "CHANGELOG.md")
}
// Run all tests together, excluding slow and unit integration tests.
func Test() {
mg.SerialDeps(Lint, CheckLicenses, Unit, Integration)
}
// Run all tests together.
func TestFull() {
mg.SerialDeps(Lint, CheckLicenses, UnitFull, IntegrationFull)
}
// Cleanup directory.
func Clean() {
fmt.Println("Cleaning directory...")
os.Remove(ttExecutableName)
}
// GenerateGoCode generates code from lua files.
func GenerateGoCode() error {
err := sh.RunWith(getBuildEnvironment(), goExecutableName, "run", generateModePath)
if err != nil {
return err
}
return nil
}
// getDefaultConfigPath returns the path to the configuration file,
// determining it based on the OS.
func getDefaultConfigPath() string {
switch runtime.GOOS {
case "linux":
return defaultLinuxConfigPath
case "darwin":
return defaultDarwinConfigPath
}
log.Fatalf("Trying to get default config path file on an unsupported OS")
return ""
}
// getBuildEnvironment return map with build environment variables.
func getBuildEnvironment() map[string]string {
var err error
var currentDir string
var gitTag string
var gitCommit string
if currentDir, err = os.Getwd(); err != nil {
log.Warnf("Failed to get current directory: %s", err)
}
if _, err := exec.LookPath("git"); err == nil {
gitTag, _ = sh.Output("git", "describe", "--tags")
gitCommit, _ = sh.Output("git", "rev-parse", "--short", "HEAD")
}
return map[string]string{
"PACKAGE": goPackageName,
"GIT_TAG": gitTag,
"GIT_COMMIT": gitCommit,
"VERSION_LABEL": os.Getenv("VERSION_LABEL"),
"PWD": currentDir,
"CONFIG_PATH": getDefaultConfigPath(),
"CGO_ENABLED": "1",
}
}