Inheritance: ICloudPageBlobObjectFactory
コード例 #1
0
        public AzureVMBackupBlobSasUris GenerateBlobSasUris(List<string> blobUris, CloudPageBlobObjectFactory cloudPageBlobObjectFactory)
        {
            AzureVMBackupBlobSasUris blobSASUris = new AzureVMBackupBlobSasUris();

            for (int i = 0; i < blobUris.Count; i++)
            {
                string blobUri = blobUris[i];
                BlobUri osBlobUri = null;
                if (BlobUri.TryParseUri(new Uri(blobUri), out osBlobUri))
                {
                    CloudPageBlob pageBlob = cloudPageBlobObjectFactory.Create(osBlobUri);

                    SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
                    sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(5);
                    sasConstraints.Permissions = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.List;

                    string sasUri = osBlobUri.Uri + pageBlob.GetSharedAccessSignature(sasConstraints);
                    blobSASUris.blobSASUri.Add(sasUri);
                }
                else
                {
                    throw new AzureVMBackupException(AzureVMBackupErrorCodes.WrongBlobUriFormat,"the blob uri is not in correct format.");
                }
            }
            return blobSASUris;
        }
コード例 #2
0
        /// <summary>
        /// we only support the Linux box now, if it's a windows, one AzureVMBackupException would be thrown.
        /// </summary>
        /// <param name="resourceGroupName"></param>
        /// <param name="vmName"></param>
        /// <param name="virtualMachineExtensionType"></param>
        /// <param name="location"></param>
        /// <param name="virtualMachineResponse"></param>
        /// <param name="snapshotTag"></param>
        /// <param name="virtualMachineExtensionBaseCmdlet"></param>
        public void CreateSnapshotForDisks(AzureVMBackupConfig vmConfig, string snapshotTag, VirtualMachineExtensionBaseCmdlet virtualMachineExtensionBaseCmdlet)
        {
            var virtualMachine = virtualMachineExtensionBaseCmdlet.ComputeClient.ComputeManagementClient.VirtualMachines.GetWithInstanceView(
                vmConfig.ResourceGroupName,
                vmConfig.VMName);
            StorageManagementClient storageClient = AzureSession.ClientFactory.CreateClient<StorageManagementClient>(virtualMachineExtensionBaseCmdlet.DefaultProfile.Context, AzureEnvironment.Endpoint.ResourceManager);

            StorageCredentialsFactory storageCredentialsFactory = new StorageCredentialsFactory(vmConfig.ResourceGroupName, storageClient, virtualMachineExtensionBaseCmdlet.DefaultProfile.Context.Subscription);

            CloudPageBlobObjectFactory cloudPageBlobObjectFactory = new CloudPageBlobObjectFactory(storageCredentialsFactory, TimeSpan.FromMinutes(1));

            List<string> vmPageBlobUris = this.GetDiskBlobUris(virtualMachine.Body);

            AzureVMBackupBlobSasUris blobSASUris = this.GenerateBlobSasUris(vmPageBlobUris, cloudPageBlobObjectFactory);

            string taskId = Guid.NewGuid().ToString();

            AzureVMBackupExtensionProtectedSettings privateConfig = new AzureVMBackupExtensionProtectedSettings();
            privateConfig.logsBlobUri = string.Empty;
            privateConfig.objectStr = this.GetBase64Encoding(blobSASUris);

            AzureVMBackupExtensionPublicSettings publicConfig = new AzureVMBackupExtensionPublicSettings();
            publicConfig.commandToExecute = backupSnapshotCommand;
            publicConfig.locale = backupDefaultLocale;
            publicConfig.commandStartTimeUTCTicks = DateTimeOffset.UtcNow.Ticks.ToString();
            publicConfig.taskId = taskId;
            AzureVMBackupMetadata backupMetadata = new AzureVMBackupMetadata();

            AzureVMBackupMetadataItem tagMetadataItem = new AzureVMBackupMetadataItem();
            tagMetadataItem.Key = backupExtensionMetadataName;
            tagMetadataItem.Value = snapshotTag;

            AzureVMBackupMetadataItem taskIdMetadataItem = new AzureVMBackupMetadataItem();
            taskIdMetadataItem.Key = backupExtensionIdentityMetadataName;
            taskIdMetadataItem.Value = taskId;

            backupMetadata.backupMetadata.Add(tagMetadataItem);
            backupMetadata.backupMetadata.Add(taskIdMetadataItem);

            publicConfig.objectStr = this.GetBase64Encoding(backupMetadata);

            VirtualMachineExtension vmExtensionParameters = new VirtualMachineExtension
            {
                Location = virtualMachine.Body.Location,
                Publisher = extensionPublisher,
                VirtualMachineExtensionType = extensionType,
                TypeHandlerVersion = extensionDefaultVersion,
                Settings = publicConfig,
                ProtectedSettings = privateConfig,
            };

            var vmBackupOperation = virtualMachineExtensionBaseCmdlet.VirtualMachineExtensionClient.CreateOrUpdate(
                vmConfig.ResourceGroupName,
                vmConfig.VMName,
                vmConfig.ExtensionName ?? backupExtensionName,
                vmExtensionParameters);

            // check the snapshots with the task id are all created.
            int timePeriod = 5000;
            int loopingTimes = ((int)TimeSpan.FromMinutes(10).TotalMilliseconds / timePeriod);

            Dictionary<string, string> snapshotQuery = new Dictionary<string, string>();
            snapshotQuery.Add(backupExtensionMetadataName, snapshotTag);
            snapshotQuery.Add(backupExtensionIdentityMetadataName, taskId);
            int i = 0;
            for (; i < loopingTimes; i++)
            {
                List<CloudPageBlob> snapshotsFound = this.FindSnapshot(vmPageBlobUris, snapshotQuery, storageCredentialsFactory);
                if (snapshotsFound.Count == vmPageBlobUris.Count)
                {
                    break;
                }
                else
                {
                    Thread.Sleep(timePeriod);
                }
            }
            if (i == loopingTimes)
            {
                throw new AzureVMBackupException(AzureVMBackupErrorCodes.TimeOut, "snapshot not created, or not found in time.");
            }
        }
コード例 #3
0
        public AzureVMBackupBlobSasUris GenerateBlobSasUris(List<string> blobUris, AzureContext azContext)
        {
            AzureVMBackupBlobSasUris blobSASUris = new AzureVMBackupBlobSasUris();

            for (int i = 0; i < blobUris.Count; i++)
            {
                string blobUri = blobUris[i];
                BlobUri osBlobUri = null;
                if (BlobUri.TryParseUri(new Uri(blobUri), out osBlobUri))
                {
                    StorageManagementClient storageClient = AzureSession.ClientFactory.CreateArmClient<StorageManagementClient>(azContext, AzureEnvironment.Endpoint.ResourceManager);

                    // Need to convert osBlobUri.StorageAccountName into corresponding resource group name

                    var listResponse = storageClient.StorageAccounts.List();
                    var account = listResponse.First(accTemp => accTemp.Name.Equals(osBlobUri.StorageAccountName, StringComparison.InvariantCultureIgnoreCase));
                    string resourceGroupName = GetResourceGroupFromId(account.Id);

                    StorageCredentialsFactory storageCredentialsFactory = new StorageCredentialsFactory(resourceGroupName, storageClient, azContext.Subscription);

                    CloudPageBlobObjectFactory cloudPageBlobObjectFactory = new CloudPageBlobObjectFactory(storageCredentialsFactory, TimeSpan.FromMinutes(1));

                    CloudPageBlob pageBlob = cloudPageBlobObjectFactory.Create(osBlobUri);

                    SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
                    sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(5);
                    sasConstraints.Permissions = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.List;

                    string sasUri = osBlobUri.Uri + pageBlob.GetSharedAccessSignature(sasConstraints);
                    blobSASUris.blobSASUri.Add(sasUri);
                    blobSASUris.pageBlobUri.Add(blobUri);
                    blobSASUris.storageCredentialsFactory.Add(storageCredentialsFactory);
                }
                else
                {
                    throw new AzureVMBackupException(AzureVMBackupErrorCodes.WrongBlobUriFormat, "the blob uri is not in correct format.");
                }
            }
            return blobSASUris;
        }