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); }
private CloudDrive MountDrive() { // create or mount an instance-specific drive var credentials = GetStorageCredentials(); var driveUri = GetDriveUri(); var drive = new CloudDrive(driveUri, credentials); try { drive.Create(1024); } catch (Exception ex) { if (ex.Message != "ERROR_BLOB_ALREADY_EXISTS") { throw; } } // mount the drive string mountPoint = drive.Mount(1024, DriveMountOptions.FixFileSystemErrors | DriveMountOptions.Force); this.EventLog.WriteEntry(string.Format("{0} mounted at {1}", drive.Uri, mountPoint)); return(drive); }
public static MountCloudDriveReturnValue ExecuteMethod_MountDrive(CloudDrive driveReference) { Exception exception = null; string driveLetter = null; try { const int CacheSizeMB = 1024; driveLetter = driveReference.Mount(CacheSizeMB, DriveMountOptions.None); DateTime nowUtc = DateTime.UtcNow; string refText = nowUtc.ToString(); string newFileName = Path.Combine(driveLetter, "Testfile.txt"); File.WriteAllText(newFileName, refText); string testContent = File.ReadAllText(newFileName); if (testContent != refText) throw new InvalidDataException("CloudDrive write/read is not matching original data"); } catch (Exception ex) { exception = ex; } return new MountCloudDriveReturnValue { MountedDriveLetter = driveLetter, Exception = exception }; }
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)); }
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(); }
static void Main(string[] args) { try { string BlobURL = "deploy/Testdrivesnapshot.vhd"; CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=http;AccountName=mongodbwest2;AccountKey=wZcR60wAy+zltHPV7CXJsvBo/rnZHV2FIqg+UA+H1pIhkYl4j0qRZ+GgI5V8IJhngh2DOxI+sS46KddPFWg0Xw=="); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); //Get a reference to a blob. CloudBlob blob = blobClient.GetBlobReference(BlobURL); //Take a snapshot of the blob. CloudBlob snapshot = blob.CreateSnapshot(); //Get the snapshot timestamp. DateTime timestamp = (DateTime)snapshot.Attributes.Snapshot; //Use the timestamp to get a second reference to the snapshot. CloudBlob snapshot2 = new CloudBlob(BlobURL, timestamp, blobClient); CloudDrive Snapshotdrive = new CloudDrive(snapshot2.Uri, storageAccount.Credentials); string path = Snapshotdrive.Mount(0, DriveMountOptions.None); Console.WriteLine("Mounted on " + path); Console.ReadLine(); Snapshotdrive.Unmount(); } catch (Exception ex) { Console.WriteLine(string.Format("Exception : {0} ({1}) at {2}", ex.Message, ex.InnerException == null ? "" : ex.InnerException.Message, ex.StackTrace)); } }
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()); }
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 static string MountDrive(string containerName, string vhdName, int driveSize, int driveLocalReadCacheSize) { var client = GetCloudClientInstance(); // Create the container for the drive if it does not already exist. var container = new CloudBlobContainer(containerName, client); if (container.CreateIfNotExist()) { container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Off }); } var cloudDrive = new CloudDrive(container.GetPageBlobReference(vhdName).Uri, client.Credentials); try { cloudDrive.Create(driveSize); } catch (CloudDriveException ex) { Logger.Info(string.Format("Cloud drive already exists. Uri: {0}", cloudDrive.Uri), ex); } string pathToDrive = cloudDrive.Mount(driveLocalReadCacheSize, DriveMountOptions.Force); return(pathToDrive); }
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 string GetMountedPathFromBlob(IStorageConfiguration config, string localCachePath, string cloudDir, string containerName, string blobName, int driveSize, out CloudDrive drive) { Diagnostics.TraceInformation(string.Format("In mounting cloud drive for dir {0} on {1} with {2}", cloudDir, containerName, blobName)); var blobClient = config.Account.CreateCloudBlobClient(); Diagnostics.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) { Diagnostics.Trace("Exception when creating container", e); } var blobUri = blobClient.GetContainerReference(containerName).GetPageBlobReference(blobName).Uri.ToString(); Diagnostics.TraceInformation(string.Format("Blob uri obtained {0}", blobUri)); // create the cloud drive drive = config.Account.CreateCloudDrive(blobUri); try { drive.Create(driveSize); } catch {} Diagnostics.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 Diagnostics.TraceInformation(string.Format("Trying to mount blob as azure drive on {0}", RoleEnvironment.CurrentRoleInstance.Id)); var driveLetter = drive.Mount(localStorage.MaximumSizeInMegabytes, DriveMountOptions.None); Diagnostics.TraceInformation(string.Format("Write lock acquired on azure drive, mounted as {0}, on role instance {1}", driveLetter, RoleEnvironment.CurrentRoleInstance.Id)); return driveLetter; }
/// <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()); }
public string MountDrive(string storageConnectionString, string blobRelativePath, string localResourceName, int sizeInMb) { var account = CloudStorageAccount.FromConfigurationSetting(storageConnectionString.Replace("DefaultEndpointsProtocol=https", "DefaultEndpointsProtocol=http")); var blobUri = GetBlobUri(account, blobRelativePath); var localCache = _localResourceManager.GetByConfigName(localResourceName); CloudDrive.InitializeCache(localCache.RootPath, localCache.MaximumSizeInMegabytes); var drive = new CloudDrive(blobUri, account.Credentials); drive.CreateIfNotExist(sizeInMb); var driveRootPath = drive.Mount(localCache.MaximumSizeInMegabytes, DriveMountOptions.Force); return driveRootPath; }
public string MountCloudDrive() { _blobClient = _ravenDataStorageAccount.CreateCloudBlobClient(); InitializeLocalCache(); CreateDrivesContainer(); SetContainerPermissions(); CreateDrive(); var ravenDrivePath = _cloudDrive.Mount(_localCache.MaximumSizeInMegabytes, DriveMountOptions.Force); return(ravenDrivePath); }
public override bool OnStart() { // 構成設定パブリッシャの初期設定 CloudStorageAccount.SetConfigurationSettingPublisher( (configName, configSettingPublisher) => { var connectionString = RoleEnvironment.GetConfigurationSettingValue(configName); configSettingPublisher(connectionString); } ); CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting("StorageConnectionString"); // ローカルキャッシュの設定 LocalResource localCache = RoleEnvironment.GetLocalResource("LocalStorage1"); // 【#1】Windows Azure ドライブのマウント CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer blobContainer = blobClient.GetContainerReference("trac"); azureDrive = storageAccount.CreateCloudDrive(blobContainer.GetPageBlobReference("disk.vhd").Uri.ToString()); string driveLetter = azureDrive.Mount(0, DriveMountOptions.Force); // 固定用ドライブレターを割り当てる // 今回は Z: ドライブに再接続 string diskpartFile = Path.Combine(localCache.RootPath, "diskpart.txt"); string diskpartCommand = string.Format("select volume = {0} {1} assign letter = {2}", driveLetter, Environment.NewLine, "Z:"); File.WriteAllText(diskpartFile, diskpartCommand); string exe = Path.Combine(System.Environment.GetEnvironmentVariable("WINDIR"), @"System32\diskpart.exe"); //Diskpartの実行 Process p = new Process(); p.StartInfo.FileName = exe; p.StartInfo.Arguments = "/S " + diskpartFile; p.StartInfo.CreateNoWindow = true; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardError = true; p.Start(); string results = p.StandardError.ReadToEnd(); p.WaitForExit(60000); p.Close(); return base.OnStart(); }
protected void NewDrive_Click(object sender, EventArgs e) { if (RoleEnvironment.IsAvailable) { // retrieve storage account CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString"); // build page blob URI for the new cloud drive by changing the extension in the original URI string imageStoreBlobUri = RoleEnvironment.GetConfigurationSettingValue("ImageStoreBlobUri"); string cloneStoreBlobUri = Path.ChangeExtension(imageStoreBlobUri, "bak"); // create drive and its associated page blob CloudDrive clonedDrive = account.CreateCloudDrive(cloneStoreBlobUri); if (this.MountedDrives.Items.Count < 2) { try { clonedDrive.Create(16); } catch (CloudDriveException) { // cloud drive already exists } // mount the drive and retrieve its path LocalResource cache = RoleEnvironment.GetLocalResource("LocalDriveCache"); string clonedStorePath = clonedDrive.Mount(cache.MaximumSizeInMegabytes / 2, DriveMountOptions.None); // copy the contents from the original drive to the new drive foreach (string sourceFileName in Directory.GetFiles(Global.ImageStorePath, "*.*").Where(name => name.EndsWith(".jpg") || name.EndsWith(".png"))) { string destinationFileName = Path.Combine(clonedStorePath, Path.GetFileName(sourceFileName)); File.Copy(sourceFileName, destinationFileName, true); } this.SelectImageStore(clonedStorePath); } else { clonedDrive.Unmount(); clonedDrive.Delete(); this.SelectImageStore(Global.ImageStorePath); } } }
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"); CloudStorageAccount storageAccount; LocalResource localCache; CloudBlobClient client; CloudBlobContainer drives; 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); 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("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 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); }
public static string MountDrive(string containerName, string vhdName, int driveSize, int driveLocalReadCacheSize) { var client = GetCloudClientInstance(); // Create the container for the drive if it does not already exist. var container = new CloudBlobContainer(containerName, client); if (container.CreateIfNotExist()) { container.SetPermissions(new BlobContainerPermissions {PublicAccess = BlobContainerPublicAccessType.Off}); } var cloudDrive = new CloudDrive(container.GetPageBlobReference(vhdName).Uri, client.Credentials); try { cloudDrive.Create(driveSize); } catch (CloudDriveException ex) { Logger.Info(string.Format("Cloud drive already exists. Uri: {0}", cloudDrive.Uri), ex); } string pathToDrive = cloudDrive.Mount(driveLocalReadCacheSize, DriveMountOptions.Force); return pathToDrive; }
private string GetMountedPathFromBlob( string localCachePath, string cloudDir, string containerName, string blobName, int driveSize, bool waitOnMount, out CloudDrive bermudaDrive) { Trace.TraceInformation(string.Format("In mounting cloud drive for dir {0}", cloudDir)); var storageAccount = CloudStorageAccount; var blobClient = CloudBlobClient; Trace.TraceInformation("Get container"); var driveContainer = blobClient.GetContainerReference(containerName); // create blob container (it has to exist before creating the cloud drive) try { driveContainer.CreateIfNotExist(); } catch (Exception e) { Trace.TraceInformation("Exception when creating container"); Trace.TraceInformation(e.Message); Trace.TraceInformation(e.StackTrace); } var bermudaBlobUri = blobClient.GetContainerReference(containerName).GetPageBlobReference(blobName).Uri.ToString(); Trace.TraceInformation(string.Format("Blob uri obtained {0}", bermudaBlobUri)); // create the cloud drive bermudaDrive = storageAccount.CreateCloudDrive(bermudaBlobUri); try { bermudaDrive.CreateIfNotExist(driveSize); } catch (Exception e) { // exception is thrown if all is well but the drive already exists Trace.TraceInformation("Exception when creating cloud drive. safe to ignore"); Trace.TraceInformation(e.Message); Trace.TraceInformation(e.StackTrace); } //Trace.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 if (!waitOnMount) { try { Trace.TraceInformation(string.Format("Trying to mount blob as azure drive on {0}", RoleEnvironment.CurrentRoleInstance.Id)); var driveLetter = bermudaDrive.Mount(0, DriveMountOptions.None); Trace.TraceInformation(string.Format("Write lock acquired on azure drive, mounted as {0}, on role instance", driveLetter, RoleEnvironment.CurrentRoleInstance.Id)); return driveLetter; } catch (Exception e) { Trace.TraceWarning("could not acquire blob lock."); Trace.TraceWarning(e.Message); Trace.TraceWarning(e.StackTrace); throw; } } else { string driveLetter; Trace.TraceInformation(string.Format("Trying to mount blob as azure drive on {0}", RoleEnvironment.CurrentRoleInstance.Id)); while (true) { try { driveLetter = bermudaDrive.Mount(0,DriveMountOptions.None); Trace.TraceInformation(string.Format("Write lock acquired on azure drive, mounted as {0}, on role instance", driveLetter, RoleEnvironment.CurrentRoleInstance.Id)); return driveLetter; } catch { } Thread.Sleep(MountSleep); } } }
private void MountCloudDrive() { Trace.TraceInformation("Cloud Drive: Mounting..."); CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting("StorageAccount"); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); LocalResource localCache = RoleEnvironment.GetLocalResource("RavenCache"); CloudDrive.InitializeCache(localCache.RootPath.TrimEnd('\\'), localCache.MaximumSizeInMegabytes); var containerName = RoleEnvironment.GetConfigurationSettingValue("CloudDriveContainer"); Trace.TraceInformation("Cloud Drive: Container = {0}", containerName); blobClient.GetContainerReference(containerName).CreateIfNotExist(); var vhdName = RoleEnvironment.CurrentRoleInstance.Id.Replace(RoleEnvironment.DeploymentId + ".", "") + ".ravendata.vhd"; Trace.TraceInformation("Cloud Drive: VHD = {0}", vhdName); var indexvhdName = RoleEnvironment.CurrentRoleInstance.Id.Replace(RoleEnvironment.DeploymentId + ".", "") + ".ravenindex.vhd"; Trace.TraceInformation("Cloud Drive: VHD = {0}", indexvhdName); var vhdUrl = blobClient.GetContainerReference(containerName).GetPageBlobReference(vhdName).Uri.ToString(); _dataDrive = storageAccount.CreateCloudDrive(vhdUrl); _dataDrive.CreateIfNotExist(localCache.MaximumSizeInMegabytes); // TODO Size var indexvhdUrl = blobClient.GetContainerReference(containerName).GetPageBlobReference(indexvhdName).Uri.ToString(); _indexDrive = storageAccount.CreateCloudDrive(indexvhdUrl); _indexDrive.CreateIfNotExist(localCache.MaximumSizeInMegabytes); //TODO Size var localPath = _dataDrive.Mount(localCache.MaximumSizeInMegabytes / 2, DriveMountOptions.Force); Trace.TraceInformation("Cloud Drive mounted to {0}", localPath); var indexPath = _indexDrive.Mount(localCache.MaximumSizeInMegabytes / 2, DriveMountOptions.Force); Trace.TraceInformation("Cloud Drive mounted to {0}", indexPath); var dataPath = _dataDrive.LocalPath.EndsWith("\\") ? _dataDrive.LocalPath + "Data" : _dataDrive.LocalPath + "\\Data"; if (!System.IO.Directory.Exists(dataPath)) System.IO.Directory.CreateDirectory(dataPath); var indexFullPath = _indexDrive.LocalPath.EndsWith("\\") ? _indexDrive.LocalPath + "Index" : _indexDrive.LocalPath + "\\Index"; if (!System.IO.Directory.Exists(indexFullPath)) System.IO.Directory.CreateDirectory(indexFullPath); }
private string GetMountedPathFromBlob( string localCachePath, string cloudDir, string containerName, string blobName, int driveSize, bool waitOnMount, out CloudDrive bermudaDrive) { Trace.TraceInformation(string.Format("In mounting cloud drive for dir {0}", cloudDir)); var storageAccount = CloudStorageAccount; var blobClient = CloudBlobClient; Trace.TraceInformation("Get container"); var driveContainer = blobClient.GetContainerReference(containerName); // create blob container (it has to exist before creating the cloud drive) try { driveContainer.CreateIfNotExist(); } catch (Exception e) { Trace.TraceInformation("Exception when creating container"); Trace.TraceInformation(e.Message); Trace.TraceInformation(e.StackTrace); } var bermudaBlobUri = blobClient.GetContainerReference(containerName).GetPageBlobReference(blobName).Uri.ToString(); Trace.TraceInformation(string.Format("Blob uri obtained {0}", bermudaBlobUri)); // create the cloud drive bermudaDrive = storageAccount.CreateCloudDrive(bermudaBlobUri); try { bermudaDrive.CreateIfNotExist(driveSize); } catch (Exception e) { // exception is thrown if all is well but the drive already exists Trace.TraceInformation("Exception when creating cloud drive. safe to ignore"); Trace.TraceInformation(e.Message); Trace.TraceInformation(e.StackTrace); } //Trace.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 if (!waitOnMount) { try { Trace.TraceInformation(string.Format("Trying to mount blob as azure drive on {0}", RoleEnvironment.CurrentRoleInstance.Id)); var driveLetter = bermudaDrive.Mount(0, DriveMountOptions.None); Trace.TraceInformation(string.Format("Write lock acquired on azure drive, mounted as {0}, on role instance", driveLetter, RoleEnvironment.CurrentRoleInstance.Id)); return(driveLetter); } catch (Exception e) { Trace.TraceWarning("could not acquire blob lock."); Trace.TraceWarning(e.Message); Trace.TraceWarning(e.StackTrace); throw; } } else { string driveLetter; Trace.TraceInformation(string.Format("Trying to mount blob as azure drive on {0}", RoleEnvironment.CurrentRoleInstance.Id)); while (true) { try { driveLetter = bermudaDrive.Mount(0, DriveMountOptions.None); Trace.TraceInformation(string.Format("Write lock acquired on azure drive, mounted as {0}, on role instance", driveLetter, RoleEnvironment.CurrentRoleInstance.Id)); return(driveLetter); } catch { } Thread.Sleep(MountSleep); } } }
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"); }
//================================================================================= // // 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 static String CreateSolrStorageVhd() { CloudStorageAccount storageAccount; LocalResource localCache; CloudBlobClient client; CloudBlobContainer drives; localCache = RoleEnvironment.GetLocalResource("AzureDriveCache"); Log(String.Format(CultureInfo.InvariantCulture, "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 (StorageClientException) { }; string vhdName = string.Format(CultureInfo.InvariantCulture, "SolrStorage_{0}.vhd", RoleEnvironment.GetConfigurationSettingValue("SolrMajorVersion")); var vhdUrl = client.GetContainerReference(containerAddress).GetBlobReference(vhdName).Uri.ToString(); Log(String.Format(CultureInfo.InvariantCulture, "{0} {1}", vhdName, vhdUrl), "Information"); _solrStorageDrive = storageAccount.CreateCloudDrive(vhdUrl); int cloudDriveSizeInMB = int.Parse(RoleEnvironment.GetConfigurationSettingValue("CloudDriveSize"), CultureInfo.InvariantCulture); try { _solrStorageDrive.Create(cloudDriveSizeInMB); } catch (CloudDriveException) { } Log(String.Format(CultureInfo.InvariantCulture, "CloudDriveSize {0} MB", cloudDriveSizeInMB), "Information"); var dataPath = _solrStorageDrive.Mount(localCache.MaximumSizeInMegabytes - 50, DriveMountOptions.Force); Log(String.Format(CultureInfo.InvariantCulture, "Mounted as {0}", dataPath), "Information"); return dataPath; }
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 string GetMountedPathFromBlob( string localCachePath, string cloudDir, string containerName, string blobName, int driveSize, bool waitOnMount, bool fallback, out CloudDrive mongoDrive) { TraceInformation(string.Format("In mounting cloud drive for dir {0}", cloudDir)); CloudStorageAccount storageAccount = null; if (fallback) { try { storageAccount = CloudStorageAccount.FromConfigurationSetting(cloudDir); } catch { // case for fallback to data dir for log also TraceInformation(string.Format("{0} is not found. using backup", cloudDir)); mongoDrive = mongoDataDrive; return mongoDataDriveLetter; } } else { storageAccount = CloudStorageAccount.FromConfigurationSetting(cloudDir); } var blobClient = storageAccount.CreateCloudBlobClient(); 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) { TraceInformation("Exception when creating container"); TraceInformation(e.Message); TraceInformation(e.StackTrace); } var mongoBlobUri = blobClient.GetContainerReference(containerName).GetPageBlobReference(blobName).Uri.ToString(); 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 TraceInformation("Exception when creating cloud drive. safe to ignore"); TraceInformation(e.Message); TraceInformation(e.StackTrace); } 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 if (!waitOnMount) { try { TraceInformation(string.Format("Trying to mount blob as azure drive on {0}", RoleEnvironment.CurrentRoleInstance.Id)); var driveLetter = mongoDrive.Mount(localStorage.MaximumSizeInMegabytes, DriveMountOptions.None); TraceInformation(string.Format("Write lock acquired on azure drive, mounted as {0}, on role instance", driveLetter, RoleEnvironment.CurrentRoleInstance.Id)); return driveLetter; } catch (Exception e) { TraceWarning("could not acquire blob lock."); TraceWarning(e.Message); TraceWarning(e.StackTrace); throw; } } else { string driveLetter; TraceInformation(string.Format("Trying to mount blob as azure drive on {0}", RoleEnvironment.CurrentRoleInstance.Id)); while (true) { try { driveLetter = mongoDrive.Mount(localStorage.MaximumSizeInMegabytes, DriveMountOptions.None); TraceInformation(string.Format("Write lock acquired on azure drive, mounted as {0}, on role instance", driveLetter, RoleEnvironment.CurrentRoleInstance.Id)); return driveLetter; } catch { } Thread.Sleep(MountSleep); } } }
// Mount XDrive public bool MountXDrive() { // Create HTTP storage endpoint, needed by Windows Azure Drive CloudStorageAccount xdriveStorageAccount = WindowsAzureVMManager.GetStorageAccount(false); // Initialize local cache LocalResource localCache = RoleEnvironment.GetLocalResource("XDriveLocalCache"); Char[] backSlash = { '\\' }; String localCachePath = localCache.RootPath.TrimEnd(backSlash); CloudDrive.InitializeCache(localCachePath, localCache.MaximumSizeInMegabytes); // Get Windows Azure Drive container and blob names from service configuration file string xdriveContainerName = RoleEnvironment.GetConfigurationSettingValue("PHPApplicationsBackupContainerName"); string xdriveBlobName = RoleEnvironment.GetConfigurationSettingValue("XDrivePageBlobName"); // Create blob container, if it does not exist CloudBlobClient blobClient = xdriveStorageAccount.CreateCloudBlobClient(); blobClient.GetContainerReference(xdriveContainerName).CreateIfNotExist(); // Get Windows Azure Drive page blob reference xdrivePageBlob = blobClient .GetContainerReference(xdriveContainerName) .GetPageBlobReference(xdriveBlobName); // Get a reference to the requested Windows Azure Drive drive = xdriveStorageAccount.CreateCloudDrive( xdrivePageBlob.Uri.ToString() ); // Create drive try { drive.Create(int.Parse(RoleEnvironment.GetConfigurationSettingValue("XDriveSizeInMB"))); } catch (CloudDriveException) { // exception is also thrown if all is well but the drive already exists, hence ignore exception } try { // This is 1 VM solution only, so always mount drive in write mode. string xdriveLetter = drive.Mount( int.Parse(RoleEnvironment.GetConfigurationSettingValue("XDriveCacheSizeInMB")), DriveMountOptions.None); Trace.TraceInformation("Mounted Windows Azure Drive at uri {0}", drive.Uri); Trace.TraceInformation("Applications are durable to Windows Azure Page Blob."); // Use different mechanism for devfabric and cloud to determine applicationsAndRuntimeResourceFolder if (RoleEnvironment.DeploymentId.StartsWith("deployment")) { // This is Windows Azure SDK 1.3 Specific, so might break in future string csrunEnv = Environment.GetEnvironmentVariable("_CSRUN_STATE_DIRECTORY"); if (string.IsNullOrEmpty(csrunEnv)) { applicationsAndRuntimeResourceFolder = Path.Combine( Environment.GetEnvironmentVariable("LOCALAPPDATA"), @"dftmp\wadd\devstoreaccount1\"); } else { applicationsAndRuntimeResourceFolder = Path.Combine(csrunEnv, @"wadd\devstoreaccount1\"); } // Get Windows Azure Drive container and blob names from service configuration file applicationsAndRuntimeResourceFolder = Path.Combine(applicationsAndRuntimeResourceFolder, xdriveContainerName); applicationsAndRuntimeResourceFolder = Path.Combine(applicationsAndRuntimeResourceFolder, xdriveBlobName); } else { applicationsAndRuntimeResourceFolder = xdriveLetter; } return true; } catch (Exception ex) { applicationsAndRuntimeResourceFolder = null; Trace.TraceError("Unable to Mount Windows Azure Drive. Error: {0}, StackTrace: {1}", ex.Message, ex.StackTrace); return false; } }
/// <summary> /// ON START /// </summary> /// <returns></returns> public override bool OnStart() { Trace.TraceInformation("RoutingConfigRole entry point called - OnStart Method"); // Set the maximum number of concurrent connections ServicePointManager.DefaultConnectionLimit = 10 * 12 * Environment.ProcessorCount; //Disable small packet assembling, reducing network lattency ServicePointManager.UseNagleAlgorithm = false; //Enable TCP KeepAlive 2h, 1s ServicePointManager.SetTcpKeepAlive(true, 2 * 3600 * 1000, 1000); Trace.TraceInformation("Instance start - Configuring RoleEnvironment handlers"); #region Setup CloudStorageAccount Configuration Setting Publisher // This code sets up a handler to update CloudStorageAccount instances when their corresponding // configuration settings change in the service configuration file. CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => { // Provide the configSetter with the initial value configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)); RoleEnvironment.Changed += (sender, arg) => { if (arg.Changes.OfType<RoleEnvironmentConfigurationSettingChange>().Any((change) => (change.ConfigurationSettingName == configName))) { // If the autorestart config changed, do nothing if (configName != "AutoRestart") { // The corresponding configuration setting has changed, propagate the value if (!configSetter(RoleEnvironment.GetConfigurationSettingValue(configName))) { Trace.TraceInformation("RoleEnvironmentConfigurationSettingChange : Requesting a recycle"); // In this case, the change to the storage account credentials in the // service configuration is significant enough that the role needs to be // recycled in order to use the latest settings. (for example, the // endpoint has changed) RoleEnvironment.RequestRecycle(); } } } }; }); #endregion // For information on handling configuration changes // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357. RoleEnvironment.Changing += new EventHandler<RoleEnvironmentChangingEventArgs>(RoleEnvironment_Changing); RoleEnvironment.Stopping += new EventHandler<RoleEnvironmentStoppingEventArgs>(RoleEnvironment_Stopping); RoleEnvironment.StatusCheck += new EventHandler<RoleInstanceStatusCheckEventArgs>(RoleEnvironment_StatusCheck); Trace.TraceInformation("Instance start - Configuring Diagnostic Monitor"); #region Diagnostic Monitor configuration var cfg = DiagnosticMonitor.GetDefaultInitialConfiguration(); cfg.Logs.ScheduledTransferLogLevelFilter = LogLevel.Information; cfg.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1); cfg.PerformanceCounters.DataSources.Add(new PerformanceCounterConfiguration { CounterSpecifier = @"\Processor(_Total)\% Processor Time", SampleRate = TimeSpan.FromSeconds(10d) }); cfg.PerformanceCounters.DataSources.Add(new PerformanceCounterConfiguration { CounterSpecifier = @"\Memory\Available Bytes", SampleRate = TimeSpan.FromSeconds(10d) }); cfg.PerformanceCounters.DataSources.Add(new PerformanceCounterConfiguration { CounterSpecifier = @"\PhysicalDisk(_Total)\% Idle Time", SampleRate = TimeSpan.FromSeconds(10d) }); cfg.PerformanceCounters.DataSources.Add(new PerformanceCounterConfiguration { CounterSpecifier = @"\PhysicalDisk(_Total)\Avg. Disk Queue Length", SampleRate = TimeSpan.FromSeconds(10d) }); cfg.PerformanceCounters.ScheduledTransferPeriod = TimeSpan.FromMinutes(1d); DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", cfg); #endregion #if !DEBUG // Configuration of the firewall // FirewallManagement.ConfigureFirewall(); #else Trace.TraceInformation("DEBUG Mode : Firewall not configured !!"); #endif SetCurrentState(RoutingConfigRoleState.Starting); #region Path for the storage //Drive only for S+C role if (MongoDBAzurePlatform.Instance.MyFunctionnalRoutingRole == MongoDBAzurePlatform.FunctionnalRoutingRole.RoutingAndConfigRole) { Trace.TraceInformation("Instance start - Mounting cloud drive for MongoC data"); // Get the local cache for the cloud drive LocalResource localCache = RoleEnvironment.GetLocalResource("MongocCache"); // we'll use all the cache space we can (note: InitializeCache doesn't work with trailing slash) CloudDrive.InitializeCache(localCache.RootPath.TrimEnd('\\'), localCache.MaximumSizeInMegabytes); // connect to the storage account storageAccount = CloudStorageAccount.FromConfigurationSetting("MongoDbData"); // client for talking to our blob files CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // the container that our dive is going to live in CloudBlobContainer drives = blobClient.GetContainerReference(RoleEnvironment.GetConfigurationSettingValue("ContainerName")); // create blob container (it has to exist before creating the cloud drive) try { drives.CreateIfNotExist(); } catch { } // get the url to the vhd page blob we'll be using var vhdUrl = drives.GetPageBlobReference(string.Format("mongoc-{0}.vhd", RoleEnvironment.CurrentRoleInstance.Id)).Uri.ToString(); mongoDrive = storageAccount.CreateCloudDrive(vhdUrl); try { mongoDrive.Create(localCache.MaximumSizeInMegabytes); } catch (CloudDriveException) { // exception is thrown if all is well but the drive already exists } dbConfigPath = mongoDrive.Mount(localCache.MaximumSizeInMegabytes, DriveMountOptions.Force) + @"\"; Trace.TraceInformation("Instance start - Cloud drive mounted"); } #endregion switch (MongoDBAzurePlatform.Instance.MyFunctionnalRoutingRole) { case MongoDBAzurePlatform.FunctionnalRoutingRole.RoutingAndConfigRole: Trace.TraceInformation(string.Format("Instance Start (S+C) - Registering mongoc and mongos instance as 'stopped', MongoConfig IP is {0}, MongoS IP is {1}", MongoDBAzurePlatform.Instance.MyMongoCAddress.ToString(),MongoDBAzurePlatform.Instance.MyMongoSAddress.ToString())); break; case MongoDBAzurePlatform.FunctionnalRoutingRole.RoutingOnlyRole: Trace.TraceInformation(string.Format("Instance Start (S only) - Registering mongoc and mongos instance as 'stopped', MongoS IP is {0}", MongoDBAzurePlatform.Instance.MyMongoSAddress.ToString())); break; default: Trace.TraceError("On Start : Wrong Functionnal Role : " + (MongoDBAzurePlatform.Instance.MyFunctionnalRoutingRole.ToString())); break; } MongoHelper.RegisterInstanceOrUpdate(MongoHelper.RoutingConfigRoleMongoProcessState.Stopped, "mongoc"); MongoHelper.RegisterInstanceOrUpdate(MongoHelper.RoutingConfigRoleMongoProcessState.Stopped, "mongos"); //Start MongoDB performance counters collection for MongoC process Trace.TraceInformation("Instance start - Starting custom performance counters"); if (MongoDBAzurePlatform.Instance.MyFunctionnalRoutingRole == MongoDBAzurePlatform.FunctionnalRoutingRole.RoutingAndConfigRole) MongoDB.PerformanceCounters.PerformanceMonitor.Start(MongoDBAzurePlatform.Instance.MyMongoCAddress.Host, MongoDBAzurePlatform.Instance.MyMongoCAddress.Port, int.Parse(RoleEnvironment.GetConfigurationSettingValue("PerfCountersSamplingIntervalInMs"))); Trace.TraceInformation("Instance start - end of instance start"); switch (MongoDBAzurePlatform.Instance.MyFunctionnalRoutingRole) { case MongoDBAzurePlatform.FunctionnalRoutingRole.RoutingAndConfigRole: SetCurrentState(RoutingConfigRoleState.MongoConfig_NotRunning); break; case MongoDBAzurePlatform.FunctionnalRoutingRole.RoutingOnlyRole: SetCurrentState(RoutingConfigRoleState.MongoShard_NotRunning); break; default: Trace.TraceError("On Start : Wrong Functionnal Role : "+(MongoDBAzurePlatform.Instance.MyFunctionnalRoutingRole.ToString())); break; } return base.OnStart(); }
private string MountDrive(string localCacheName, string blobName) { //Trace.TraceInformation("MountDrive - Creating drive " + blobName); string path = ""; try { // Get the local cache for the cloud drive LocalResource localCache = RoleEnvironment.GetLocalResource(localCacheName); // we'll use all the cache space we can (note: InitializeCache doesn't work with trailing slash) CloudDrive.InitializeCache(localCache.RootPath.TrimEnd('\\'), localCache.MaximumSizeInMegabytes); // the container that our dive is going to live in CloudBlobContainer drives = blobClient.GetContainerReference(RoleEnvironment.GetConfigurationSettingValue("ContainerName")); // create blob container (it has to exist before creating the cloud drive) try { drives.CreateIfNotExist(); } catch (Exception driveEx) { if (IsVerbose) Trace.TraceInformation(driveEx.ToString()); } // get the url to the vhd page blob we'll be using var blob = blobClient.GetContainerReference(RoleEnvironment.GetConfigurationSettingValue("ContainerName")).GetPageBlobReference(blobName); var vhdUrl = blob.Uri.ToString(); mongoDrive = storageAccount.CreateCloudDrive(vhdUrl); try { if (mongoDrive.CreateIfNotExist(localCache.MaximumSizeInMegabytes)) { //mongoDrive.Create(localCache.MaximumSizeInMegabytes); Trace.TraceInformation("MountDrive - Drive created"); } } catch (CloudDriveException cloudEx) { // this exception can be thrown if the drive already exists if (IsVerbose) Trace.TraceInformation(cloudEx.Message); } path = mongoDrive.Mount(0, DriveMountOptions.None) + @"\"; try { if (!path.ToLower().Contains("m:")) { char letter = path.Split(':')[0].First(); Trace.TraceInformation("Azure Drive is mounted on {0}, will run diskpart to mount it on M:", letter); MongoDriverHelper.RunDiskPart('M', letter); } } catch (Exception ex) { Trace.TraceError("Error in diskpart failed for {0}\r\n{1}" + path, ex); return string.Empty; } } catch (Exception exception) { if (IsVerbose) { Trace.TraceError("MountDrive Exception : " + exception.ToString()); } return string.Empty; } return path; }
private void MountCloudDrive() { Trace.TraceInformation("Cloud Drive: Mounting..."); CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting("StorageAccount"); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); LocalResource localCache = RoleEnvironment.GetLocalResource("RavenCache"); CloudDrive.InitializeCache(localCache.RootPath.TrimEnd('\\'), localCache.MaximumSizeInMegabytes); var containerName = RoleEnvironment.GetConfigurationSettingValue("CloudDriveContainer"); Trace.TraceInformation("Cloud Drive: Container = {0}", containerName); blobClient.GetContainerReference(containerName).CreateIfNotExist(); var vhdName = RoleEnvironment.CurrentRoleInstance.Id + ".vhd"; Trace.TraceInformation("Cloud Drive: VHD = {0}", vhdName); var vhdUrl = blobClient.GetContainerReference(containerName).GetPageBlobReference(vhdName).Uri.ToString(); _dataDrive = storageAccount.CreateCloudDrive(vhdUrl); _dataDrive.CreateIfNotExist(localCache.MaximumSizeInMegabytes); var localPath = _dataDrive.Mount(localCache.MaximumSizeInMegabytes, DriveMountOptions.Force); Trace.TraceInformation("Cloud Drive mounted to {0}", localPath); }
private void MountCloudDrive(string container, string vhdName, int size) { Tracer.WriteLine("Configuring CloudDrive", "Information"); LocalResource localCache = RoleEnvironment.GetLocalResource("MyAzureDriveCache"); const int TRIES = 30; // Temporary workaround for ERROR_UNSUPPORTED_OS seen with Windows Azure Drives // See http://blogs.msdn.com/b/windowsazurestorage/archive/2010/12/17/error-unsupported-os-seen-with-windows-azure-drives.aspx for (int i = 0; i < TRIES; i++) { try { CloudDrive.InitializeCache(localCache.RootPath, localCache.MaximumSizeInMegabytes); break; } catch (CloudDriveException ex) { if (!ex.Message.Equals("ERROR_UNSUPPORTED_OS")) { throw; } if (i >= (TRIES - 1)) { // If the workaround fails then it would be dangerous to continue silently, so exit Tracer.WriteLine("Workaround for ERROR_UNSUPPORTED_OS see http://bit.ly/fw7qzo FAILED", "Error"); System.Environment.Exit(-1); } Tracer.WriteLine("Using temporary workaround for ERROR_UNSUPPORTED_OS see http://bit.ly/fw7qzo", "Information"); Thread.Sleep(10000); } } CloudStorageAccount cloudDriveStorageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(CLOUD_DRIVE_CONNECTION_STRING)); CloudBlobClient blobClient = cloudDriveStorageAccount.CreateCloudBlobClient(); blobClient.GetContainerReference(container).CreateIfNotExist(); CloudPageBlob pageBlob = blobClient .GetContainerReference(container) .GetPageBlobReference(vhdName); cloudDrive = cloudDriveStorageAccount.CreateCloudDrive(pageBlob.Uri.ToString()); try { if (!pageBlob.Exists()) { Tracer.WriteLine(string.Format("Creating page blob {0}", cloudDrive.Uri), "Information"); cloudDrive.Create(size); } Tracer.WriteLine(string.Format("Mounting {0}", cloudDrive.Uri), "Information"); cloudDrive.Mount(25, DriveMountOptions.Force); } catch (CloudDriveException e) { Tracer.WriteLine(e, "Error"); } Tracer.WriteLine(string.Format("CloudDrive {0} mounted at {1}", cloudDrive.Uri, cloudDrive.LocalPath), "Information"); }
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 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"); }