Exemplo 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);
            }
        }
Exemplo n.º 2
0
        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;
            }
        }
        /// <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());
        }
        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";
            }
        }