This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
/
operation_task.go
292 lines (251 loc) · 7.6 KB
/
operation_task.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
package runner
import (
"context"
"fmt"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/opaqueany"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/hashicorp/waypoint-plugin-sdk/component"
"github.com/hashicorp/waypoint-plugin-sdk/terminal"
"github.com/hashicorp/waypoint/internal/plugin"
pb "github.com/hashicorp/waypoint/pkg/server/gen"
)
func (r *Runner) executeStartTaskOp(
ctx context.Context,
log hclog.Logger,
ui terminal.UI,
job *pb.Job,
) (*pb.Job_Result, error) {
op, ok := job.Operation.(*pb.Job_StartTask)
if !ok {
// this shouldn't happen since the call to this function is gated
// on the above type match.
panic("operation not expected type")
}
params := op.StartTask.Params
pi, c, err := plugin.Open(ctx, log, &plugin.PluginRequest{
Config: plugin.Config{
Name: params.PluginType,
},
Dir: "/tmp",
ConfigData: params.HclConfig,
JsonConfig: params.HclFormat == pb.Hcl_JSON,
Type: component.TaskLauncherType,
})
if err != nil {
return nil, err
}
defer pi.Close()
tli := &component.TaskLaunchInfo{}
sti := op.StartTask.Info
if sti != nil {
tli.OciUrl = sti.OciUrl
tli.EnvironmentVariables = sti.EnvironmentVariables
tli.Entrypoint = sti.Entrypoint
tli.Arguments = sti.Arguments
}
fn := c.(component.TaskLauncher).StartTaskFunc()
val, err := pi.Invoke(ctx, log, fn, tli)
if err != nil {
return nil, err
}
result, err := component.ProtoAny(val)
if err != nil {
return nil, err
}
return &pb.Job_Result{
StartTask: &pb.Job_StartTaskResult{
State: result,
},
}, nil
}
func (r *Runner) executeStopTaskOp(
ctx context.Context,
log hclog.Logger,
ui terminal.UI,
job *pb.Job,
) (*pb.Job_Result, error) {
op, ok := job.Operation.(*pb.Job_StopTask)
if !ok {
// this shouldn't happen since the call to this function is gated
// on the above type match.
panic("operation not expected type")
}
// Get our state first
// TODO(briancain): Update this to use the Task job id ref instead of looking
// at the task state proto
var state *opaqueany.Any
switch v := op.StopTask.State.(type) {
case *pb.Job_StopTaskLaunchOp_Direct:
log.Debug("using directly provided state")
state = v.Direct
case *pb.Job_StopTaskLaunchOp_StartJobId:
// Look up the state from a start job.
log.Debug("looking up start job to get state", "start-id", v.StartJobId)
job, err := r.client.GetJob(ctx, &pb.GetJobRequest{
JobId: v.StartJobId,
})
log.Trace(fmt.Sprintf("Got this job back: %+v", job))
// If the job is not found, this is not an error. This means the
// start job never ran for whatever reason and we should not stop
// anything.
if status.Code(err) == codes.NotFound {
log.Warn("start job not found, not stopping anything",
"start-id", v.StartJobId)
return nil, nil
} else if err != nil {
return nil, errors.Wrapf(err, "failed to look up job with id %s", v.StartJobId)
}
// If the job is not in a terminal state, then its an error.
if job.State != pb.Job_SUCCESS && job.State != pb.Job_ERROR {
return nil, status.Errorf(codes.FailedPrecondition,
"cannot stop task when the start job is not terminal: %q",
job.State)
}
// If the job is not a start task launch operation, then error.
_, ok := job.Operation.(*pb.Job_StartTask)
if !ok {
return nil, status.Errorf(codes.FailedPrecondition,
"start job ID must reference a job with a StartTask op, got %T",
job.Operation)
}
// If we have no result, do nothing.
if job.Result == nil {
log.Warn("start job has no result, ignoring")
return nil, nil
}
result := job.Result.StartTask
if result == nil || result.State == nil {
log.Warn("start job has no state, ignoring")
return nil, nil
}
// The state we use is the resulting state
state = result.State
}
// At this point, state should not be nil. There are cases earlier
// where we may exit early with a nil state, but we do not allow a
// nil state here.
if state == nil {
return nil, status.Errorf(codes.FailedPrecondition,
"nil start task state provided")
}
// Launch our plugin
params := op.StopTask.Params
pi, c, err := plugin.Open(ctx, log, &plugin.PluginRequest{
Config: plugin.Config{
Name: params.PluginType,
},
Dir: "/tmp",
ConfigData: params.HclConfig,
JsonConfig: params.HclFormat == pb.Hcl_JSON,
Type: component.TaskLauncherType,
})
if err != nil {
return nil, err
}
defer pi.Close()
stop := c.(component.TaskLauncher).StopTaskFunc()
_, err = pi.Invoke(ctx, log, stop, plugin.ArgNamedAny("state", state))
if err != nil {
return nil, err
}
return &pb.Job_Result{}, nil
}
func (r *Runner) executeWatchTaskOp(
ctx context.Context,
log hclog.Logger,
ui terminal.UI,
job *pb.Job,
) (*pb.Job_Result, error) {
op, ok := job.Operation.(*pb.Job_WatchTask)
if !ok {
// this shouldn't happen since the call to this function is gated
// on the above type match.
panic("operation not expected type")
}
// Look up the state from a start job.
startId := op.WatchTask.StartJob.Id
log = log.With("start-job-id", startId)
log.Debug("looking up start job to get state")
job, err := r.client.GetJob(ctx, &pb.GetJobRequest{
JobId: startId,
})
// If the job is not found, this is not an error. This means the
// start job never ran for whatever reason and we should not watch
// anything.
if status.Code(err) == codes.NotFound {
log.Warn("start job not found, skipping watch")
return nil, nil
} else if err != nil {
return nil, errors.Wrapf(err, "failed to look up job with id %s", startId)
}
// If the job is not in a terminal state, then its an error.
if job.State != pb.Job_SUCCESS && job.State != pb.Job_ERROR {
return nil, status.Errorf(codes.FailedPrecondition,
"cannot watch task when the start job is not terminal: %q",
job.State)
}
// If the job is not a start task launch operation, then error.
startOp, ok := job.Operation.(*pb.Job_StartTask)
if !ok {
return nil, status.Errorf(codes.FailedPrecondition,
"start job ID must reference a job with a StartTask op, got %T",
job.Operation)
}
// If we have no result, do nothing, assume start failed.
if job.Result == nil {
log.Warn("start job has no result, ignoring")
return nil, nil
}
result := job.Result.StartTask
if result == nil || result.State == nil {
log.Warn("start job has no state, ignoring")
return nil, nil
}
// The state we use is the resulting state
state := result.State
// At this point, state should not be nil. There are cases earlier
// where we may exit early with a nil state, but we do not allow a
// nil state here.
if state == nil {
return nil, status.Errorf(codes.FailedPrecondition,
"nil start task state provided")
}
// We copy the launch params from the start task because we should
// be using the same task launcher plugin.
params := startOp.StartTask.Params
// Launch our plugin
pi, c, err := plugin.Open(ctx, log, &plugin.PluginRequest{
Config: plugin.Config{
Name: params.PluginType,
},
Dir: "/tmp",
ConfigData: params.HclConfig,
JsonConfig: params.HclFormat == pb.Hcl_JSON,
Type: component.TaskLauncherType,
})
if err != nil {
return nil, err
}
defer pi.Close()
watch := c.(component.TaskLauncher).WatchTaskFunc()
output, err := pi.Invoke(ctx, log, watch,
plugin.ArgNamedAny("state", state),
ui,
)
if err != nil {
return nil, err
}
taskResult, ok := output.(*component.TaskResult)
if !ok {
return nil, status.Errorf(codes.FailedPrecondition,
"plugin should've returned TaskResult, got %T", output)
}
return &pb.Job_Result{
WatchTask: &pb.Job_WatchTaskResult{
ExitCode: int32(taskResult.ExitCode),
},
}, nil
}