-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathAgentJobRequestMessage.cs
More file actions
477 lines (420 loc) · 14.4 KB
/
AgentJobRequestMessage.cs
File metadata and controls
477 lines (420 loc) · 14.4 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using GitHub.DistributedTask.ObjectTemplating.Tokens;
using GitHub.DistributedTask.Pipelines.ContextData;
using GitHub.DistributedTask.WebApi;
using GitHub.Services.WebApi;
using Newtonsoft.Json;
namespace GitHub.DistributedTask.Pipelines
{
[DataContract]
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class AgentJobRequestMessage
{
[JsonConstructor]
internal AgentJobRequestMessage()
{
}
/// <summary>
/// Job request message sent to the runner
/// </summary>
/// <param name="environmentVariables">Hierarchy of environment variables to overlay, last wins.</param>
public AgentJobRequestMessage(
TaskOrchestrationPlanReference plan,
TimelineReference timeline,
Guid jobId,
String jobDisplayName,
String jobName,
TemplateToken jobContainer,
TemplateToken jobServiceContainers,
IList<TemplateToken> environmentVariables,
IDictionary<String, VariableValue> variables,
IList<MaskHint> maskHints,
JobResources jobResources,
DictionaryContextData contextData,
WorkspaceOptions workspaceOptions,
IEnumerable<JobStep> steps,
IList<String> fileTable,
TemplateToken jobOutputs,
IList<TemplateToken> defaults,
ActionsEnvironmentReference actionsEnvironment,
TemplateToken snapshot,
String messageType = JobRequestMessageTypes.PipelineAgentJobRequest)
{
this.MessageType = messageType;
this.Plan = plan;
this.JobId = jobId;
this.JobDisplayName = jobDisplayName;
this.JobName = jobName;
this.JobContainer = jobContainer;
this.JobServiceContainers = jobServiceContainers;
this.Timeline = timeline;
this.Resources = jobResources;
this.Workspace = workspaceOptions;
this.JobOutputs = jobOutputs;
this.ActionsEnvironment = actionsEnvironment;
this.Snapshot = snapshot;
m_variables = new Dictionary<String, VariableValue>(variables, StringComparer.OrdinalIgnoreCase);
m_maskHints = new List<MaskHint>(maskHints);
m_steps = new List<JobStep>(steps);
if (environmentVariables?.Count > 0)
{
m_environmentVariables = new List<TemplateToken>(environmentVariables);
}
if (defaults?.Count > 0)
{
m_defaults = new List<TemplateToken>(defaults);
}
this.ContextData = new Dictionary<String, PipelineContextData>(StringComparer.OrdinalIgnoreCase);
if (contextData?.Count > 0)
{
foreach (var pair in contextData)
{
this.ContextData[pair.Key] = pair.Value;
}
}
if (fileTable?.Count > 0)
{
m_fileTable = new List<String>(fileTable);
}
}
[DataMember]
public String MessageType
{
get;
private set;
}
[DataMember]
public TaskOrchestrationPlanReference Plan
{
get;
private set;
}
[DataMember]
public TimelineReference Timeline
{
get;
private set;
}
[DataMember]
public Guid JobId
{
get;
private set;
}
[DataMember]
public String JobDisplayName
{
get;
private set;
}
[DataMember]
public String JobName
{
get;
private set;
}
[DataMember(EmitDefaultValue = false)]
public TemplateToken JobContainer
{
get;
private set;
}
[DataMember(EmitDefaultValue = false)]
public TemplateToken JobServiceContainers
{
get;
private set;
}
[DataMember(EmitDefaultValue = false)]
public TemplateToken JobOutputs
{
get;
private set;
}
[DataMember]
public Int64 RequestId
{
get;
internal set;
}
[DataMember]
public DateTime LockedUntil
{
get;
internal set;
}
[DataMember]
public JobResources Resources
{
get;
private set;
}
[DataMember(EmitDefaultValue = false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public IDictionary<String, PipelineContextData> ContextData
{
get;
private set;
}
[DataMember(EmitDefaultValue = false)]
public WorkspaceOptions Workspace
{
get;
private set;
}
/// <summary>
/// Gets the collection of mask hints
/// </summary>
public List<MaskHint> MaskHints
{
get
{
if (m_maskHints == null)
{
m_maskHints = new List<MaskHint>();
}
return m_maskHints;
}
}
/// <summary>
/// Gets the hierarchy of environment variables to overlay, last wins.
/// </summary>
public IList<TemplateToken> EnvironmentVariables
{
get
{
if (m_environmentVariables == null)
{
m_environmentVariables = new List<TemplateToken>();
}
return m_environmentVariables;
}
}
/// <summary>
/// Gets the hierarchy of defaults to overlay, last wins.
/// </summary>
public IList<TemplateToken> Defaults
{
get
{
if (m_defaults == null)
{
m_defaults = new List<TemplateToken>();
}
return m_defaults;
}
}
[DataMember(EmitDefaultValue = false)]
public ActionsEnvironmentReference ActionsEnvironment
{
get;
set;
}
[DataMember(EmitDefaultValue = false)]
public TemplateToken Snapshot
{
get;
set;
}
[DataMember(EmitDefaultValue = false)]
public String BillingOwnerId
{
get;
set;
}
[DataMember(EmitDefaultValue = false)]
public bool EnableDebugger
{
get;
set;
}
[DataMember(EmitDefaultValue = false)]
public DebuggerTunnelInfo DebuggerTunnel
{
get;
set;
}
/// <summary>
/// Gets the collection of variables associated with the current context.
/// </summary>
public IDictionary<String, VariableValue> Variables
{
get
{
if (m_variables == null)
{
m_variables = new Dictionary<String, VariableValue>(StringComparer.OrdinalIgnoreCase);
}
return m_variables;
}
}
public IList<JobStep> Steps
{
get
{
if (m_steps == null)
{
m_steps = new List<JobStep>();
}
return m_steps;
}
}
/// <summary>
/// Gets the table of files used when parsing the pipeline (e.g. yaml files)
/// </summary>
public IList<String> FileTable
{
get
{
if (m_fileTable == null)
{
m_fileTable = new List<String>();
}
return m_fileTable;
}
}
// todo: remove after feature-flag DistributedTask.EvaluateContainerOnRunner is enabled everywhere
public void SetJobSidecarContainers(IDictionary<String, String> value)
{
m_jobSidecarContainers = value;
}
public TaskAgentMessage GetAgentMessage()
{
var body = JsonUtility.ToString(this);
return new TaskAgentMessage
{
Body = body,
MessageType = JobRequestMessageTypes.PipelineAgentJobRequest
};
}
// todo: remove after feature-flag DistributedTask.EvaluateContainerOnRunner is enabled everywhere
internal static TemplateToken ConvertToTemplateToken(ContainerResource resource)
{
var result = new MappingToken(null, null, null);
var image = resource.Image;
if (!string.IsNullOrEmpty(image))
{
result.Add(new StringToken(null, null, null, "image"), new StringToken(null, null, null, image));
}
var options = resource.Options;
if (!string.IsNullOrEmpty(options))
{
result.Add(new StringToken(null, null, null, "options"), new StringToken(null, null, null, options));
}
var environment = resource.Environment;
if (environment?.Count > 0)
{
var mapping = new MappingToken(null, null, null);
foreach (var pair in environment)
{
mapping.Add(new StringToken(null, null, null, pair.Key), new StringToken(null, null, null, pair.Value));
}
result.Add(new StringToken(null, null, null, "env"), mapping);
}
var ports = resource.Ports;
if (ports?.Count > 0)
{
var sequence = new SequenceToken(null, null, null);
foreach (var item in ports)
{
sequence.Add(new StringToken(null, null, null, item));
}
result.Add(new StringToken(null, null, null, "ports"), sequence);
}
var volumes = resource.Volumes;
if (volumes?.Count > 0)
{
var sequence = new SequenceToken(null, null, null);
foreach (var item in volumes)
{
sequence.Add(new StringToken(null, null, null, item));
}
result.Add(new StringToken(null, null, null, "volumes"), sequence);
}
return result;
}
[OnDeserialized]
private void OnDeserialized(StreamingContext context)
{
// todo: remove after feature-flag DistributedTask.EvaluateContainerOnRunner is enabled everywhere
if (JobContainer is StringToken jobContainerStringToken)
{
var resourceAlias = jobContainerStringToken.Value;
var resource = Resources?.Containers.SingleOrDefault(x => string.Equals(x.Alias, resourceAlias, StringComparison.OrdinalIgnoreCase));
if (resource != null)
{
JobContainer = ConvertToTemplateToken(resource);
m_jobContainerResourceAlias = resourceAlias;
}
}
// todo: remove after feature-flag DistributedTask.EvaluateContainerOnRunner is enabled everywhere
if (m_jobSidecarContainers?.Count > 0 && (JobServiceContainers == null || JobServiceContainers.Type == TokenType.Null))
{
var services = new MappingToken(null, null, null);
foreach (var pair in m_jobSidecarContainers)
{
var networkAlias = pair.Key;
var serviceResourceAlias = pair.Value;
var serviceResource = Resources.Containers.Single(x => string.Equals(x.Alias, serviceResourceAlias, StringComparison.OrdinalIgnoreCase));
services.Add(new StringToken(null, null, null, networkAlias), ConvertToTemplateToken(serviceResource));
}
JobServiceContainers = services;
}
}
[OnSerializing]
private void OnSerializing(StreamingContext context)
{
if (m_environmentVariables?.Count == 0)
{
m_environmentVariables = null;
}
if (m_defaults?.Count == 0)
{
m_defaults = null;
}
if (m_fileTable?.Count == 0)
{
m_fileTable = null;
}
if (m_maskHints?.Count == 0)
{
m_maskHints = null;
}
else if (m_maskHints != null)
{
m_maskHints = new List<MaskHint>(this.m_maskHints.Distinct());
}
if (m_variables?.Count == 0)
{
m_variables = null;
}
// todo: remove after feature-flag DistributedTask.EvaluateContainerOnRunner is enabled everywhere
if (!string.IsNullOrEmpty(m_jobContainerResourceAlias))
{
JobContainer = new StringToken(null, null, null, m_jobContainerResourceAlias);
}
}
[DataMember(Name = "EnvironmentVariables", EmitDefaultValue = false)]
private List<TemplateToken> m_environmentVariables;
[DataMember(Name = "Defaults", EmitDefaultValue = false)]
private List<TemplateToken> m_defaults;
[DataMember(Name = "FileTable", EmitDefaultValue = false)]
private List<String> m_fileTable;
[DataMember(Name = "Mask", EmitDefaultValue = false)]
private List<MaskHint> m_maskHints;
[DataMember(Name = "Steps", EmitDefaultValue = false)]
private List<JobStep> m_steps;
[DataMember(Name = "Variables", EmitDefaultValue = false)]
private IDictionary<String, VariableValue> m_variables;
// todo: remove after feature-flag DistributedTask.EvaluateContainerOnRunner is enabled everywhere
[DataMember(Name = "JobSidecarContainers", EmitDefaultValue = false)]
private IDictionary<String, String> m_jobSidecarContainers;
// todo: remove after feature-flag DistributedTask.EvaluateContainerOnRunner is enabled everywhere
[IgnoreDataMember]
private string m_jobContainerResourceAlias;
}
}