-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHDInsightManagementCLIHelpers.cs
363 lines (319 loc) · 15 KB
/
HDInsightManagementCLIHelpers.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
using log4net;
using Microsoft.Azure.Management.HDInsight.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Http;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace HDInsightManagementCLI
{
public static class HDInsightManagementCLIHelpers
{
private static readonly ILog Logger = LogManager.GetLogger(typeof(HDInsightManagementCLIHelpers));
/// <summary>
/// Regex to validate cluster user password.
/// Checks for following
/// - Minimum of 10 characters
/// - Atleast one upper case character
/// - Atleast one lower case character
/// - Atleast one number
/// - Atleast one non whitespace special character
/// DONT CHANGE this without corresponding change to the AUX code base.
/// </summary>
public const string HDInsightPasswordValidationRegex =
@"^.*(?=.{10,})(?=.*[a-z])(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]).*$";
public static string GetResourceGroupNameFromClusterId(string clusterId)
{
var resourceGroupUriPortion = "resourceGroups/";
var startIndex = clusterId.IndexOf(resourceGroupUriPortion, StringComparison.OrdinalIgnoreCase) + resourceGroupUriPortion.Length;
var endIndex = clusterId.IndexOf("/", startIndex, StringComparison.OrdinalIgnoreCase);
return clusterId.Substring(startIndex, endIndex - startIndex);
}
public static string GetResourceGroupName(string subscriptionId, string location, string extensionPrefix = "hdinsight")
{
string hashedSubId = string.Empty;
using (SHA256 sha256 = SHA256Managed.Create())
{
hashedSubId = Base32NoPaddingEncode(sha256.ComputeHash(UTF8Encoding.UTF8.GetBytes(subscriptionId)));
}
return string.Format(CultureInfo.InvariantCulture, "{0}{1}-{2}", extensionPrefix, hashedSubId, location.Replace(' ', '-'));
}
private static string Base32NoPaddingEncode(byte[] data)
{
const string Base32StandardAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
StringBuilder result = new StringBuilder(Math.Max((int)Math.Ceiling(data.Length * 8 / 5.0), 1));
byte[] emptyBuffer = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
byte[] workingBuffer = new byte[8];
// Process input 5 bytes at a time
for (int i = 0; i < data.Length; i += 5)
{
int bytes = Math.Min(data.Length - i, 5);
Array.Copy(emptyBuffer, workingBuffer, emptyBuffer.Length);
Array.Copy(data, i, workingBuffer, workingBuffer.Length - (bytes + 1), bytes);
Array.Reverse(workingBuffer);
ulong val = BitConverter.ToUInt64(workingBuffer, 0);
for (int bitOffset = ((bytes + 1) * 8) - 5; bitOffset > 3; bitOffset -= 5)
{
result.Append(Base32StandardAlphabet[(int)((val >> bitOffset) & 0x1f)]);
}
}
return result.ToString();
}
public static void CreateRdpFile(string clusterDnsName, string rdpUsername, string rdpPassword)
{
Logger.InfoFormat("Creating RDP file...");
try
{
var roleName = "IsotopeHeadNode";
var rdpFileName = clusterDnsName + String.Format("_{0}_IN_0.rdp", roleName);
var rdpFileContents = new StringBuilder();
rdpFileContents.AppendLine(String.Format("full address:s:{0}.cloudapp.net", clusterDnsName));
rdpFileContents.AppendLine(String.Format("username:s:{0}", rdpUsername));
rdpFileContents.AppendLine(String.Format("LoadBalanceInfo:s:Cookie: mstshash={0}#{0}_IN_0",
roleName));
rdpFileContents.AppendLine(String.Format("#RdpPassword={0}", rdpPassword));
File.WriteAllText(rdpFileName, rdpFileContents.ToString());
Logger.InfoFormat("RDP file for Cluster: {0} is available at: {1} (password is available in the file contents).", clusterDnsName, Path.Combine(Directory.GetCurrentDirectory(), rdpFileName));
}
catch (Exception)
{
Logger.InfoFormat("Unable to create rdp file for cluster: {0}", clusterDnsName);
throw;
}
}
public static System.Reflection.PropertyInfo GetPropertyInfo<TObj, TProp>(
this TObj obj,
Expression<Func<TObj, TProp>> propertyAccessor)
{
var memberExpression = propertyAccessor.Body as MemberExpression;
if (memberExpression != null)
{
var propertyInfo = memberExpression.Member as System.Reflection.PropertyInfo;
if (propertyInfo != null)
{
return propertyInfo;
}
}
throw new ArgumentException("propertyAccessor");
}
public static string ToDisplayString<T>(this T item, bool multiline = true, bool ignoreInheritedProperties = false)
{
if (item == null)
{
return "null";
}
Type itemType = item.GetType();
ICollection collection = item as ICollection;
if (collection != null)
{
List<string> itemDisplayStrings = new List<string>();
foreach (var obj in collection)
{
itemDisplayStrings.Add(ToDisplayString(obj, multiline, ignoreInheritedProperties));
}
return string.Join(multiline ? "," + Environment.NewLine : ", ", itemDisplayStrings);
}
else if (itemType.IsEnum)
{
if (itemType.GetCustomAttributes(typeof(FlagsAttribute), false).Any())
{
return string.Join(" | ", Enum.GetValues(itemType).Cast<Enum>().Where((item as Enum).HasFlag));
}
else
{
return item.ToString();
}
}
else if (itemType.GetMethod("ToString", System.Type.EmptyTypes).DeclaringType.Equals(itemType))
{
try
{
return item.ToString();
}
catch (Exception e)
{
return string.Format("Error({0})", e.Message);
}
}
System.Reflection.BindingFlags bindingFlags =
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.ExactBinding | System.Reflection.BindingFlags.GetProperty;
if (ignoreInheritedProperties)
{
bindingFlags |= System.Reflection.BindingFlags.DeclaredOnly;
}
return string.Join(
multiline ? Environment.NewLine : " ",
itemType.GetProperties(bindingFlags)
.Select(p =>
{
string displayString;
try
{
if (p.GetIndexParameters().Length > 0)
{
displayString = "Indexed Property (unavailable)";
}
else
{
displayString = ToDisplayString(p.GetValue(item, null), multiline);
}
}
catch (Exception e)
{
displayString = string.Format("Error({0})", e.Message);
}
return string.Format("{0}: {1}", p.Name, displayString);
}));
}
public static KeyValuePair<string, string> GenerateSshKeyPair(string keyName, string keyPassword)
{
var privateKeyName = keyName + ".key";
if (File.Exists(privateKeyName))
{
throw new ApplicationException("A Private key already exists with same name, please move it before generating a new one. Name: " + privateKeyName);
}
Logger.InfoFormat("Generating a new Ssh key pair. Name: {0}, Passphrase: {1}", privateKeyName, keyPassword);
try
{
RunExecutable("ssh-keygen.exe", String.Format("-t rsa -C {0} -f {1} -N {2}", keyName, privateKeyName, keyPassword));
}
catch (Exception)
{
Logger.Error("Failed to generate keys, please make sure you are running this executable from Git shell as it uses the ssh-keygen.exe provided by Git. " +
"Alternatively you may place ssh-keygen.exe from PortableGit in the current execution directory." +
Environment.NewLine);
throw;
}
return new KeyValuePair<string, string>(privateKeyName + ".pub", privateKeyName);
}
/// <summary>
/// Blocking executable launch
/// </summary>
/// <param name="exePath"></param>
/// <param name="exeArgs"></param>
/// <param name="workingDir"></param>
public static void RunExecutable(string exePath, string exeArgs, string workingDir = null)
{
Logger.InfoFormat("Running executable - Path: {0}, Args: {1}, WorkingDir: {2}",
exePath, exeArgs, workingDir);
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = exePath;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = exeArgs;
if (!String.IsNullOrWhiteSpace(workingDir))
{
startInfo.WorkingDirectory = workingDir;
}
try
{
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
if (exeProcess.ExitCode != 0)
{
var message = String.Format("Executable returned non-zero exit code. Path: {0}, Code: {1}",
exePath, exeProcess.ExitCode);
throw new ApplicationException(message);
}
}
}
catch (Exception ex)
{
var message = String.Format("Executable failed. Path: {0}, Args: {1}, WorkingDir: {2}",
exePath, exeArgs, workingDir);
Logger.Error(message, ex);
throw new ApplicationException(message, ex);
}
Logger.InfoFormat("Executable run successfully - Path: {0}, Args: {1}, WorkingDir: {2}",
exePath, exeArgs, workingDir);
}
/// <summary>
/// Non blocking process launch
/// </summary>
/// <param name="exePath"></param>
/// <param name="exeArgs"></param>
/// <param name="workingDir"></param>
public static void LaunchProcess(string exePath, string exeArgs, string workingDir = null)
{
Logger.InfoFormat("Launch Process - Path: {0}, Args: {1}, WorkingDir: {2}",
exePath, exeArgs, workingDir);
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = exePath;
startInfo.Arguments = exeArgs;
if (!String.IsNullOrWhiteSpace(workingDir))
{
startInfo.WorkingDirectory = workingDir;
}
try
{
Process exeProcess = Process.Start(startInfo);
}
catch (Exception ex)
{
var message = String.Format("Launch failed. Path: {0}, Args: {1}, WorkingDir: {2}",
exePath, exeArgs, workingDir);
Logger.Error(message, ex);
throw new ApplicationException(message, ex);
}
Logger.InfoFormat("Process launched successfully - Path: {0}, Args: {1}, WorkingDir: {2}",
exePath, exeArgs, workingDir);
}
public static string GetUserClusterTablePrefix(Cluster cluster)
{
Regex alphaNumRgx = new Regex("[^a-zA-Z0-9]");
string sanitizedClusterName = alphaNumRgx.Replace(cluster.Name, string.Empty);
if (sanitizedClusterName.Length > 20)
{
sanitizedClusterName = sanitizedClusterName.Substring(0, 20);
}
return String.Format("u{0}{1}", sanitizedClusterName, cluster.Properties.CreatedDate.AddMinutes(1).ToString("ddMMMyyyyATHH")).ToLowerInvariant();
}
}
public class AzureResourceProviderHandler : DelegatingHandler
{
private static readonly ILog Logger = LogManager.GetLogger(typeof(AzureResourceProviderHandler));
public string AzureResourceProviderNamespace {get; set;}
public const string HDInsightResourceProviderNamespace = "Microsoft.HDInsight";
public AzureResourceProviderHandler(string resourceProviderNamespace)
{
this.AzureResourceProviderNamespace = resourceProviderNamespace;
InnerHandler = new HttpClientHandler();
Logger.InfoFormat("AzureResourceProviderHandler initialized with ResourceProviderNamespace: {0}", this.AzureResourceProviderNamespace);
}
protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var originalRequestUri = request.RequestUri;
Logger.InfoFormat("Request Uri: {0}", originalRequestUri);
var newRequestUri =
new Uri(originalRequestUri.AbsoluteUri.Replace(
string.Format("/{0}/", HDInsightResourceProviderNamespace),
string.Format("/{0}/", AzureResourceProviderNamespace)));
request.RequestUri = newRequestUri;
Logger.InfoFormat("Request Uri (NEW): {0}", newRequestUri);
if (request.Content != null)
{
string s = request.Content.ReadAsStringAsync().Result;
Logger.InfoFormat("Request Content:\r\n{0}", s);
}
return base.SendAsync(request, cancellationToken).ContinueWith(task =>
{
var response = task.Result;
Logger.InfoFormat("Response Status code: {0} ", response.StatusCode);
Logger.InfoFormat("Response content: {0}", response.Content.ReadAsStringAsync().Result);
return response;
});
}
}
}