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); }
/// <summary> /// /// </summary> /// <param name="accountName"></param> /// <param name="accountKey"></param> /// <param name="azureDriveContainerName"></param> /// <param name="azureDrivePageBlobName"></param> /// <param name="?"></param> /// <returns></returns> public static string SnapshotAzureDrive(string accountName, string accountKey, string azureDriveContainerName, string azureDrivePageBlobName) { try { CloudStorageAccount csa = WAStorageHelper.GetCloudStorageAccount(accountName, accountKey, false); // Create the blob client CloudBlobClient client = csa.CreateCloudBlobClient(); // Create the blob container which will contain the pageblob corresponding to the azure drive. CloudBlobContainer container = client.GetContainerReference(azureDriveContainerName); container.CreateIfNotExist(); // Get the page blob reference which will be used by the azure drive. CloudPageBlob blob = container.GetPageBlobReference(azureDrivePageBlobName); CloudDrive drive = new CloudDrive(blob.Uri, csa.Credentials); if (drive != null) { return(drive.Snapshot().ToString()); } } catch (Exception ex) { WindowsAzureSystemHelper.LogError(String.Format("Error in snapshot drive {0} - {1}", azureDrivePageBlobName, ex.Message)); } return(string.Empty); }
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); }
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)); } }
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); }
private void RoleEnvironmentChanged(object sender, RoleEnvironmentChangedEventArgs e) { if (!e.Changes.OfType <RoleEnvironmentConfigurationSettingChange>().Any(c => c.ConfigurationSettingName == "LogDriveService.BlobPath")) { return; } try { // perform a rolling drive change var oldDrive = this.currentDrive; var newDrive = MountDrive(); try { ConfigureWebServer(newDrive.LocalPath); } catch (Exception) { UnmountDrive(newDrive); throw; } this.currentDrive = newDrive; UnmountDrive(oldDrive); } catch (Exception ex) { this.EventLog.WriteEntry(ex.ToString(), EventLogEntryType.Error); throw; } }
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()); }
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(); }
public void ListAllMountedDrives() { try { IDictionary <string, Uri> drives = CloudDrive.GetMountedDrives(); if (drives != null) { IEnumerator <KeyValuePair <string, Uri> > e = drives.GetEnumerator(); while (e.MoveNext()) { string item = string.Format("{0}>{1}", e.Current.Key, e.Current.Value); if (!lstDrives.Items.Contains(new ListItem(item))) { lstDrives.Items.Add(new ListItem(string.Format("{0}>{1}", e.Current.Key, e.Current.Value))); } } } } catch (Exception ex) { //lblMsg.Text = ex.Message; } }
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 void UnmountDrive(string containerName, string vhdName) { var client = GetCloudClientInstance(); var vhdBlob = client.GetContainerReference(containerName).GetPageBlobReference(vhdName); var cloudDrive = new CloudDrive(vhdBlob.Uri, client.Credentials); cloudDrive.Unmount(); }
void Application_End(object sender, EventArgs e) { // obtain a reference to the cloud drive and unmount it CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString"); string imageStoreBlobUri = RoleEnvironment.GetConfigurationSettingValue("ImageStoreBlobUri"); CloudDrive imageStoreDrive = account.CreateCloudDrive(imageStoreBlobUri); imageStoreDrive.Unmount(); }
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 AzureDriveExample(String containerName, String pageBlobName) { CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString")); CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient(); CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName); cloudBlobContainer.CreateIfNotExist(); Char[] forwardSlash = { '/' }; String trimmedUri = cloudStorageAccount.BlobEndpoint.ToString().TrimEnd(forwardSlash); String pageBlobUri = String.Format("{0}/{1}/{2}", trimmedUri, containerName, pageBlobName); cloudDrive = cloudStorageAccount.CreateCloudDrive(pageBlobUri); }
public QueueProcessor(string inboundQueue, string outboundQueue, int commandTimeout, string workingDirectory, string environmentVariables, CloudDrive cloudDrive, string inboundBlobContainer, string outboundBlobContainer, string command) { this.InboundQueue = inboundQueue; this.OutboundQueue = outboundQueue; this.CommandTimeout = commandTimeout; this.WorkingDirectory = workingDirectory; this.EnvironmentVariables = environmentVariables; this.CloudDrive = cloudDrive; this.InboundBlobContainer = inboundBlobContainer; this.OutboundBlobContainer = outboundBlobContainer; this.Command = command; }
private void CreateDrive() { try { var vhdUrl = _blobClient.GetContainerReference(_containerName).GetPageBlobReference(_vhdName).Uri.ToString(); _cloudDrive = _ravenDataStorageAccount.CreateCloudDrive(vhdUrl); _cloudDrive.Create(_localCache.MaximumSizeInMegabytes); } catch (CloudDriveException ex) { Trace.TraceWarning(ex.Message); } }
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> /// Delete /// </summary> /// <param name="drive"></param> public static void DeleteAzureDrive(CloudDrive drive) { try { if (drive != null) { drive.Delete(); } } catch (Exception ex) { WindowsAzureSystemHelper.LogError(String.Format("Error deleting drive {0} ", ex.Message)); } }
public static ICloudDriveRepository GetCloudDrive(CloudDrive cloudDrive, string directory, string fileName) { switch (cloudDrive) { case CloudDrive.GoogleDrive: return(new GoogleDriveRepository(directory, fileName)); case CloudDrive.TestStorage: return(new TestStorageRepository()); default: return(new TestStorageRepository()); } }
public void CreateDrive() { try { _cloudDrive = _account.CreateCloudDrive(_vhdName); _logger.Log("using account " + _account.BlobEndpoint + " to create " + _vhdName); var rv = _cloudDrive.CreateIfNotExist(DISK_SIZE); _logger.Log("done create drive"); } catch (Exception ex) { _logger.Log("error on CreateDrive", ex); } }
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()); }
/// <summary> /// /// </summary> /// <param name="csa"></param> /// <param name="uriString"></param> public static void UnmountAzureDrive(CloudStorageAccount csa, string uriString) { try { CloudDrive drive = new CloudDrive(new Uri(uriString), csa.Credentials); if (drive != null) { drive.Unmount(); } } catch (Exception ex) { WindowsAzureSystemHelper.LogError(String.Format("Error unmounting drive with Uri {0} - {1}", uriString, ex.Message)); } }
/// <summary> /// Snapshot the specified drive /// </summary> /// <param name="drive"></param> public static string SnapshotAzureDrive(CloudDrive drive) { try { if (drive != null) { return(drive.Snapshot().ToString()); } } catch (Exception ex) { WindowsAzureSystemHelper.LogError(String.Format("Error in shapshot drive {0} ", ex.Message)); } return(string.Empty); }
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; }
/// <summary> /// /// </summary> /// <param name="accountName"></param> /// <param name="accountKey"></param> /// <param name="uriString"></param> public static void DeleteAzureDrive(string accountName, string accountKey, string uriString) { try { CloudStorageAccount csa = WAStorageHelper.GetCloudStorageAccount(accountName, accountKey, false); CloudDrive drive = new CloudDrive(new Uri(uriString), csa.Credentials); if (drive != null) { drive.Delete(); } } catch (Exception ex) { WindowsAzureSystemHelper.LogError(String.Format("Error deleting drive with Uri {0} - {1}", uriString, ex.Message)); } }
public static void GetPreviousFiles(CloudDrive cloudDrive, List <FileProps> previousFilesPropsList) { switch (cloudDrive) { case CloudDrive.GoogleDrive: var googleDrive = new GoogleDriveRepository(previousFilesPropsList); break; case CloudDrive.TestStorage: var testStorageDrive = new TestStorageRepository(previousFilesPropsList); break; default: var testStorageDrive2 = new TestStorageRepository(previousFilesPropsList); break; } }
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); } } }
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(); }
/// <summary> /// Snapshot the drive /// </summary> /// <param name="accountName"></param> /// <param name="accountKey"></param> /// <param name="uriString"></param> public static string SnapshotAzureDrive(string accountName, string accountKey, string uriString) { try { CloudStorageAccount csa = WAStorageHelper.GetCloudStorageAccount(accountName, accountKey, false); CloudDrive drive = new CloudDrive(new Uri(uriString), csa.Credentials); if (drive != null) { return(drive.Snapshot().ToString()); } } catch (Exception ex) { WindowsAzureSystemHelper.LogError(String.Format("Error in snapshot drive with Uri {0} - {1}", uriString, ex.Message)); } return(string.Empty); }
public string Mount() { CloudDrive drive = null; string path = GetMountedPathFromBlob( BermudaLocalDataDir, BermudaCloudDataDir, BermudaDataBlobContainerName, BermudaDataBlobName, MaxDriveSize, true, out drive); Trace.TraceInformation(string.Format("Obtained data drive as {0}", path)); var dir = Directory.CreateDirectory(Path.Combine(path, @"bermuda")); Trace.TraceInformation(string.Format("Data directory is {0}", dir.FullName)); return(dir.FullName); }
private void OnStopInternal() { try { ConfigureWebServer(@"%SystemDrive%"); if (this.currentDrive != null) { UnmountDrive(this.currentDrive); this.currentDrive = null; } } catch (Exception ex) { this.EventLog.WriteEntry(ex.ToString(), EventLogEntryType.Error); throw; } }
public ActionResult Edit([Bind(Include = "Id,Name,Memo, Provider,Location, Uid, Password, Token, CertificateFile, CertificatePassword,ServiceAccountEmail, ClientId")] ModelCloudDriveViewEdit item) { if (ModelState.IsValid) { var v = item.Id.Equals(Guid.Empty.ToString()) ? null : DbContext.Current.GetCloudDrives().Find(s => s.Id == item.Id); if (v != null) { v.Name = item.Name; v.Memo = item.Memo; v.Provider = item.Provider; v.Location = item.Location; v.Uid = item.Uid; v.Password = item.Password; v.Token = item.Token; v.CertificateFile = item.CertificateFile; v.CertificatePassword = item.CertificatePassword; v.ServiceAccountEmail = item.ServiceAccountEmail; v.ClientId = item.ClientId; } else { v = new CloudDrive(); v.Id = item.Id; v.Name = item.Name; v.Memo = item.Memo; v.Provider = item.Provider; v.Location = item.Location; v.Uid = item.Uid; v.Password = item.Password; v.Token = item.Token; v.CertificateFile = item.CertificateFile; v.CertificatePassword = item.CertificatePassword; v.ServiceAccountEmail = item.ServiceAccountEmail; v.ClientId = item.ClientId; if (item.Id.Equals(Guid.Empty.ToString())) { v.NewId(); } } DbContext.Current.Save(v); return(RedirectToAction("Index")); } return(View(item)); }
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); }
protected void Page_PreRender(object sender, EventArgs e) { this.GridView1.Columns[this.GridView1.Columns.Count - 1].Visible = this.CurrentPath != Global.ImageStorePath; if (RoleEnvironment.IsAvailable) { this.MountedDrives.DataSource = from item in CloudDrive.GetMountedDrives() select new { Name = item.Key + " => " + item.Value, Value = item.Key }; this.MountedDrives.DataBind(); this.MountedDrives.SelectedValue = this.CurrentPath; this.SelectDrive.Visible = true; this.NewDrive.Text = this.MountedDrives.Items.Count < 2 ? "New Drive" : "Delete Drive"; } }
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); }
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; } }
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 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; }
public void CreateDriveEx() { try { CloudBlobClient client = _account.CreateCloudBlobClient(); // Create the container for the drive if it does not already exist. CloudBlobContainer container = new CloudBlobContainer("mydrives", client); container.CreateIfNotExist(); // Get a reference to the page blob that will back the drive. CloudPageBlob pageBlob = container.GetPageBlobReference(_vhdName); _cloudDrive = new CloudDrive(pageBlob.Uri, _account.Credentials); //_cloudDrive = _account.CreateCloudDrive(_vhdName); _logger.Log("using account " + _account.BlobEndpoint + " to create " + _vhdName); var rv = _cloudDrive.CreateIfNotExist(DISK_SIZE); _logger.Log("done create drive"); } catch (Exception ex) { _logger.Log("error on CreateDrive", ex); } }
// 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; } }
// Reset Windows Azure Drive On Another Thread public bool ResetWindowsAzureDriveOnAnotherThread() { Trace.TraceInformation("Started Reset for Windows Azure Drive..."); try { // Stop all runtimes if (StopAllRuntimes() == false) { UpdateProgressInformation("Unbale to stop all runtime and failed to reset Windows Azure Drive.", true); return false; } // Delete PHP Web site from IIS (if any) ServerManager serverManager = new ServerManager(); Site secondaryWebSite = serverManager.Sites[WindowsAzureVMManager.SecondaryWebSiteName]; if (secondaryWebSite != null) { UpdateProgressInformation("Deleting PHP Web site...", false); DeletePHPWebSite(); UpdateProgressInformation("Deleted PHP Web site.", false); } // Unmount and delete Windows Azure Drive if (IsDriveMounted()) { if (drive != null) { UpdateProgressInformation("Unmounting Windows Azure Drive...", false); UnmountXDrive(); UpdateProgressInformation("Unmounted Windows Azure Drive.", false); Thread.Sleep(5000); // Delete all snapshots and parent page blob if (WindowsAzureVMManager.BlobExists(xdrivePageBlob)) { xdrivePageBlob.Delete(new BlobRequestOptions() { DeleteSnapshotsOption = DeleteSnapshotsOption.IncludeSnapshots }); UpdateProgressInformation("Deleted pageblob and associated snapshots for the Windows Azure Drive.", false); } else { // In devfabric, delete the drive drive.Delete(); UpdateProgressInformation("Deleted Windows Azure Drive.", false); } // Reset references xdrivePageBlob = null; drive = null; } } // Reset status.xml file UpdateProgressInformation("Resetting status.xml file...", false); installationStatusCollection.Clear(); SerializeNameValueCollectionToBlob(installationStatusCollection, installationStatusBlob); UpdateProgressInformation("Reset status.xml file completed.", false); // Mount drive again UpdateProgressInformation("Mounting Windows Azure Drive again...", false); if (MountXDrive()) { UpdateProgressInformation("Reset Windows Azure Drive completed.", false); } else { UpdateProgressInformation("Unable to reset Windows Azure Drive as Windows Azure Drive could not be remounted", false); return false; } } catch (Exception ex) { UpdateProgressInformation("Unable to reset Windows Azure Drive. Error: " + ex.Message, true); return false; } ClearProgressInformation(); return true; }
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); } } }
private static void unmountDrive(CloudDrive drive) { drive.Unmount(); }
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"); }
private void MountCloudDrive() { // If specified, mount a cloud drive string cloudDrive = RoleEnvironment.GetConfigurationSettingValue(CLOUD_DRIVE); if (cloudDrive != "") { int cloudDriveSize = Int32.Parse(RoleEnvironment.GetConfigurationSettingValue(CLOUD_DRIVE_SIZE)); cloudDrive = ExpandKeywords(cloudDrive); string[] parts = cloudDrive.Split('\\'); MountCloudDrive(parts[0], parts[1], cloudDriveSize); } }
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"); }
public static void UnmountDrive(CloudDrive cloudDrive) { cloudDrive.Unmount(); }
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 UnmountDrive(CloudDrive drive) { drive.Unmount(); this.EventLog.WriteEntry(string.Format("{0} unmounted", drive.Uri)); }
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 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; } }