-
Notifications
You must be signed in to change notification settings - Fork 242
/
build_nodejs.go
123 lines (104 loc) · 3.83 KB
/
build_nodejs.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
package pipeline
import (
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/gaia-pipeline/gaia/helper/filehelper"
"github.com/gaia-pipeline/gaia/helper/pipelinehelper"
"github.com/gaia-pipeline/gaia"
"github.com/gaia-pipeline/gaia/services"
"github.com/gofrs/uuid"
)
const nodeJSInternalCloneFolder = "jsclone"
// BuildPipelineNodeJS is the real implementation of BuildPipeline for NodeJS
type BuildPipelineNodeJS struct {
Type gaia.PipelineType
}
// PrepareEnvironment prepares the environment before we start the build process.
func (b *BuildPipelineNodeJS) PrepareEnvironment(p *gaia.CreatePipeline) error {
// create uniqueName for destination folder
v4, err := uuid.NewV4()
if err != nil {
gaia.Cfg.Logger.Debug("unable to generate uuid", "error", err.Error())
return err
}
uniqueName := uuid.Must(v4, nil)
// Create local temp folder for clone
cloneFolder := filepath.Join(gaia.Cfg.HomePath, gaia.TmpFolder, gaia.TmpNodeJSFolder, srcFolder, uniqueName.String(), nodeJSInternalCloneFolder)
err = os.MkdirAll(cloneFolder, 0700)
if err != nil {
return err
}
// Set new generated path in pipeline obj for later usage
if p.Pipeline.Repo == nil {
p.Pipeline.Repo = &gaia.GitRepo{}
}
p.Pipeline.Repo.LocalDest = cloneFolder
p.Pipeline.UUID = uniqueName.String()
return nil
}
// ExecuteBuild executes the NodeJS build process
func (b *BuildPipelineNodeJS) ExecuteBuild(p *gaia.CreatePipeline) error {
// Look for Tar binary executable
path, err := exec.LookPath(tarName)
if err != nil {
gaia.Cfg.Logger.Debug("cannot find tar binary executable", "error", err.Error())
return err
}
// Set local destination
localDest := ""
if p.Pipeline.Repo != nil {
localDest = p.Pipeline.Repo.LocalDest
}
// Set command args for archive process
pipelineFileName := pipelinehelper.AppendTypeToName(p.Pipeline.Name, p.Pipeline.Type)
args := []string{
"--exclude=.git",
"-czvf",
pipelineFileName,
"-C",
localDest,
".",
}
// Execute and wait until finish or timeout
uniqueFolder := filepath.Join(gaia.Cfg.HomePath, gaia.TmpFolder, gaia.TmpNodeJSFolder, srcFolder, p.Pipeline.UUID)
output, err := executeCmd(path, args, os.Environ(), uniqueFolder)
p.Output = string(output[:])
if err != nil {
gaia.Cfg.Logger.Debug("cannot build pipeline", "error", err.Error(), "output", string(output[:]))
return err
}
// Build has been finished. Set execution path to the build result archive.
// This will be used during pipeline verification phase which will happen after this step.
p.Pipeline.ExecPath = filepath.Join(uniqueFolder, pipelineFileName)
// Set the the local destination variable to the unique folder because this is now the place
// where our binary is located.
p.Pipeline.Repo.LocalDest = uniqueFolder
return nil
}
// CopyBinary copies the final compiled binary to the
// destination folder.
func (b *BuildPipelineNodeJS) CopyBinary(p *gaia.CreatePipeline) error {
// Define src and destination
src := filepath.Join(p.Pipeline.Repo.LocalDest, pipelinehelper.AppendTypeToName(p.Pipeline.Name, p.Pipeline.Type))
dest := filepath.Join(gaia.Cfg.PipelinePath, pipelinehelper.AppendTypeToName(p.Pipeline.Name, p.Pipeline.Type))
// Copy binary
if err := filehelper.CopyFileContents(src, dest); err != nil {
return err
}
// Set +x (execution right) for pipeline
return os.Chmod(dest, gaia.ExecutablePermission)
}
// SavePipeline saves the current pipeline configuration.
func (b *BuildPipelineNodeJS) SavePipeline(p *gaia.Pipeline) error {
dest := filepath.Join(gaia.Cfg.PipelinePath, pipelinehelper.AppendTypeToName(p.Name, p.Type))
p.ExecPath = dest
p.Type = gaia.PTypeNodeJS
p.Name = strings.TrimSuffix(filepath.Base(dest), typeDelimiter+gaia.PTypeNodeJS.String())
p.Created = time.Now()
// Our pipeline is finished constructing. Save it.
storeService, _ := services.StorageService()
return storeService.PipelinePut(p)
}