/// <summary> /// Used to copy or register an image from one subscription to another /// </summary> public void CopyAndRegisterImageInNewSubscription(ImageProperties imageProperties, bool copyImageOnlyIfNotExists = true) { // by default we won't copy the image if it exists // TODO: need to check this implementation as the index is confusing int index = imageProperties.Version; if (Exists(GetFormattedImageName(imageProperties.ImageNameRoot, imageProperties.Version, false)) && copyImageOnlyIfNotExists) { return; } RaiseClientUpdate(5, "Checked for formatted image existence"); // get the storage account to copy to and the blob var storageAccountClient = new StorageClient(SubscriptionId, ManagementCertificate); var destinationAccountKeys = storageAccountClient.GetStorageAccountKeys(imageProperties.DestinationAccountName); var storageAccount = new CloudStorageAccount(new StorageCredentials(imageProperties.DestinationAccountName, destinationAccountKeys[0]), true); var blobClient = storageAccount.CreateCloudBlobClient(); // list all of the containers in the blob - if they are not present then create a new one // create this container if it doesn't exist as this will contain the blob which will be registered as the image var containerReference = blobClient.GetContainerReference(imageProperties.DestinationAccountContainer); containerReference.CreateIfNotExists(); // make sure that this image name dis a .vhd //imageName = imageName.EndsWith(".vhd") ? imageName + ".vhd" : imageName; var blobImage = containerReference.GetPageBlobReference(GetFormattedImageName(imageProperties.ImageNameRoot, index, true)); while (blobImage.Exists()) { // eventually we'll find a name we don't have! blobImage = containerReference.GetPageBlobReference(GetFormattedImageName(imageProperties.ImageNameRoot, ++index, true)); } RaiseClientUpdate(8, "Checked to see whether images exist with index " + index); // create a SAS from the source account for the image if (imageProperties.SourceAccountName != null && imageProperties.SourceAccountKey != null) { var client = new StorageClient(imageProperties.SourceAccountName, imageProperties.SourceAccountKey); imageProperties.ImageCopyLocation = client.GetSaSFromBlobUri(imageProperties.ImageCopyLocation); RaiseClientUpdate(10, "Calculated SaS blob uri"); } try { blobImage.StartCopyFromBlob(new Uri(imageProperties.ImageCopyLocation)); double percentCopied = 0; while (blobImage.CopyState.Status != CopyStatus.Success && blobImage.CopyState.Status != CopyStatus.Failed) { blobImage = (CloudPageBlob)containerReference.GetBlobReferenceFromServer(GetFormattedImageName(imageProperties.ImageNameRoot, index, true)); if (blobImage.CopyState.BytesCopied == null || blobImage.CopyState.TotalBytes == null) continue; // wait one second until we have the copy status working properly double innerPercent = Math.Round(((double)blobImage.CopyState.BytesCopied.Value / (double)blobImage.CopyState.TotalBytes.Value) * 70) + 10; if (innerPercent != percentCopied) { RaiseClientUpdate(Convert.ToInt32(innerPercent), "Copied part of image file to blob storage"); } percentCopied = innerPercent; Thread.Sleep(1000); } } catch (Exception ex) { Console.WriteLine("Exception is: ", ex.ToString()); } // use the copy blob API to copy the image across // when the copy process is complete we want to register the image imageProperties.Name = imageProperties.Label = GetFormattedImageName(imageProperties.ImageNameRoot, index, false); imageProperties.MediaLink = blobImage.Uri.ToString(); var registerImageCommand = new RegisterImageCommand(imageProperties) { SubscriptionId = SubscriptionId, Certificate = ManagementCertificate }; registerImageCommand.Execute(); RaiseClientUpdate(100, "Completed registration of image into target account"); }
/// <summary> /// Used to copy or register an image from one subscription to another /// </summary> public void CopyAndRegisterImageInNewSubscription(ImageProperties imageProperties, bool copyImageOnlyIfNotExists = true) { // by default we won't copy the image if it exists // TODO: need to check this implementation as the index is confusing int index = imageProperties.Version; if (Exists(GetFormattedImageName(imageProperties.ImageNameRoot, imageProperties.Version, false)) && copyImageOnlyIfNotExists) { return; } RaiseClientUpdate(5, "Checked for formatted image existence"); // get the storage account to copy to and the blob var storageAccountClient = new StorageClient(SubscriptionId, ManagementCertificate); var destinationAccountKeys = storageAccountClient.GetStorageAccountKeys(imageProperties.DestinationAccountName); var storageAccount = new CloudStorageAccount(new StorageCredentials(imageProperties.DestinationAccountName, destinationAccountKeys[0]), true); var blobClient = storageAccount.CreateCloudBlobClient(); // list all of the containers in the blob - if they are not present then create a new one // create this container if it doesn't exist as this will contain the blob which will be registered as the image var containerReference = blobClient.GetContainerReference(imageProperties.DestinationAccountContainer); containerReference.CreateIfNotExists(); // make sure that this image name dis a .vhd //imageName = imageName.EndsWith(".vhd") ? imageName + ".vhd" : imageName; var blobImage = containerReference.GetPageBlobReference(GetFormattedImageName(imageProperties.ImageNameRoot, index, true)); while (blobImage.Exists()) { // eventually we'll find a name we don't have! blobImage = containerReference.GetPageBlobReference(GetFormattedImageName(imageProperties.ImageNameRoot, ++index, true)); } RaiseClientUpdate(8, "Checked to see whether images exist with index " + index); // create a SAS from the source account for the image if (imageProperties.SourceAccountName != null && imageProperties.SourceAccountKey != null) { var client = new StorageClient(imageProperties.SourceAccountName, imageProperties.SourceAccountKey); imageProperties.ImageCopyLocation = client.GetSaSFromBlobUri(imageProperties.ImageCopyLocation); RaiseClientUpdate(10, "Calculated SaS blob uri"); } try { blobImage.StartCopyFromBlob(new Uri(imageProperties.ImageCopyLocation)); double percentCopied = 0; while (blobImage.CopyState.Status != CopyStatus.Success && blobImage.CopyState.Status != CopyStatus.Failed) { blobImage = (CloudPageBlob)containerReference.GetBlobReferenceFromServer(GetFormattedImageName(imageProperties.ImageNameRoot, index, true)); if (blobImage.CopyState.BytesCopied == null || blobImage.CopyState.TotalBytes == null) { continue; } // wait one second until we have the copy status working properly double innerPercent = Math.Round(((double)blobImage.CopyState.BytesCopied.Value / (double)blobImage.CopyState.TotalBytes.Value) * 70) + 10; if (innerPercent != percentCopied) { RaiseClientUpdate(Convert.ToInt32(innerPercent), "Copied part of image file to blob storage"); } percentCopied = innerPercent; Thread.Sleep(1000); } } catch (Exception ex) { Console.WriteLine("Exception is: ", ex.ToString()); } // use the copy blob API to copy the image across // when the copy process is complete we want to register the image imageProperties.Name = imageProperties.Label = GetFormattedImageName(imageProperties.ImageNameRoot, index, false); imageProperties.MediaLink = blobImage.Uri.ToString(); var registerImageCommand = new RegisterImageCommand(imageProperties) { SubscriptionId = SubscriptionId, Certificate = ManagementCertificate }; registerImageCommand.Execute(); RaiseClientUpdate(100, "Completed registration of image into target account"); }