public static string Mount() { CloudStorageAccount storageAccount; if (RoleEnvironment.IsEmulated) { storageAccount = CloudStorageAccount.DevelopmentStorageAccount; } else { storageAccount = CloudStorageAccount.Parse(connectionString); } LocalResource localCache = RoleEnvironment.GetLocalResource("InstanceDriveCache"); CloudDrive.InitializeCache(localCache.RootPath, localCache.MaximumSizeInMegabytes); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); blobClient.GetContainerReference("drives").CreateIfNotExist(); myCloudDrive = storageAccount.CreateCloudDrive( blobClient .GetContainerReference("drives") .GetPageBlobReference("mysupercooldrive.vhd") .Uri.ToString() ); myCloudDrive.CreateIfNotExist(64); return(myCloudDrive.Mount(25, DriveMountOptions.None)); }
void Application_Start(object sender, EventArgs e) { // Code that runs on application startup BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterOpenAuth(); if (imageStorePath == null) { ImageStorePath = WebConfigurationManager.AppSettings["ImageStorePath"]; } // initialize storage account configuration setting publisher CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => { string connectionString = RoleEnvironment.GetConfigurationSettingValue(configName); configSetter(connectionString); }); try { // initialize the local cache for the Azure drive LocalResource cache = RoleEnvironment.GetLocalResource("LocalDriveCache"); CloudDrive.InitializeCache(cache.RootPath + "cache", cache.MaximumSizeInMegabytes); // retrieve storage account CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString"); // retrieve URI for the page blob that contains the cloud drive from configuration settings string imageStoreBlobUri = RoleEnvironment.GetConfigurationSettingValue("ImageStoreBlobUri"); // unmount any previously mounted drive. foreach (var drive in CloudDrive.GetMountedDrives()) { var mountedDrive = new CloudDrive(drive.Value, account.Credentials); mountedDrive.Unmount(); } // create the Windows Azure drive and its associated page blob CloudDrive imageStoreDrive = account.CreateCloudDrive(imageStoreBlobUri); if (CloudDrive.GetMountedDrives().Count == 0) { try { imageStoreDrive.Create(16); } catch (CloudDriveException) { // drive already exists } } // mount the drive and initialize the application with the path to the image store on the Azure drive Global.ImageStorePath = imageStoreDrive.Mount(cache.MaximumSizeInMegabytes / 2, DriveMountOptions.None); } catch (CloudDriveException driveException) { Trace.WriteLine("Error: " + driveException.Message); } }
public override bool OnStart() { Trace.WriteLine("WebRole.OnStart", "Error"); CloudStorageAccount storageAccount; try { FUSE.Weld.Azure.Configuration.SetConfigurationSettingPublisher(); storageAccount = Utility.GetStorageAccount(); Trace.WriteLine("WebRole.OnStart: Initializing Cache", "Verbose"); var localCache = RoleEnvironment.GetLocalResource("DriveCache"); CloudDrive.InitializeCache(localCache.RootPath, localCache.MaximumSizeInMegabytes); Trace.WriteLine("WebRole.OnStart: Creating Drive", "Verbose"); drive = new CloudDrive(new Uri(storageAccount.BlobEndpoint + "root/proxy.vhd"), storageAccount.Credentials); drive.CreateIfNotExist(10 * 1000); Trace.WriteLine("WebRole.OnStart: Mounting Drive", "Verbose"); var cloudDriveLetter = drive.Mount(localCache.MaximumSizeInMegabytes, DriveMountOptions.Force); } catch (Exception x) { Trace.TraceError("WebRole.OnStart:\n" + x.ToString()); } return(base.OnStart()); }
private void MountCloudDrive() { LocalResource localCache; try { Log.Info("Mounting CloudDrive..."); localCache = RoleEnvironment.GetLocalResource("RavenCache"); Log.Info("Local cache: RootPath = {0}, Size = {1}", localCache.RootPath, localCache.MaximumSizeInMegabytes); CloudDrive.InitializeCache(localCache.RootPath.TrimEnd('\\'), localCache.MaximumSizeInMegabytes); Log.Info("Local cache initialized."); //var ravenDataStorageAccount = CloudStorageAccount.DevelopmentStorageAccount; var ravenDataStorageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageAccount")); var blobClient = ravenDataStorageAccount.CreateCloudBlobClient(); var ravenDrives = blobClient.GetContainerReference("ravendrives"); ravenDrives.CreateIfNotExist(); var vhdUrl = blobClient.GetContainerReference("ravendrives").GetPageBlobReference("RavenData.vhd").Uri.ToString(); Log.Info("CloudDrive Blob URL: {0}", vhdUrl); BlobContainerPermissions permissions = ravenDrives.GetPermissions(); permissions.PublicAccess = BlobContainerPublicAccessType.Container; ravenDrives.SetPermissions(permissions); Log.Info("Permissions set"); _ravenDataDrive = ravenDataStorageAccount.CreateCloudDrive(vhdUrl); } catch (Exception ex) { Log.Error("{0}: {1}", ex.GetType().Name, ex.Message); throw; } try { _ravenDataDrive.Create(localCache.MaximumSizeInMegabytes); Log.Info("CloudDrive instance created"); } catch (CloudDriveException ex) { Log.Error("ravenDataDrive.Create threw exception: " + ex.Message); } _ravenDrivePath = _ravenDataDrive.Mount(localCache.MaximumSizeInMegabytes, DriveMountOptions.Force); Log.Info("Drive mounted as {0}", _ravenDrivePath); if (!Directory.Exists(Path.Combine(_ravenDrivePath, "Raven"))) { Directory.CreateDirectory(Path.Combine(_ravenDrivePath, "Raven")); } }
public static void PrepareLocalReadCache(string localResourceName) { // Get a reference to the local resource. LocalResource localResource = RoleEnvironment.GetLocalResource(localResourceName); if (localResource == null) { throw new ArgumentException("Local resource not found"); } // Initialize the drive cache. CloudDrive.InitializeCache(localResource.RootPath, localResource.MaximumSizeInMegabytes); }
/// <summary> /// /// </summary> /// <param name="csa"></param> /// <param name="vhdUri"></param> /// <param name="azureDriveCacheDirName"></param> /// <param name="azureDriveCacheSizeInMB"></param> /// <param name="azureDriveSizeInMB"></param> /// <param name="driveOptions"></param> /// <returns></returns> public static string MountAzureDrive(CloudStorageAccount csa, Uri vhdUri, string azureDriveCacheDirName, int azureDriveCacheSizeInMB, int azureDriveSizeInMB, DriveMountOptions driveOptions, out bool wasAlreadyMounted) { wasAlreadyMounted = false; // Initialize the cache for mounting the drive. CloudDrive.InitializeCache(azureDriveCacheDirName, azureDriveCacheSizeInMB); CloudDrive drive = null; drive = new CloudDrive(vhdUri, csa.Credentials); Uri driveUri = CloudDrive.GetMountedDrives().Values.Where(tuple => tuple == vhdUri).FirstOrDefault(); // Find out whether the drive has already been mounted by some other instance. if (driveUri == null) { try { // Create the drive. Currently no method is provided to verify whether the // drive is already created or not. drive.Create(azureDriveSizeInMB); } catch (CloudDriveException) { // An exception can be thrown if the drive already exists. Hence ignore the // exception here. If anything is not right, the Mount() will fail. } // Mount the drive. string driveLetter = drive.Mount(azureDriveCacheSizeInMB, driveOptions); return(driveLetter); } else { //Drive is already mounted. So get the drive letter for the drive IDictionary <string, Uri> drives = CloudDrive.GetMountedDrives(); IEnumerator <KeyValuePair <string, Uri> > e = drives.GetEnumerator(); while (e.MoveNext()) { if (e.Current.Value == vhdUri) { wasAlreadyMounted = true; return(e.Current.Key); } } } throw new Exception("Unable to mount the drive." + vhdUri.ToString()); }
private String CreateSolrStorageVhd() { CloudStorageAccount storageAccount; LocalResource localCache; CloudBlobClient client; CloudBlobContainer drives; // get the version of solr we are using _solrVersion = Decimal.Parse(RoleEnvironment.GetConfigurationSettingValue("SolrVersion")); localCache = RoleEnvironment.GetLocalResource("AzureDriveCache"); Log(String.Format("AzureDriveCache {0} {1} MB", localCache.RootPath, localCache.MaximumSizeInMegabytes - 50), "Information"); CloudDrive.InitializeCache(localCache.RootPath.TrimEnd('\\'), localCache.MaximumSizeInMegabytes - 50); storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString")); client = storageAccount.CreateCloudBlobClient(); string roleId = RoleEnvironment.CurrentRoleInstance.Id; string containerAddress = ContainerNameFromRoleId(roleId); drives = client.GetContainerReference(containerAddress); try { drives.CreateIfNotExist(); } catch { }; var vhdUrl = client.GetContainerReference(containerAddress).GetBlobReference("SolrStorage.vhd").Uri.ToString(); Log(String.Format("SolrStorage.vhd {0}", vhdUrl), "Information"); _solrStorageDrive = storageAccount.CreateCloudDrive(vhdUrl); int cloudDriveSizeInMB = int.Parse(RoleEnvironment.GetConfigurationSettingValue("CloudDriveSize")); try { _solrStorageDrive.Create(cloudDriveSizeInMB); } catch (CloudDriveException) { } Log(String.Format("CloudDriveSize {0} MB", cloudDriveSizeInMB), "Information"); var dataPath = _solrStorageDrive.Mount(localCache.MaximumSizeInMegabytes - 50, DriveMountOptions.Force); Log(String.Format("Mounted as {0}", dataPath), "Information"); return(dataPath); }
private String CreateElasticStorageVhd() { Log("ElasticSearch - creating VHD", "Information"); var localCache = RoleEnvironment.GetLocalResource("ESLocation"); Log(String.Format("ESLocation {0} {1} MB", localCache.RootPath, localCache.MaximumSizeInMegabytes - 50), "Information"); CloudDrive.InitializeCache(localCache.RootPath.TrimEnd('\\'), localCache.MaximumSizeInMegabytes - 50); var storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString")); var client = storageAccount.CreateCloudBlobClient(); var roleId = RoleEnvironment.CurrentRoleInstance.Id; var containerAddress = ContainerNameFromRoleId(roleId); var drives = client.GetContainerReference(containerAddress); try { drives.CreateIfNotExist(); } catch { }; var vhdUrl = client.GetContainerReference(containerAddress).GetBlobReference("ElasticStorage.vhd").Uri.ToString(); Log(String.Format("ElasticStorage.vhd {0}", vhdUrl), "Information"); _elasticStorageDrive = storageAccount.CreateCloudDrive(vhdUrl); int cloudDriveSizeInMb = int.Parse(RoleEnvironment.GetConfigurationSettingValue("CloudDriveSize")); try { _elasticStorageDrive.Create(cloudDriveSizeInMb); } catch (CloudDriveException) { } Log(String.Format("CloudDriveSize {0} MB", cloudDriveSizeInMb), "Information"); //var dataPath = _elasticStorageDrive.Mount(localCache.MaximumSizeInMegabytes - 50, DriveMountOptions.Force); var dataPath = _elasticStorageDrive.Mount(localCache.MaximumSizeInMegabytes - 50, DriveMountOptions.Force); Log(String.Format("Mounted as {0}", dataPath), "Information"); Log("ElasticSearch - created VHD", "Information"); return(dataPath); }
private void OnStartInternal() { try { // initialize the drive cache string cachePath = RoleEnvironment.GetLocalResource("Data").RootPath; CloudDrive.InitializeCache(cachePath, 4096); this.EventLog.WriteEntry("initialization succeeded"); // mount the current drive this.currentDrive = MountDrive(); // configure IIS ConfigureWebServer(this.currentDrive.LocalPath); this.busy = false; } catch (Exception ex) { this.EventLog.WriteEntry(ex.ToString(), EventLogEntryType.Error); throw; } }
//================================================================================= // // PRIVATE METHODS // //================================================================================= /// <summary> /// The actual backup logic itself. /// We mount the VHD snapshot, then TAR and copy the contents to a new blob. /// </summary> private void Run() { CloudDrive snapshottedDrive = null; bool mountedSnapshot = false; try { Log("Backup started for " + UriToBackup + "..."); // Set up the cache, storage account, and blob client. Log("Getting the cache..."); var localResource = RoleEnvironment.GetLocalResource(Constants.BackupLocalStorageName); Log("Initializing the cache..."); CloudDrive.InitializeCache(localResource.RootPath, localResource.MaximumSizeInMegabytes); Log("Setting up storage account..."); var storageAccount = CloudStorageAccount.Parse(Credential); var client = storageAccount.CreateCloudBlobClient(); // Mount the snapshot. Log("Mounting the snapshot..."); snapshottedDrive = new CloudDrive(UriToBackup, storageAccount.Credentials); string driveLetter = snapshottedDrive.Mount(0, DriveMountOptions.None); mountedSnapshot = true; Log("...snapshot mounted to " + driveLetter); // Create the destination blob. Log("Opening (or creating) the backup container..."); CloudBlobContainer backupContainer = client.GetContainerReference(BackupContainerName); backupContainer.CreateIfNotExist(); var blobFileName = String.Format(Constants.BackupFormatString, DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute); var blob = backupContainer.GetBlobReference(blobFileName); // Write everything in the mounted snapshot, to the TarWriter stream, to the BlobStream, to the blob. Log("Backing up:\n\tpath: " + driveLetter + "\n\tto blob: " + blobFileName + "\n"); using (var outputStream = blob.OpenWrite()) { using (var tar = new TarWriter(outputStream)) { Log("Writing to the blob/tar..."); AddAllToTar(driveLetter, tar); } } // Set the blob's metadata. Log("Setting the blob's metadata..."); blob.Metadata["FileName"] = blobFileName; blob.Metadata["Submitter"] = "BlobBackup"; blob.SetMetadata(); Log("Unmounting the drive..."); // Keep this here because we want "terminating now" to be the last log event in a failure. } catch (Exception e) { Log("========================="); Log("FAILURE: " + e.Message); Log(e.StackTrace); Log(""); Log("Terminating now."); } finally { // Unmount the drive. if (mountedSnapshot) { snapshottedDrive.Unmount(); } DateFinished = DateTime.Now; } }
internal static string GetMountedPathFromBlob( string localCachePath, string containerName, string blobName, int driveSize, out CloudDrive elasticDrive) { DiagnosticsHelper.TraceInformation( "In mounting cloud drive for on {0} with {1}", containerName, blobName); var connectionString = RoleEnvironment.GetConfigurationSettingValue("DataConnectionString"); connectionString = connectionString.Replace("DefaultEndpointsProtocol=https", "DefaultEndpointsProtocol=http"); var storageAccount = CloudStorageAccount.Parse(connectionString); var blobClient = storageAccount.CreateCloudBlobClient(); DiagnosticsHelper.TraceInformation("Get container"); // this should be the name of your replset var driveContainer = blobClient.GetContainerReference(containerName); // create blob container (it has to exist before creating the cloud drive) try { driveContainer.CreateIfNotExist(); } catch (StorageException e) { DiagnosticsHelper.TraceInformation( "Container creation failed with {0} {1}", e.Message, e.StackTrace); } var blobUri = blobClient.GetContainerReference(containerName).GetPageBlobReference(blobName).Uri.ToString(); DiagnosticsHelper.TraceInformation("Blob uri obtained {0}", blobUri); // create the cloud drive elasticDrive = storageAccount.CreateCloudDrive(blobUri); try { elasticDrive.CreateIfNotExist(driveSize); } catch (CloudDriveException e) { DiagnosticsHelper.TraceInformation( "Drive creation failed with {0} {1}", e.Message, e.StackTrace); } DiagnosticsHelper.TraceInformation("Initialize cache"); var localStorage = RoleEnvironment.GetLocalResource(localCachePath); CloudDrive.InitializeCache(localStorage.RootPath.TrimEnd('\\'), localStorage.MaximumSizeInMegabytes); // mount the drive and get the root path of the drive it's mounted as try { DiagnosticsHelper.TraceInformation( "Trying to mount blob as azure drive"); var driveLetter = elasticDrive.Mount(localStorage.MaximumSizeInMegabytes, DriveMountOptions.None); DiagnosticsHelper.TraceInformation( "Write lock acquired on azure drive, mounted as {0}", driveLetter); return driveLetter; } catch (CloudDriveException e) { DiagnosticsHelper.TraceCritical( "Failed to mount cloud drive with {0} {1}", e.Message, e.StackTrace); throw; } }
private void OnInitialize() { var selfInstance = InstanceEnumerator.EnumerateInstances().First(i => i.IsSelf); cloudStorageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(ConfigurationSettingsKeys.StorageConnectionString)); log.Info("Storage account selected: {0}", cloudStorageAccount.BlobEndpoint); cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient(); log.Info("Storage client created"); var containerName = ConfigurationProvider.GetSetting(ConfigurationSettingsKeys.StorageContainerName, "ravendb"); // In order to force a connection we just enumerate all available containers: var availableContainers = cloudBlobClient.ListContainers().ToArray(); foreach (var container in availableContainers) { log.Info("Available container: {0}", container.Name); } if (!availableContainers.Any(c => c.Name.Equals(containerName))) { log.Info("Container {0} does not exist, creating", containerName); // Container does not exist: cloudBlobClient.GetContainerReference(containerName).Create(); } cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName); log.Info("Container {0} selected", cloudBlobContainer.Name); localCache = RoleEnvironment.GetLocalResource(ConfigurationSettingsKeys.StorageCacheResource); log.Info("Cache resource retrieved: {0}, path: {1}", localCache.Name, localCache.RootPath); CloudDrive.InitializeCache(localCache.RootPath, localCache.MaximumSizeInMegabytes); log.Info("Cache initialized: {0} mb", localCache.MaximumSizeInMegabytes); var driveName = string.Format("{0}{1}.vhd", selfInstance.InstanceType, selfInstance.InstanceIndex).ToLowerInvariant(); log.Info("Virtual drive name: {0}", driveName); var pageBlob = cloudBlobContainer.GetPageBlobReference(driveName); log.Info("Virtual drive blob: {0}", pageBlob.Uri); cloudDrive = cloudStorageAccount.CreateCloudDrive(pageBlob.Uri.ToString()); log.Info("Virtual drive created: {0}", cloudDrive.Uri); var storageSize = ConfigurationProvider.GetSetting(ConfigurationSettingsKeys.StorageSize, 50000); log.Info("Storage size: {0} mb", storageSize); cloudDrive.CreateIfNotExist(storageSize); log.Info("Virtual drive initialized: {0}", cloudDrive.Uri); var mountedDirectoryPath = cloudDrive.Mount(storageSize, DriveMountOptions.None); log.Info("Virtual drive mounted at: {0}", mountedDirectoryPath); mountedDirectory = new DirectoryInfo(mountedDirectoryPath); log.Info("Ensuring drive is available: {0}", mountedDirectoryPath); UpdateTestFile(); log.Info("Storage initialization succeeded"); }
internal static string GetMountedPathFromBlob( string localCachePath, string cloudDir, string containerName, string blobName, int driveSize, out CloudDrive mongoDrive) { DiagnosticsHelper.TraceInformation(string.Format("In mounting cloud drive for dir {0} on {1} with {2}", cloudDir, containerName, blobName)); CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting(cloudDir); var blobClient = storageAccount.CreateCloudBlobClient(); DiagnosticsHelper.TraceInformation("Get container"); // this should be the name of your replset var driveContainer = blobClient.GetContainerReference(containerName); // create blob container (it has to exist before creating the cloud drive) try { driveContainer.CreateIfNotExist(); } catch (Exception e) { DiagnosticsHelper.TraceInformation("Exception when creating container"); DiagnosticsHelper.TraceInformation(e.Message); DiagnosticsHelper.TraceInformation(e.StackTrace); } var mongoBlobUri = blobClient.GetContainerReference(containerName).GetPageBlobReference(blobName).Uri.ToString(); DiagnosticsHelper.TraceInformation(string.Format("Blob uri obtained {0}", mongoBlobUri)); // create the cloud drive mongoDrive = storageAccount.CreateCloudDrive(mongoBlobUri); try { mongoDrive.Create(driveSize); } catch (Exception e) { // exception is thrown if all is well but the drive already exists DiagnosticsHelper.TraceInformation("Exception when creating cloud drive. safe to ignore"); DiagnosticsHelper.TraceInformation(e.Message); DiagnosticsHelper.TraceInformation(e.StackTrace); } DiagnosticsHelper.TraceInformation("Initialize cache"); var localStorage = RoleEnvironment.GetLocalResource(localCachePath); CloudDrive.InitializeCache(localStorage.RootPath.TrimEnd('\\'), localStorage.MaximumSizeInMegabytes); // mount the drive and get the root path of the drive it's mounted as try { DiagnosticsHelper.TraceInformation(string.Format("Trying to mount blob as azure drive on {0}", RoleEnvironment.CurrentRoleInstance.Id)); var driveLetter = mongoDrive.Mount(localStorage.MaximumSizeInMegabytes, DriveMountOptions.None); DiagnosticsHelper.TraceInformation(string.Format("Write lock acquired on azure drive, mounted as {0}, on role instance", driveLetter, RoleEnvironment.CurrentRoleInstance.Id)); return(driveLetter); } catch (Exception e) { DiagnosticsHelper.TraceWarning("could not acquire blob lock."); DiagnosticsHelper.TraceWarning(e.Message); DiagnosticsHelper.TraceWarning(e.StackTrace); throw; } }
private void InitializeLocalCache() { _localCache = RoleEnvironment.GetLocalResource("RavenCache"); CloudDrive.InitializeCache(_localCache.RootPath.TrimEnd('\\'), _localCache.MaximumSizeInMegabytes); }