-
Notifications
You must be signed in to change notification settings - Fork 321
/
DcpHostService.cs
535 lines (455 loc) · 18.9 KB
/
DcpHostService.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
530
531
532
533
534
535
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.Collections;
using System.Globalization;
using System.IO.Pipelines;
using System.Net.Sockets;
using System.Text;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using Aspire.Dashboard;
using Aspire.Dashboard.Model;
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Dashboard;
using Aspire.Hosting.Dcp.Process;
using Aspire.Hosting.Properties;
using Aspire.Hosting.Publishing;
using Aspire.Hosting.Utils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Aspire.Hosting.Dcp;
internal sealed partial class DcpHostService : IHostedLifecycleService, IAsyncDisposable
{
private const int LoggingSocketConnectionBacklog = 3;
private readonly ApplicationExecutor _appExecutor;
private readonly DistributedApplicationModel _applicationModel;
private IAsyncDisposable? _dcpRunDisposable;
private readonly ILoggerFactory _loggerFactory;
private readonly ILogger _logger;
private readonly DashboardWebApplication? _dashboard;
private readonly DcpOptions _dcpOptions;
private readonly PublishingOptions _publishingOptions;
private readonly Locations _locations;
public DcpHostService(DistributedApplicationModel applicationModel,
DistributedApplicationOptions options,
ILoggerFactory loggerFactory,
IOptions<DcpOptions> dcpOptions,
IOptions<PublishingOptions> publishingOptions,
ApplicationExecutor appExecutor,
Locations locations,
KubernetesService kubernetesService)
{
_applicationModel = applicationModel;
_loggerFactory = loggerFactory;
_logger = loggerFactory.CreateLogger<DcpHostService>();
_dcpOptions = dcpOptions.Value;
_publishingOptions = publishingOptions.Value;
_appExecutor = appExecutor;
_locations = locations;
// HACK: Manifest publisher check is temporary util DcpHostService is integrated with DcpPublisher.
if (options.DashboardEnabled && publishingOptions.Value.Publisher != "manifest")
{
var dashboardLogger = _loggerFactory.CreateLogger<DashboardWebApplication>();
_dashboard = new DashboardWebApplication(dashboardLogger, serviceCollection =>
{
serviceCollection.AddSingleton(_applicationModel);
serviceCollection.AddSingleton(kubernetesService);
serviceCollection.AddSingleton<IDashboardViewModelService, DashboardViewModelService>();
});
}
}
public async Task StartAsync(CancellationToken cancellationToken = default)
{
if (_publishingOptions.Publisher is not null && _publishingOptions.Publisher != "dcp")
{
return;
}
var dockerHealthCheckTask = EnsureDockerIfNecessaryAsync(cancellationToken);
var dcpVersionCheckTask = EnsureDcpVersionAsync(cancellationToken);
await Task.WhenAll(dockerHealthCheckTask, dcpVersionCheckTask).ConfigureAwait(false);
EnsureDcpHostRunning();
await _appExecutor.RunApplicationAsync(cancellationToken).ConfigureAwait(false);
if (_dashboard is not null)
{
await _dashboard.StartAsync(cancellationToken).ConfigureAwait(false);
}
}
public async Task StopAsync(CancellationToken cancellationToken = default)
{
if (_publishingOptions.Publisher != "dcp" || _publishingOptions.Publisher is not null)
{
return;
}
await _appExecutor.StopApplicationAsync(cancellationToken).ConfigureAwait(false);
// Stop the dashboard after the application has stopped
if (_dashboard is not null)
{
await _dashboard.StopAsync(cancellationToken).ConfigureAwait(false);
}
}
public async ValueTask DisposeAsync()
{
if (_dcpRunDisposable is null)
{
return;
}
await _appExecutor.StopApplicationAsync().ConfigureAwait(false);
await _dcpRunDisposable.DisposeAsync().ConfigureAwait(false);
_dcpRunDisposable = null;
}
private void EnsureDcpHostRunning()
{
AspireEventSource.Instance.DcpApiServerLaunchStart();
try
{
var dcpProcessSpec = CreateDcpProcessSpec(_locations);
// Enable Unix Domain Socket based log streaming from DCP
try
{
AspireEventSource.Instance.DcpLogSocketCreateStart();
Socket loggingSocket = CreateLoggingSocket(_locations.DcpLogSocket);
loggingSocket.Listen(LoggingSocketConnectionBacklog);
dcpProcessSpec.EnvironmentVariables.Add("DCP_LOG_SOCKET", _locations.DcpLogSocket);
_ = Task.Run(() => StartLoggingSocketAsync(loggingSocket), CancellationToken.None);
}
catch (Exception ex)
{
Console.Error.WriteLine($"Failed to enable orchestration logging: {ex}");
}
finally
{
AspireEventSource.Instance.DcpLogSocketCreateStop();
}
(_, _dcpRunDisposable) = ProcessUtil.Run(dcpProcessSpec);
}
finally
{
AspireEventSource.Instance.DcpApiServerLaunchStop();
}
}
private ProcessSpec CreateDcpProcessSpec(Locations locations)
{
string? dcpExePath = _dcpOptions.CliPath;
if (!File.Exists(dcpExePath))
{
throw new FileNotFoundException($"The Aspire application host is not installed at \"{dcpExePath}\". The application cannot be run without it.", dcpExePath);
}
ProcessSpec dcpProcessSpec = new ProcessSpec(dcpExePath)
{
WorkingDirectory = Directory.GetCurrentDirectory(),
Arguments = $"start-apiserver --monitor {Environment.ProcessId} --detach --kubeconfig \"{locations.DcpKubeconfigPath}\"",
OnOutputData = Console.Out.Write,
OnErrorData = Console.Error.Write,
};
_logger.LogInformation("Starting DCP with arguments: {Arguments}", dcpProcessSpec.Arguments);
foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
{
var key = de.Key?.ToString();
var val = de.Value?.ToString();
if (key is not null && val is not null)
{
dcpProcessSpec.EnvironmentVariables.Add(key, val);
}
}
if (!string.IsNullOrEmpty(_dcpOptions.ExtensionsPath))
{
dcpProcessSpec.EnvironmentVariables.Add("DCP_EXTENSIONS_PATH", _dcpOptions.ExtensionsPath);
}
if (!string.IsNullOrEmpty(_dcpOptions.BinPath))
{
dcpProcessSpec.EnvironmentVariables.Add("DCP_BIN_PATH", _dcpOptions.BinPath);
}
// Set an environment variable to contain session info that should be deleted when DCP is done
// Currently this contains the Unix socket for logging and the kubeconfig
dcpProcessSpec.EnvironmentVariables.Add("DCP_SESSION_FOLDER", locations.DcpSessionDir);
return dcpProcessSpec;
}
// Docker goes to into resource saver mode after 5 minutes of not running a container (by default).
// While in this mode, the commands we use for the docker check take quite some time
private const int WaitTimeForDockerTestCommandInSeconds = 25;
private async Task EnsureDockerIfNecessaryAsync(CancellationToken cancellationToken)
{
// If we don't have any resources that need a container then we
// don't need to check for Docker.
if (!_applicationModel.Resources.Any(c => c.Annotations.OfType<ContainerImageAnnotation>().Any()))
{
return;
}
AspireEventSource.Instance.DockerHealthCheckStart();
IAsyncDisposable? processDisposable = null;
try
{
Task<ProcessResult> task;
var dockerCommandArgs = "ps --latest --quiet";
var processSpec = new ProcessSpec(FileUtil.FindFullPathFromPath("docker"))
{
Arguments = dockerCommandArgs,
ThrowOnNonZeroReturnCode = false,
};
(task, processDisposable) = ProcessUtil.Run(processSpec);
var taskResult = task.WaitAsync(TimeSpan.FromSeconds(WaitTimeForDockerTestCommandInSeconds), cancellationToken);
var processResult = await taskResult.ConfigureAwait(false);
if (taskResult.IsCompletedSuccessfully)
{
if (processResult.ExitCode != 0)
{
Console.Error.WriteLine(string.Format(
CultureInfo.InvariantCulture,
Resources.DockerUnhealthyExceptionMessage,
$"docker {dockerCommandArgs}",
processResult.ExitCode
));
Environment.Exit((int)DockerHealthCheckFailures.Unhealthy);
}
}
else
{
Console.Error.WriteLine(string.Format(
CultureInfo.InvariantCulture,
Resources.DockerUnresponsiveExceptionMessage,
$"docker {dockerCommandArgs}",
WaitTimeForDockerTestCommandInSeconds
));
Environment.Exit((int)DockerHealthCheckFailures.Unresponsive);
}
// If we get to here all is good!
}
catch (Exception ex) when (ex is not DistributedApplicationException)
{
Console.Error.WriteLine(string.Format(
CultureInfo.InvariantCulture,
Resources.DockerPrerequisiteMissingExceptionMessage,
ex.ToString()
));
Environment.Exit((int)DockerHealthCheckFailures.PrerequisiteMissing);
}
finally
{
AspireEventSource.Instance?.DockerHealthCheckStop();
if (processDisposable != null)
{
await processDisposable.DisposeAsync().ConfigureAwait(false);
}
}
}
[GeneratedRegex("[^\\d\\.].*$")]
private static partial Regex VersionRegex();
private const int WaitTimeForDcpVersionCommandInSeconds = 10;
private async Task EnsureDcpVersionAsync(CancellationToken cancellationToken)
{
AspireEventSource.Instance.DcpVersionCheckStart();
IAsyncDisposable? processDisposable = null;
try
{
// Init
Task<ProcessResult> task;
var outputStringBuilder = new StringBuilder();
string? dcpExePath = _dcpOptions.CliPath;
if (!File.Exists(dcpExePath))
{
throw new FileNotFoundException($"The Aspire application host is not installed at \"{dcpExePath}\". The application cannot be run without it.", dcpExePath);
}
// Run `dcp version`
var processSpec = new ProcessSpec(dcpExePath)
{
Arguments = $"version",
OnOutputData = s => outputStringBuilder.Append(s),
};
(task, processDisposable) = ProcessUtil.Run(processSpec);
await task.WaitAsync(TimeSpan.FromSeconds(WaitTimeForDcpVersionCommandInSeconds), cancellationToken).ConfigureAwait(false);
// Parse the output as JSON
var output = outputStringBuilder.ToString();
if (output == string.Empty)
{
return; // Best effort
}
var dcpVersionString = JsonNode.Parse(outputStringBuilder.ToString())?.AsObject()["version"]?.GetValue<string>();
if (dcpVersionString == null
|| dcpVersionString == string.Empty
|| dcpVersionString == "dev")
{
// If empty, null, or a dev version, pass
return;
}
// Early DCP versions (e.g. preview 1) have a +x at the end of their version string, e.g. 0.1.42+5,
// which does not parse. Strip off anything like that.
dcpVersionString = VersionRegex().Replace(dcpVersionString, string.Empty);
Version? dcpVersion;
if (Version.TryParse(dcpVersionString, out dcpVersion))
{
if (dcpVersion < DcpVersion.MinimumVersionInclusive)
{
Console.Error.WriteLine(string.Format(
CultureInfo.InvariantCulture,
Resources.DcpVersionCheckTooLowMessage
));
Environment.Exit((int)DcpVersionCheckFailures.DcpVersionIncompatible);
}
else if (dcpVersion >= DcpVersion.MaximumVersionExclusive)
{
Console.Error.WriteLine(string.Format(
CultureInfo.InvariantCulture,
Resources.DcpVersionCheckTooHighMessage,
DcpVersion.MinimumVersionInclusive.ToString()
));
Environment.Exit((int)DcpVersionCheckFailures.DcpVersionIncompatible);
}
}
}
catch (Exception ex) when (ex is not DistributedApplicationException)
{
Console.Error.WriteLine(string.Format(
CultureInfo.InvariantCulture,
Resources.DcpVersionCheckFailedMessage,
ex.ToString()
));
// Do not exit, this is a best-effort check
}
finally
{
AspireEventSource.Instance?.DcpVersionCheckStop();
if (processDisposable != null)
{
await processDisposable.DisposeAsync().ConfigureAwait(false);
}
}
}
private static Socket CreateLoggingSocket(string socketPath)
{
string? directoryName = Path.GetDirectoryName(socketPath);
if (!string.IsNullOrEmpty(directoryName))
{
if (OperatingSystem.IsWindows())
{
Directory.CreateDirectory(directoryName);
}
else
{
Directory.CreateDirectory(directoryName, UnixFileMode.UserExecute | UnixFileMode.UserWrite | UnixFileMode.UserRead);
}
}
Socket socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
socket.Bind(new UnixDomainSocketEndPoint(socketPath));
return socket;
}
private async Task StartLoggingSocketAsync(Socket socket)
{
while (true)
{
try
{
Socket acceptedSocket = await socket.AcceptAsync().ConfigureAwait(false);
_ = Task.Run(() => LogSocketOutputAsync(acceptedSocket), CancellationToken.None);
}
catch
{
// Suppress exceptions reading logs from DCP controllers
}
}
}
private async Task LogSocketOutputAsync(Socket socket)
{
var reader = PipeReader.Create(new NetworkStream(socket));
// Logger cache to avoid creating a new string per log line, for a few categories
var loggerCache = new Dictionary<int, ILogger>();
(ILogger, LogLevel, string message) GetLogInfo(ReadOnlySpan<byte> line)
{
// The log format is
// <date>\t<level>\t<category>\t<log message>
// e.g. 2023-09-19T20:40:50.509-0700 info dcpctrl.ServiceReconciler service /apigateway is now in state Ready {"ServiceName": {"name":"apigateway"}}
var tab = line.IndexOf((byte)'\t');
var date = line[..tab];
line = line[(tab + 1)..];
tab = line.IndexOf((byte)'\t');
var level = line[..tab];
line = line[(tab + 1)..];
tab = line.IndexOf((byte)'\t');
var category = line[..tab];
line = line[(tab + 1)..];
var message = line;
var logLevel = LogLevel.Information;
if (level.SequenceEqual("info"u8))
{
logLevel = LogLevel.Information;
}
else if (level.SequenceEqual("error"u8))
{
logLevel = LogLevel.Error;
}
else if (level.SequenceEqual("warning"u8))
{
logLevel = LogLevel.Warning;
}
else if (level.SequenceEqual("debug"u8))
{
logLevel = LogLevel.Debug;
}
else if (level.SequenceEqual("trace"u8))
{
logLevel = LogLevel.Trace;
}
var hash = new HashCode();
hash.AddBytes(category);
var hashValue = hash.ToHashCode();
if (!loggerCache.TryGetValue(hashValue, out var logger))
{
// loggerFactory.CreateLogger internally caches, but we may as well cache the logger as well as the string
// for the lifetime of this socket
loggerCache[hashValue] = logger = _loggerFactory.CreateLogger($"Aspire.Hosting.Dcp.{Encoding.UTF8.GetString(category)}");
}
return (logger, logLevel, Encoding.UTF8.GetString(message));
}
try
{
void LogLines(in ReadOnlySequence<byte> buffer, out SequencePosition position)
{
var seq = new SequenceReader<byte>(buffer);
while (seq.TryReadTo(out ReadOnlySpan<byte> line, (byte)'\n'))
{
var (logger, logLevel, message) = GetLogInfo(line);
logger.Log(logLevel, 0, message, null, static (value, ex) => value);
}
position = seq.Position;
}
while (true)
{
var result = await reader.ReadAsync().ConfigureAwait(false);
if (result.IsCompleted)
{
break;
}
LogLines(result.Buffer, out var position);
reader.AdvanceTo(position);
}
}
catch
{
// Suppress exceptions reading logs from DCP controllers
}
finally
{
reader.Complete();
}
}
public Task StartedAsync(CancellationToken _)
{
AspireEventSource.Instance.DcpHostStartupStop();
return Task.CompletedTask;
}
public Task StartingAsync(CancellationToken cancellationToken)
{
AspireEventSource.Instance.DcpHostStartupStart();
return Task.CompletedTask;
}
public Task StoppedAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public Task StoppingAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}