-
Notifications
You must be signed in to change notification settings - Fork 447
/
WebHostServiceCollectionExtensions.cs
320 lines (275 loc) · 14.8 KB
/
WebHostServiceCollectionExtensions.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.IO.Abstractions;
using System.Net.Http;
using System.Runtime.InteropServices;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Script.Config;
using Microsoft.Azure.WebJobs.Script.Configuration;
using Microsoft.Azure.WebJobs.Script.Diagnostics;
using Microsoft.Azure.WebJobs.Script.Grpc;
using Microsoft.Azure.WebJobs.Script.Middleware;
using Microsoft.Azure.WebJobs.Script.WebHost.Configuration;
using Microsoft.Azure.WebJobs.Script.WebHost.ContainerManagement;
using Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection;
using Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics;
using Microsoft.Azure.WebJobs.Script.WebHost.Filters;
using Microsoft.Azure.WebJobs.Script.WebHost.Management;
using Microsoft.Azure.WebJobs.Script.WebHost.Management.LinuxSpecialization;
using Microsoft.Azure.WebJobs.Script.WebHost.Metrics;
using Microsoft.Azure.WebJobs.Script.WebHost.Middleware;
using Microsoft.Azure.WebJobs.Script.WebHost.Security.Authorization;
using Microsoft.Azure.WebJobs.Script.WebHost.Security.Authorization.Policies;
using Microsoft.Azure.WebJobs.Script.WebHost.Standby;
using Microsoft.Azure.WebJobs.Script.Workers.FunctionDataCache;
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
using Microsoft.Azure.WebJobs.Script.Workers.SharedMemoryDataTransfer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Microsoft.Azure.WebJobs.Script.WebHost
{
public static class WebHostServiceCollectionExtensions
{
public static IServiceCollection AddWebJobsScriptHostRouting(this IServiceCollection services)
{
// Add our script route handler
services.TryAddSingleton<IWebJobsRouteHandler, ScriptRouteHandler>();
return services.AddHttpBindingRouting();
}
public static IServiceCollection AddWebJobsScriptHostAuthentication(this IServiceCollection services)
{
services.AddAuthentication()
.AddArmToken()
.AddScriptAuthLevel()
.AddScriptJwtBearer();
return services;
}
public static IServiceCollection AddWebJobsScriptHostAuthorization(this IServiceCollection services)
{
services.AddAuthorization(o =>
{
o.AddScriptPolicies();
});
services.AddSingleton<IAuthorizationHandler, AuthLevelAuthorizationHandler>();
services.AddSingleton<IAuthorizationHandler, NamedAuthLevelAuthorizationHandler>();
return services.AddSingleton<IAuthorizationHandler, FunctionAuthorizationHandler>();
}
public static void AddWebJobsScriptHost(this IServiceCollection services, IConfiguration configuration)
{
services.AddHttpContextAccessor();
services.AddWebJobsScriptHostRouting();
services.AddMvc(o =>
{
o.EnableEndpointRouting = false;
o.Filters.Add(new ArmExtensionResourceFilter());
})
.AddNewtonsoftJson()
.AddXmlDataContractSerializerFormatters();
// Standby services
services.AddStandbyServices();
services.AddSingleton<IScriptHostManager>(s => s.GetRequiredService<WebJobsScriptHostService>());
services.AddSingleton<IScriptWebHostEnvironment, ScriptWebHostEnvironment>();
services.TryAddSingleton<IStandbyManager, StandbyManager>();
services.TryAddSingleton<IScriptHostBuilder, DefaultScriptHostBuilder>();
// Linux container services
services.AddLinuxContainerServices();
// ScriptSettingsManager should be replaced. We're setting this here as a temporary step until
// broader configuration changes are made:
services.AddSingleton<ScriptSettingsManager>();
services.AddSingleton<IEventGenerator>(p =>
{
var environment = p.GetService<IEnvironment>();
if (environment.IsLinuxConsumptionOnAtlas())
{
return new LinuxContainerEventGenerator(environment);
}
else if (environment.IsLinuxConsumptionOnLegion())
{
//todo: Replace with legion specific logger
return new LinuxContainerEventGenerator(environment);
}
else if (SystemEnvironment.Instance.IsLinuxAppService())
{
var hostNameProvider = p.GetService<HostNameProvider>();
return new LinuxAppServiceEventGenerator(new LinuxAppServiceFileLoggerFactory(), hostNameProvider);
}
else if (environment.IsKubernetesManagedHosting())
{
return new KubernetesEventGenerator();
}
else
{
return new EtwEventGenerator();
}
});
// Management services
services.AddSingleton<IFunctionsSyncManager, FunctionsSyncManager>();
services.AddSingleton<IFunctionMetadataManager, FunctionMetadataManager>();
services.AddSingleton<IWebFunctionsManager, WebFunctionsManager>();
services.AddSingleton<IInstanceManager, InstanceManager>();
services.AddSingleton(_ => new HttpClient());
services.AddSingleton<StartupContextProvider>();
services.AddSingleton<IFileSystem>(_ => FileUtility.Instance);
services.AddTransient<VirtualFileSystem>();
services.AddTransient<VirtualFileSystemMiddleware>();
// Logging and diagnostics
services.AddSingleton<IMetricsLogger, WebHostMetricsLogger>();
// Secret management
services.TryAddSingleton<ISecretManagerProvider, DefaultSecretManagerProvider>();
// Shared memory data transfer and function data cache
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
services.AddSingleton<IMemoryMappedFileAccessor, MemoryMappedFileAccessorWindows>();
}
else
{
services.AddSingleton<IMemoryMappedFileAccessor, MemoryMappedFileAccessorUnix>();
}
services.AddSingleton<ISharedMemoryManager, SharedMemoryManager>();
services.AddSingleton<IFunctionDataCache, FunctionDataCache>();
// Grpc
services.AddScriptGrpc();
// Register common services with the WebHost
// Language Worker Hosted Services need to be intialized before WebJobsScriptHostService
ScriptHostBuilderExtensions.AddCommonServices(services);
services.AddSingleton<IFunctionMetadataProvider>(sp =>
{
return new FunctionMetadataProvider(
sp.GetRequiredService<ILogger<FunctionMetadataProvider>>(),
ActivatorUtilities.CreateInstance<WorkerFunctionMetadataProvider>(sp),
ActivatorUtilities.CreateInstance<HostFunctionMetadataProvider>(sp));
});
// Core script host services
services.AddSingleton<WebJobsScriptHostService>();
services.AddSingleton<IHostedService>(s => s.GetRequiredService<WebJobsScriptHostService>());
// Handles shutdown of services that need to happen after StopAsync() of all services of type IHostedService are complete.
// Order is important.
// All other IHostedService injections need to go before this.
services.AddSingleton<IHostedService, HostedServiceManager>();
// Configuration
// ScriptApplicationHostOptions are special in that they need to be reset on specialization, but the reset
// must happen after the StandbyOptions have reset. For this reason, we have a special ChangeTokenSource that
// will reset the ScriptApplicationHostOptions only after StandbyOptions have been reset.
services.ConfigureOptions<ScriptApplicationHostOptionsSetup>();
services.AddSingleton<IOptionsChangeTokenSource<ScriptApplicationHostOptions>, ScriptApplicationHostOptionsChangeTokenSource>();
services.ConfigureOptions<StandbyOptionsSetup>();
services.ConfigureOptions<LanguageWorkerOptionsSetup>();
services.ConfigureOptionsWithChangeTokenSource<AppServiceOptions, AppServiceOptionsSetup, SpecializationChangeTokenSource<AppServiceOptions>>();
services.ConfigureOptionsWithChangeTokenSource<HttpBodyControlOptions, HttpBodyControlOptionsSetup, SpecializationChangeTokenSource<HttpBodyControlOptions>>();
services.ConfigureOptions<FunctionsHostingConfigOptionsSetup>();
if (configuration != null)
{
services.Configure<FunctionsHostingConfigOptions>(configuration.GetSection(ScriptConstants.FunctionsHostingConfigSectionName));
}
services.TryAddSingleton<IDependencyValidator, DependencyValidator>();
services.TryAddSingleton<IJobHostMiddlewarePipeline>(s => DefaultMiddlewarePipeline.Empty);
// Add AzureStorageProvider to WebHost (also needed for ScriptHost)
services.AddAzureStorageProvider();
}
private static void AddStandbyServices(this IServiceCollection services)
{
services.AddSingleton<IOptionsChangeTokenSource<StandbyOptions>, StandbyChangeTokenSource>();
// Core script host service
services.AddSingleton<IHostedService>(p =>
{
var standbyOptions = p.GetService<IOptionsMonitor<StandbyOptions>>();
if (standbyOptions.CurrentValue.InStandbyMode)
{
var standbyManager = p.GetService<IStandbyManager>();
return new StandbyInitializationService(standbyManager);
}
return NullHostedService.Instance;
});
}
private static void AddLinuxContainerServices(this IServiceCollection services)
{
services.AddSingleton<IHostedService>(s =>
{
var environment = s.GetService<IEnvironment>();
//todo: Replace with legion specific service
if (environment.IsAnyLinuxConsumption())
{
var instanceManager = s.GetService<IInstanceManager>();
var logger = s.GetService<ILogger<LinuxContainerInitializationHostService>>();
var startupContextProvider = s.GetService<StartupContextProvider>();
return new LinuxContainerInitializationHostService(environment, instanceManager, logger, startupContextProvider);
}
return NullHostedService.Instance;
});
services.AddSingleton<IMetricsPublisher>(s =>
{
var environment = s.GetService<IEnvironment>();
if (environment.IsLinuxMetricsPublishingEnabled())
{
var logger = s.GetService<ILogger<LinuxContainerMetricsPublisher>>();
var standbyOptions = s.GetService<IOptionsMonitor<StandbyOptions>>();
var hostNameProvider = s.GetService<HostNameProvider>();
var hostingConfigOptions = s.GetService<IOptions<FunctionsHostingConfigOptions>>();
return new LinuxContainerMetricsPublisher(environment, standbyOptions, logger, hostNameProvider, hostingConfigOptions);
}
return NullMetricsPublisher.Instance;
});
services.AddSingleton<IMeshServiceClient>(s =>
{
var environment = s.GetService<IEnvironment>();
if (environment.IsAnyLinuxConsumption())
{
var httpClient = s.GetService<HttpClient>();
var logger = s.GetService<ILogger<MeshServiceClient>>();
return new MeshServiceClient(httpClient, environment, logger);
}
return NullMeshServiceClient.Instance;
});
services.AddSingleton<LinuxContainerActivityPublisher>(s =>
{
var environment = s.GetService<IEnvironment>();
if (environment.IsAnyLinuxConsumption())
{
var logger = s.GetService<ILogger<LinuxContainerActivityPublisher>>();
var meshInitServiceClient = s.GetService<IMeshServiceClient>();
var standbyOptions = s.GetService<IOptionsMonitor<StandbyOptions>>();
return new LinuxContainerActivityPublisher(standbyOptions, meshInitServiceClient, environment, logger);
}
return null;
});
services.AddSingleton<IHostedService>(s =>
{
var environment = s.GetService<IEnvironment>();
if (environment.IsAnyLinuxConsumption())
{
return s.GetRequiredService<LinuxContainerActivityPublisher>();
}
return NullHostedService.Instance;
});
services.AddSingleton<ILinuxContainerActivityPublisher>(s =>
{
var environment = s.GetService<IEnvironment>();
if (environment.IsAnyLinuxConsumption())
{
return s.GetRequiredService<LinuxContainerActivityPublisher>();
}
return NullLinuxContainerActivityPublisher.Instance;
});
services.AddSingleton<IRunFromPackageHandler, RunFromPackageHandler>();
services.AddSingleton<IPackageDownloadHandler, PackageDownloadHandler>();
services.AddSingleton<IManagedIdentityTokenProvider, ManagedIdentityTokenProvider>();
services.AddSingleton<IUnZipHandler, UnZipHandler>();
services.AddSingleton<IBashCommandHandler, BashCommandHandler>();
}
private static IServiceCollection ConfigureOptionsWithChangeTokenSource<TOptions, TOptionsSetup, TOptionsChangeTokenSource>(this IServiceCollection services)
where TOptions : class
where TOptionsSetup : class, IConfigureOptions<TOptions>
where TOptionsChangeTokenSource : class, IOptionsChangeTokenSource<TOptions>
{
services.ConfigureOptions<TOptionsSetup>();
services.AddSingleton<IOptionsChangeTokenSource<TOptions>, TOptionsChangeTokenSource>();
return services;
}
}
}