/
run.go
173 lines (147 loc) · 4.1 KB
/
run.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
package doltgres_builder
import (
"context"
"fmt"
"os"
"os/signal"
"path/filepath"
"runtime"
"sync"
"syscall"
builder "github.com/dolthub/dolt/go/performance/utils/dolt_builder"
"golang.org/x/sync/errgroup"
)
const envDoltgresBin = "DOLTGRES_BIN"
func Run(parentCtx context.Context, commitList []string) error {
doltgresBin, err := getDoltgresBin()
if err != nil {
return err
}
// check for git on path
err = builder.GitVersion(parentCtx)
if err != nil {
return err
}
cwd, err := os.Getwd()
if err != nil {
return err
}
// make temp dir for cloning/copying doltgres source
tempDir := filepath.Join(cwd, "clones-copies")
err = os.MkdirAll(tempDir, os.ModePerm)
if err != nil {
return err
}
// clone doltgres source
err = builder.GitCloneBare(parentCtx, tempDir, GithubDoltgres)
if err != nil {
return err
}
repoDir := filepath.Join(tempDir, "doltgresql.git")
withKeyCtx, cancel := context.WithCancel(parentCtx)
g, ctx := errgroup.WithContext(withKeyCtx)
// handle user interrupt
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
var wg sync.WaitGroup
wg.Add(1)
go func() {
<-quit
defer wg.Done()
signal.Stop(quit)
cancel()
}()
for _, commit := range commitList {
commit := commit // https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
return buildBinaries(ctx, tempDir, repoDir, doltgresBin, commit)
})
}
builderr := g.Wait()
close(quit)
wg.Wait()
// remove clones-copies after all go routines complete
// will exit successfully if removal fails
if err := os.RemoveAll(tempDir); err != nil {
fmt.Printf("WARN: %s was not removed\n", tempDir)
fmt.Printf("WARN: error: %v\n", err)
}
if builderr != nil {
return builderr
}
return nil
}
// getDoltgresBin creates and returns the absolute path for DOLTGRES_BIN
// if it was found, otherwise uses the current working directory
// as the parent directory for a `doltgresBin` directory
func getDoltgresBin() (string, error) {
var doltgresBin string
dir := os.Getenv(envDoltgresBin)
if dir == "" {
cwd, err := os.Getwd()
if err != nil {
return "", err
}
doltgresBin = filepath.Join(cwd, "doltgresBin")
} else {
abs, err := filepath.Abs(dir)
if err != nil {
return "", err
}
doltgresBin = abs
}
err := os.MkdirAll(doltgresBin, os.ModePerm)
if err != nil {
return "", err
}
return doltgresBin, nil
}
// buildBinaries builds a doltgres binary at the given commit and stores it in the doltgresBin
func buildBinaries(ctx context.Context, tempDir, repoDir, doltgresBinDir, commit string) error {
checkoutDir := filepath.Join(tempDir, commit)
if err := os.MkdirAll(checkoutDir, os.ModePerm); err != nil {
return err
}
err := builder.GitCheckoutTree(ctx, repoDir, checkoutDir, commit)
if err != nil {
return err
}
commitDir := filepath.Join(doltgresBinDir, commit)
if err := os.MkdirAll(commitDir, os.ModePerm); err != nil {
return err
}
parserScriptPath := filepath.Join(checkoutDir, "postgres", "parser", "build.sh")
command, err := goBuild(ctx, parserScriptPath, checkoutDir, commitDir)
if err != nil {
return err
}
return doltgresVersion(ctx, commitDir, command)
}
// goBuild builds the doltgres parser and doltgres binary and returns the filename for the doltgres binary
func goBuild(ctx context.Context, parserScriptPath, source, dest string) (string, error) {
buildParser := builder.ExecCommand(ctx, "/bin/bash", "-c", parserScriptPath)
err := buildParser.Run()
if err != nil {
return "", err
}
doltgresFileName := "doltgres"
if runtime.GOOS == "windows" {
doltgresFileName = "doltgres.exe"
}
toBuild := filepath.Join(dest, doltgresFileName)
build := builder.ExecCommand(ctx, "go", "build", "-o", toBuild, ".")
build.Dir = source
err = build.Run()
if err != nil {
return "", err
}
return toBuild, nil
}
// doltgresVersion prints doltgres version of binary
func doltgresVersion(ctx context.Context, dir, command string) error {
doltgresVersion := builder.ExecCommand(ctx, command, "version")
doltgresVersion.Stderr = os.Stderr
doltgresVersion.Stdout = os.Stdout
doltgresVersion.Dir = dir
return doltgresVersion.Run()
}