-
Notifications
You must be signed in to change notification settings - Fork 857
/
TaskRunner.cs
529 lines (470 loc) Β· 25.8 KB
/
TaskRunner.cs
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Agent.Sdk;
using Agent.Sdk.Knob;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
using Microsoft.TeamFoundation.DistributedTask.Expressions;
using Microsoft.TeamFoundation.DistributedTask.WebApi;
using Pipelines = Microsoft.TeamFoundation.DistributedTask.Pipelines;
using Microsoft.VisualStudio.Services.Agent.Util;
using Microsoft.VisualStudio.Services.Agent.Worker.Handlers;
using Microsoft.VisualStudio.Services.Agent.Worker.Container;
namespace Microsoft.VisualStudio.Services.Agent.Worker
{
public enum JobRunStage
{
PreJob,
Main,
PostJob,
}
[ServiceLocator(Default = typeof(TaskRunner))]
public interface ITaskRunner : IStep, IAgentService
{
JobRunStage Stage { get; set; }
Pipelines.TaskStep Task { get; set; }
}
public sealed class TaskRunner : AgentService, ITaskRunner
{
public JobRunStage Stage { get; set; }
public IExpressionNode Condition { get; set; }
public bool ContinueOnError => Task?.ContinueOnError ?? default(bool);
public string DisplayName => Task?.DisplayName;
public bool Enabled => Task?.Enabled ?? default(bool);
public IExecutionContext ExecutionContext { get; set; }
public Pipelines.TaskStep Task { get; set; }
public TimeSpan? Timeout => (Task?.TimeoutInMinutes ?? 0) > 0 ? (TimeSpan?)TimeSpan.FromMinutes(Task.TimeoutInMinutes) : null;
public Pipelines.StepTarget Target => Task?.Target;
public async Task RunAsync()
{
// Validate args.
Trace.Entering();
ArgUtil.NotNull(ExecutionContext, nameof(ExecutionContext));
ArgUtil.NotNull(ExecutionContext.Variables, nameof(ExecutionContext.Variables));
ArgUtil.NotNull(Task, nameof(Task));
var taskManager = HostContext.GetService<ITaskManager>();
var handlerFactory = HostContext.GetService<IHandlerFactory>();
// Set the task id and display name variable.
using (var scope = ExecutionContext.Variables.CreateScope())
{
scope.Set(Constants.Variables.Task.DisplayName, DisplayName);
scope.Set(WellKnownDistributedTaskVariables.TaskInstanceId, Task.Id.ToString("D"));
scope.Set(WellKnownDistributedTaskVariables.TaskDisplayName, DisplayName);
scope.Set(WellKnownDistributedTaskVariables.TaskInstanceName, Task.Name);
// Load the task definition and choose the handler.
// TODO: Add a try catch here to give a better error message.
Definition definition = taskManager.Load(Task);
ArgUtil.NotNull(definition, nameof(definition));
// Verify Signatures and Re-Extract Tasks if neccessary
await VerifyTask(taskManager, definition);
// Print out task metadata
PrintTaskMetaData(definition);
ExecutionData currentExecution = null;
switch (Stage)
{
case JobRunStage.PreJob:
currentExecution = definition.Data?.PreJobExecution;
break;
case JobRunStage.Main:
currentExecution = definition.Data?.Execution;
break;
case JobRunStage.PostJob:
currentExecution = definition.Data?.PostJobExecution;
break;
};
HandlerData handlerData = GetHandlerData(ExecutionContext, currentExecution, PlatformUtil.HostOS);
if (handlerData == null)
{
if (PlatformUtil.RunningOnWindows)
{
throw new InvalidOperationException(StringUtil.Loc("SupportedTaskHandlerNotFoundWindows", $"{PlatformUtil.HostOS}({PlatformUtil.HostArchitecture})"));
}
throw new InvalidOperationException(StringUtil.Loc("SupportedTaskHandlerNotFoundLinux"));
}
Trace.Info($"Handler data is of type {handlerData}");
Variables runtimeVariables = ExecutionContext.Variables;
IStepHost stepHost = HostContext.CreateService<IDefaultStepHost>();
var stepTarget = ExecutionContext.StepTarget();
// Setup container stephost and the right runtime variables for running job inside container.
if (stepTarget is ContainerInfo containerTarget)
{
if (Stage == JobRunStage.PostJob
&& AgentKnobs.SkipPostExeceutionIfTargetContainerStopped.GetValue(ExecutionContext).AsBoolean())
{
try
{
// Check that the target container is still running, if not Skip task execution
IDockerCommandManager dockerManager = HostContext.GetService<IDockerCommandManager>();
bool isContainerRunning = await dockerManager.IsContainerRunning(ExecutionContext, containerTarget.ContainerId);
if (!isContainerRunning)
{
ExecutionContext.Result = TaskResult.Skipped;
ExecutionContext.ResultCode = $"Target container - {containerTarget.ContainerName} has been stopped, task post-execution will be skipped";
return;
}
}
catch (Exception ex)
{
ExecutionContext.Write(WellKnownTags.Warning, $"Failed to check container state for task post-execution. Exception: {ex}");
}
}
if (handlerData is AgentPluginHandlerData)
{
// plugin handler always runs on the Host, the runtime variables needs to the variable works on the Host, ex: file path variable System.DefaultWorkingDirectory
Dictionary<string, VariableValue> variableCopy = new Dictionary<string, VariableValue>(StringComparer.OrdinalIgnoreCase);
foreach (var publicVar in ExecutionContext.Variables.Public)
{
variableCopy[publicVar.Key] = new VariableValue(stepTarget.TranslateToHostPath(publicVar.Value));
}
foreach (var secretVar in ExecutionContext.Variables.Private)
{
variableCopy[secretVar.Key] = new VariableValue(stepTarget.TranslateToHostPath(secretVar.Value), true);
}
List<string> expansionWarnings;
runtimeVariables = new Variables(HostContext, variableCopy, out expansionWarnings);
expansionWarnings?.ForEach(x => ExecutionContext.Warning(x));
}
else if (handlerData is BaseNodeHandlerData || handlerData is PowerShell3HandlerData)
{
// Only the node, node10, and powershell3 handlers support running inside container.
// Make sure required container is already created.
ArgUtil.NotNullOrEmpty(containerTarget.ContainerId, nameof(containerTarget.ContainerId));
var containerStepHost = HostContext.CreateService<IContainerStepHost>();
containerStepHost.Container = containerTarget;
stepHost = containerStepHost;
}
else
{
throw new NotSupportedException(String.Format("Task '{0}' is using legacy execution handler '{1}' which is not supported in container execution flow.", definition.Data.FriendlyName, handlerData.GetType().ToString()));
}
}
// Load the default input values from the definition.
Trace.Verbose("Loading default inputs.");
var inputs = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (var input in (definition.Data?.Inputs ?? new TaskInputDefinition[0]))
{
string key = input?.Name?.Trim() ?? string.Empty;
if (!string.IsNullOrEmpty(key))
{
if (AgentKnobs.DisableInputTrimming.GetValue(ExecutionContext).AsBoolean())
{
inputs[key] = input.DefaultValue ?? string.Empty;
}
else
{
inputs[key] = input.DefaultValue?.Trim() ?? string.Empty;
}
}
}
// Merge the instance inputs.
Trace.Verbose("Loading instance inputs.");
foreach (var input in (Task.Inputs as IEnumerable<KeyValuePair<string, string>> ?? new KeyValuePair<string, string>[0]))
{
string key = input.Key?.Trim() ?? string.Empty;
if (!string.IsNullOrEmpty(key))
{
if (AgentKnobs.DisableInputTrimming.GetValue(ExecutionContext).AsBoolean())
{
inputs[key] = input.Value ?? string.Empty;
}
else
{
inputs[key] = input.Value?.Trim() ?? string.Empty;
}
}
}
// Expand the inputs.
Trace.Verbose("Expanding inputs.");
runtimeVariables.ExpandValues(target: inputs);
VarUtil.ExpandEnvironmentVariables(HostContext, target: inputs);
// Translate the server file path inputs to local paths.
foreach (var input in definition.Data?.Inputs ?? new TaskInputDefinition[0])
{
if (string.Equals(input.InputType, TaskInputType.FilePath, StringComparison.OrdinalIgnoreCase))
{
Trace.Verbose($"Translating file path input '{input.Name}': '{inputs[input.Name]}'");
inputs[input.Name] = stepHost.ResolvePathForStepHost(TranslateFilePathInput(inputs[input.Name] ?? string.Empty));
Trace.Verbose($"Translated file path input '{input.Name}': '{inputs[input.Name]}'");
}
}
// Load the task environment.
Trace.Verbose("Loading task environment.");
var environment = new Dictionary<string, string>(VarUtil.EnvironmentVariableKeyComparer);
foreach (var env in (Task.Environment ?? new Dictionary<string, string>(0)))
{
string key = env.Key?.Trim() ?? string.Empty;
if (!string.IsNullOrEmpty(key))
{
environment[key] = env.Value?.Trim() ?? string.Empty;
}
}
// Expand the inputs.
Trace.Verbose("Expanding task environment.");
runtimeVariables.ExpandValues(target: environment);
VarUtil.ExpandEnvironmentVariables(HostContext, target: environment);
// Expand the handler inputs.
Trace.Verbose("Expanding handler inputs.");
VarUtil.ExpandValues(HostContext, source: inputs, target: handlerData.Inputs);
runtimeVariables.ExpandValues(target: handlerData.Inputs);
// Get each endpoint ID referenced by the task.
var endpointIds = new List<Guid>();
foreach (var input in definition.Data?.Inputs ?? new TaskInputDefinition[0])
{
if ((input.InputType ?? string.Empty).StartsWith("connectedService:", StringComparison.OrdinalIgnoreCase))
{
string inputKey = input?.Name?.Trim() ?? string.Empty;
string inputValue;
if (!string.IsNullOrEmpty(inputKey) &&
inputs.TryGetValue(inputKey, out inputValue) &&
!string.IsNullOrEmpty(inputValue))
{
foreach (string rawId in inputValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
Guid parsedId;
if (Guid.TryParse(rawId.Trim(), out parsedId) && parsedId != Guid.Empty)
{
endpointIds.Add(parsedId);
}
}
}
}
}
if (endpointIds.Count > 0 &&
(runtimeVariables.GetBoolean(WellKnownDistributedTaskVariables.RestrictSecrets) ?? false) &&
(runtimeVariables.GetBoolean(Microsoft.TeamFoundation.Build.WebApi.BuildVariables.IsFork) ?? false))
{
ExecutionContext.Result = TaskResult.Skipped;
ExecutionContext.ResultCode = $"References service endpoint. PRs from repository forks are not allowed to access secrets in the pipeline. For more information see https://go.microsoft.com/fwlink/?linkid=862029 ";
return;
}
// Get the endpoints referenced by the task.
var endpoints = (ExecutionContext.Endpoints ?? new List<ServiceEndpoint>(0))
.Join(inner: endpointIds,
outerKeySelector: (ServiceEndpoint endpoint) => endpoint.Id,
innerKeySelector: (Guid endpointId) => endpointId,
resultSelector: (ServiceEndpoint endpoint, Guid endpointId) => endpoint)
.ToList();
// Add the system endpoint.
foreach (ServiceEndpoint endpoint in (ExecutionContext.Endpoints ?? new List<ServiceEndpoint>(0)))
{
if (string.Equals(endpoint.Name, WellKnownServiceEndpointNames.SystemVssConnection, StringComparison.OrdinalIgnoreCase))
{
endpoints.Add(endpoint);
break;
}
}
// Get each secure file ID referenced by the task.
var secureFileIds = new List<Guid>();
foreach (var input in definition.Data?.Inputs ?? new TaskInputDefinition[0])
{
if (string.Equals(input.InputType ?? string.Empty, "secureFile", StringComparison.OrdinalIgnoreCase))
{
string inputKey = input?.Name?.Trim() ?? string.Empty;
string inputValue;
if (!string.IsNullOrEmpty(inputKey) &&
inputs.TryGetValue(inputKey, out inputValue) &&
!string.IsNullOrEmpty(inputValue))
{
foreach (string rawId in inputValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
Guid parsedId;
if (Guid.TryParse(rawId.Trim(), out parsedId) && parsedId != Guid.Empty)
{
secureFileIds.Add(parsedId);
}
}
}
}
}
if (secureFileIds.Count > 0 &&
(runtimeVariables.GetBoolean(WellKnownDistributedTaskVariables.RestrictSecrets) ?? false) &&
(runtimeVariables.GetBoolean(Microsoft.TeamFoundation.Build.WebApi.BuildVariables.IsFork) ?? false))
{
ExecutionContext.Result = TaskResult.Skipped;
ExecutionContext.ResultCode = $"References secure file. PRs from repository forks are not allowed to access secrets in the pipeline. For more information see https://go.microsoft.com/fwlink/?linkid=862029";
return;
}
// Get the endpoints referenced by the task.
var secureFiles = (ExecutionContext.SecureFiles ?? new List<SecureFile>(0))
.Join(inner: secureFileIds,
outerKeySelector: (SecureFile secureFile) => secureFile.Id,
innerKeySelector: (Guid secureFileId) => secureFileId,
resultSelector: (SecureFile secureFile, Guid secureFileId) => secureFile)
.ToList();
// Set output variables.
foreach (var outputVar in definition.Data?.OutputVariables ?? new OutputVariable[0])
{
if (outputVar != null && !string.IsNullOrEmpty(outputVar.Name))
{
ExecutionContext.OutputVariables.Add(outputVar.Name);
}
}
// translate inputs
inputs = inputs.ToDictionary(kvp => kvp.Key, kvp => ExecutionContext.TranslatePathForStepTarget(kvp.Value));
// Create the handler.
IHandler handler = handlerFactory.Create(
ExecutionContext,
Task.Reference,
stepHost,
endpoints,
secureFiles,
handlerData,
inputs,
environment,
runtimeVariables,
taskDirectory: definition.Directory);
// Run the task.
await handler.RunAsync();
}
}
public async Task VerifyTask(ITaskManager taskManager, Definition definition)
{
// Verify task signatures if a fingerprint is configured for the Agent.
var configurationStore = HostContext.GetService<IConfigurationStore>();
AgentSettings settings = configurationStore.GetSettings();
SignatureVerificationMode verificationMode = SignatureVerificationMode.None;
if (settings.SignatureVerification != null)
{
verificationMode = settings.SignatureVerification.Mode;
}
if (verificationMode != SignatureVerificationMode.None)
{
ISignatureService signatureService = HostContext.CreateService<ISignatureService>();
Boolean verificationSuccessful = await signatureService.VerifyAsync(definition, ExecutionContext.CancellationToken);
if (verificationSuccessful)
{
ExecutionContext.Output(StringUtil.Loc("TaskSignatureVerificationSucceeeded"));
// Only extract if it's not the checkout task.
if (!String.IsNullOrEmpty(definition.ZipPath))
{
taskManager.Extract(ExecutionContext, Task);
}
}
else
{
String message = StringUtil.Loc("TaskSignatureVerificationFailed");
if (verificationMode == SignatureVerificationMode.Error)
{
throw new InvalidOperationException(message);
}
else
{
ExecutionContext.Warning(message);
}
}
}
else if (settings.AlwaysExtractTask)
{
// Only extract if it's not the checkout task.
if (!String.IsNullOrEmpty(definition.ZipPath))
{
taskManager.Extract(ExecutionContext, Task);
}
}
}
public HandlerData GetHandlerData(IExecutionContext ExecutionContext, ExecutionData currentExecution, PlatformUtil.OS hostOS)
{
ArgUtil.NotNull(ExecutionContext, nameof(ExecutionContext));
if (currentExecution == null)
{
return null;
}
if ((currentExecution.All.Any(x => x is PowerShell3HandlerData)) &&
(currentExecution.All.Any(x => x is PowerShellHandlerData && x.Platforms != null && x.Platforms.Contains("windows", StringComparer.OrdinalIgnoreCase))))
{
// When task contains both PS and PS3 implementations, we will always prefer PS3 over PS regardless of the platform pinning.
Trace.Info("Ignore platform pinning for legacy PowerShell execution handler.");
var legacyPShandler = currentExecution.All.Where(x => x is PowerShellHandlerData).FirstOrDefault();
legacyPShandler.Platforms = null;
}
var targetOS = hostOS;
var stepTarget = ExecutionContext.StepTarget();
var preferPowershellHandler = true;
if (!AgentKnobs.PreferPowershellHandlerOnContainers.GetValue(ExecutionContext).AsBoolean() && stepTarget != null)
{
targetOS = stepTarget.ExecutionOS;
if (stepTarget is ContainerInfo)
{
if ((currentExecution.All.Any(x => x is PowerShell3HandlerData)) &&
(currentExecution.All.Any(x => x is BaseNodeHandlerData)))
{
Trace.Info($"Since we are targeting a container, we will prefer a node handler if one is available");
preferPowershellHandler = false;
}
}
}
Trace.Info($"Get handler data for target platform {targetOS.ToString()}");
return currentExecution.All
.OrderBy(x => !(x.PreferredOnPlatform(targetOS) && (preferPowershellHandler || !(x is PowerShell3HandlerData)))) // Sort true to false.
.ThenBy(x => x.Priority)
.FirstOrDefault();
}
private string TranslateFilePathInput(string inputValue)
{
Trace.Entering();
if (PlatformUtil.RunningOnWindows && !string.IsNullOrEmpty(inputValue))
{
Trace.Verbose("Trim double quotes around filepath type input on Windows.");
inputValue = inputValue.Trim('\"');
Trace.Verbose($"Replace any '{Path.AltDirectorySeparatorChar}' with '{Path.DirectorySeparatorChar}'.");
inputValue = inputValue.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
}
// if inputValue is rooted, return full path.
string fullPath;
if (!string.IsNullOrEmpty(inputValue) &&
inputValue.IndexOfAny(Path.GetInvalidPathChars()) < 0 &&
Path.IsPathRooted(inputValue))
{
try
{
fullPath = Path.GetFullPath(inputValue);
Trace.Info($"The original input is a rooted path, return absolute path: {fullPath}");
return fullPath;
}
catch (Exception ex)
{
Trace.Error(ex);
Trace.Info($"The original input is a rooted path, but it is not full qualified, return the path: {inputValue}");
return inputValue;
}
}
// use jobextension solve inputValue, if solved result is rooted, return full path.
var extensionManager = HostContext.GetService<IExtensionManager>();
IJobExtension[] extensions =
(extensionManager.GetExtensions<IJobExtension>() ?? new List<IJobExtension>())
.Where(x => x.HostType.HasFlag(ExecutionContext.Variables.System_HostType))
.ToArray();
foreach (IJobExtension extension in extensions)
{
fullPath = extension.GetRootedPath(ExecutionContext, inputValue);
if (!string.IsNullOrEmpty(fullPath))
{
// Stop on the first path root found.
Trace.Info($"{extension.HostType.ToString()} JobExtension resolved a rooted path:: {fullPath}");
return fullPath;
}
}
// return original inputValue.
Trace.Info("Cannot root path even by using JobExtension, return original input.");
return inputValue;
}
private void PrintTaskMetaData(Definition taskDefinition)
{
ArgUtil.NotNull(Task, nameof(Task));
ArgUtil.NotNull(Task.Reference, nameof(Task.Reference));
ArgUtil.NotNull(taskDefinition.Data, nameof(taskDefinition.Data));
ExecutionContext.Output("==============================================================================");
ExecutionContext.Output($"Task : {taskDefinition.Data.FriendlyName}");
ExecutionContext.Output($"Description : {taskDefinition.Data.Description}");
ExecutionContext.Output($"Version : {Task.Reference.Version}");
ExecutionContext.Output($"Author : {taskDefinition.Data.Author}");
ExecutionContext.Output($"Help : {taskDefinition.Data.HelpUrl ?? taskDefinition.Data.HelpMarkDown}");
ExecutionContext.Output("==============================================================================");
}
}
}