Пример #1
0
 private void SetBase(MasterTreeNode node)
 {
     if (node.Type != NodeType.VirtualMachines || node.VmType?.Value != VirtualMachineType.NONE)
     {
         return;
     }
     try
     {
         HyperV.CheckpointVm(node.Host, node.Name, "Base Checkpoint DNR");
     }
     catch (Exception e)
     {
         MessageBox.Show(e.Message, "Exception", MessageBoxButtons.OK);
     }
     databaseManager.SetVmType(DbVirtualMachine.FromTreeNode(node), VirtualMachineType.BASE);
     RefreshUI();
 }
Пример #2
0
        private MasterTreeNode GetRootTreeNode(DbHostComputer dbHost, HostState state, List <VirtualMachine> cache)
        {
            var root = (MasterTreeNode)dbHost;

            root.State = new NodeState(state);
            var vvhds = new List <string>();

            foreach (var v in cache)
            {
                var vdb = GetVmDb(v.Uuid);
                v.Type       = vdb == null ? VirtualMachineType.NONE : (VirtualMachineType)vdb.VmType;
                v.ParentHost = vdb == null ? v.Host : vdb.ParentHost;
                v.ParentUuid = vdb == null ? v.Uuid : vdb.ParentUuid;

                // First, add all virtual machines associated with this host
                var vnode = (MasterTreeNode)v;
                // Then, add all virtual hard disks associated with this host
                vvhds.AddRange(v.VhdPath);
                foreach (var vhd in v.VhdPath)
                {
                    vnode.Children.Add(MasterTreeNode.GetTreeNode(vhd, v.Host, NodeType.VirtualHardDisks));
                }
                root.Children.Add(vnode);
            }

            // Get orphaned VHDs
            try
            {
                var vhds = Interface.GetChildItems(dbHost.HostName, Settings.Default.vhdPath, "*.*vhd*");
                if (vhds != null)
                {
                    vhds = vhds.Except(vvhds).ToArray();
                    foreach (var v in vhds)
                    {
                        root.Children.Add(MasterTreeNode.GetTreeNode(v, dbHost.HostName, NodeType.OrphanedVirtualHardDisks));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK);
            }

            return(root);
        }
Пример #3
0
        /// <summary>
        /// Exception safe cache flushing for virtual machines.
        /// </summary>
        public void FlushCache(PsStreamEventHandlers handlers = null)
        {
            foreach (var e in Directory)
            {
                e.Value.BurnChildren();
            }
            Directory.Clear();
            var hosts = GetCollection <DbHostComputer>();
            var virts = GetCollection <DbVirtualMachine>();

            // Loop through each host
            foreach (var h in hosts.FindAll())
            {
                MasterTreeNode root = null;
                HostState      st   = HostState.Unknown;
                try
                {
                    Interface.BringOnline(h.HostName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK);
                    st = HostState.Offline;
                }

                var vm = new List <VirtualMachine>();
                try
                {
                    vm = HyperV.GetVm(h.HostName, handlers);
                    st = HostState.Online;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK);
                    st = HostState.Offline;
                }

                root = this.GetRootTreeNode(h, st, vm);
                // Finally, add this host to our tree
                if (root != null)
                {
                    Directory.Add(h.HostName, root);
                }
            }
        }
Пример #4
0
 private void UnsetBase(MasterTreeNode node)
 {
     if (node.Type != NodeType.VirtualMachines || node.VmType?.Value != VirtualMachineType.BASE)
     {
         return;
     }
     if (MessageBox.Show(
             "This action may corrupt all existing templates of this base VM. Data loss may occur. Are you sure you want to continue?",
             "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Hand) == DialogResult.No)
     {
         return;
     }
     try
     {
         HyperV.RestoreVmSnapshot(node.Host, node.Name, "Base Checkpoint DNR");
         HyperV.RemoveVmSnapshot(node.Host, node.Name, "Base Checkpoint DNR");
     }
     catch (Exception e)
     {
         MessageBox.Show(e.Message, "Exception", MessageBoxButtons.OK);
     }
     databaseManager.SetVmType(DbVirtualMachine.FromTreeNode(node), VirtualMachineType.NONE);
     RefreshUI();
 }
Пример #5
0
        private void SetupMasterTree()
        {
            this.treeListView.CanExpandGetter = (x) =>
            {
                return(((MasterTreeNode)x).Children.Count > 0);
            };

            this.treeListView.ChildrenGetter = (x) =>
            {
                return(((MasterTreeNode)x).Children);
            };

            this.olvColumn1.ImageGetter = (x) =>
            {
                if (((MasterTreeNode)x).Type == NodeType.HostComputer)
                {
                    return(imageList1.Images[1]);
                }
                else if (((MasterTreeNode)x).Type == NodeType.VirtualMachines)
                {
                    return(imageList1.Images[2]);
                }
                else if (((MasterTreeNode)x).Type == NodeType.VirtualHardDisks)
                {
                    return(imageList1.Images[3]);
                }
                else if (((MasterTreeNode)x).Type == NodeType.OrphanedVirtualHardDisks)
                {
                    return(imageList1.Images[4]);
                }
                return(imageList1.Images[0]);
            };

            this.treeListView.UseCellFormatEvents = true;
            this.treeListView.FormatCell         += (o, e) =>
            {
                if (e.ColumnIndex == this.olvColumn1.Index)
                {
                    MasterTreeNode node = (MasterTreeNode)e.Model;
                    if (node.VmType?.Value == VirtualMachineType.BASE)
                    {
                        e.SubItem.Font = new Font(e.SubItem.Font, FontStyle.Bold);
                    }
                    else if (node.VmType?.Value == VirtualMachineType.TEMPLATE)
                    {
                        e.SubItem.Font = new Font(e.SubItem.Font, FontStyle.Italic);
                    }
                    else if (node.VmType?.Value == VirtualMachineType.NONE)
                    {
                        e.SubItem.ForeColor = Color.Gray;
                    }
                }
            };

            this.treeListView.Roots = databaseManager.Directory.Values;

            // TODO: High DPI Awareness
            TreeListView.TreeRenderer renderer = this.treeListView.TreeColumnRenderer;
            renderer.LinePen           = new Pen(Color.Firebrick, 0.5f);
            renderer.LinePen.DashStyle = DashStyle.Dot;
            renderer.IsShowGlyphs      = true;
            renderer.UseTriangles      = true;
            //TreeListView.TreeRenderer.PIXELS_PER_LEVEL = 13;
            renderer.CellPadding = new Rectangle(0, 2, 0, 0);
            renderer.IsShowLines = false;
            this.treeListView.Refresh();
        }