This repository has been archived by the owner on Feb 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
interactive.go
318 lines (284 loc) · 8.55 KB
/
interactive.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
package interactive
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"strings"
"sync"
schematypes "github.com/taskcluster/go-schematypes"
"github.com/taskcluster/taskcluster-worker/engines"
"github.com/taskcluster/taskcluster-worker/plugins"
"github.com/taskcluster/taskcluster-worker/runtime"
"github.com/taskcluster/taskcluster-worker/runtime/ioext"
"github.com/taskcluster/taskcluster-worker/runtime/util"
"github.com/taskcluster/taskcluster-worker/runtime/webhookserver"
)
// defaultArtifactPrefix is the default artifact prefix used if nothing is
// configured or given in the task definition
const defaultArtifactPrefix = "private/interactive/"
// defaultShellToolURL is the default URL for the tool that can connect to the
// shell socket and display an interactive shell session.
const defaultShellToolURL = "https://tools.taskcluster.net/shell/"
// defaultShellToolURL is the default URL for the tool that can list displays
// and connect to the display socket with interactive noVNC session.
const defaultDisplayToolURL = "https://tools.taskcluster.net/display/"
type provider struct {
plugins.PluginProviderBase
}
func (provider) ConfigSchema() schematypes.Schema {
return configSchema
}
func (provider) NewPlugin(options plugins.PluginOptions) (plugins.Plugin, error) {
var c config
schematypes.MustValidateAndMap(configSchema, options.Config, &c)
if c.ArtifactPrefix == "" {
c.ArtifactPrefix = defaultArtifactPrefix
}
if c.ShellToolURL == "" {
c.ShellToolURL = defaultShellToolURL
}
if c.DisplayToolURL == "" {
c.DisplayToolURL = defaultDisplayToolURL
}
// IF no WebHookServer is available we disabling the interactive plugin
if options.Environment.WebHookServer == nil {
options.Monitor.Info("interactive plugin should be disabled when no WebHookServer is configured")
return plugins.PluginBase{}, nil
}
return &plugin{
config: c,
monitor: options.Monitor,
webhookserver: options.Environment.WebHookServer,
}, nil
}
type plugin struct {
plugins.PluginBase
config config
monitor runtime.Monitor
webhookserver webhookserver.WebHookServer
}
func (p *plugin) PayloadSchema() schematypes.Object {
s := schematypes.Object{
Title: "Interactive Features",
Description: util.Markdown(`
Settings for interactive features, all options are optional,
an empty object can be used to enable the interactive features with
default options.
`),
Properties: schematypes.Properties{
"disableDisplay": schematypes.Boolean{
Title: "Disable Display",
Description: util.Markdown(`
Disable the interactive display, defaults to enabled if any options
is given for 'interactive', even an empty object.
`),
},
"disableShell": schematypes.Boolean{
Title: "Disable Shell",
Description: util.Markdown(`
Disable the interactive shell, defaults to enabled if any options
is given for 'interactive', even an empty object.
`),
},
},
}
if !p.config.ForbidCustomArtifactPrefix {
s.Properties["artifactPrefix"] = schematypes.String{
Title: "Artifact Prefix",
Description: util.Markdown(`
Prefix for the interactive artifacts will be used to create
'<prefix>/shell.html', '<prefix>/display.html' and
'<prefix>/sockets.json'. The prefix defaults to
'` + p.config.ArtifactPrefix + `'.
`),
Pattern: `^[\x20-.0-\x7e][\x20-\x7e]*/$`,
MaximumLength: 255,
}
}
return schematypes.Object{
Properties: schematypes.Properties{
"interactive": s,
},
}
}
func (p *plugin) NewTaskPlugin(options plugins.TaskPluginOptions) (
plugins.TaskPlugin, error,
) {
var P payload
schematypes.MustValidateAndMap(p.PayloadSchema(), options.Payload, &P)
// If not always enabled or no options are given then this is disabled
if P.Interactive == nil && !p.config.AlwaysEnabled {
return plugins.TaskPluginBase{}, nil
}
// Extract options
o := opts{}
if P.Interactive != nil {
o = *P.Interactive
}
if o.ArtifactPrefix == "" || p.config.ForbidCustomArtifactPrefix {
o.ArtifactPrefix = p.config.ArtifactPrefix
}
return &taskPlugin{
context: options.TaskContext,
webhooks: webhookserver.NewWebHookSet(p.webhookserver),
opts: o,
monitor: options.Monitor,
parent: p,
}, nil
}
type taskPlugin struct {
plugins.TaskPluginBase
parent *plugin
webhooks *webhookserver.WebHookSet
monitor runtime.Monitor
opts opts
sandbox engines.Sandbox
context *runtime.TaskContext
shellURL string
shellServer *ShellServer
displaysURL string
displaySocketURL string
displayServer *DisplayServer
}
func (p *taskPlugin) Started(sandbox engines.Sandbox) error {
p.sandbox = sandbox
// Setup shell and display in parallel
wg := sync.WaitGroup{}
wg.Add(2)
var err1, err2 error
go func() {
err1 = p.setupShell()
wg.Done()
}()
go func() {
err2 = p.setupDisplay()
wg.Done()
}()
wg.Wait()
// Return any of the two errors
if err1 != nil {
return fmt.Errorf("Setting up interactive shell failed, error: %s", err1)
}
if err2 != nil {
return fmt.Errorf("Setting up interactive display failed, error: %s", err2)
}
err := p.createSocketsFile()
if err != nil {
return fmt.Errorf("Failed to create sockets.json file, error: %s", err)
}
return nil
}
func (p *taskPlugin) Stopped(_ engines.ResultSet) (bool, error) {
return true, p.Dispose()
}
func (p *taskPlugin) Exception(_ runtime.ExceptionReason) error {
return p.Dispose()
}
func (p *taskPlugin) Dispose() error {
// NOTE: This is also called from Stopped() and Exception()
util.Parallel(func() {
if p.shellServer != nil {
p.shellServer.Abort()
p.shellServer.Wait()
}
p.shellServer = nil
}, func() {
if p.displayServer != nil {
p.displayServer.Abort()
}
p.displayServer = nil
}, func() {
if p.webhooks != nil {
p.webhooks.Dispose()
}
p.webhooks = nil
})
return nil
}
func (p *taskPlugin) setupShell() error {
// Setup shell if not disabled
if p.opts.DisableShell {
return nil
}
debug("Setting up interactive shell")
// Create shell server and get a URL to reach it
p.shellServer = NewShellServer(
p.sandbox.NewShell, p.monitor.WithPrefix("shell-server"),
)
u := p.webhooks.AttachHook(p.shellServer)
p.shellURL = urlProtocolToWebsocket(u)
query := url.Values{}
query.Set("v", "2")
query.Set("taskId", p.context.TaskID)
query.Set("runId", fmt.Sprintf("%d", p.context.RunID))
query.Set("socketUrl", p.shellURL)
return p.context.CreateRedirectArtifact(runtime.RedirectArtifact{
Name: p.opts.ArtifactPrefix + "shell.html",
Mimetype: "text/html",
URL: p.parent.config.ShellToolURL + "?" + query.Encode(),
Expires: p.context.TaskInfo.Deadline,
})
}
func (p *taskPlugin) setupDisplay() error {
// Setup display if not disabled
if p.opts.DisableDisplay {
return nil
}
debug("Setting up interactive display")
// Create display server
p.displayServer = NewDisplayServer(
p.sandbox, p.monitor.WithPrefix("display-server"),
)
u := p.webhooks.AttachHook(p.displayServer)
p.displaysURL = u
p.displaySocketURL = urlProtocolToWebsocket(u)
query := url.Values{}
query.Set("v", "1")
query.Set("taskId", p.context.TaskID)
query.Set("runId", fmt.Sprintf("%d", p.context.RunID))
query.Set("socketUrl", p.displaySocketURL)
query.Set("displaysUrl", p.displaysURL)
// TODO: Make this an option the engine can specify in ListDisplays
// Probably requires changing display list result to contain websocket
// URLs. Hence, introducing v=2, so leaving it for later.
query.Set("shared", "true")
return p.context.CreateRedirectArtifact(runtime.RedirectArtifact{
Name: p.opts.ArtifactPrefix + "display.html",
Mimetype: "text/html",
URL: p.parent.config.DisplayToolURL + "?" + query.Encode(),
Expires: p.context.TaskInfo.Deadline,
})
}
func (p *taskPlugin) createSocketsFile() error {
debug("Uploading sockets.json")
// Create sockets.json
sockets := map[string]interface{}{
"version": 2,
}
if p.shellURL != "" {
sockets["shellSocketUrl"] = p.shellURL
}
if p.displaysURL != "" {
sockets["displaysUrl"] = p.displaysURL
}
if p.displaySocketURL != "" {
sockets["displaySocketUrl"] = p.displaySocketURL
}
data, _ := json.MarshalIndent(sockets, "", " ")
return p.context.UploadS3Artifact(runtime.S3Artifact{
Name: p.opts.ArtifactPrefix + "sockets.json",
Mimetype: "application/json",
Expires: p.context.TaskInfo.Deadline,
Stream: ioext.NopCloser(bytes.NewReader(data)),
})
}
func urlProtocolToWebsocket(u string) string {
if strings.HasPrefix(u, "http://") {
return "ws://" + u[7:]
}
if strings.HasPrefix(u, "https://") {
return "wss://" + u[8:]
}
return u
}