-
Notifications
You must be signed in to change notification settings - Fork 445
/
EnvironmentExtensions.cs
694 lines (604 loc) · 30.9 KB
/
EnvironmentExtensions.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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
// 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.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Azure.WebJobs.Script.Config;
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
using static Microsoft.Azure.WebJobs.Script.EnvironmentSettingNames;
namespace Microsoft.Azure.WebJobs.Script
{
internal static class EnvironmentExtensions
{
private static bool? isApplicationInsightsAgentEnabled;
private static bool? isMultiLanguageEnabled;
internal static string BaseDirectory { get; set; }
public static string GetEnvironmentVariableOrDefault(this IEnvironment environment, string name, string defaultValue)
{
return environment.GetEnvironmentVariable(name) ?? defaultValue;
}
public static bool IsLinuxMetricsPublishingEnabled(this IEnvironment environment)
{
return environment.IsLinuxConsumptionOnAtlas() && string.IsNullOrEmpty(environment.GetEnvironmentVariable(ContainerStartContext));
}
public static bool IsPlaceholderModeEnabled(this IEnvironment environment)
{
return environment.GetEnvironmentVariable(AzureWebsitePlaceholderMode) == "1";
}
public static bool UsePlaceholderDotNetIsolated(this IEnvironment environment)
{
return environment.GetEnvironmentVariable(AzureWebsiteUsePlaceholderDotNetIsolated) == "1";
}
public static bool IsLegacyPlaceholderTemplateSite(this IEnvironment environment)
{
string siteName = environment.GetEnvironmentVariable(AzureWebsiteName);
return string.IsNullOrEmpty(siteName) ? false : siteName.Equals(ScriptConstants.LegacyPlaceholderTemplateSiteName, StringComparison.InvariantCultureIgnoreCase);
}
public static bool IsRuntimeScaleMonitoringEnabled(this IEnvironment environment)
{
return environment.GetEnvironmentVariable(FunctionsRuntimeScaleMonitoringEnabled) == "1";
}
public static bool IsAdminIsolationEnabled(this IEnvironment environment)
{
return environment.GetEnvironmentVariable(FunctionsAdminIsolationEnabled) == "1";
}
public static bool IsEasyAuthEnabled(this IEnvironment environment)
{
bool.TryParse(environment.GetEnvironmentVariable(EasyAuthEnabled), out bool isEasyAuthEnabled);
return isEasyAuthEnabled;
}
/// <summary>
/// Returns true if any Functions AzureMonitor log categories are enabled.
/// </summary>
public static bool IsAzureMonitorEnabled(this IEnvironment environment)
{
string value = environment.GetEnvironmentVariable(AzureMonitorCategories);
if (value == null)
{
return true;
}
string[] categories = value.Split(',');
return categories.Contains(ScriptConstants.AzureMonitorTraceCategory);
}
/// <summary>
/// Gets if <a href="https://docs.microsoft.com/azure/azure-functions/functions-proxies">proxies.json support</a> is enabled.
/// </summary>
public static bool IsProxiesEnabled(this IEnvironment environment)
{
return FeatureFlags.IsEnabled(ScriptConstants.FeatureFlagEnableProxies, environment);
}
public static bool IsRunningAsHostedSiteExtension(this IEnvironment environment)
{
if (environment.IsAppService())
{
string siteExtensionsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "SiteExtensions", "Functions");
return (BaseDirectory ?? AppContext.BaseDirectory).StartsWith(siteExtensionsPath, StringComparison.OrdinalIgnoreCase);
}
return false;
}
public static bool IsRemoteDebuggingEnabled(this IEnvironment environment)
{
return !string.IsNullOrEmpty(environment.GetEnvironmentVariable(RemoteDebuggingPort));
}
public static bool ZipDeploymentAppSettingsExist(this IEnvironment environment)
{
return !string.IsNullOrEmpty(environment.GetEnvironmentVariable(AzureWebsiteZipDeployment)) ||
!string.IsNullOrEmpty(environment.GetEnvironmentVariable(AzureWebsiteAltZipDeployment)) ||
!string.IsNullOrEmpty(environment.GetEnvironmentVariable(AzureWebsiteRunFromPackage)) ||
!string.IsNullOrEmpty(environment.GetEnvironmentVariable(ScmRunFromPackage));
}
public static bool AzureFilesAppSettingsExist(this IEnvironment environment)
{
return !string.IsNullOrEmpty(environment.GetEnvironmentVariable(AzureFilesConnectionString)) &&
!string.IsNullOrEmpty(environment.GetEnvironmentVariable(AzureFilesContentShare));
}
public static string GetAzureWebsiteHomePath(this IEnvironment environment)
{
return environment.GetEnvironmentVariable(AzureWebsiteHomePath);
}
public static string GetSitePackagesPath(this IEnvironment environment)
{
return Path.Combine(environment.GetAzureWebsiteHomePath(), ScriptConstants.DataFolderName, ScriptConstants.SitePackagesFolderName);
}
public static string GetSitePackageNameTxtPath(this IEnvironment environment)
{
return Path.Combine(environment.GetSitePackagesPath(), ScriptConstants.SitePackageNameTxtFileName);
}
public static bool IsCoreTools(this IEnvironment environment)
{
return !string.IsNullOrEmpty(environment.GetEnvironmentVariable(CoreToolsEnvironment));
}
public static bool IsV2CompatibilityMode(this IEnvironment environment)
{
string compatModeString = environment.GetEnvironmentVariable(FunctionsV2CompatibilityModeKey);
bool.TryParse(compatModeString, out bool isFunctionsV2CompatibilityMode);
string extensionVersion = environment.GetEnvironmentVariable(FunctionsExtensionVersion);
bool isV2ExtensionVersion = string.Compare(extensionVersion, "~2", CultureInfo.InvariantCulture, CompareOptions.OrdinalIgnoreCase) == 0;
return isFunctionsV2CompatibilityMode || isV2ExtensionVersion;
}
public static bool IsV2CompatibleOnV3Extension(this IEnvironment environment)
{
string compatModeString = environment.GetEnvironmentVariable(FunctionsV2CompatibilityModeKey);
bool.TryParse(compatModeString, out bool isFunctionsV2CompatibilityMode);
string extensionVersion = environment.GetEnvironmentVariable(FunctionsExtensionVersion);
bool isV3ExtensionVersion = string.Compare(extensionVersion, "~3", CultureInfo.InvariantCulture, CompareOptions.OrdinalIgnoreCase) == 0;
return isFunctionsV2CompatibilityMode && isV3ExtensionVersion;
}
public static bool IsContainer(this IEnvironment environment)
{
var runningInContainer = environment.GetEnvironmentVariable(RunningInContainer);
return !string.IsNullOrEmpty(runningInContainer)
&& bool.TryParse(runningInContainer, out bool runningInContainerValue)
&& runningInContainerValue;
}
public static bool IsCorsConfigurationEnabled(this IEnvironment environment)
{
return environment.GetEnvironmentVariable(EnableCorsConfiguration) == "1";
}
public static bool IsPersistentFileSystemAvailable(this IEnvironment environment)
{
return environment.IsWindowsAzureManagedHosting()
|| environment.IsLinuxAppServiceWithPersistentFileSystem()
|| environment.IsCoreTools();
}
public static bool IsLinuxAppServiceWithPersistentFileSystem(this IEnvironment environment)
{
if (environment.IsLinuxAppService())
{
string storageConfig = environment.GetEnvironmentVariable(LinuxAzureAppServiceStorage);
// AzureAppServiceStorage is enabled by default, So return if true it is not set
if (string.IsNullOrEmpty(storageConfig))
{
return true;
}
return bool.TryParse(storageConfig, out bool storageConfigValue) && storageConfigValue;
}
return false;
}
/// <summary>
/// Gets a value indicating whether the application is running in a Windows Consumption (dynamic)
/// App Service environment.
/// </summary>
/// <param name="environment">The environment to verify.</param>
/// <returns><see cref="true"/> if running in a Windows Consumption App Service app; otherwise, false.</returns>
public static bool IsWindowsConsumption(this IEnvironment environment)
{
string value = environment.GetEnvironmentVariable(AzureWebsiteSku);
return string.Equals(value, ScriptConstants.DynamicSku, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Gets a value indicating whether the application is running in one of our consumption SKUs (e.g. Windows or Linux Consumption (Dynamic),
/// or Flex Consumption).
/// App Service environment.
/// </summary>
/// <param name="environment">The environment to verify.</param>
/// <returns><see cref="true"/> if running in one of our Consumption SKUs, false otherwise.</returns>
public static bool IsConsumptionSku(this IEnvironment environment)
{
return IsWindowsConsumption(environment) || IsAnyLinuxConsumption(environment) || IsFlexConsumptionSku(environment);
}
/// <summary>
/// Gets a value indicating whether the application is running in the Flex Consumption Sku.
/// </summary>
/// <param name="environment">The environment to verify.</param>
/// <returns><see cref="true"/> if running in the FlexConsumption Sku, false otherwise.</returns>
public static bool IsFlexConsumptionSku(this IEnvironment environment)
{
string value = environment.GetEnvironmentVariable(AzureWebsiteSku);
if (string.Equals(value, ScriptConstants.FlexConsumptionSku, StringComparison.OrdinalIgnoreCase))
{
return true;
}
// when in placeholder mode, site settings like SKU are not available
// to enable this check to run in both modes, we check additional settings
return environment.IsLinuxConsumptionOnLegion();
}
/// <summary>
/// Returns true if the app is running on Virtual Machine Scale Sets (VMSS).
/// </summary>
public static bool IsVMSS(this IEnvironment environment)
{
string value = environment.GetEnvironmentVariable(EnvironmentSettingNames.RoleInstanceId);
return value != null && value.IndexOf("HostRole", StringComparison.OrdinalIgnoreCase) >= 0;
}
/// <summary>
/// Gets the number of effective cores taking into account SKU/environment restrictions.
/// </summary>
public static int GetEffectiveCoresCount(this IEnvironment environment)
{
// When not running on VMSS, the dynamic plan has some limits that mean that a given instance is using effectively a single core,
// so we should not use Environment.Processor count in this case.
var effectiveCores = (environment.IsWindowsConsumption() && !environment.IsVMSS()) ? 1 : Environment.ProcessorCount;
return effectiveCores;
}
/// <summary>
/// Gets a value indicating whether the application is running in a Windows Elastic Premium
/// App Service environment.
/// </summary>
/// <param name="environment">The environment to verify.</param>
/// <returns><see cref="true"/> if running in a Windows Elastic Premium app; otherwise, false.</returns>
public static bool IsWindowsElasticPremium(this IEnvironment environment)
{
string value = environment.GetEnvironmentVariable(AzureWebsiteSku);
return string.Equals(value, ScriptConstants.ElasticPremiumSku, StringComparison.OrdinalIgnoreCase);
}
public static bool IsDynamicSku(this IEnvironment environment)
{
return environment.IsConsumptionSku() || environment.IsWindowsElasticPremium();
}
/// <summary>
/// Gets a value indicating whether the application is running in an Azure Windows managed hosting environment
/// (i.e. Windows Consumption or Windows Dedicated).
/// </summary>
/// <param name="environment">The environment to verify.</param>
/// <returns><see cref="true"/> if running in a Windows Azure managed hosting environment; otherwise, false.</returns>
public static bool IsWindowsAzureManagedHosting(this IEnvironment environment)
{
return environment.IsAppService() && RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
}
/// <summary>
/// Gets a value indicating whether the application is running in Kubernetes Environment.
/// </summary>
/// <param name="environment">The environment to verify.</param>
/// <returns><see cref="true"/> if running in Kubernetes environment; otherwise, false.</returns>
public static bool IsAnyKubernetesEnvironment(this IEnvironment environment)
{
return environment.IsKubernetesManagedHosting() || environment.IsManagedAppEnvironment();
}
/// <summary>
/// Gets a value indicating whether the application is running in Managed App environment.
/// </summary>
/// <param name="environment">The environment to verify.</param>
/// <returns><see cref="true"/> if running in Managed App environment; otherwise, false.</returns>
public static bool IsManagedAppEnvironment(this IEnvironment environment)
{
return !string.IsNullOrEmpty(environment.GetEnvironmentVariable(ManagedEnvironment));
}
/// <summary>
/// Gets a value indicating whether the application is running in a Linux Consumption (dynamic)
/// App Service environment.
/// </summary>
/// <param name="environment">The environment to verify.</param>
/// <returns><see cref="true"/> if running in a Linux Consumption App Service app; otherwise, false.</returns>
public static bool IsAnyLinuxConsumption(this IEnvironment environment)
{
return (environment.IsLinuxConsumptionOnAtlas() || environment.IsFlexConsumptionSku()) && !environment.IsManagedAppEnvironment();
}
public static bool IsLinuxConsumptionOnAtlas(this IEnvironment environment)
{
return !environment.IsAppService() &&
!string.IsNullOrEmpty(environment.GetEnvironmentVariable(ContainerName)) &&
string.IsNullOrEmpty(environment.GetEnvironmentVariable(LegionServiceHost));
}
private static bool IsLinuxConsumptionOnLegion(this IEnvironment environment)
{
return !environment.IsAppService() &&
(!string.IsNullOrEmpty(environment.GetEnvironmentVariable(ContainerName)) ||
!string.IsNullOrEmpty(environment.GetEnvironmentVariable(WebsitePodName))) &&
!string.IsNullOrEmpty(environment.GetEnvironmentVariable(LegionServiceHost));
}
/// <summary>
/// Gets a value indicating whether the application is running in a Linux App Service
/// environment (Dedicated Linux).
/// </summary>
/// <param name="environment">The environment to verify.</param>
/// <returns><see cref="true"/> if running in a Linux Azure App Service; otherwise, false.</returns>
public static bool IsLinuxAppService(this IEnvironment environment)
{
return environment.IsAppService() && !string.IsNullOrEmpty(environment.GetEnvironmentVariable(FunctionsLogsMountPath));
}
/// <summary>
/// Gets a value indicating whether the application is running in an Azure Linux managed hosting environment
/// (i.e. Linux Consumption or Linux Dedicated).
/// </summary>
/// <param name="environment">The environment to verify.</param>
/// <returns><see cref="true"/> if running in a Linux Azure managed hosting environment; otherwise, false.</returns>
public static bool IsLinuxAzureManagedHosting(this IEnvironment environment)
{
return environment.IsAnyLinuxConsumption() || environment.IsLinuxAppService();
}
/// <summary>
/// Gets a value indicating whether the application is running in App Service
/// (Windows Consumption, Windows Dedicated or Linux Dedicated).
/// </summary>
/// <param name="environment">The environment to verify.</param>
/// <returns><see cref="true"/> if running in a Azure App Service; otherwise, false.</returns>
public static bool IsAppService(this IEnvironment environment)
{
return !string.IsNullOrEmpty(environment.GetEnvironmentVariable(AzureWebsiteInstanceId));
}
/// <summary>
/// Gets a value indicating whether the application is running in Kubernetes App Service environment(K8SE).
/// </summary>
/// <param name="environment">The environment to verify.</param>
/// <returns><see cref="true"/> If running in a Kubernetes Azure App Service; otherwise, false.</returns>
public static bool IsKubernetesManagedHosting(this IEnvironment environment)
{
return !string.IsNullOrEmpty(environment.GetEnvironmentVariable(KubernetesServiceHost))
&& !string.IsNullOrEmpty(environment.GetEnvironmentVariable(PodNamespace))
&& !environment.IsManagedAppEnvironment();
}
/// <summary>
/// Gets a value that uniquely identifies the site and slot.
/// </summary>
public static string GetAzureWebsiteUniqueSlotName(this IEnvironment environment)
{
string name = environment.GetEnvironmentVariable(AzureWebsiteName);
string slotName = environment.GetEnvironmentVariable(AzureWebsiteSlotName);
if (!string.IsNullOrEmpty(slotName) &&
!string.Equals(slotName, ScriptConstants.DefaultProductionSlotName, StringComparison.OrdinalIgnoreCase))
{
name += $"-{slotName}";
}
return name?.ToLowerInvariant();
}
/// <summary>
/// Gets the computer name.
/// </summary>
public static string GetAntaresComputerName(this IEnvironment environment)
{
return environment.GetEnvironmentVariableOrDefault(AntaresComputerName, string.Empty);
}
/// <summary>
/// Gets if runtime environment is logic apps.
/// </summary>
public static bool IsLogicApp(this IEnvironment environment)
{
string appKind = environment.GetEnvironmentVariable(AppKind)?.ToLower();
return !string.IsNullOrEmpty(appKind) && appKind.Contains(ScriptConstants.WorkFlowAppKind);
}
/// <summary>
/// Gets if runtime environment needs multi language.
/// </summary>
public static bool IsMultiLanguageRuntimeEnvironment(this IEnvironment environment)
{
if (!isMultiLanguageEnabled.HasValue)
{
isMultiLanguageEnabled = environment.IsLogicApp();
}
return isMultiLanguageEnabled.Value;
}
/// <summary>
/// Gets the Antares version.
/// </summary>
public static string GetAntaresVersion(this IEnvironment environment)
{
if (environment.IsLinuxAzureManagedHosting())
{
return environment.GetEnvironmentVariableOrDefault(AntaresPlatformVersionLinux, string.Empty);
}
else
{
return environment.GetEnvironmentVariableOrDefault(AntaresPlatformVersionWindows, string.Empty);
}
}
/// <summary>
/// Gets the Instance id.
/// </summary>
public static string GetInstanceId(this IEnvironment environment)
{
if (environment.IsAnyLinuxConsumption())
{
var instanceId = environment.GetEnvironmentVariableOrDefault(WebsitePodName, string.Empty);
if (string.IsNullOrEmpty(instanceId))
{
instanceId = environment.GetEnvironmentVariableOrDefault(ContainerName, string.Empty);
}
return instanceId;
}
else
{
return environment.GetEnvironmentVariableOrDefault(AzureWebsiteInstanceId, string.Empty);
}
}
/// <summary>
/// Gets a the subscription Id of the current site.
/// </summary>
public static string GetSubscriptionId(this IEnvironment environment)
{
string ownerName = environment.GetEnvironmentVariable(AzureWebsiteOwnerName) ?? string.Empty;
if (!string.IsNullOrEmpty(ownerName))
{
int idx = ownerName.IndexOf('+');
if (idx > 0)
{
return ownerName.Substring(0, idx);
}
}
return null;
}
/// <summary>
/// Gets the name of the currently deployed site name.
/// </summary>
public static string GetRuntimeSiteName(this IEnvironment environment)
{
string runtimeSiteName = environment.GetEnvironmentVariable(AzureWebsiteRuntimeSiteName);
return runtimeSiteName?.ToLowerInvariant();
}
/// <summary>
/// Gets the name of the app's slot, if it exists.
/// </summary>
public static string GetSlotName(this IEnvironment environment)
{
string slotName = environment.GetEnvironmentVariable(AzureWebsiteSlotName);
return slotName?.ToLowerInvariant();
}
/// <summary>
/// Gets a value indicating whether it is safe to start specializing the host instance (e.g. file system is ready, etc.)
/// </summary>
public static bool IsContainerReady(this IEnvironment environment)
{
return !string.IsNullOrEmpty(environment.GetEnvironmentVariable(AzureWebsiteContainerReady));
}
public static string GetKubernetesApiServerUrl(this IEnvironment environment)
{
string host = environment.GetEnvironmentVariable(KubernetesServiceHost);
string port = environment.GetEnvironmentVariable(KubernetesServiceHttpsPort);
if (string.IsNullOrEmpty(host) || string.IsNullOrEmpty(port))
{
throw new InvalidOperationException($"Both {KubernetesServiceHost} and {KubernetesServiceHttpsPort} are required for {nameof(GetKubernetesApiServerUrl)}.");
}
return $"https://{host}:{port}";
}
public static bool IsMountEnabled(this IEnvironment environment)
{
return string.Equals(environment.GetEnvironmentVariable(MountEnabled), "1")
&& !string.IsNullOrEmpty(environment.GetEnvironmentVariable(MeshInitURI));
}
public static bool IsMountDisabled(this IEnvironment environment)
{
return string.Equals(environment.GetEnvironmentVariable(MountEnabled), "0")
|| string.IsNullOrEmpty(environment.GetEnvironmentVariable(MeshInitURI));
}
public static CloudName GetCloudName(this IEnvironment environment)
{
var cloudName = environment.GetEnvironmentVariable(EnvironmentSettingNames.CloudName);
if (Enum.TryParse(cloudName, true, out CloudName cloud))
{
return cloud;
}
return CloudName.Azure;
}
public static string GetStorageSuffix(this IEnvironment environment)
{
switch (GetCloudName(environment))
{
case CloudName.Azure:
return CloudConstants.AzureStorageSuffix;
case CloudName.Blackforest:
return CloudConstants.BlackforestStorageSuffix;
case CloudName.Fairfax:
return CloudConstants.FairfaxStorageSuffix;
case CloudName.Mooncake:
return CloudConstants.MooncakeStorageSuffix;
case CloudName.USNat:
return CloudConstants.USNatStorageSuffix;
case CloudName.USSec:
return CloudConstants.USSecStorageSuffix;
default:
return CloudConstants.AzureStorageSuffix;
}
}
public static string GetVaultSuffix(this IEnvironment environment)
{
{
switch (GetCloudName(environment))
{
case CloudName.Azure:
return CloudConstants.AzureVaultSuffix;
case CloudName.Blackforest:
return CloudConstants.BlackforestVaultSuffix;
case CloudName.Fairfax:
return CloudConstants.FairfaxVaultSuffix;
case CloudName.Mooncake:
return CloudConstants.MooncakeVaultSuffix;
default:
return CloudConstants.AzureVaultSuffix;
}
}
}
public static string GetFunctionsWorkerRuntime(this IEnvironment environment)
{
return environment.GetEnvironmentVariableOrDefault(FunctionWorkerRuntime, string.Empty);
}
/// <summary>
/// Gets a value indicating whether AzureFileShare should be mounted when specializing Linux Consumption workers.
/// </summary>
public static bool SupportsAzureFileShareMount(this IEnvironment environment)
{
return string.Equals(environment.GetFunctionsWorkerRuntime(), RpcWorkerConstants.PowerShellLanguageWorkerName,
StringComparison.OrdinalIgnoreCase);
}
public static string GetHttpLeaderEndpoint(this IEnvironment environment)
{
return environment.GetEnvironmentVariableOrDefault(HttpLeaderEndpoint, string.Empty);
}
public static bool DrainOnApplicationStoppingEnabled(this IEnvironment environment)
{
return !string.IsNullOrEmpty(environment.GetEnvironmentVariable(KubernetesServiceHost)) ||
(bool.TryParse(environment.GetEnvironmentVariable(DrainOnApplicationStopping), out bool v) && v);
}
public static bool IsWorkerDynamicConcurrencyEnabled(this IEnvironment environment)
{
if (bool.TryParse(environment.GetEnvironmentVariable(RpcWorkerConstants.FunctionsWorkerDynamicConcurrencyEnabled), out bool concurrencyEnabled))
{
return concurrencyEnabled && string.IsNullOrEmpty(environment.GetEnvironmentVariable(RpcWorkerConstants.FunctionsWorkerProcessCountSettingName));
}
return false;
}
public static HashSet<string> GetLanguageWorkerListToStartInPlaceholder(this IEnvironment environment)
{
string placeholderList = environment.GetEnvironmentVariableOrDefault(RpcWorkerConstants.FunctionWorkerPlaceholderModeListSettingName, string.Empty);
var placeholderRuntimeSet = new HashSet<string>(placeholderList.Trim().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()));
string workerRuntime = environment.GetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeSettingName);
if (!environment.IsInProc(workerRuntime))
{
placeholderRuntimeSet.Add(workerRuntime);
}
return placeholderRuntimeSet;
}
public static bool IsInProc(this IEnvironment environment, string workerRuntime = null)
{
if (workerRuntime is null)
{
workerRuntime = environment.GetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeSettingName);
}
return string.IsNullOrEmpty(workerRuntime) || string.Equals(workerRuntime, RpcWorkerConstants.DotNetLanguageWorkerName, StringComparison.OrdinalIgnoreCase);
}
public static bool IsApplicationInsightsAgentEnabled(this IEnvironment environment)
{
// cache the value of the environment variable
if (isApplicationInsightsAgentEnabled.HasValue)
{
return isApplicationInsightsAgentEnabled.Value;
}
else if (!environment.IsPlaceholderModeEnabled())
{
bool.TryParse(environment.GetEnvironmentVariable(AppInsightsAgent), out bool isEnabled);
isApplicationInsightsAgentEnabled = isEnabled;
return isApplicationInsightsAgentEnabled.Value;
}
return false;
}
/// <summary>
/// Clears all cached static flags in <see cref="EnvironmentExtensions"/>.
/// Currently we only use it to purge static initialisations in across tests.
/// </summary>
public static void ClearCache()
{
isMultiLanguageEnabled = null;
isApplicationInsightsAgentEnabled = null;
}
/// <summary>
/// Gets a value indicated in the variable FUNCTIONS_EXTENSION_VERSION.
/// </summary>
/// <returns>Value of FUNCTIONS_EXTENSION_VERSION variable.</returns>
public static string GetFunctionsExtensionVersion(this IEnvironment environment)
{
return environment.GetEnvironmentVariableOrDefault(FunctionsExtensionVersion, string.Empty);
}
public static bool IsTargetBasedScalingEnabled(this IEnvironment environment)
{
return !string.Equals(environment.GetEnvironmentVariable(TargetBaseScalingEnabled), "0");
}
/// <summary>
/// Tries to get the target functions group to run, if any specified.
/// </summary>
/// <param name="environment">The environment to use.</param>
/// <param name="group">The target group, if any.</param>
/// <returns>True if group specified, false otherwise.</returns>
public static bool TryGetFunctionsTargetGroup(this IEnvironment environment, out string group)
{
group = environment.GetEnvironmentVariable(FunctionsTargetGroup);
if (group is "")
{
// standardize empty string to null
group = null;
}
return group is not null;
}
}
}