static void Main(string[] args) { // should have 5 arguments though subscription_id_source, path_to_pub_settings_source, // destination_account_name, destination_account_key, vhd_blob_path // 1. test the copy of the image from source to destination account // ReSharper disable once InconsistentNaming string subscription_id_source = args[0]; // ReSharper disable once InconsistentNaming var certificate_source = new PublishSettingsExtractor(args[1]).AddPublishSettingsToPersonalMachineStore(); // ReSharper disable once InconsistentNaming string account_name_destination = args[2]; // ReSharper disable once InconsistentNaming string account_key_destination = args[3]; // ReSharper disable once InconsistentNaming string source_image_path = args[4]; var properties = new ImageProperties() { OperatingSystem = PlatformType.Linux, Description = "Test from Azure Fluent Management", ShowInGui = false, IsPremium = true }; var client = new ImageManagementClient(subscription_id_source, certificate_source); var imageList = client.ImageList; imageList.ForEach(image => Console.WriteLine(image.Label)); Console.WriteLine("Image sparkius exists: {0}", client.Exists("sparkius1")); //client.CopyAndRegisterImageInNewSubscription(account_name_destination, account_key_destination, null, // "elastaimage", "sparkius", source_image_path, properties); }
// https://management.core.windows.net/<subscription-id>/services/images /// <summary> /// Registers a virtual machine image for either Linux or Windowss /// </summary> internal RegisterImageCommand(ImageProperties properties) { AdditionalHeaders["x-ms-version"] = "2012-08-01"; OperationId = "images"; ServiceType = "services"; HttpVerb = HttpVerbPost; properties.PublishedDate = DateTime.UtcNow; properties.IsPremium = properties.IsPremium.HasValue && properties.IsPremium.Value; properties.ShowInGui = properties.ShowInGui.HasValue && properties.ShowInGui.Value; Properties = properties; }
/// <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"); }
internal override void Parse() { /* * <Images xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <OSImage> <AffinityGroup>name-of-the-affinity-group</AffinityGroup> <Category>category-of-the-image</Category> <Label>image-description</Label> <Location>geo-location-of-the-stored-image</Location> <LogicalSizeInGB>size-of-the-image</LogicalSizeInGB> <MediaLink>url-of-the-containing-blob</MediaLink> <Name>image-name</Name> <OS>operating-system-of-the-image</OS> <Eula>image-eula</Eula> <Description>image-description</Description> <ImageFamily>image-family</ImageFamily> <ShowInGui>true|false</ShowInGui> <PublishedDate>published-date</PublishedDate> <IsPremium>true|false</IsPremium> <PrivacyUri>uri-of-privacy-policy</PrivacyUri> <RecommendedVMSize>size-of-the-virtual-machine</RecommendedVMSize> <PublisherName>publisher-identifier</PublisherName> <PricingDetailLink>pricing-details</PricingDetailLink> <SmallIconUri>uri-of-icon</SmallIconUri> <Language>language-of-image</Language> </OSImage> … </Images>*/ IEnumerable<XElement> rootElements = Document.Element(GetSchema() + "Images") .Elements(GetSchema() + "OSImage"); foreach (XElement osDetail in rootElements) { //if (osDetail.Element(GetSchema() + "MediaLink") == null) //{ // continue; //} //if (osDetail.Element(GetSchema() + "MediaLink").Value == String.Empty) //{ // continue; //} if (osDetail.Element(GetSchema() + "ImageFamily") != null) { if (osDetail.Element(GetSchema() + "ImageFamily").Value.Contains("RightScale")) { continue; } } var imageProperties = new ImageProperties(); // get the affinity group if it exists if (osDetail.Element(GetSchema() + "Description") != null) { imageProperties.Description = osDetail.Element(GetSchema() + "Description").Value; } if (osDetail.Element(GetSchema() + "Name") != null) { imageProperties.Name = osDetail.Element(GetSchema() + "Name").Value; } if (osDetail.Element(GetSchema() + "Label") != null) { imageProperties.Label = osDetail.Element(GetSchema() + "Label").Value; } if (osDetail.Element(GetSchema() + "Eula") != null) { imageProperties.Eula = osDetail.Element(GetSchema() + "Eula").Value; } if (osDetail.Element(GetSchema() + "ImageFamily") != null) { imageProperties.ImageFamily = osDetail.Element(GetSchema() + "ImageFamily").Value; } if (osDetail.Element(GetSchema() + "IsPremium") != null) { imageProperties.IsPremium = bool.Parse(osDetail.Element(GetSchema() + "IsPremium").Value); } if (osDetail.Element(GetSchema() + "ShowInGui") != null) { imageProperties.ShowInGui = bool.Parse(osDetail.Element(GetSchema() + "ShowInGui").Value); } if (osDetail.Element(GetSchema() + "MediaLink") != null) { imageProperties.MediaLink = osDetail.Element(GetSchema() + "MediaLink").Value; } if (osDetail.Element(GetSchema() + "OS") != null) { imageProperties.OperatingSystem = (PlatformType)Enum.Parse(typeof(PlatformType), osDetail.Element(GetSchema() + "OS").Value); } if (osDetail.Element(GetSchema() + "PublishedDate") != null) { imageProperties.PublishedDate = DateTime.Parse(osDetail.Element(GetSchema() + "PublishedDate").Value); } CommandResponse.Add(imageProperties); } }