-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmain.go
245 lines (220 loc) · 6.66 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/drone/drone-exec/docker"
"github.com/drone/drone-exec/parser"
"github.com/drone/drone-exec/runner"
"github.com/drone/drone-exec/yaml/inject"
"github.com/drone/drone-exec/yaml/path"
"github.com/drone/drone-exec/yaml/secure"
"github.com/drone/drone-exec/yaml/shasum"
"github.com/drone/drone-plugin-go/plugin"
"github.com/samalba/dockerclient"
log "github.com/Sirupsen/logrus"
)
var (
cache bool // execute cache steps
clone bool // execute clone steps
build bool // execute build steps
deploy bool // execute deploy steps
notify bool // execute notify steps
debug bool // execute in debug mode
force bool // force pull plugin images
)
// payload defines the raw plugin payload that
// stores the build metadata and configuration.
var payload = struct {
Yaml string `json:"config"`
YamlEnc string `json:"secret"`
Repo *plugin.Repo `json:"repo"`
Build *plugin.Build `json:"build"`
BuildLast *plugin.Build `json:"build_last"`
Job *plugin.Job `json:"job"`
System *plugin.System `json:"system"`
Workspace *plugin.Workspace `json:"workspace"`
}{}
func main() {
// parses command line flags
flag.BoolVar(&cache, "cache", false, "")
flag.BoolVar(&clone, "clone", false, "")
flag.BoolVar(&build, "build", false, "")
flag.BoolVar(&deploy, "deploy", false, "")
flag.BoolVar(¬ify, "notify", false, "")
flag.BoolVar(&debug, "debug", false, "")
flag.BoolVar(&force, "pull", false, "")
flag.Parse()
// unmarshal the json payload via stdin or
// via the command line args (whichever was used)
plugin.MustUnmarshal(&payload)
// configure the default log format and
// log levels
if debug {
log.SetLevel(log.DebugLevel)
}
var sec *secure.Secure
if payload.Workspace.Keys != nil {
var err error
sec, err = secure.Parse(payload.YamlEnc, payload.Workspace.Keys.Private)
if err != nil {
log.Debugln("Unable to decrypt encrypted secrets", err)
} else {
log.Debugln("Successfully decrypted secrets")
}
}
// TODO This block of code (and the above block) need to be cleaned
// up and written in a manner that facilitates better unit testing.
if sec != nil {
verified := shasum.Check(payload.Yaml, sec.Checksum)
// the checksum should be invalidated if the repository is
// public, and the build is a pull request, and the checksum
// value was not provided.
if plugin.IsPullRequest(payload.Build) && !payload.Repo.Private && len(sec.Checksum) == 0 {
verified = false
}
switch {
case verified && plugin.IsPullRequest(payload.Build):
log.Debugln("Injected secrets into Yaml safely")
var err error
payload.Yaml, err = inject.InjectSafe(payload.Yaml, sec.Environment.Map())
if err != nil {
fmt.Println("Error injecting Yaml secrets")
os.Exit(1)
}
case verified:
log.Debugln("Injected secrets into Yaml")
payload.Yaml = inject.Inject(payload.Yaml, sec.Environment.Map())
case !verified:
// if we can't validate the Yaml file we don't inject
// secrets, and therefore shouldn't bother running the
// deploy and notify tests.
deploy = false
notify = false
fmt.Println("Unable to validate Yaml checksum.", sec.Checksum)
}
}
// injects the matrix configuration parameters
// into the yaml prior to parsing.
payload.Yaml = inject.Inject(payload.Yaml, payload.Job.Environment)
payload.Yaml = inject.Inject(payload.Yaml, map[string]string{
"COMMIT": payload.Build.Commit.Sha[:7],
"BRANCH": payload.Build.Commit.Branch,
"BUILD_NUMBER": strconv.Itoa(payload.Build.Number),
})
// extracts the clone path from the yaml. If
// the clone path doesn't exist it uses a path
// derrived from the repository uri.
payload.Workspace.Path = path.Parse(payload.Yaml, payload.Repo.Link)
payload.Workspace.Root = "/drone/src"
rules := []parser.RuleFunc{
parser.ImageName,
parser.ImageMatchFunc(payload.System.Plugins),
parser.ImagePullFunc(force),
parser.SanitizeFunc(payload.Repo.Trusted), //&& !plugin.PullRequest(payload.Build)
parser.CacheFunc(payload.Repo.FullName),
parser.Escalate,
}
tree, err := parser.Parse(payload.Yaml, rules)
if err != nil {
log.Debugln(err) // print error messages in debug mode only
log.Fatalln("Error parsing the .drone.yml")
os.Exit(1)
}
r := runner.Load(tree)
client, err := dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil)
if err != nil {
log.Debugln(err)
log.Fatalln("Error creating the docker client.")
os.Exit(1)
}
// // creates a wrapper Docker client that uses an ambassador
// // container to create a pod-like environment.
controller, err := docker.NewClient(client)
if err != nil {
log.Debugln(err)
log.Fatalln("Error creating the docker ambassador.")
os.Exit(1)
}
defer controller.Destroy()
// watch for sigkill (timeout or cancel build)
killc := make(chan os.Signal, 1)
signal.Notify(killc, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-killc
log.Println("Cancel request received, killing process")
controller.Destroy() // possibe race here. implement lock on the other end
os.Exit(130) // cancel is treated like ctrl+c
}()
go func() {
var timeout = payload.Repo.Timeout
if timeout == 0 {
timeout = 60
}
<-time.After(time.Duration(timeout) * time.Minute)
log.Println("Timeout request received, killing process")
controller.Destroy() // possibe race here. implement lock on the other end
os.Exit(128) // cancel is treated like ctrl+c
}()
state := &runner.State{
Client: controller,
Stdout: os.Stdout,
Stderr: os.Stdout,
Repo: payload.Repo,
Build: payload.Build,
BuildLast: payload.BuildLast,
Job: payload.Job,
System: payload.System,
Workspace: payload.Workspace,
}
if cache {
err = r.RunNode(state, parser.NodeCache)
if err != nil {
log.Debugln(err)
}
}
if clone {
err = r.RunNode(state, parser.NodeClone)
if err != nil {
log.Debugln(err)
}
}
if build && !state.Failed() {
err = r.RunNode(state, parser.NodeCompose|parser.NodeBuild)
if err != nil {
log.Debugln(err)
}
}
if deploy && !state.Failed() {
err = r.RunNode(state, parser.NodePublish|parser.NodeDeploy)
if err != nil {
log.Debugln(err)
}
}
// if the build is not failed, at this point
// we can mark as successful
if !state.Failed() {
state.Job.Status = plugin.StateSuccess
state.Build.Status = plugin.StateSuccess
}
if cache {
err = r.RunNode(state, parser.NodeCache)
if err != nil {
log.Debugln(err)
}
}
if notify {
err = r.RunNode(state, parser.NodeNotify)
if err != nil {
log.Debugln(err)
}
}
if state.Failed() {
controller.Destroy()
os.Exit(state.ExitCode())
}
}