public override void ExecuteCmdlet() { base.ExecuteCmdlet(); ExecuteClientAction(() => { try { WriteVerbose("To be compatible with Azure, Add-AzVhd will automatically try to convert VHDX files to VHD and resize VHD files to N * Mib using Hyper-V Platform, a Windows native virtualization product. During the process the cmdlet will temporarily create a converted/resized file in the same directory as the provided VHD/VHDX file. \nFor more information visit https://aka.ms/usingAdd-AzVhd \n"); Program.SyncOutput = new PSSyncOutputEvents(this); // 1. CONVERT VHDX TO VHD if (this.LocalFilePath.Extension == ".vhdx") { convertVhd(); } // 2. RESIZE VHD CheckForInvalidVhd(); if (this.ParameterSetName == DirectUploadToManagedDiskSet) { // 3. DIRECT UPLOAD TO MANAGED DISK // 3-1. CREATE DISK CONFIG checkForExistingDisk(this.ResourceGroupName, this.DiskName); var diskConfig = CreateDiskConfig(); // 3-2: CREATE DISK createManagedDisk(this.ResourceGroupName, this.DiskName, diskConfig); // 3-3: GENERATE SAS WriteVerbose("Generating SAS"); var grantAccessData = new GrantAccessData(); grantAccessData.Access = "Write"; long gbInBytes = 1073741824; int gb = (int)(this.LocalFilePath.Length / gbInBytes); grantAccessData.DurationInSeconds = 86400 * Math.Max(gb / 100, 1); // 24h per 100gb var accessUri = this.ComputeClient.ComputeManagementClient.Disks.GrantAccess(this.ResourceGroupName, this.DiskName, grantAccessData); Uri sasUri = new Uri(accessUri.AccessSAS); WriteVerbose("SAS generated: " + accessUri.AccessSAS); // 3-4: UPLOAD WriteVerbose("Preparing for Upload"); PSPageBlobClient managedDisk = new PSPageBlobClient(sasUri); DiskUploadCreator diskUploadCreator = new DiskUploadCreator(); var uploadContext = diskUploadCreator.Create(this.LocalFilePath, managedDisk, false); var synchronizer = new DiskSynchronizer(uploadContext, this.NumberOfUploaderThreads ?? DefaultNumberOfUploaderThreads); WriteVerbose("Uploading"); if (synchronizer.Synchronize()) { var result = new VhdUploadContext { LocalFilePath = this.LocalFilePath, DestinationUri = sasUri }; WriteObject(result); } else { WriteVerbose("Upload failed"); } // 3-5: REVOKE SAS WriteVerbose("Revoking SAS"); var RevokeResult = this.ComputeClient.ComputeManagementClient.Disks.RevokeAccessWithHttpMessagesAsync(this.ResourceGroupName, this.DiskName).GetAwaiter().GetResult(); PSOperationStatusResponse output = new PSOperationStatusResponse { StartTime = this.StartTime, EndTime = DateTime.Now }; if (RevokeResult != null && RevokeResult.Request != null && RevokeResult.Request.RequestUri != null) { output.Name = GetOperationIdFromUrlString(RevokeResult.Request.RequestUri.ToString()); } WriteVerbose("SAS revoked."); WriteVerbose("\nUpload complete."); } else { var parameters = ValidateParameters(); var vhdUploadContext = VhdUploaderModel.Upload(parameters); WriteObject(vhdUploadContext); } } finally { if (temporaryFileCreated) { WriteVerbose("Deleting file: " + this.LocalFilePath.FullName); File.Delete(this.LocalFilePath.FullName); } } }); }
protected void AssertUploadContextAndContentMD5UsingSaveVhd (string destination, FileInfo localFile, VhdUploadContext vhdUploadContext, string md5hash, bool deleteBlob = true, bool deleteLocal = true) { AssertUploadContext(destination, localFile, vhdUploadContext); FileInfo downloadFile = new FileInfo(localFile.FullName + "_download.vhd"); BlobHandle blobHandle = getBlobHandle(destination); Assert.IsTrue(VerifyMD5hash(blobHandle, md5hash)); SaveVhdAndAssertContent(blobHandle, downloadFile, true, deleteBlob, deleteLocal); }
protected void AssertUploadContext(string destination, FileInfo localFile, VhdUploadContext vhdUploadContext) { Assert.IsNotNull(vhdUploadContext); Assert.AreEqual(new Uri(destination), vhdUploadContext.DestinationUri); Assert.AreEqual(vhdUploadContext.LocalFilePath.FullName, localFile.FullName); }
public override void ExecuteCmdlet() { base.ExecuteCmdlet(); ExecuteClientAction(() => { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("To be compatible with Azure, Add-AzVhd will automatically try to convert VHDX files to VHD, and resize VHD files to N * Mib using Hyper-V Platform, a Windows naitive virtualization product. \nFor more information visit https://aka.ms/usingAdd-AzVhd \n"); Console.ResetColor(); Program.SyncOutput = new PSSyncOutputEvents(this); // 1. CONVERT VHDX TO VHD if (this.LocalFilePath.Extension == ".vhdx") { convertVhd(); } checkForCorruptedAndDynamicallySizedVhd(); checkVhdFileSize(this.LocalFilePath); // 2. RESIZE VHD if (this.skipResizing.IsPresent) { Console.WriteLine("Skipping VHD resizing."); } else { if ((this.LocalFilePath.Length - 512) % 1048576 != 0) { resizeVhdFile(); } else // does not need resizing { WriteVerbose("Vhd file already sized correctly. Proceeding to uploading."); } } if (this.ParameterSetName == DirectUploadToManagedDiskSet) { // 3. DIRECT UPLOAD TO MANAGED DISK // 3-1. CREATE DISK CONFIG checkForExistingDisk(this.ResourceGroupName, this.DiskName); var diskConfig = CreateDiskConfig(); // 3-2: CREATE DISK createManagedDisk(this.ResourceGroupName, this.DiskName, diskConfig); // 3-3: GENERATE SAS Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Generating SAS"); Console.ResetColor(); var grantAccessData = new GrantAccessData(); grantAccessData.Access = "Write"; long gbInBytes = 1073741824; int gb = (int)(this.LocalFilePath.Length / gbInBytes); grantAccessData.DurationInSeconds = 86400 * Math.Max(gb / 100, 1); // 24h per 100gb var accessUri = this.ComputeClient.ComputeManagementClient.Disks.GrantAccess(this.ResourceGroupName, this.DiskName, grantAccessData); Uri sasUri = new Uri(accessUri.AccessSAS); Console.WriteLine("SAS generated: " + accessUri.AccessSAS); // 3-4: UPLOAD Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Preparing for Upload"); Console.ResetColor(); PSPageBlobClient managedDisk = new PSPageBlobClient(sasUri); DiskUploadCreator diskUploadCreator = new DiskUploadCreator(); var uploadContext = diskUploadCreator.Create(this.LocalFilePath, managedDisk, false); var synchronizer = new DiskSynchronizer(uploadContext, this.NumberOfUploaderThreads ?? DefaultNumberOfUploaderThreads); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Uploading"); Console.ResetColor(); if (synchronizer.Synchronize()) { var result = new VhdUploadContext { LocalFilePath = this.LocalFilePath, DestinationUri = sasUri }; WriteObject(result); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Upload failed"); Console.ResetColor(); } // 3-5: REVOKE SAS Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Revoking SAS"); Console.ResetColor(); var RevokeResult = this.ComputeClient.ComputeManagementClient.Disks.RevokeAccessWithHttpMessagesAsync(this.ResourceGroupName, this.DiskName).GetAwaiter().GetResult(); PSOperationStatusResponse output = new PSOperationStatusResponse { StartTime = this.StartTime, EndTime = DateTime.Now }; if (RevokeResult != null && RevokeResult.Request != null && RevokeResult.Request.RequestUri != null) { output.Name = GetOperationIdFromUrlString(RevokeResult.Request.RequestUri.ToString()); } Console.WriteLine("SAS revoked."); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("\nUpload complete."); Console.ResetColor(); } else { var parameters = ValidateParameters(); var vhdUploadContext = VhdUploaderModel.Upload(parameters); WriteObject(vhdUploadContext); } }); }