Skip to content

Commit 21e4254

Browse files
author
Matt Brooks
committed
Merge branch 'develop'
2 parents 61067bc + f041241 commit 21e4254

File tree

5 files changed

+226
-13
lines changed

5 files changed

+226
-13
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text;
5+
using System.Text.RegularExpressions;
6+
using StackifyLib.Utils;
7+
using System.Linq;
8+
#if NETFULL
9+
using Microsoft.Win32;
10+
#endif
11+
12+
namespace StackifyLib.Models
13+
{
14+
public class AzureConfig
15+
{
16+
private static bool? _inAzure;
17+
private static AzureRoleType _azureRoleType = AzureRoleType.Unknown;
18+
private static string _azureRoleName;
19+
private static string _azureInstanceName;
20+
private static string _entryPoint;
21+
22+
private static readonly DateTime AppStarted = DateTime.UtcNow;
23+
24+
private static readonly object Locker = new object();
25+
26+
27+
public static string AzureAppWebConfigEnvironment { get; set; }
28+
public static string AzureAppWebConfigApiKey { get; set; }
29+
public static string AzureDriveLetter { get; set; }
30+
31+
public static string AzureInstanceName
32+
{
33+
get
34+
{
35+
EnsureInAzureRan();
36+
return _azureInstanceName;
37+
}
38+
}
39+
40+
public static bool InAzure
41+
{
42+
get
43+
{
44+
if (_inAzure == null)
45+
{
46+
lock (Locker)
47+
{
48+
try
49+
{
50+
_inAzure = false;
51+
52+
LoadAzureSettings();
53+
}
54+
catch (Exception ex)
55+
{
56+
StackifyLib.Utils.StackifyAPILogger.Log("Unable to load azure runtime", ex);
57+
}
58+
}
59+
}
60+
61+
return _inAzure ?? false;
62+
}
63+
}
64+
65+
public static bool IsWebsite
66+
{
67+
get
68+
{
69+
if (InAzure)
70+
{
71+
return _azureRoleType == AzureRoleType.WebApp;
72+
}
73+
74+
return false;
75+
}
76+
}
77+
78+
private static void EnsureInAzureRan()
79+
{
80+
bool ensureTestHasBeenDone = InAzure;
81+
}
82+
83+
public static void LoadAzureSettings()
84+
{
85+
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME")) == false && string.IsNullOrEmpty(Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID")) == false)
86+
{
87+
_inAzure = true;
88+
_azureRoleType = AzureRoleType.WebApp;
89+
90+
var slotId = GetDeploymentSlotId() ?? "0000";
91+
92+
_azureRoleName = Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME");
93+
_azureInstanceName = $"{Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME")} {ServerConfigHelper.GetEnvironment()} [{slotId}-{Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID").Left(6)}]";
94+
95+
return;
96+
}
97+
}
98+
99+
public static string GetDeploymentSlotId()
100+
{
101+
try
102+
{
103+
var siteName2 = Environment.GetEnvironmentVariable("WEBSITE_IIS_SITE_NAME") ?? string.Empty;
104+
if (siteName2.Contains("__"))
105+
{
106+
return siteName2.Right(4);
107+
}
108+
}
109+
catch (Exception ex)
110+
{
111+
StackifyLib.Utils.StackifyAPILogger.Log("#Azure #GetDeploymentSlotId failed", ex);
112+
}
113+
114+
return null;
115+
}
116+
}
117+
118+
public enum AzureRoleType : short
119+
{
120+
Unknown = 0,
121+
NotAzure = 1,
122+
Web = 2,
123+
Worker = 3,
124+
Cache = 4,
125+
WebApp = 5
126+
}
127+
}

Src/StackifyLib/Models/EnvironmentDetail.cs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,22 @@ private void GetAzureInfo()
110110
public static string GetDeviceName()
111111
{
112112
var deviceName = Environment.MachineName;
113-
var isDefaultDeviceNameEc2 = IsEc2MachineName(deviceName);
114113

115-
if (Config.IsEc2 == null || Config.IsEc2 == true || isDefaultDeviceNameEc2)
114+
if (AzureConfig.InAzure && ((AzureConfig.IsWebsite) || (AzureConfig.InAzure && Environment.MachineName.StartsWith("RD"))))
116115
{
117-
var ec2InstanceId = GetEC2InstanceId();
118-
if (string.IsNullOrWhiteSpace(ec2InstanceId) == false)
116+
deviceName = AzureConfig.AzureInstanceName;
117+
}
118+
else
119+
{
120+
var isDefaultDeviceNameEc2 = IsEc2MachineName(deviceName);
121+
122+
if (Config.IsEc2 == null || Config.IsEc2 == true || isDefaultDeviceNameEc2)
119123
{
120-
deviceName = ec2InstanceId;
124+
var ec2InstanceId = GetEC2InstanceId();
125+
if (string.IsNullOrWhiteSpace(ec2InstanceId) == false)
126+
{
127+
deviceName = ec2InstanceId;
128+
}
121129
}
122130
}
123131

@@ -187,15 +195,23 @@ public static string GetEC2InstanceId()
187195
public static string GetDeviceName()
188196
{
189197
var deviceName = Environment.MachineName;
190-
var isDefaultDeviceNameEc2 = IsEc2MachineName(deviceName);
191198

192-
if (Config.IsEc2 == null || Config.IsEc2 == true || isDefaultDeviceNameEc2)
199+
if (AzureConfig.InAzure && ((AzureConfig.IsWebsite) || (AzureConfig.InAzure && Environment.MachineName.StartsWith("RD"))))
193200
{
194-
var instanceID_task = GetEC2InstanceId();
195-
instanceID_task.Wait();
196-
if (string.IsNullOrWhiteSpace(instanceID_task.Result) == false)
201+
deviceName = AzureConfig.AzureInstanceName;
202+
}
203+
else
204+
{
205+
var isDefaultDeviceNameEc2 = IsEc2MachineName(deviceName);
206+
207+
if (Config.IsEc2 == null || Config.IsEc2 == true || isDefaultDeviceNameEc2)
197208
{
198-
deviceName = instanceID_task.Result;
209+
var instanceID_task = GetEC2InstanceId();
210+
instanceID_task.Wait();
211+
if (string.IsNullOrWhiteSpace(instanceID_task.Result) == false)
212+
{
213+
deviceName = instanceID_task.Result;
214+
}
199215
}
200216
}
201217

Src/StackifyLib/StackifyLib.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
<RepositoryUrl>https://github.com/stackify/stackify-api-dotnet</RepositoryUrl>
2121
<RepositoryType>git</RepositoryType>
2222
<PackageIconUrl>https://stackify.com/wp-content/uploads/2017/02/stk.png</PackageIconUrl>
23-
<AssemblyVersion>2.1.3.0</AssemblyVersion>
24-
<FileVersion>2.1.3.0</FileVersion>
23+
<AssemblyVersion>2.1.6.0</AssemblyVersion>
24+
<FileVersion>2.1.6.0</FileVersion>
2525
<PackageReleaseNotes>Remove default internal file logger</PackageReleaseNotes>
2626
</PropertyGroup>
2727

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using StackifyLib.Models;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace StackifyLib.Utils
7+
{
8+
public class ServerConfigHelper
9+
{
10+
public const string ProductionEnvName = "Production";
11+
12+
public static string GetEnvironment()
13+
{
14+
if (AzureConfig.IsWebsite)
15+
{
16+
var key = Environment.GetEnvironmentVariable("Stackify.Environment");
17+
18+
if (string.IsNullOrEmpty(key) == false)
19+
{
20+
return key;
21+
}
22+
23+
if (string.IsNullOrEmpty(AzureConfig.AzureAppWebConfigEnvironment) == false)
24+
{
25+
return AzureConfig.AzureAppWebConfigEnvironment;
26+
}
27+
28+
return ProductionEnvName;
29+
}
30+
31+
return string.Empty;
32+
}
33+
}
34+
}

Src/StackifyLib/Utils/StringExtensions.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,41 @@ public static string ToMD5Hash(this string input)
3030
}
3131
return sb.ToString();
3232
}
33+
34+
public static string Right(this string sValue, int iMaxLength)
35+
{
36+
//Check if the value is valid
37+
if (string.IsNullOrEmpty(sValue))
38+
{
39+
//Set valid empty string as string could be null
40+
sValue = string.Empty;
41+
}
42+
else if (sValue.Length > iMaxLength)
43+
{
44+
//Make the string no longer than the max length
45+
sValue = sValue.Substring(sValue.Length - iMaxLength, iMaxLength);
46+
}
47+
48+
//Return the string
49+
return sValue;
50+
}
51+
52+
public static string Left(this string sValue, int iMaxLength)
53+
{
54+
//Check if the value is valid
55+
if (string.IsNullOrEmpty(sValue))
56+
{
57+
//Set valid empty string as string could be null
58+
sValue = string.Empty;
59+
}
60+
else if (sValue.Length > iMaxLength)
61+
{
62+
//Make the string no longer than the max length
63+
sValue = sValue.Substring(0, iMaxLength);
64+
}
65+
66+
//Return the string
67+
return sValue;
68+
}
3369
}
3470
}

0 commit comments

Comments
 (0)