PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ResourceManager/Compute/Commands.Compute/Extension/DSC/PublishAzureVMDscConfigurationCommand.cs

https://gitlab.com/jslee1/azure-powershell
C# | 206 lines | 141 code | 22 blank | 43 comment | 11 complexity | 8775a73f814b9c3d0587625ad3471caf MD5 | raw file
  1. using Microsoft.Azure.Commands.Common.Authentication.Models;
  2. using Microsoft.Azure.Commands.Compute.Common;
  3. using Microsoft.WindowsAzure.Commands.Common.Extensions.DSC;
  4. using Microsoft.WindowsAzure.Commands.Common.Extensions.DSC.Publish;
  5. using Microsoft.WindowsAzure.Storage.Auth;
  6. using System;
  7. using System.Management.Automation;
  8. namespace Microsoft.Azure.Commands.Compute.Extension.DSC
  9. {
  10. /// <summary>
  11. /// Uploads a Desired State Configuration script to Azure blob storage, which
  12. /// later can be applied to Azure Virtual Machines using the
  13. /// Set-AzureRmVMDscExtension cmdlet.
  14. /// </summary>
  15. [Cmdlet(
  16. VerbsData.Publish,
  17. ProfileNouns.VirtualMachineDscConfiguration,
  18. SupportsShouldProcess = true,
  19. DefaultParameterSetName = UploadArchiveParameterSetName),
  20. OutputType(
  21. typeof(String))]
  22. public class PublishAzureVMDscConfigurationCommand : DscExtensionPublishCmdletCommonBase
  23. {
  24. private const string CreateArchiveParameterSetName = "CreateArchive";
  25. private const string UploadArchiveParameterSetName = "UploadArchive";
  26. [Parameter(
  27. Mandatory = true,
  28. Position = 2,
  29. ParameterSetName = UploadArchiveParameterSetName,
  30. ValueFromPipelineByPropertyName = true,
  31. HelpMessage = "The name of the resource group that contains the storage account.")]
  32. [ValidateNotNullOrEmpty]
  33. public string ResourceGroupName { get; set; }
  34. /// <summary>
  35. /// Path to a file containing one or more configurations; the file can be a
  36. /// PowerShell script (*.ps1) or MOF interface (*.mof).
  37. /// </summary>
  38. [Parameter(
  39. Mandatory = true,
  40. Position = 1,
  41. ValueFromPipeline = true,
  42. ValueFromPipelineByPropertyName = true,
  43. HelpMessage = "Path to a file containing one or more configurations")]
  44. [ValidateNotNullOrEmpty]
  45. public string ConfigurationPath { get; set; }
  46. /// <summary>
  47. /// Name of the Azure Storage Container the configuration is uploaded to.
  48. /// </summary>
  49. [Parameter(
  50. ValueFromPipelineByPropertyName = true,
  51. Position = 4,
  52. ParameterSetName = UploadArchiveParameterSetName,
  53. HelpMessage = "Name of the Azure Storage Container the configuration is uploaded to")]
  54. [ValidateNotNullOrEmpty]
  55. public string ContainerName { get; set; }
  56. /// <summary>
  57. /// The Azure Storage Account name used to upload the configuration script to the container specified by ContainerName.
  58. /// </summary>
  59. [Parameter(
  60. Mandatory = true,
  61. Position = 3,
  62. ValueFromPipelineByPropertyName = true,
  63. ParameterSetName = UploadArchiveParameterSetName,
  64. HelpMessage = "The Azure Storage Account name used to upload the configuration script to the container " +
  65. "specified by ContainerName ")]
  66. [ValidateNotNullOrEmpty]
  67. public String StorageAccountName { get; set; }
  68. /// <summary>
  69. /// Path to a local ZIP file to write the configuration archive to.
  70. /// When using this parameter, Publish-AzureRmVMDscConfiguration creates a
  71. /// local ZIP archive instead of uploading it to blob storage..
  72. /// </summary>
  73. [Alias("ConfigurationArchivePath")]
  74. [Parameter(
  75. Position = 2,
  76. ValueFromPipelineByPropertyName = true,
  77. ParameterSetName = CreateArchiveParameterSetName,
  78. HelpMessage = "Path to a local ZIP file to write the configuration archive to.")]
  79. [ValidateNotNullOrEmpty]
  80. public string OutputArchivePath { get; set; }
  81. /// <summary>
  82. /// Suffix for the storage end point, e.g. core.windows.net
  83. /// </summary>
  84. [Parameter(
  85. ValueFromPipelineByPropertyName = true,
  86. ParameterSetName = UploadArchiveParameterSetName,
  87. HelpMessage = "Suffix for the storage end point, e.g. core.windows.net")]
  88. [ValidateNotNullOrEmpty]
  89. public string StorageEndpointSuffix { get; set; }
  90. /// <summary>
  91. /// By default Publish-AzureRmVMDscConfiguration will not overwrite any existing blobs.
  92. /// Use -Force to overwrite them.
  93. /// </summary>
  94. [Parameter(HelpMessage = "By default Publish-AzureRmVMDscConfiguration will not overwrite any existing blobs")]
  95. public SwitchParameter Force { get; set; }
  96. /// <summary>
  97. /// Excludes DSC resource dependencies from the configuration archive
  98. /// </summary>
  99. [Parameter(HelpMessage = "Excludes DSC resource dependencies from the configuration archive")]
  100. public SwitchParameter SkipDependencyDetection { get; set; }
  101. /// <summary>
  102. ///Path to a .psd1 file that specifies the data for the Configuration. This
  103. /// file must contain a hashtable with the items described in
  104. /// http://technet.microsoft.com/en-us/library/dn249925.aspx.
  105. /// </summary>
  106. [Parameter(
  107. ValueFromPipelineByPropertyName = true,
  108. HelpMessage = "Path to a .psd1 file that specifies the data for the Configuration. This is added to the configuration " +
  109. "archive and then passed to the configuration function. It gets overwritten by the configuration data path " +
  110. "provided through the Set-AzureRmVMDscExtension cmdlet")]
  111. [ValidateNotNullOrEmpty]
  112. public string ConfigurationDataPath { get; set; }
  113. /// <summary>
  114. /// Path to a file or a directory to include in the configuration archive
  115. /// </summary>
  116. [Parameter(
  117. ValueFromPipelineByPropertyName = true,
  118. HelpMessage = "Path to a file or a directory to include in the configuration archive. It gets downloaded to the " +
  119. "VM along with the configuration")]
  120. [ValidateNotNullOrEmpty]
  121. public String[] AdditionalPath { get; set; }
  122. //Private Variables
  123. /// <summary>
  124. /// Credentials used to access Azure Storage
  125. /// </summary>
  126. private StorageCredentials _storageCredentials;
  127. public override void ExecuteCmdlet()
  128. {
  129. base.ExecuteCmdlet();
  130. try
  131. {
  132. ValidatePsVersion();
  133. //validate cmdlet params
  134. ConfigurationPath = GetUnresolvedProviderPathFromPSPath(ConfigurationPath);
  135. ValidateConfigurationPath(ConfigurationPath, ParameterSetName);
  136. if (ConfigurationDataPath != null)
  137. {
  138. ConfigurationDataPath = GetUnresolvedProviderPathFromPSPath(ConfigurationDataPath);
  139. ValidateConfigurationDataPath(ConfigurationDataPath);
  140. }
  141. if (AdditionalPath != null && AdditionalPath.Length > 0)
  142. {
  143. for (var count = 0; count < AdditionalPath.Length; count++)
  144. {
  145. AdditionalPath[count] = GetUnresolvedProviderPathFromPSPath(AdditionalPath[count]);
  146. }
  147. }
  148. switch (ParameterSetName)
  149. {
  150. case CreateArchiveParameterSetName:
  151. OutputArchivePath = GetUnresolvedProviderPathFromPSPath(OutputArchivePath);
  152. break;
  153. case UploadArchiveParameterSetName:
  154. _storageCredentials = this.GetStorageCredentials(ResourceGroupName, StorageAccountName);
  155. if (ContainerName == null)
  156. {
  157. ContainerName = DscExtensionCmdletConstants.DefaultContainerName;
  158. }
  159. if (StorageEndpointSuffix == null)
  160. {
  161. StorageEndpointSuffix =
  162. DefaultProfile.Context.Environment.GetEndpoint(AzureEnvironment.Endpoint.StorageEndpointSuffix);
  163. }
  164. break;
  165. }
  166. PublishConfiguration(
  167. ConfigurationPath,
  168. ConfigurationDataPath,
  169. AdditionalPath,
  170. OutputArchivePath,
  171. StorageEndpointSuffix,
  172. ContainerName,
  173. ParameterSetName,
  174. Force.IsPresent,
  175. SkipDependencyDetection.IsPresent,
  176. _storageCredentials);
  177. }
  178. finally
  179. {
  180. DeleteTemporaryFiles();
  181. }
  182. }
  183. }
  184. }