Defines a class which can perform operations on blobs
Наследование: IStorageClient
 /// <summary>
 /// Constructor used to set the manager reference 
 /// </summary>
 internal StorageActivity(StorageManager manager)
 {
     Manager = manager;
     _client = new StorageClient(manager.SubscriptionId, manager.ManagementCertificate);
 }
        public string ExportToStorageAccount(string account, string container, string folder)
        {
            if(String.IsNullOrEmpty(SubscriptionId) && ManagementCertificate == null)
                throw new FluentManagementException("please provide a subscription id and management certificate to continue", "CertificateGenerator");

            var client = new StorageClient(SubscriptionId, ManagementCertificate);
            var storageKeys = client.GetStorageAccountKeys(account);
            var key = storageKeys[0];

            var credentials = new CloudStorageAccount(new StorageCredentials(account, key), true);
            var blobClient = credentials.CreateCloudBlobClient();
            var blobContainer = blobClient.GetContainerReference(container);
            blobContainer.CreateIfNotExists();

            var pemBlob = blobContainer.GetBlockBlobReference(String.Format("{0}/{1}.{2}.pem", folder, DerEncodedCertificate.Thumbprint, DateTime.UtcNow.ToString("ddMMyyyy")));
            pemBlob.UploadText(GetPemData());

            var pfxBlob = blobContainer.GetBlockBlobReference(String.Format("{0}/{1}.{2}.pfx", folder, DerEncodedCertificate.Thumbprint, DateTime.UtcNow.ToString("ddMMyyyy")));
            pfxBlob.UploadFromByteArray(GetPfxData(), 0, GetPfxData().Count());

            var cerBlob = blobContainer.GetBlockBlobReference(String.Format("{0}/{1}.{2}.cer", folder, DerEncodedCertificate.Thumbprint, DateTime.UtcNow.ToString("ddMMyyyy")));
            cerBlob.UploadFromByteArray(GetCerData(), 0, GetCerData().Count());

            return String.Format("http://{0}.blob.core.windows.net/{1}/{2}/{3}.{4}", account, container, folder,
                DerEncodedCertificate.Thumbprint, DateTime.UtcNow.ToString("ddMMyyyy"));
        }
        public void CreateStorageClient()
        {
            storageClient = new StorageClient(subscriptionId, certificate);

            Assert.IsNotNull(storageClient);
        }
        /// <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");
        }
 void IBuilder.SpinUp()
 {
     var storageClient = new StorageClient(_subscriptionId, _certificate);
     storageClient.CreateNewStorageAccountIfNotExists(_storageAccountName, _storageLocationName);
     var client = new WindowsVirtualMachineClient(_subscriptionId, _certificate);
     var newClient = client.CreateNewVirtualMachineFromTemplateGallery(_properties);
     Console.WriteLine("Virtual machine now created - with diskname {0}", newClient.VirtualMachine.OSHardDisk.DiskName);
     Console.WriteLine("Getting and saving RD file");
     client.SaveRemoteDesktopFile(_rdpFile);
 }
Пример #6
0
 public void PostProcess()
 {
     var extractor = new PublishSettingsExtractor(PublishSettings);
     var client = new StorageClient(SubscriptionId, extractor.AddPublishSettingsToPersonalMachineStore());
     var keys = client.GetStorageAccountKeys(StorageName);
     ConnectionString = String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", StorageName, keys[0]);
     // put a container create process here - add this method to fluent!!!
     var blobClient = new BlobClient(SubscriptionId, "packages", StorageName, keys[0]);
     blobClient.CreatBlobContainer();
 }
Пример #7
0
        /// <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");
        }