-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgoreal_config.go
196 lines (168 loc) · 4.77 KB
/
goreal_config.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
package gobench
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"text/template"
"time"
)
func GoRealConfigPath(bug Bug) string {
cfgpath := filepath.Join(BackupPath, "configures")
if bug.isBlocking() {
cfgpath = filepath.Join(cfgpath, "goreal", "blocking.json")
} else {
cfgpath = filepath.Join(cfgpath, "goreal", "nonblocking.json")
}
return cfgpath
}
type GitDep struct {
Url string `json:"url"`
Dst string `json:"dst"`
}
func (d GitDep) String() string {
return strings.Join([]string{"git", "clone", d.Url, d.Dst}, " ")
}
type PkgDep string
func (d PkgDep) String() string {
return strings.Join([]string{"go", "get", "-v", "-d", string(d)}, " ")
}
type GoRealBugConfig struct {
bug Bug
Type string `json:"type"`
SubType string `json:"subtype"`
GoVersion string `json:"goversion"`
DevDeps []string `json:"dev_deps"`
PkgDeps []PkgDep `json:"pkg_deps"`
GitDeps []GitDep `json:"git_deps"`
RepoUrl string `json:"repo_url"`
SrcPath string `json:"src_path"`
PullSHA string `json:"pull_sha"`
TestEnvs []string `json:"testenvs"`
TestFunc string `json:"testfunc"`
WorkDir string `json:"workdir"`
DefaultT time.Duration `json:"default_timeout"`
DefaultC int `json:"default_timeout"`
PredStageCmd string `json:"pred_stage_build_command"`
PredBuildCmds []string `json:"pred_commands"`
BuildCmds []string `json:"build_commands"`
PostBuildCmds []string `json:"post_commands"`
}
func NewGoRealBugConfig(bug Bug, config string) *GoRealBugConfig {
bugid := bug.ID
if config == "" {
config = bug.gobenchCfg
}
b, err := ioutil.ReadFile(config)
if err != nil {
panic(err)
}
var result map[string]*GoRealBugConfig
if err := json.Unmarshal(b, &result); err != nil {
panic(err)
}
result[bugid].bug = bug
return result[bugid]
}
var DefaultTemp = `FROM golang:{{.GoVersion}}
# Clone the project to local
RUN git clone {{.RepoUrl}}.git /go/src/{{.SrcPath}}
{{if .HasDevDeps}}
# Update development envirnoment
RUN apt update && apt install -y {{join .DevDeps " "}}
{{- end}}
{{if .HasGoProxy}}
# Set GOPROXY if needed
RUN go env -w GO111MODULE=on && go env -w GOPROXY=https://goproxy.cn,direct
{{- end}}
{{if .HasHttpProxy}}
RUN git config --global http.proxy {{.HttpProxy}} && \
git config --global https.proxy {{.HttpProxy}}
ENV HTTP_PROXY {{.HttpProxy}}
ENV HTTPS_PROXY {{.HttpProxy}}
{{- end}}
{{if .HasPkgDeps}}
# Download needed packages
RUN {{depjoin .PkgDeps}}
{{- end}}
WORKDIR /go/src/{{.SrcPath}}
{{if .HasGitDeps}}
# Download git submodules
RUN {{depjoin .GitDeps}}
{{- end}}
# Rollback to the latest bug-free version
RUN git reset --hard {{.PullSHA}}
# Apply the revert patch to this bug
COPY ./bug_patch.diff {{.SrcPath}}/bug_patch.diff
RUN git apply {{.SrcPath}}/bug_patch.diff
{{if .HasPredCMD}}
# Pred-build
RUN {{.StrPredBuildCmds}}
{{- end}}
{{if .HasBuildCMD}}
# Build
RUN {{.StrBuildCmds}}
{{- end}}
{{if .HasPostCMD}}
# Post-build
RUN {{.StrPostBuildCmds}}
{{- end}}
# For entrypoint
WORKDIR /go/src/{{.SrcPath}}/{{.WorkDir}}
`
func (c *GoRealBugConfig) UpdateDockerfile() {
type ConfigWarpper struct {
*GoRealBugConfig
HasDevDeps, HasGitDeps, HasPkgDeps bool
HasPredCMD, HasBuildCMD, HasPostCMD bool
HasGoProxy, HasHttpProxy bool
HttpProxy string
BugPath, StrPredBuildCmds, StrBuildCmds, StrPostBuildCmds string
}
genCMD := func(cmds []string) string {
return strings.Join(cmds, " && \\\n ")
}
depCMD := func(t interface{}) string {
var result []string
s := reflect.ValueOf(t)
for i := 0; i < s.Len(); i++ {
result = append(result, fmt.Sprintf("%v", s.Index(i)))
}
return genCMD(result)
}
needProxy := len(os.Getenv("NEED_PROXY")) > 0
var warpper = ConfigWarpper{
GoRealBugConfig: c,
HasDevDeps: len(c.DevDeps) > 0,
HasGitDeps: len(c.GitDeps) > 0,
HasPkgDeps: len(c.PkgDeps) > 0,
HasPredCMD: len(c.PredBuildCmds) > 0,
HasBuildCMD: len(c.BuildCmds) > 0,
HasPostCMD: len(c.PostBuildCmds) > 0,
HasGoProxy: false,
HasHttpProxy: needProxy,
HttpProxy: os.Getenv("NEED_PROXY"),
StrPredBuildCmds: genCMD(c.PredBuildCmds),
StrBuildCmds: genCMD(c.BuildCmds),
StrPostBuildCmds: genCMD(c.PostBuildCmds),
}
var funcs = template.FuncMap{"join": strings.Join, "depjoin": depCMD}
var t = template.Must(template.New("dockerfile").Funcs(funcs).Parse(DefaultTemp))
var dockerfile string
buf := bytes.NewBufferString(dockerfile)
if err := t.Execute(buf, warpper); err != nil {
panic(err)
}
if c.bug.forkedPath == "" {
c.bug.fork(TestPath)
}
path := filepath.Join(c.bug.forkedPath, "bug.Dockerfile")
// log.Println(path)
if err := ioutil.WriteFile(path, buf.Bytes(), 0644); err != nil {
panic(err)
}
}