-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathexec.go
188 lines (155 loc) · 4.37 KB
/
exec.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
// SPDX-License-Identifier: Apache-2.0
package pipeline
import (
"context"
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/go-vela/cli/version"
"github.com/go-vela/server/compiler"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/go-vela/worker/executor"
"github.com/go-vela/worker/runtime"
"github.com/sirupsen/logrus"
)
// Exec executes a pipeline based off the provided configuration.
func (c *Config) Exec(client compiler.Engine) error {
logrus.Debug("executing exec for pipeline configuration")
// send Filesystem call to capture base directory path
base, err := os.Getwd()
if err != nil {
return err
}
// create full path for pipeline file
path := filepath.Join(base, c.File)
// check if custom path was provided for pipeline file
if len(c.Path) > 0 {
// create custom full path for pipeline file
path = filepath.Join(c.Path, c.File)
}
path, err = validateFile(path)
if err != nil {
return err
}
// check if full path to pipeline file exists
_, err = os.Stat(path)
if err != nil {
return fmt.Errorf("unable to find pipeline %s: %w", path, err)
}
// create build object for use in pipeline
b := new(library.Build)
b.SetBranch(c.Branch)
b.SetDeploy(c.Target)
b.SetEvent(c.Event)
if c.Tag == "" && c.Event == constants.EventPull {
b.SetRef("refs/pull/1")
} else {
b.SetRef(c.Tag)
}
// create repo object for use in pipeline
r := new(library.Repo)
r.SetOrg(c.Org)
r.SetName(c.Repo)
r.SetFullName(fmt.Sprintf("%s/%s", c.Org, c.Repo))
r.SetPipelineType(c.PipelineType)
logrus.Tracef("compiling pipeline %s", path)
// compile into a pipeline
_pipeline, _, err := client.
Duplicate().
WithBuild(b).
WithComment(c.Comment).
WithFiles(c.FileChangeset).
WithLocal(true).
WithRepo(r).
WithLocalTemplates(c.TemplateFiles).
Compile(path)
if err != nil {
return err
}
// create current directory path for local mount
mount := fmt.Sprintf("%s:%s:rw", base, constants.WorkspaceDefault)
// add the current directory path to volume mounts
c.Volumes = append(c.Volumes, mount)
logrus.Tracef("creating runtime engine %s", constants.DriverDocker)
// setup the runtime
//
// https://pkg.go.dev/github.com/go-vela/worker/runtime?tab=doc#New
_runtime, err := runtime.New(&runtime.Setup{
Driver: constants.DriverDocker,
HostVolumes: c.Volumes,
PrivilegedImages: c.PrivilegedImages,
})
if err != nil {
return err
}
logrus.Tracef("creating executor engine %s", constants.DriverLocal)
// setup the executor
//
// https://godoc.org/github.com/go-vela/worker/executor#New
_executor, err := executor.New(&executor.Setup{
Driver: constants.DriverLocal,
Runtime: _runtime,
Pipeline: _pipeline.Sanitize(constants.DriverDocker),
Build: b,
Repo: r,
Version: version.New().Semantic(),
})
if err != nil {
return err
}
// create a background context
ctx, done := context.WithCancel(context.Background())
defer done()
// handle aborting local build process
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
// spawn go routine to wait for syscall signals
go func() {
// wait for signal
<-signalChan
logrus.Info("pipeline exec canceled! cleaning up - you may see some errors during cleanup")
// cancel the context passed into build process
done()
}()
defer func() {
// destroy the build with the executor
err = _executor.DestroyBuild(context.Background())
if err != nil {
logrus.Errorf("unable to destroy build: %v", err)
}
}()
// create the build with the executor
err = _executor.CreateBuild(ctx)
if err != nil {
return fmt.Errorf("unable to create build: %w", err)
}
// plan the build with the executor
err = _executor.PlanBuild(ctx)
if err != nil {
return fmt.Errorf("unable to plan build: %w", err)
}
// log/event streaming
go func() {
logrus.Debug("streaming build logs")
// start process to handle StreamRequests
// from Steps and Services
err = _executor.StreamBuild(ctx)
if err != nil {
logrus.Errorf("unable to stream build logs: %v", err)
}
}()
// assemble the build with the executor
err = _executor.AssembleBuild(ctx)
if err != nil {
return fmt.Errorf("unable to assemble build: %w", err)
}
// execute the build with the executor
err = _executor.ExecBuild(ctx)
if err != nil {
return fmt.Errorf("unable to execute build: %w", err)
}
return nil
}