-
Notifications
You must be signed in to change notification settings - Fork 791
/
Copy pathcode_build.go
576 lines (549 loc) · 18.8 KB
/
code_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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// RAINBOND, Application Management Platform
// Copyright (C) 2014-2017 Goodrain Co., Ltd.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package build
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"strings"
"time"
"github.com/eapache/channels"
"github.com/goodrain/rainbond/builder"
jobc "github.com/goodrain/rainbond/builder/job"
"github.com/goodrain/rainbond/builder/parser/code"
"github.com/goodrain/rainbond/builder/sources"
"github.com/goodrain/rainbond/util"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func slugBuilder() (Build, error) {
return &slugBuild{}, nil
}
type slugBuild struct {
tgzDir string
buildCacheDir string
re *Request
}
func (s *slugBuild) Build(re *Request) (*Response, error) {
re.Logger.Info(util.Translation("Start compiling the source code"), map[string]string{"step": "build-exector"})
s.tgzDir = re.TGZDir
s.re = re
s.buildCacheDir = re.CacheDir
packageName := fmt.Sprintf("%s/%s.tgz", s.tgzDir, re.DeployVersion)
//Stops previous build tasks for the same component
//If an error occurs, it does not affect the current build task
if err := s.stopPreBuildJob(re); err != nil {
logrus.Errorf("stop pre build job for service %s failure %s", re.ServiceID, err.Error())
}
if err := s.runBuildJob(re); err != nil {
re.Logger.Error(util.Translation("Compiling the source code failure"), map[string]string{"step": "build-code", "status": "failure"})
logrus.Error("build slug in container error,", err.Error())
return nil, err
}
re.Logger.Info("code build success", map[string]string{"step": "build-exector"})
defer func() {
if err := os.Remove(packageName); err != nil && !strings.Contains(err.Error(), "no such file or directory") {
logrus.Warningf("pkg name: %s; remove slug pkg: %v", packageName, err)
}
}()
fileInfo, err := os.Stat(packageName)
if err != nil {
re.Logger.Error(util.Translation("Check that the build result failure"), map[string]string{"step": "build-code", "status": "failure"})
logrus.Error("build package check error", err.Error())
return nil, fmt.Errorf("build package failure")
}
if fileInfo.Size() == 0 {
re.Logger.Error(util.Translation("Source build failure and result slug size is 0"),
map[string]string{"step": "build-code", "status": "failure"})
return nil, fmt.Errorf("build package failure")
}
//After 5.1.5 version, wrap slug pacage in the runner image
imageName, err := s.buildRunnerImage(packageName)
if err != nil {
re.Logger.Error(util.Translation("Build runner image failure"),
map[string]string{"step": "build-code", "status": "failure"})
return nil, fmt.Errorf("build runner image failure")
}
re.Logger.Info(util.Translation("build runtime image success"), map[string]string{"step": "build-code", "status": "success"})
res := &Response{
MediumType: ImageMediumType,
MediumPath: imageName,
}
return res, nil
}
func (s *slugBuild) writeRunDockerfile(sourceDir, packageName string, envs map[string]string) error {
runDockerfile := `
FROM %s
ARG CODE_COMMIT_HASH
ARG CODE_COMMIT_USER
ARG CODE_COMMIT_MESSAGE
COPY %s /tmp/slug/slug.tgz
RUN chown rain:rain /tmp/slug/slug.tgz
ENV CODE_COMMIT_HASH=${CODE_COMMIT_HASH}
ENV CODE_COMMIT_USER=${CODE_COMMIT_USER}
ENV CODE_COMMIT_MESSAGE=${CODE_COMMIT_MESSAGE}
ENV VERSION=%s
`
logrus.Debugf("cacheDir:%v, from:%v, packageName:%v, Dir(slugPackage):%v", sourceDir, builder.RUNNERIMAGENAME, packageName, path.Dir(packageName))
result := util.ParseVariable(fmt.Sprintf(runDockerfile, builder.RUNNERIMAGENAME, packageName, s.re.DeployVersion), envs)
return ioutil.WriteFile(path.Join(sourceDir, "Dockerfile"), []byte(result), 0755)
}
//buildRunnerImage Wrap slug in the runner image
func (s *slugBuild) buildRunnerImage(slugPackage string) (string, error) {
imageName := CreateImageName(s.re.ServiceID, s.re.DeployVersion)
cacheDir := path.Join(path.Dir(slugPackage), "."+s.re.DeployVersion)
if err := util.CheckAndCreateDir(cacheDir); err != nil {
return "", fmt.Errorf("create cache package dir failure %s", err.Error())
}
defer func() {
if err := os.RemoveAll(cacheDir); err != nil {
logrus.Errorf("remove cache dir %s failure %s", cacheDir, err.Error())
}
}()
packageName := path.Base(slugPackage)
if err := util.Rename(slugPackage, path.Join(cacheDir, packageName)); err != nil {
return "", fmt.Errorf("move code package failure %s", err.Error())
}
//write default runtime dockerfile
if err := s.writeRunDockerfile(cacheDir, packageName, s.re.BuildEnvs); err != nil {
return "", fmt.Errorf("write default runtime dockerfile error:%s", err.Error())
}
//build runtime image
if err := s.re.ImageClient.ImagesPullAndPush(builder.RUNNERIMAGENAME, builder.ONLINERUNNERIMAGENAME, "", "", s.re.Logger); err != nil {
return "", fmt.Errorf("pull image %s: %v", builder.RUNNERIMAGENAME, err)
}
logrus.Infof("pull image %s successfully.", builder.RUNNERIMAGENAME)
err := sources.ImageBuild(cacheDir, "", "", s.re.RbdNamespace, s.re.ServiceID, s.re.DeployVersion, s.re.Logger, "run-build", "", s.re.KanikoImage, s.re.KanikoArgs)
if err != nil {
s.re.Logger.Error(fmt.Sprintf("build image %s of new version failure", imageName), map[string]string{"step": "builder-exector", "status": "failure"})
logrus.Errorf("build image error: %s", err.Error())
return "", err
}
return imageName, nil
}
func (s *slugBuild) getSourceCodeTarFile(re *Request) (string, error) {
var cmd []string
sourceTarFile := fmt.Sprintf("%s/%s-%s.tar", util.GetParentDirectory(re.SourceDir), re.ServiceID, re.DeployVersion)
if re.ServerType == "svn" {
cmd = append(cmd, "tar", "-cf", sourceTarFile, "./")
}
if re.ServerType == "git" {
cmd = append(cmd, "tar", "-cf", sourceTarFile, "./")
}
source := exec.Command(cmd[0], cmd[1:]...)
source.Dir = re.SourceDir
logrus.Debugf("tar source code to file %s", sourceTarFile)
if err := source.Run(); err != nil && err.Error() != "exit status 1" {
return "", fmt.Errorf("command %s: %v", source.String(), err)
}
return sourceTarFile, nil
}
//stopPreBuildJob Stops previous build tasks for the same component
//The same component retains only one build task to perform
func (s *slugBuild) stopPreBuildJob(re *Request) error {
jobList, err := jobc.GetJobController().GetServiceJobs(re.ServiceID)
if err != nil {
logrus.Errorf("get pre build job for service %s failure ,%s", re.ServiceID, err.Error())
}
if len(jobList) > 0 {
for _, job := range jobList {
jobc.GetJobController().DeleteJob(job.Name)
}
}
return nil
}
func (s *slugBuild) createVolumeAndMount(re *Request, sourceTarFileName string) (volumes []corev1.Volume, volumeMounts []corev1.VolumeMount) {
slugSubPath := strings.TrimPrefix(re.TGZDir, "/grdata/")
lazyloading := sourceTarFileName == ""
sourceTarPath := strings.TrimPrefix(sourceTarFileName, "/cache/")
cacheSubPath := strings.TrimPrefix(re.CacheDir, "/cache/")
hostPathType := corev1.HostPathDirectoryOrCreate
unset := corev1.HostPathUnset
if re.CacheMode == "hostpath" {
volumeMounts = []corev1.VolumeMount{
{
Name: "cache",
MountPath: "/tmp/cache",
},
{
Name: "slug",
MountPath: "/tmp/slug",
SubPath: slugSubPath,
},
}
if !lazyloading {
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: "source-file",
MountPath: "/tmp/app-source.tar",
})
}
volumes = []corev1.Volume{
{
Name: "slug",
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: s.re.GRDataPVCName,
},
},
},
{
Name: "cache",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: path.Join(re.CachePath, cacheSubPath),
Type: &hostPathType,
},
},
},
}
if !lazyloading {
volumes = append(volumes, corev1.Volume{
Name: "source-file",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: path.Join(re.CachePath, sourceTarPath),
// host file type can not auto create parent dir, so can not use.
Type: &unset,
},
},
})
}
} else {
volumes = []corev1.Volume{
{
Name: "slug",
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: s.re.GRDataPVCName,
},
},
},
{
Name: "app",
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: re.CachePVCName,
},
},
},
}
volumeMounts = []corev1.VolumeMount{
{
Name: "app",
MountPath: "/tmp/cache",
SubPath: cacheSubPath,
},
{
Name: "slug",
MountPath: "/tmp/slug",
SubPath: slugSubPath,
},
}
if !lazyloading {
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: "app",
MountPath: "/tmp/app-source.tar",
SubPath: sourceTarPath,
})
}
}
if re.ServerType == "pkg" {
volumeMounts = append(volumeMounts, corev1.VolumeMount{
Name: "slug",
MountPath: "/tmp/app",
SubPath: strings.TrimPrefix(re.RepositoryURL, "/grdata/"),
})
}
return volumes, volumeMounts
}
func (s *slugBuild) runBuildJob(re *Request) error {
//prepare build code dir
re.Logger.Info(util.Translation("Start make code package"), map[string]string{"step": "build-exector"})
start := time.Now()
var sourceTarFileName string
if re.ServerType != "oss" && re.ServerType != "pkg" {
var err error
// handle nodejs or static dir
if err := s.HandleNodeJsDir(re); err != nil {
logrus.Error("handle nodejs code error:", err)
return err
}
sourceTarFileName, err = s.getSourceCodeTarFile(re)
if err != nil {
return fmt.Errorf("create source code tar file error:%s", err.Error())
}
// remove source cache tar file
defer func() {
os.Remove(sourceTarFileName)
}()
}
re.Logger.Info(util.Translation("make code package success"), map[string]string{"step": "build-exector"})
logrus.Infof("package code for building service %s version %s successful, take time %s", re.ServiceID, re.DeployVersion, time.Now().Sub(start))
name := fmt.Sprintf("%s-%s", re.ServiceID, re.DeployVersion)
namespace := re.RbdNamespace
job := corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
"service": re.ServiceID,
"job": "codebuild",
},
},
}
envs := []corev1.EnvVar{
{Name: "SLUG_VERSION", Value: re.DeployVersion},
{Name: "SERVICE_ID", Value: re.ServiceID},
{Name: "TENANT_ID", Value: re.TenantID},
{Name: "CODE_COMMIT_HASH", Value: re.Commit.Hash},
{Name: "CODE_COMMIT_USER", Value: re.Commit.User},
{Name: "CODE_COMMIT_MESSAGE", Value: re.Commit.Message},
{Name: "LANGUAGE", Value: re.Lang.String()},
}
if re.ServerType == "oss" {
envs = append(envs, corev1.EnvVar{Name: "PACKAGE_DOWNLOAD_URL", Value: re.RepositoryURL})
envs = append(envs, corev1.EnvVar{Name: "PACKAGE_DOWNLOAD_USER", Value: re.CodeSouceInfo.User})
envs = append(envs, corev1.EnvVar{Name: "PACKAGE_DOWNLOAD_PASS", Value: re.CodeSouceInfo.Password})
}
var mavenSettingName string
for k, v := range re.BuildEnvs {
if k == "MAVEN_SETTING_NAME" {
mavenSettingName = v
continue
}
if k == "PROCFILE" {
if !strings.HasPrefix(v, "web:") {
v = "web: " + v
} else if v[4] != ' ' {
v = "web: " + v[4:]
}
}
envs = append(envs, corev1.EnvVar{Name: k, Value: v})
if k == "PROC_ENV" {
var mapdata = make(map[string]interface{})
if err := json.Unmarshal([]byte(v), &mapdata); err == nil {
if runtime, ok := mapdata["runtimes"]; ok {
envs = append(envs, corev1.EnvVar{Name: "RUNTIME", Value: runtime.(string)})
}
}
}
}
podSpec := corev1.PodSpec{RestartPolicy: corev1.RestartPolicyOnFailure} // only support never and onfailure
// schedule builder
hostIP := os.Getenv("HOST_IP")
if hostIP != "" {
podSpec.NodeSelector = map[string]string{
"kubernetes.io/hostname": hostIP,
}
podSpec.Tolerations = []corev1.Toleration{
{
Operator: "Exists",
},
}
}
logrus.Debugf("request is: %+v", re)
volumes, mounts := s.createVolumeAndMount(re, sourceTarFileName)
podSpec.Volumes = volumes
container := corev1.Container{
Name: name,
Image: builder.BUILDERIMAGENAME,
Stdin: true,
StdinOnce: true,
Env: envs,
Args: []string{"local"},
}
container.VolumeMounts = mounts
//set maven setting
var mavenSettingConfigName string
if mavenSettingName != "" && re.Lang.String() == code.JavaMaven.String() {
if setting := jobc.GetJobController().GetLanguageBuildSetting(re.Ctx, code.JavaMaven, mavenSettingName); setting != "" {
mavenSettingConfigName = setting
}
}
if mavenSettingConfigName == "" {
if settingName := jobc.GetJobController().GetDefaultLanguageBuildSetting(re.Ctx, code.JavaMaven); settingName != "" {
mavenSettingConfigName = settingName
} else {
logrus.Warnf("maven setting config %s not found", mavenSettingName)
}
}
if mavenSettingConfigName != "" {
podSpec.Volumes = append(podSpec.Volumes, corev1.Volume{
Name: "mavensetting",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: mavenSettingConfigName,
},
},
},
})
mountPath := "/etc/maven/setting.xml"
container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{
MountPath: mountPath,
SubPath: "mavensetting",
Name: "mavensetting",
})
container.Env = append(container.Env, corev1.EnvVar{
Name: "MAVEN_SETTINGS_PATH",
Value: mountPath,
})
container.Env = append(container.Env, corev1.EnvVar{
Name: "MAVEN_MIRROR_DISABLE",
Value: "true",
})
logrus.Infof("set maven setting config %s success", mavenSettingName)
}
podSpec.Containers = append(podSpec.Containers, container)
for _, ha := range re.HostAlias {
podSpec.HostAliases = append(podSpec.HostAliases, corev1.HostAlias{IP: ha.IP, Hostnames: ha.Hostnames})
}
job.Spec = podSpec
s.setImagePullSecretsForPod(&job)
writer := re.Logger.GetWriter("builder", "info")
reChan := channels.NewRingChannel(10)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Get builder image at build time
if err := s.re.ImageClient.ImagesPullAndPush(builder.BUILDERIMAGENAME, builder.ONLINEBUILDERIMAGENAME, "", "", re.Logger); err != nil {
return err
}
logrus.Debugf("create job[name: %s; namespace: %s]", job.Name, job.Namespace)
err := jobc.GetJobController().ExecJob(ctx, &job, writer, reChan)
if err != nil {
logrus.Errorf("create new job:%s failed: %s", name, err.Error())
return err
}
re.Logger.Info(util.Translation("create build code job success"), map[string]string{"step": "build-exector"})
logrus.Infof("create build job %s for service %s build version %s", job.Name, re.ServiceID, re.DeployVersion)
// delete job after complete
defer jobc.GetJobController().DeleteJob(job.Name)
return s.waitingComplete(re, reChan)
}
func (s *slugBuild) waitingComplete(re *Request, reChan *channels.RingChannel) (err error) {
var logComplete = false
var jobComplete = false
timeout := time.NewTimer(time.Minute * 60)
for {
select {
case <-timeout.C:
return fmt.Errorf("build time out (more than 60 minute)")
case jobStatus := <-reChan.Out():
status := jobStatus.(string)
switch status {
case "complete":
jobComplete = true
if logComplete {
return nil
}
re.Logger.Info(util.Translation("build code job exec completed"), map[string]string{"step": "build-exector"})
case "failed":
jobComplete = true
err = fmt.Errorf("build code job exec failure")
if logComplete {
return err
}
re.Logger.Info(util.Translation("build code job exec failed"), map[string]string{"step": "build-exector"})
case "cancel":
jobComplete = true
err = fmt.Errorf("build code job is canceled")
if logComplete {
return err
}
case "logcomplete":
logComplete = true
if jobComplete {
return err
}
}
}
}
}
func (s *slugBuild) setImagePullSecretsForPod(pod *corev1.Pod) {
imagePullSecretName := os.Getenv("IMAGE_PULL_SECRET")
if imagePullSecretName == "" {
return
}
pod.Spec.ImagePullSecrets = []corev1.LocalObjectReference{
{Name: imagePullSecretName},
}
}
//ErrorBuild build error
type ErrorBuild struct {
Code int
}
func (e *ErrorBuild) Error() string {
return fmt.Sprintf("Run build return %d", e.Code)
}
func (s *slugBuild) HandleNodeJsDir(re *Request) error {
if re.Lang == code.NodeJSStatic {
if ok, _ := util.FileExists(path.Join(re.SourceDir, "nodestatic.json")); ok {
if err := os.RemoveAll(path.Join(re.SourceDir, "nodestatic.json")); err != nil {
logrus.Error("remove nodestatic json error:", err)
return err
}
}
filePtr, err := os.Create(path.Join(re.SourceDir, "nodestatic.json"))
if err != nil {
logrus.Error("create nodestatic json error:", err)
return err
}
defer filePtr.Close()
_, err = io.WriteString(filePtr, fmt.Sprintf("{\"path\":\"%v\"}", re.BuildEnvs["DIST_DIR"]))
if err != nil {
logrus.Error("write nodestatic json error:", err)
return err
}
}
if re.BuildEnvs["PACKAGE_TOOL"] == "yarn" {
if ok, _ := util.FileExists(path.Join(re.SourceDir, "yarn.lock")); !ok {
filePtr, err := os.Create(path.Join(re.SourceDir, "yarn.lock"))
if err != nil {
logrus.Error("create nodestatic json error:", err)
return err
}
defer filePtr.Close()
}
if ok, _ := util.FileExists(path.Join(re.SourceDir, "package-lock.json")); ok {
if err := os.RemoveAll(path.Join(re.SourceDir, "package-lock.json")); err != nil {
logrus.Error("remove package-lock json error:", err)
return err
}
}
}
if re.BuildEnvs["PACKAGE_TOOL"] == "npm" {
if ok, _ := util.FileExists(path.Join(re.SourceDir, "package-lock.json")); !ok {
filePtr, err := os.Create(path.Join(re.SourceDir, "package-lock.json"))
if err != nil {
logrus.Error("create package-lock json error:", err)
return err
}
defer filePtr.Close()
}
if ok, _ := util.FileExists(path.Join(re.SourceDir, "yarn.lock")); ok {
if err := os.RemoveAll(path.Join(re.SourceDir, "yarn.lock")); err != nil {
logrus.Error("remove yarn.lock error:", err)
return err
}
}
}
return nil
}