-
Notifications
You must be signed in to change notification settings - Fork 250
/
interactive.go
187 lines (159 loc) · 4.76 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
//go:build darwin || linux || freebsd
package main
import (
"context"
"fmt"
"net/url"
"os/exec"
"path"
"strconv"
"time"
tcclient "github.com/taskcluster/taskcluster/v64/clients/client-go"
"github.com/taskcluster/taskcluster/v64/internal/scopes"
"github.com/taskcluster/taskcluster/v64/workers/generic-worker/artifacts"
"github.com/taskcluster/taskcluster/v64/workers/generic-worker/expose"
"github.com/taskcluster/taskcluster/v64/workers/generic-worker/interactive"
)
type InteractiveFeature struct {
}
func (feature *InteractiveFeature) Name() string {
return "Interactive"
}
func (feature *InteractiveFeature) Initialise() error {
return nil
}
func (feature *InteractiveFeature) PersistState() error {
return nil
}
func (feature *InteractiveFeature) IsEnabled(task *TaskRun) bool {
return task.Payload.Features.Interactive
}
type InteractiveTask struct {
task *TaskRun
interactive *interactive.Interactive
exposure expose.Exposure
artifactName string
cancel context.CancelFunc
}
func (feature *InteractiveFeature) NewTaskFeature(task *TaskRun) TaskFeature {
return &InteractiveTask{
task: task,
artifactName: "private/generic-worker/shell.html",
}
}
func (it *InteractiveTask) RequiredScopes() scopes.Required {
return scopes.Required{}
}
func (it *InteractiveTask) ReservedArtifacts() []string {
return []string{
it.artifactName,
}
}
func (it *InteractiveTask) Start() *CommandExecutionError {
if !config.EnableInteractive {
workerPoolID := config.ProvisionerID + "/" + config.WorkerType
workerManagerURL := config.RootURL + "/worker-manager/" + url.PathEscape(workerPoolID)
return MalformedPayloadError(fmt.Errorf(`this task has payload.features.interactive set to true, but enableInteractive is not enabled on this worker pool (%s)
If you do not require an interactive task, remove payload.features.interactive from the task definition.
If you do require an interactive task, please do one of two things:
1. Contact the owner of the worker pool %s (see %s) and ask for interactive tasks to be enabled.
2. Use a worker pool that already allows interactive tasks (search for "enableInteractive": "true" in the worker pool definition)`, workerPoolID, workerPoolID, workerManagerURL))
}
ctx, cancel := context.WithCancel(context.Background())
cmd := func() (*exec.Cmd, error) {
return it.task.generateInteractiveCommand(ctx)
}
interactive, err := interactive.New(config.InteractivePort, cmd, ctx)
if err != nil {
it.task.Warnf("[interactive] could not create interactive session: %v", err)
cancel()
return nil
}
it.interactive = interactive
it.cancel = cancel
done := make(chan error, 1)
go func() {
done <- it.interactive.ListenAndServe(ctx)
}()
err = it.uploadInteractiveArtifact()
if err != nil {
it.task.Warnf("[interactive] could not upload interactive artifact: %v", err)
}
select {
case err := <-done:
if err != nil {
return &CommandExecutionError{
Cause: err,
}
}
return nil
default:
return nil
}
}
func (it *InteractiveTask) Stop(err *ExecutionErrors) {
if it.interactive == nil {
return
}
it.cancel()
if it.exposure != nil {
closeErr := it.exposure.Close()
it.exposure = nil
if closeErr != nil {
it.task.Warnf("[interactive] could not terminate interactive exposure: %v", closeErr)
}
}
}
func (it *InteractiveTask) uploadInteractiveArtifact() error {
var err error
it.exposure, err = exposer.ExposeHTTP(it.interactive.TCPPort)
if err != nil {
return err
}
// combine the path from the interactive URL with the expose URL
interactiveURL, err := url.Parse(it.interactive.GetURL)
if err != nil {
return err
}
exposeURL := it.exposure.GetURL()
if exposeURL.Path == "/" {
exposeURL.Path = interactiveURL.Path
} else {
exposeURL.Path = path.Join(exposeURL.Path, interactiveURL.Path)
}
if exposeURL.Scheme == "https" {
exposeURL.Scheme = "wss"
} else {
exposeURL.Scheme = "ws"
}
// query params required in ui/src/views/Shell/index.jsx
queryParams := url.Values{}
queryParams.Set("v", "2")
queryParams.Set("socketUrl", exposeURL.String())
queryParams.Set("taskId", it.task.TaskID)
queryParams.Set("runId", strconv.FormatUint(uint64(it.task.RunID), 10))
// defined in ui/src/App/routes.jsx
shellPath := "/shell/"
u, err := url.Parse(config.RootURL + shellPath)
if err != nil {
return err
}
u.RawQuery = queryParams.Encode()
url := u.String()
expires := time.Now().Add(time.Duration(it.task.Payload.MaxRunTime+900) * time.Second)
uploadErr := it.task.uploadArtifact(
&artifacts.RedirectArtifact{
BaseArtifact: &artifacts.BaseArtifact{
Name: it.artifactName,
Expires: tcclient.Time(expires),
},
ContentType: "text/html; charset=utf-8",
URL: url,
HideURL: true,
},
)
if uploadErr != nil {
return uploadErr
}
return nil
}