forked from harness/gitness
/
drone.go
308 lines (253 loc) · 7.15 KB
/
drone.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
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
"time"
"github.com/drone/drone/pkg/build"
"github.com/drone/drone/pkg/build/docker"
"github.com/drone/drone/pkg/build/log"
"github.com/drone/drone/pkg/build/repo"
"github.com/drone/drone/pkg/build/script"
"launchpad.net/goyaml"
)
var (
// identity file (id_rsa) that will be injected
// into the container if specified
identity = flag.String("identity", "", "")
// runs Drone in parallel mode if True
parallel = flag.Bool("parallel", false, "")
// build will timeout after N milliseconds.
// this will default to 500 minutes (6 hours)
timeout = flag.Duration("timeout", 300*time.Minute, "")
// build will run in a privileged container
privileged = flag.Bool("privileged", false, "")
// runs Drone with verbose output if True
verbose = flag.Bool("v", false, "")
// displays the help / usage if True
help = flag.Bool("h", false, "")
// version number, currently deterined by the
// git revision number (sha)
version string
)
func init() {
// default logging
log.SetPrefix("\033[2m[DRONE] ")
log.SetSuffix("\033[0m\n")
log.SetOutput(os.Stdout)
log.SetPriority(log.LOG_NOTICE)
}
func main() {
// Parse the input parameters
flag.Usage = usage
flag.Parse()
if *help {
flag.Usage()
os.Exit(0)
}
if *verbose {
log.SetPriority(log.LOG_DEBUG)
}
// Must speicify a command
args := flag.Args()
if len(args) == 0 {
flag.Usage()
os.Exit(0)
}
switch {
// run drone build assuming the current
// working directory contains the drone.yml
case args[0] == "build" && len(args) == 1:
path, _ := os.Getwd()
path = filepath.Join(path, ".drone.yml")
run(path)
// run drone build where the path to the
// source directory is provided
case args[0] == "build" && len(args) == 2:
path := args[1]
path = filepath.Clean(path)
path, _ = filepath.Abs(path)
path = filepath.Join(path, ".drone.yml")
run(path)
// run drone vet where the path to the
// source directory is provided
case args[0] == "vet" && len(args) == 2:
path := args[1]
path = filepath.Clean(path)
path, _ = filepath.Abs(path)
path = filepath.Join(path, ".drone.yml")
vet(path)
// run drone vet assuming the current
// working directory contains the drone.yml
case args[0] == "vet" && len(args) == 1:
path, _ := os.Getwd()
path = filepath.Join(path, ".drone.yml")
vet(path)
// print the version / revision number
case args[0] == "version" && len(args) == 1:
println(version)
// print the help message
case args[0] == "help" && len(args) == 1:
flag.Usage()
}
os.Exit(0)
}
func vet(path string) {
// parse the Drone yml file
script, err := script.ParseBuildFile(path)
if err != nil {
log.Err(err.Error())
os.Exit(1)
return
}
// print the Drone yml as parsed
out, _ := goyaml.Marshal(script)
log.Noticef("parsed yaml:\n%s", string(out))
}
func run(path string) {
dockerClient := docker.New()
// parse the Drone yml file
s, err := script.ParseBuildFile(path)
if err != nil {
log.Err(err.Error())
os.Exit(1)
return
}
// get the repository root directory
dir := filepath.Dir(path)
code := repo.Repo{
Name: filepath.Base(dir),
Branch: "HEAD", // should we do this?
Path: dir,
}
// does the local repository match the
// $GOPATH/src/{package} pattern? This is
// important so we know the target location
// where the code should be copied inside
// the container.
if gopath, ok := getRepoPath(dir); ok {
code.Dir = gopath
} else if gopath, ok := getGoPath(dir); ok {
// in this case we found a GOPATH and
// reverse engineered the package path
code.Dir = gopath
} else {
// otherwise just use directory name
code.Dir = filepath.Base(dir)
}
// this is where the code gets uploaded to the container
// TODO move this code to the build package
code.Dir = filepath.Join("/var/cache/drone/src", filepath.Clean(code.Dir))
// track all build results
var builders []*build.Builder
// ssh key to import into container
var key []byte
if len(*identity) != 0 {
key, err = ioutil.ReadFile(*identity)
if err != nil {
fmt.Printf("[Error] Could not find or read identity file %s\n", *identity)
os.Exit(1)
return
}
}
builds := []*script.Build{s}
// loop through and create builders
for _, b := range builds { //script.Builds {
builder := build.New(dockerClient)
builder.Build = b
builder.Repo = &code
builder.Key = key
builder.Stdout = os.Stdout
builder.Timeout = *timeout
builder.Privileged = *privileged
if *parallel == true {
var buf bytes.Buffer
builder.Stdout = &buf
}
builders = append(builders, builder)
}
switch *parallel {
case false:
runSequential(builders)
case true:
runParallel(builders)
}
// if in parallel mode, print out the buffer
// if we had a failure
for _, builder := range builders {
if builder.BuildState.ExitCode == 0 {
continue
}
if buf, ok := builder.Stdout.(*bytes.Buffer); ok {
log.Noticef("printing stdout for failed build %s", builder.Build.Name)
println(buf.String())
}
}
// this exit code is initially 0 and will
// be set to an error code if any of the
// builds fail.
var exit int
fmt.Printf("\nDrone Build Results \033[90m(%v)\033[0m\n", len(builders))
// loop through and print results
for _, builder := range builders {
build := builder.Build
res := builder.BuildState
duration := time.Duration(res.Finished - res.Started)
switch {
case builder.BuildState.ExitCode == 0:
fmt.Printf(" \033[32m\u2713\033[0m %v \033[90m(%v)\033[0m\n", build.Name, humanizeDuration(duration*time.Second))
case builder.BuildState.ExitCode != 0:
fmt.Printf(" \033[31m\u2717\033[0m %v \033[90m(%v)\033[0m\n", build.Name, humanizeDuration(duration*time.Second))
exit = builder.BuildState.ExitCode
}
}
os.Exit(exit)
}
func runSequential(builders []*build.Builder) {
// loop through and execute each build
for _, builder := range builders {
if err := builder.Run(); err != nil {
log.Errf("Error executing build: %s", err.Error())
os.Exit(1)
}
}
}
func runParallel(builders []*build.Builder) {
// spawn four worker goroutines
var wg sync.WaitGroup
for _, builder := range builders {
// Increment the WaitGroup counter
wg.Add(1)
// Launch a goroutine to run the build
go func(builder *build.Builder) {
defer wg.Done()
builder.Run()
}(builder)
time.Sleep(500 * time.Millisecond) // get weird iptables failures unless we sleep.
}
// wait for the workers to finish
wg.Wait()
}
var usage = func() {
fmt.Println(`Drone is a tool for building and testing code in Docker containers.
Usage:
drone command [arguments]
The commands are:
build build and test the repository
version print the version number
vet validate the yaml configuration file
-v runs drone with verbose output
-h display this help and exit
--parallel runs drone build tasks in parallel
--timeout=300ms timeout build after 300 milliseconds
--privileged runs drone build in a privileged container
Examples:
drone build builds the source in the pwd
drone build /path/to/repo builds the source repository
Use "drone help [command]" for more information about a command.
`)
}