-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathAzureConfig.cs
More file actions
167 lines (138 loc) · 4.5 KB
/
Copy pathAzureConfig.cs
File metadata and controls
167 lines (138 loc) · 4.5 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
// Copyright (c) 2024-2025 BMC Software, Inc.
// Copyright (c) 2021-2024 Netreo
// Copyright (c) 2019 Stackify
using System;
using StackifyLib.Utils;
#if NETFULL
using Microsoft.Win32;
#endif
namespace StackifyLib.Models
{
public class AzureConfig
{
private static AzureConfig _instance = new AzureConfig();
public static AzureConfig Instance => _instance;
private bool? _inAzure;
private AzureRoleType _azureRoleType = AzureRoleType.Unknown;
private string _azureRoleName;
private string _azureInstanceName;
private string _entryPoint;
private readonly DateTime AppStarted = DateTime.UtcNow;
private readonly object Locker = new object();
public const string ProductionEnvName = "Production";
public string AzureAppWebConfigEnvironment { get; set; }
public string AzureAppWebConfigApiKey { get; set; }
public string AzureDriveLetter { get; set; }
public string AzureInstanceName
{
get
{
EnsureInAzureRan();
return _azureInstanceName;
}
}
public 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 bool IsWebsite
{
get
{
if (InAzure)
{
return _azureRoleType == AzureRoleType.WebApp;
}
return false;
}
}
private EnvironmentDetail _environmentDetail = null;
public AzureConfig()
{
}
public AzureConfig(EnvironmentDetail environmentDetail)
{
_environmentDetail = environmentDetail;
}
private void EnsureInAzureRan()
{
bool ensureTestHasBeenDone = InAzure;
}
public 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")} {GetEnvironment()} [{slotId}-{Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID").Left(6)}]";
return;
}
}
public 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 string GetEnvironment()
{
if (IsWebsite)
{
if (_environmentDetail == null)
{
_environmentDetail = EnvironmentDetail.Get();
}
var key = Environment.GetEnvironmentVariable("Stackify.Environment");
if (string.IsNullOrEmpty(key))
{
key = _environmentDetail.ConfiguredEnvironmentName;
}
if (string.IsNullOrEmpty(key) == false)
{
return key;
}
return ProductionEnvName;
}
return string.Empty;
}
}
public enum AzureRoleType : short
{
Unknown = 0,
NotAzure = 1,
Web = 2,
Worker = 3,
Cache = 4,
WebApp = 5
}
}