PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/Extensions/CustomScript/SetAzureVMCustomScriptExtension.cs

https://gitlab.com/jslee1/azure-powershell
C# | 260 lines | 213 code | 29 blank | 18 comment | 10 complexity | b983b672bd996b99f8d4d8ec956f196e MD5 | raw file
  1. // ----------------------------------------------------------------------------------
  2. //
  3. // Copyright Microsoft Corporation
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // ----------------------------------------------------------------------------------
  14. using System;
  15. using System.Linq;
  16. using System.Management.Automation;
  17. using Microsoft.Azure.Commands.Common.Authentication.Models;
  18. using Microsoft.WindowsAzure.Commands.Common;
  19. using Microsoft.WindowsAzure.Commands.ServiceManagement.Common;
  20. using Microsoft.WindowsAzure.Commands.ServiceManagement.Model;
  21. using Microsoft.WindowsAzure.Commands.ServiceManagement.Properties;
  22. using Microsoft.WindowsAzure.Management.Storage;
  23. using Microsoft.WindowsAzure.Storage;
  24. using Microsoft.WindowsAzure.Storage.Auth;
  25. using Microsoft.WindowsAzure.Storage.Blob;
  26. namespace Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.Extensions
  27. {
  28. [Cmdlet(
  29. VerbsCommon.Set,
  30. ProfileNouns.VirtualMachineCustomScriptExtension,
  31. DefaultParameterSetName = SetCustomScriptExtensionByContainerBlobsParamSetName),
  32. OutputType(
  33. typeof(IPersistentVM))]
  34. public class SetAzureVMCustomScriptExtensionCommand : VirtualMachineCustomScriptExtensionCmdletBase
  35. {
  36. protected const string SetCustomScriptExtensionByContainerBlobsParamSetName = "SetCustomScriptExtensionByContainerAndFileNames";
  37. protected const string SetCustomScriptExtensionByUrisParamSetName = "SetCustomScriptExtensionByUriLinks";
  38. protected const string DisableCustomScriptExtensionParamSetName = "DisableCustomScriptExtension";
  39. protected const string UninstallCustomScriptExtensionParamSetName = "UninstalleCustomScriptExtension";
  40. [Parameter(
  41. Mandatory = false,
  42. Position = 0,
  43. ValueFromPipelineByPropertyName = true,
  44. HelpMessage = "The Extension Reference Name.")]
  45. [ValidateNotNullOrEmpty]
  46. public override string ReferenceName { get; set; }
  47. [Parameter(
  48. Mandatory = false,
  49. Position = 1,
  50. ValueFromPipelineByPropertyName = true,
  51. HelpMessage = "The Extension Version.")]
  52. [ValidateNotNullOrEmpty]
  53. public override string Version { get; set; }
  54. [Parameter(
  55. ParameterSetName = DisableCustomScriptExtensionParamSetName,
  56. Mandatory = false,
  57. Position = 2,
  58. ValueFromPipelineByPropertyName = true,
  59. HelpMessage = "Disable VM Custom Script Extension")]
  60. public override SwitchParameter Disable { get; set; }
  61. [Parameter(
  62. ParameterSetName = UninstallCustomScriptExtensionParamSetName,
  63. Mandatory = false,
  64. Position = 2,
  65. ValueFromPipelineByPropertyName = true,
  66. HelpMessage = "Uninstall VM Custom Script Extension")]
  67. public override SwitchParameter Uninstall { get; set; }
  68. [Parameter(
  69. ParameterSetName = SetCustomScriptExtensionByContainerBlobsParamSetName,
  70. Mandatory = true,
  71. Position = 2,
  72. ValueFromPipelineByPropertyName = true,
  73. HelpMessage = "The Name of the Container.")]
  74. [ValidateNotNullOrEmpty]
  75. public override string ContainerName { get; set; }
  76. [Parameter(
  77. ParameterSetName = SetCustomScriptExtensionByContainerBlobsParamSetName,
  78. Mandatory = true,
  79. Position = 3,
  80. ValueFromPipelineByPropertyName = true,
  81. HelpMessage = "The Blob Files in the Container.")]
  82. [ValidateNotNullOrEmpty]
  83. public override string[] FileName { get; set; }
  84. [Parameter(
  85. ParameterSetName = SetCustomScriptExtensionByContainerBlobsParamSetName,
  86. Mandatory = false,
  87. Position = 4,
  88. ValueFromPipelineByPropertyName = true,
  89. HelpMessage = "The Storage Account Name.")]
  90. [ValidateNotNullOrEmpty]
  91. public override string StorageAccountName { get; set; }
  92. [Parameter(
  93. ParameterSetName = SetCustomScriptExtensionByContainerBlobsParamSetName,
  94. Mandatory = false,
  95. Position = 5,
  96. ValueFromPipelineByPropertyName = true,
  97. HelpMessage = "The Storage Endpoint Suffix.")]
  98. [ValidateNotNullOrEmpty]
  99. public override string StorageEndpointSuffix { get; set; }
  100. [Parameter(
  101. ParameterSetName = SetCustomScriptExtensionByContainerBlobsParamSetName,
  102. Mandatory = false,
  103. Position = 6,
  104. ValueFromPipelineByPropertyName = true,
  105. HelpMessage = "The Storage Account Key.")]
  106. [ValidateNotNullOrEmpty]
  107. public override string StorageAccountKey { get; set; }
  108. [Parameter(
  109. ParameterSetName = SetCustomScriptExtensionByUrisParamSetName,
  110. Mandatory = false,
  111. Position = 2,
  112. ValueFromPipelineByPropertyName = true,
  113. HelpMessage = "The File URIs.")]
  114. [ValidateNotNullOrEmpty]
  115. public override string[] FileUri { get; set; }
  116. [Parameter(
  117. ParameterSetName = SetCustomScriptExtensionByContainerBlobsParamSetName,
  118. Mandatory = false,
  119. Position = 7,
  120. ValueFromPipelineByPropertyName = true,
  121. HelpMessage = "The Run File to Execute in PowerShell on the VM.")]
  122. [Parameter(
  123. ParameterSetName = SetCustomScriptExtensionByUrisParamSetName,
  124. Mandatory = true,
  125. Position = 3,
  126. ValueFromPipelineByPropertyName = true,
  127. HelpMessage = "The Run File to Execute in PowerShell on the VM.")]
  128. [ValidateNotNullOrEmpty]
  129. [Alias("RunFile", "Command")]
  130. public override string Run { get; set; }
  131. [Parameter(
  132. ParameterSetName = SetCustomScriptExtensionByContainerBlobsParamSetName,
  133. Mandatory = false,
  134. Position = 8,
  135. ValueFromPipelineByPropertyName = true,
  136. HelpMessage = "The Argument String for the Run File.")]
  137. [Parameter(
  138. ParameterSetName = SetCustomScriptExtensionByUrisParamSetName,
  139. Mandatory = false,
  140. Position = 4,
  141. ValueFromPipelineByPropertyName = true,
  142. HelpMessage = "The Argument String for the Run File.")]
  143. [ValidateNotNullOrEmpty]
  144. public override string Argument { get; set; }
  145. [Parameter(
  146. Mandatory = false,
  147. Position = 9,
  148. HelpMessage = "Re-apply a configuration to an extension when the configuration has not been updated.")]
  149. public override SwitchParameter ForceUpdate { get; set; }
  150. protected override void ProcessRecord()
  151. {
  152. base.ProcessRecord();
  153. ExecuteCommand();
  154. }
  155. internal void ExecuteCommand()
  156. {
  157. ValidateParameters();
  158. RemovePredicateExtensions();
  159. AddResourceExtension();
  160. WriteObject(VM);
  161. }
  162. protected override void ValidateParameters()
  163. {
  164. base.ValidateParameters();
  165. if (string.Equals(this.ParameterSetName, SetCustomScriptExtensionByContainerBlobsParamSetName))
  166. {
  167. this.StorageEndpointSuffix = string.IsNullOrEmpty(this.StorageEndpointSuffix) ?
  168. Profile.Context.Environment.GetEndpoint(AzureEnvironment.Endpoint.StorageEndpointSuffix) : this.StorageEndpointSuffix;
  169. var sName = string.IsNullOrEmpty(this.StorageAccountName) ? GetStorageName() : this.StorageAccountName;
  170. var sKey = string.IsNullOrEmpty(this.StorageAccountKey) ? GetStorageKey(sName) : this.StorageAccountKey;
  171. if (this.FileName != null && this.FileName.Any())
  172. {
  173. this.FileUri = (from blobName in this.FileName
  174. select GetSasUrlStr(sName, sKey, this.ContainerName, blobName)).ToArray();
  175. if (string.IsNullOrEmpty(this.Run))
  176. {
  177. WriteWarning(Resources.CustomScriptExtensionTryToUseTheFirstSpecifiedFileAsRunScript);
  178. this.Run = this.FileName[0];
  179. }
  180. }
  181. }
  182. this.ReferenceName = string.IsNullOrEmpty(this.ReferenceName) ? ExtensionDefaultName : this.ReferenceName;
  183. this.PublicConfiguration = GetPublicConfiguration();
  184. this.PrivateConfiguration = GetPrivateConfiguration();
  185. this.Version = this.Version ?? ExtensionDefaultVersion;
  186. }
  187. protected string GetStorageName()
  188. {
  189. return Profile.Context.Subscription.GetStorageAccountName();
  190. }
  191. protected string GetStorageKey(string storageName)
  192. {
  193. string storageKey = string.Empty;
  194. if (!string.IsNullOrEmpty(storageName))
  195. {
  196. var storageAccount = this.StorageClient.StorageAccounts.Get(storageName);
  197. if (storageAccount != null)
  198. {
  199. var keys = this.StorageClient.StorageAccounts.GetKeys(storageName);
  200. if (keys != null)
  201. {
  202. storageKey = !string.IsNullOrEmpty(keys.PrimaryKey) ? keys.PrimaryKey : keys.SecondaryKey;
  203. }
  204. }
  205. }
  206. return storageKey;
  207. }
  208. protected string GetSasUrlStr(string storageName, string storageKey, string containerName, string blobName)
  209. {
  210. var cred = new StorageCredentials(storageName, storageKey);
  211. var storageAccount = string.IsNullOrEmpty(this.StorageEndpointSuffix)
  212. ? new CloudStorageAccount(cred, true)
  213. : new CloudStorageAccount(cred, this.StorageEndpointSuffix, true);
  214. var blobClient = storageAccount.CreateCloudBlobClient();
  215. var container = blobClient.GetContainerReference(containerName);
  216. var cloudBlob = container.GetBlockBlobReference(blobName);
  217. var sasToken = cloudBlob.GetSharedAccessSignature(
  218. new SharedAccessBlobPolicy()
  219. {
  220. SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24.0),
  221. Permissions = SharedAccessBlobPermissions.Read
  222. });
  223. // Try not to use a Uri object in order to keep the following
  224. // special characters in the SAS signature section:
  225. // '+' -> '%2B'
  226. // '/' -> '%2F'
  227. // '=' -> '%3D'
  228. return cloudBlob.Uri + sasToken;
  229. }
  230. }
  231. }