This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
build.go
288 lines (267 loc) · 6.49 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
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"time"
beeutils "github.com/astaxie/beego/utils"
"github.com/codeskyblue/go-sh"
"github.com/codeskyblue/go-uuid"
"github.com/codeskyblue/gobuild/database"
"github.com/codeskyblue/gobuild/utils"
"github.com/qiniu/log"
)
var history = make(map[string]string)
var logfd io.WriteCloser
func init() {
var err error
logfd, err = os.OpenFile("build.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatal("create build.log file failed:", err)
}
}
type Builder struct {
wbc *utils.WriteBroadcaster
sh *sh.Session
project string //
ref string
os string
arch string
fullname string // p + ref + os + arch
gopath string // init
gobin string // init
srcDir string // init
sha string // get
rc *Assembly // get
framework string // build
pid int64 // db
tag string // tag = pid + str(os-arch), set after pid set
}
func NewBuilder(project, ref string, goos, arch string, wbc *utils.WriteBroadcaster) *Builder {
b := &Builder{
wbc: wbc,
sh: sh.NewSession(),
project: project,
ref: ref,
os: goos,
arch: arch,
fullname: strings.Join([]string{project, ref, goos, arch}, "-"),
}
b.sh.ShowCMD = true
if wbc != nil {
b.sh.Stdout = io.MultiWriter(logfd, wbc)
b.sh.Stderr = io.MultiWriter(logfd, wbc)
}
selfbin := beeutils.SelfDir() + "/bin"
env := map[string]string{
"PATH": strings.Join([]string{"/bin:/usr/bin", selfbin, os.Getenv("PATH")}, ":"),
"PROJECT": project,
"GOROOT": opts.GOROOT,
}
// enable cgo on current os-arch
if goos == runtime.GOOS && arch == runtime.GOARCH {
env["CGO_ENABLED"] = "1"
}
b.sh.Env = env
return b
}
// prepare environ
func (b *Builder) init() (err error) {
gobin, err := ioutil.TempDir("tmp", "gobin-")
if err != nil {
return
}
b.gobin, _ = filepath.Abs(gobin)
b.gopath, _ = filepath.Abs("gopath")
b.sh.Env["GOPATH"] = b.gopath
b.sh.Env["GOBIN"] = b.gobin
b.srcDir = filepath.Join(b.gopath, "src", b.project)
return
}
// build src
func (this *Builder) build(os, arch string) (file string, err error) {
this.sh.Env["GOOS"] = os
this.sh.Env["GOARCH"] = arch
this.sh.SetDir(this.srcDir)
// switch framework
this.framework = this.rc.Framework
switch this.rc.Framework {
case "beego":
err = this.sh.SetDir(this.srcDir).Call("bee", "pack", "-f", "zip")
file = filepath.Join(this.srcDir, filepath.Base(this.project)) + ".zip"
return
case "revel":
err = this.sh.SetDir(this.srcDir).Call("revel", "package", this.project)
file = filepath.Join(this.srcDir, filepath.Base(this.project)) + ".tar.gz"
return
default:
this.framework = ""
}
//if this.sh.Test("f", ".gopmfile") {
// this.sh.Alias("go", "gopm")
//}
/* // close godep now
if this.sh.Test("d", "Godeps") {
err = this.sh.Call("godep", "go", "install")
return
}
*/
err = this.sh.Call("go", "get", "-u", "-v", ".")
if err != nil {
return
}
// find binary
target := filepath.Base(this.project)
if os == "windows" {
target += ".exe"
}
return beeutils.SearchFile(target, this.gobin, filepath.Join(this.gobin, os+"_"+arch))
}
// achieve and upload
func (b *Builder) publish(file string) (addr string, err error) {
var path string
if b.framework == "" {
path, err = b.pack([]string{file}, filepath.Join(b.srcDir, ".gobuild.yml"))
} else {
path, err = utils.TempFile("files", "tmp-", "-"+filepath.Base(file))
if err != nil {
return
}
err = sh.Command("mv", "-v", file, path).Run()
}
if err != nil {
return
}
// file ext<zip|tar.gz>
suffix := ".zip"
if strings.HasSuffix(path, ".tar.gz") {
suffix = ".tar.gz"
}
go func() {
defer func() {
log.Debug("delete history:", b.tag)
delete(history, b.tag)
go func() {
// leave 5min gap for unfinished downloading.
time.Sleep(time.Minute * 5)
//time.Sleep(time.Second * 5)
os.Remove(path)
}()
}()
// upload
var cdnAddr string
var err error
if *environment == "development" {
cdnAddr, err = UploadLocal(path)
} else {
name := fmt.Sprintf("%s-%s-%s-%s",
filepath.Base(b.project),
b.os, b.arch, b.ref) + suffix
cdnAddr, err = UploadFile(path, uuid.New()+"/"+name)
}
if err != nil {
return
}
log.Debug("upload ok:", cdnAddr)
output := ""
if b.wbc != nil {
output = string(b.wbc.Bytes())
}
err = database.AddFile(b.pid, b.tag, cdnAddr, output)
if err != nil {
log.Error(err)
}
}()
tmpAddr := "http://" + opts.Hostname + "/" + path
history[b.tag] = tmpAddr
return tmpAddr, nil
}
// remove tmp file
func (b *Builder) clean() (err error) {
b.sh.Call("echo", "cleaning...")
err = os.RemoveAll(b.gobin)
return
}
// init + build + publish + clean
func (j *Builder) Auto() (addr string, err error) {
lock := utils.NewNameLock(j.project)
lock.Lock()
defer func() {
lock.Unlock()
if j.wbc != nil {
j.wbc.CloseWriters()
}
}()
if err = j.init(); err != nil {
return
}
// defer clean should start when GOPATH success created
defer func() {
er := j.clean()
if er != nil {
log.Warn(er)
}
if err != nil && j.wbc != nil {
io.WriteString(j.wbc, err.Error())
}
// FIXME: delete WriteBroadcaster after finish
// if build error, output will not saved, it is not a good idea
// better to change database func to -..
// SearchProject(project) AddProject(project)
// SearchFile(pid, ref, os, arch)
// AddFile(pid, ref, os, arch, sha)
}()
// download src (in order to get sha)
err = j.get()
if err != nil {
log.Error("download src error:", err)
return
}
// search db for history project record
log.Info("request project:", j.project)
log.Info("current sha:", j.sha)
p, err := database.SearchProject(j.project, j.sha)
if err != nil {
log.Info("exists in db", j.project, j.ref, j.sha)
pid, er := database.AddProject(j.project, j.ref, j.sha)
if er != nil {
err = er
return
}
j.pid = pid // project id
} else {
j.pid = p.Id
}
// generate tag for builded-file search
j.tag = fmt.Sprintf("%d-%s-%s", j.pid, j.os, j.arch)
//log.Info("tag:", j.tag)
// search memory history
hisAddr, ok := history[j.tag]
if ok {
return hisAddr, nil
}
// search database history
f, err := database.SearchFile(j.pid, j.tag)
log.Debugf("search db: %v", f)
if err == nil {
addr = f.Addr
return
}
// build xc
// file maybe empty
file, err := j.build(j.os, j.arch)
if err != nil {
log.Error("build error:", err)
return
}
// package build file(include upload)
addr, err = j.publish(file)
if err != nil {
return
}
return
}