forked from Azure/azure-sdk-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGitProcessHandler.cs
More file actions
435 lines (377 loc) · 18.6 KB
/
GitProcessHandler.cs
File metadata and controls
435 lines (377 loc) · 18.6 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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Azure.Sdk.Tools.TestProxy.Common;
using Azure.Sdk.Tools.TestProxy.Common.Exceptions;
namespace Azure.Sdk.Tools.TestProxy.Store
{
public class CommandResult
{
public int ExitCode;
public string StdErr;
public string StdOut;
public string Arguments;
public Exception CommandException;
}
/// <summary>
/// This class offers an easy wrapper abstraction for shelling out to git.
/// </summary>
public class GitProcessHandler
{
public const int RETRY_INTERMITTENT_FAILURE_COUNT = 3;
/// <summary>
/// Internal class to hold the minimum supported version of git. If that
/// version changes we only need to change it here.
/// </summary>
class GitMinVersion
{
// As per https://github.com/Azure/azure-sdk-tools/issues/4146, the min version of git
// that supports what we need is 2.25.0.
public static int Major = 2;
public static int Minor = 25;
public static int Patch = 0;
public static string minVersionString = $"{Major}.{Minor}.{Patch}";
}
/// <summary>
/// This dictionary is used to ensure that each git directory will only ever have a SINGLE git command running it at a time.
/// </summary>
private ConcurrentDictionary<string, TaskQueue> AssetTasks = new ConcurrentDictionary<string, TaskQueue>();
/// <summary>
/// Create a ProcessStartInfo that's exclusively used for execution of git commands
/// </summary>
/// <param name="workingDirectory">The directory where the commands are to be executed. For normal processing
/// through GitStore, this will end up being GitAssetsConfiguration's AssetsRepoLocation.</param>
/// <returns></returns>
public virtual ProcessStartInfo CreateGitProcessInfo(string workingDirectory)
{
var startInfo = new ProcessStartInfo("git")
{
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
WorkingDirectory = workingDirectory,
};
startInfo.EnvironmentVariables["PATH"] = Environment.GetEnvironmentVariable("PATH");
return startInfo;
}
/// <summary>
/// Invokes a git command. If it fails in any way, throws GitProcessException. Otherwise returns the result of the git invocation.
/// </summary>
/// <param name="config"></param>
/// <param name="inputCommands"></param>
/// <returns>A list of command results. One can assume success in all of them unless an exception has been thrown</returns>
/// <exception cref="GitProcessException">Throws GitProcessException on returnCode != 0 OR if an unexpected exception is thrown during invocation.</exception>
public virtual IEnumerable<CommandResult> Run(GitAssetsConfiguration config, params string[] inputCommands)
{
List<CommandResult> results = new List<CommandResult>();
foreach (var inputCommand in inputCommands)
{
results.Add(Run(inputCommand, config));
}
return results;
}
/// <summary>
/// Invokes a git command. If it fails in any way, throws GitProcessException. Otherwise returns the result of the git invocation.
/// </summary>
/// <param name="arguments">git command line arguments</param>
/// <param name="config">GitAssetsConfiguration</param>
/// <returns></returns>
/// <exception cref="GitProcessException">Throws GitProcessException on returnCode != 0 OR if an unexpected exception is thrown during invocation.</exception>
public virtual CommandResult Run(string arguments, GitAssetsConfiguration config)
{
return Run(arguments, config.AssetsRepoLocation.ToString());
}
/// <summary>
/// Invokes a git command. If it fails in any way, throws GitProcessException. Otherwise returns the result of the git invocation.
/// </summary>
/// <param name="arguments">git command line arguments</param>
/// <param name="workingDirectory">effectively the AssetsRepoLocation</param>
/// <returns></returns>
/// <exception cref="GitProcessException">Throws GitProcessException on returnCode != 0 OR if an unexpected exception is thrown during invocation.</exception>
public virtual CommandResult Run(string arguments, string workingDirectory)
{
// Surface an easy to understand error when we shoot ourselves in the foot
if (arguments.StartsWith("git"))
{
throw new Exception("GitProcessHandler commands should not start with 'git'");
}
ProcessStartInfo processStartInfo = CreateGitProcessInfo(workingDirectory);
processStartInfo.Arguments = arguments;
CommandResult result = new CommandResult()
{
Arguments = arguments
};
var queue = AssetTasks.GetOrAdd(workingDirectory, new TaskQueue());
queue.Enqueue(() =>
{
try
{
int attempts = 1;
while (attempts <= RETRY_INTERMITTENT_FAILURE_COUNT)
{
DebugLogger.LogInformation($"git {arguments}");
var output = new List<string>();
var error = new List<string>();
using (var process = new Process())
{
process.StartInfo = processStartInfo;
process.OutputDataReceived += (s, e) =>
{
lock (output)
{
output.Add(e.Data);
}
};
process.ErrorDataReceived += (s, e) =>
{
lock (error)
{
error.Add(e.Data);
}
};
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
int returnCode = process.ExitCode;
var stdOut = string.Join(Environment.NewLine, output);
var stdError = string.Join(Environment.NewLine, error);
DebugLogger.LogDebug($"StdOut: {stdOut}");
DebugLogger.LogDebug($"StdErr: {stdError}");
DebugLogger.LogDebug($"ExitCode: {process.ExitCode}");
result.ExitCode = process.ExitCode;
result.StdErr = string.Join(Environment.NewLine, stdError);
result.StdOut = string.Join(Environment.NewLine, stdOut);
if (result.ExitCode == 0)
{
break;
}
var continueToAttempt = IsRetriableGitError(result);
if (!continueToAttempt)
{
throw new GitProcessException(result);
}
attempts++;
if (continueToAttempt && attempts < RETRY_INTERMITTENT_FAILURE_COUNT)
{
Task.Delay(attempts * 2 * 1000).Wait();
}
}
}
}
// exceptions caught here will be to do with inability to start the git process
// otherwise all "error" states should be handled by the output to stdErr and non-zero exitcode.
catch (Exception e)
{
DebugLogger.LogDebug(e.Message);
result.ExitCode = -1;
result.CommandException = e;
throw new GitProcessException(result);
}
});
return result;
}
/// <summary>
/// This function evaluates a git command invocation result. The result of "yes you should retry this" only occurs
/// when the necessary data is available. Otherwise we default to NOT retry.
///
/// Check Azure/azure-sdk-tools#5660 for additional detail on occurrence.
/// </summary>
/// <param name="result"></param>
/// <returns></returns>
public bool IsRetriableGitError(CommandResult result)
{
if (result.ExitCode != 0) {
// we cannot evaluate an empty stderr to see if it is retriable
if (string.IsNullOrEmpty(result.StdErr))
{
return false;
}
// fatal: unable to access 'https://github.com/Azure/azure-sdk-assets/': The requested URL returned error: 429
if (result.StdErr.Contains("The requested URL returned error: 429"))
{
return true;
}
// fatal: unable to access 'https://github.com/Azure/azure-sdk-assets/': Failed to connect to github.com port 443: Connection timed out
if (result.StdErr.Contains("Failed to connect to github.com port 443: Connection timed out"))
{
return true;
}
// fatal: unable to access 'https://github.com/Azure/azure-sdk-assets/': Failed to connect to github.com port 443: Operation timed out
if (result.StdErr.Contains("Failed to connect to github.com port 443: Operation timed out"))
{
return true;
}
// fatal: unable to access 'https://github.com/Azure/azure-sdk-assets/': Failed to connect to github.com port 443 after 21019 ms: Couldn't connect to server
var regex = new Regex(@"Failed to connect to github.com port 443 after [\d]+ ms: Couldn't connect to server");
if (regex.IsMatch(result.StdErr))
{
return true;
}
// fatal: unable to access 'https://github.com/Azure/azure-sdk-assets/': Recv failure: Operation timed out
if (result.StdErr.Contains("Recv failure: Operation timed out"))
{
return true;
}
}
return false;
}
/// <summary>
/// Invokes git binary against a GitAssetsConfiguration.
/// </summary>
/// <param name="arguments"></param>
/// <param name="workingDirectory"></param>
/// <param name="result"></param>
/// <returns></returns>
public virtual bool TryRun(string arguments, string workingDirectory, out CommandResult result)
{
// Surface an easy to understand error when we shoot ourselves in the foot
if (arguments.StartsWith("git"))
{
throw new Exception("GitProcessHandler commands should not start with 'git'");
}
ProcessStartInfo processStartInfo = CreateGitProcessInfo(workingDirectory);
processStartInfo.Arguments = arguments;
var commandResult = new CommandResult();
var queue = AssetTasks.GetOrAdd(workingDirectory, new TaskQueue());
queue.Enqueue(() =>
{
try
{
int attempts = 1;
bool continueToAttempt = true;
while (continueToAttempt && attempts <= RETRY_INTERMITTENT_FAILURE_COUNT)
{
DebugLogger.LogInformation($"git {arguments}");
var output = new List<string>();
var error = new List<string>();
using (var process = new Process())
{
process.StartInfo = processStartInfo;
process.OutputDataReceived += (s, e) =>
{
lock (output)
{
output.Add(e.Data);
}
};
process.ErrorDataReceived += (s, e) =>
{
lock (error)
{
error.Add(e.Data);
}
};
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
int returnCode = process.ExitCode;
var stdOut = string.Join(Environment.NewLine, output);
var stdError = string.Join(Environment.NewLine, error);
DebugLogger.LogDebug($"StdOut: {stdOut}");
DebugLogger.LogDebug($"StdErr: {stdError}");
DebugLogger.LogDebug($"ExitCode: {process.ExitCode}");
commandResult = new CommandResult()
{
ExitCode = process.ExitCode,
StdErr = stdError,
StdOut = stdOut,
Arguments = arguments
};
if (commandResult.ExitCode == 0)
{
break;
}
continueToAttempt = IsRetriableGitError(commandResult);
attempts++;
if (continueToAttempt && attempts < RETRY_INTERMITTENT_FAILURE_COUNT)
{
Task.Delay(attempts * 2 * 1000).Wait();
}
}
}
}
// exceptions caught here will be to do with inability to start the git process
// otherwise all "error" states should be handled by the output to stdErr and non-zero exitcode.
catch (Exception e)
{
DebugLogger.LogDebug(e.Message);
commandResult = new CommandResult()
{
ExitCode = -1,
CommandException = e
};
}
});
result = commandResult;
if (result.ExitCode != 0)
{
return false;
}
return true;
}
/// <summary>
/// Verify that the version of git running on the machine is greater equal the git minimum version.
/// This is more for the people running the CLI/TestProxy locally than for lab machines which seem
/// to be running on the latest, released versions. The reason is that any git version less than
/// 2.37.0 won't have the cone/no-cone options used by sparse-checkout.
/// </summary>
/// <exception cref="GitProcessException">Thrown by the internal call to Run.</exception>
/// <exception cref="GitVersionException">Thrown if the version doesn't meet the min or if we can't determine it.</exception>
public void VerifyGitMinVersion(string testVersion=null)
{
string localGitVersion = testVersion;
if (String.IsNullOrEmpty(testVersion))
{
// We need to run git --version to get the current version. The directory in which
// the process executes is irrelevant.
CommandResult result = Run("--version", Directory.GetCurrentDirectory());
localGitVersion = result.StdOut.Trim();
}
// Sample git versions from the various platforms:
// Windows: git version 2.37.2.windows.2
// Mac: git version 2.32.1 (Apple Git-133)
// Linux: git version 2.25.1
// Regex to scrape major, minor and patch versions from the git version string
Regex rx = new Regex(@"git version (?<Major>\d*)\.(?<Minor>\d*)(\.(?<Patch>\d*)?)?", RegexOptions.Compiled);
Match match = rx.Match(localGitVersion);
if (match.Success)
{
string gitVersionExceptionMessage = $"{localGitVersion} is less than the minimum supported Git version {GitMinVersion.minVersionString}";
// match.Groups["Major"].Value, match.Groups["Minor"].Value, match.Groups["Patch"].Value
int major = int.Parse(match.Groups["Major"].Value);
int minor = int.Parse(match.Groups["Minor"].Value);
int patch = int.Parse(match.Groups["Patch"].Value);
if (major < GitMinVersion.Major)
{
throw new GitVersionException(gitVersionExceptionMessage);
}
else if (major == GitMinVersion.Major)
{
if (minor < GitMinVersion.Minor)
{
throw new GitVersionException(gitVersionExceptionMessage);
}
else if (minor == GitMinVersion.Minor)
{
if (patch < GitMinVersion.Patch)
{
throw new GitVersionException(gitVersionExceptionMessage);
}
}
}
}
else
{
// In theory we shouldn't get here. If git isn't installed or on the path, the Run command should throw.
throw new GitVersionException($"Unable to determine the local git version from the returned version string '{localGitVersion}'. Please ensure that git is installed on the machine and has been added to the PATH.");
}
}
}
}