-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathAzureConfig.cs
More file actions
127 lines (108 loc) · 3.52 KB
/
AzureConfig.cs
File metadata and controls
127 lines (108 loc) · 3.52 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using StackifyLib.Utils;
using System.Linq;
#if NETFULL
using Microsoft.Win32;
#endif
namespace StackifyLib.Models
{
public class AzureConfig
{
private static bool? _inAzure;
private static AzureRoleType _azureRoleType = AzureRoleType.Unknown;
private static string _azureRoleName;
private static string _azureInstanceName;
private static string _entryPoint;
private static readonly DateTime AppStarted = DateTime.UtcNow;
private static readonly object Locker = new object();
public static string AzureAppWebConfigEnvironment { get; set; }
public static string AzureAppWebConfigApiKey { get; set; }
public static string AzureDriveLetter { get; set; }
public static string AzureInstanceName
{
get
{
EnsureInAzureRan();
return _azureInstanceName;
}
}
public static bool InAzure
{
get
{
if (_inAzure == null)
{
lock (Locker)
{
try
{
_inAzure = false;
LoadAzureSettings();
}
catch (Exception ex)
{
StackifyLib.Utils.StackifyAPILogger.Log("Unable to load azure runtime", ex);
}
}
}
return _inAzure ?? false;
}
}
public static bool IsWebsite
{
get
{
if (InAzure)
{
return _azureRoleType == AzureRoleType.WebApp;
}
return false;
}
}
private static void EnsureInAzureRan()
{
bool ensureTestHasBeenDone = InAzure;
}
public static void LoadAzureSettings()
{
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME")) == false && string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID")) == false)
{
_inAzure = true;
_azureRoleType = AzureRoleType.WebApp;
var slotId = GetDeploymentSlotId() ?? "0000";
_azureRoleName = Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME");
_azureInstanceName = $"{Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME")} {ServerConfigHelper.GetEnvironment()} [{slotId}-{Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID").Left(6)}]";
return;
}
}
public static string GetDeploymentSlotId()
{
try
{
var siteName2 = Environment.GetEnvironmentVariable("WEBSITE_IIS_SITE_NAME") ?? string.Empty;
if (siteName2.Contains("__"))
{
return siteName2.Right(4);
}
}
catch (Exception ex)
{
StackifyLib.Utils.StackifyAPILogger.Log("#Azure #GetDeploymentSlotId failed", ex);
}
return null;
}
}
public enum AzureRoleType : short
{
Unknown = 0,
NotAzure = 1,
Web = 2,
Worker = 3,
Cache = 4,
WebApp = 5
}
}