Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        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();
        }
Ejemplo n.º 3
0
 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);
     }
 }
        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);
                }
            }
        }
Ejemplo n.º 5
0
        private static string MountCloudDrive(string uri, string connectionString)
        {
            Console.WriteLine("Configuring CloudDrive", "Information");

            /*
             * LocalResource localCache = RoleEnvironment.GetLocalResource("Two10.MountVHD.Cache");
             *
             * 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
             *          Console.WriteLine("Workaround for ERROR_UNSUPPORTED_OS see http://bit.ly/fw7qzo FAILED", "Error");
             *          System.Environment.Exit(-1);
             *      }
             *
             *      Console.WriteLine("Using temporary workaround for ERROR_UNSUPPORTED_OS see http://bit.ly/fw7qzo", "Information");
             *      Thread.Sleep(10000);
             *  }
             * }*/

            CloudStorageAccount cloudDriveStorageAccount = CloudStorageAccount.Parse(connectionString);
            CloudBlobClient     blobClient = cloudDriveStorageAccount.CreateCloudBlobClient();
            CloudPageBlob       pageBlob   = blobClient.GetPageBlobReference(uri);

            var cloudDrive = cloudDriveStorageAccount.CreateCloudDrive(pageBlob.Uri.ToString());

            cloudDrive.CreateIfNotExist(1024 * 250);

            try
            {
                Console.WriteLine(string.Format("Mounting {0}", cloudDrive.Uri), "Information");
                return(cloudDrive.Mount(0, DriveMountOptions.None));
            }
            catch (CloudDriveException e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                Console.WriteLine(string.Format("CloudDrive {0} mounted at {1}", cloudDrive.Uri, cloudDrive.LocalPath), "Information");
            }

            return(null);
        }
Ejemplo n.º 6
0
        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");
        }
Ejemplo n.º 7
0
        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;
            }
        }