public ActionResult _Partial_CreateDiskPool(Libvirt_WebManager.Areas.Storage_Pool.Models.Disk_Pool pool)
 {
     if (ModelState.IsValid)
     {
         _Storage_Service.CreatePool(pool);
         return CloseDialog();
     }
     return PartialView(pool);
 }
 public ActionResult _Partial_OS_PoolVolume_Selector(Libvirt_WebManager.Areas.Domain.Models.BasePoolVolume_Selector pselector)
 {
     var h = GetHost(pselector.Host);
     var pools = h.virConnectListAllStoragePools( Libvirt.virConnectListAllStoragePoolsFlags.VIR_CONNECT_LIST_STORAGE_POOLS_DEFAULT);
     AddToAutomaticDisposal(pools);
     var vm = Utilities.AutoMapper.Mapper<Libvirt_WebManager.Areas.Domain.Models.PoolVolume_Selector_Down>.Map(pselector);
     vm.Pools = pools;
     return PartialView(vm);
 }
 private void SendErrorEvent(Libvirt_WebManager.Models.IMessage message)
 {
     var mes = message as Libvirt_WebManager.Models.Message.ErrorMessage;
     if (mes != null)
     {
         var errmsg = Libvirt_WebManager.Utilities.AutoMapper.Mapper<Libvirt_WebManager.ViewModels.Message.ErrorMessage>.Map(mes);
         errmsg.Message_Type = mes.Message_Type.ToString();
         _hubContext.Clients.All.ErrorEventReceived(errmsg);
     }
 }
 private void SendLogEvent(Libvirt_WebManager.Models.IMessage message)
 {
     var mes = message as Libvirt_WebManager.Models.Message.LogMessage;
     if (mes != null)
     {
         Task.Factory.StartNew(() => {
             _hubContext.Clients.All.LogEventReceived(new Libvirt_WebManager.ViewModels.Message.LogMessage() { Body = mes.Body, Title = mes.Title, Message_Type = mes.Message_Type.ToString(), Time = DateTime.Now });
         });
     }
 }
 public static void Send(Libvirt_WebManager.Models.IMessage message)
 {
     foreach (var item in _instance.Value.Filters)
     {
         item.Send(message);
     }
     foreach (var item in _instance.Value.FuncFilters)
     {
         item(message);
     }
 }
 public ActionResult _Partial_Create(Libvirt_WebManager.Areas.Domain.Models.New_Domain_VM domain)
 {
     if (ModelState.IsValid)
     {
         _Domain_Service.QuickCreate(domain);
     }
     if (ModelState.IsValid) return CloseDialog();
     var d = GetDomainDown(domain.Host);
     d.Domain = domain;
     return PartialView(d);
 }
        public ActionResult _Partial_Create(Libvirt_WebManager.Areas.Host.Models.virConnectOpen obj)
        {
            if (ModelState.IsValid)
            {
                if (Libvirt_WebManager.Service.VM_Manager.Instance.virConnectOpen(obj.host_or_ip) != null)
                {

                    return CloseDialog();
                }
            }
            return PartialView(obj);
        }
 public bool Edit(Libvirt_WebManager.Areas.Network.Models.Create_Network_Down vm)
 {
     if (_Create(vm))
     {
         Message_Manager.Send(new Libvirt_WebManager.Models.Message.LogMessage { Title = "Update Network: '" + vm.name + "' ", Message_Type = Libvirt_WebManager.Models.Message_Types.success, Body = "Successfully updated the network configuration!" });
         return true;
     }
     else
     {
         Message_Manager.Send(new Libvirt_WebManager.Models.Message.LogMessage { Title = "Update Network: '" + vm.name + "' ", Message_Type = Libvirt_WebManager.Models.Message_Types.warning, Body = "Failed to updated the network configuration!" });
         return false;
     }
 }
        public ActionResult _Partial_OS_Volume_Selector(Libvirt_WebManager.Areas.Domain.Models.BasePoolVolume_Selector pselector)
        {
            var h = GetHost(pselector.Host);
            var p = h.virStoragePoolLookupByName(pselector.Parent);
            AddToAutomaticDisposal(p);
            var vm = Utilities.AutoMapper.Mapper<Libvirt_WebManager.Areas.Domain.Models.PoolVolume_Selector>.Map(pselector);

            Libvirt.CS_Objects.Storage_Volume[] vols;
            p.virStoragePoolListAllVolumes(out vols);
            AddToAutomaticDisposal(vols);
            vm.Volumes = vols;

            return PartialView(vm);
        }
        private bool _Create(Libvirt_WebManager.Areas.Network.Models.Create_Network_Down vm)
        {
            var h = Libvirt_WebManager.Service.VM_Manager.Instance.virConnectOpen(vm.Host);
            var n = Utilities.AutoMapper.Mapper<Libvirt.Models.Concrete.Network>.Map(vm);
            if (n.Forward_Type == Libvirt.Models.Concrete.Network.Forward_Types.nat)
            {
                n.Configuration = Utilities.AutoMapper.Mapper<Libvirt.Models.Concrete.Network_Configuration>.Map(vm.Configuration);
                if (vm.Inbound_QOS || vm.Outbound_QOS)
                {
                    n.QOS = new Libvirt.Models.Concrete.Network_QOS();

                    if (vm.Inbound_QOS) n.QOS.Inbound = Utilities.AutoMapper.Mapper<Libvirt.Models.Concrete.Network_Bandwith>.Map(vm.Inbound);
                    if (vm.Outbound_QOS) n.QOS.Outbound = Utilities.AutoMapper.Mapper<Libvirt.Models.Concrete.Network_Bandwith>.Map(vm.Outbound);
                }
            }
            using (var newnet = h.virNetworkDefineXML(n))
            {
                if (newnet.virNetworkIsActive() == 0) newnet.virNetworkCreate();//start the network up
                return newnet.IsValid;
            }
        }
        public ActionResult _Partial_CreatePool(Libvirt_WebManager.Areas.Storage_Pool.Models.Storage_Pool pool)
        {
            if (ModelState.IsValid)
            {
                var t = new Libvirt.Service.Concrete.Storage_Pool_Validator();
                var v = Utilities.AutoMapper.Mapper<Libvirt.Models.Concrete.Storage_Pool>.Map(pool);
                var h = GetHost(pool.Host);
                if (ModelState.IsValid)
                {
                    t.Validate(new Libvirt_WebManager.Models.Validator(ModelState), v, h);
                    if (ModelState.IsValid)
                    {

                        if (pool.pool_type == Libvirt.Models.Interface.IStorage_Pool_Item.Pool_Types.dir)
                        {
                            return PartialView("_Partial_CreateDirPool", new Libvirt_WebManager.Areas.Storage_Pool.Models.Dir_Pool { Storage_Pool = pool });
                        }
                        else if (pool.pool_type == Libvirt.Models.Interface.IStorage_Pool_Item.Pool_Types.disk)
                        {
                            using (var nodes = h.virConnectListAllNodeDevices(Libvirt.virConnectListAllNodeDeviceFlags.VIR_CONNECT_LIST_NODE_DEVICES_CAP_STORAGE))
                            using (var pools = h.virConnectListAllStoragePools(Libvirt.virConnectListAllStoragePoolsFlags.VIR_CONNECT_LIST_STORAGE_POOLS_DISK))
                            {
                                var poolsxml = pools.Select(a => a.virStoragePoolGetXMLDesc(Libvirt.virStorageXMLFlags.VIR_DEFAULT)).Select(b => b.Storage_Pool_Item as Libvirt.Models.Concrete.Storage_Pool_Disk);
                                var nodesxml = nodes.Select(a => a.virNodeDeviceGetXMLDesc() as Libvirt.Models.Concrete.Node.Storage)
                                    .Where(a => !a.block.ToLower().Contains("/sda")
                                    && !a.drive_type.ToLower().Contains("cdrom")
                                    && !poolsxml.Any(b=>b.device_path.ToLower().Contains(a.block.ToLower())));

                                return PartialView("_Partial_CreateDiskPool", new Libvirt_WebManager.Areas.Storage_Pool.Models.Disk_Pool { Storage_Pool = pool, Devices = nodesxml.ToList() });
                            }
                        }
                    }
                }
            }
            return PartialView(pool);
        }
 public static void AddFilter(Libvirt_WebManager.Models.Message.IFilter filter)
 {
     _instance.Value.Filters.Add(filter);
 }
        public void QuickCreate(Libvirt_WebManager.Areas.Domain.Models.New_Domain_VM v)
        {
            var h = Service.VM_Manager.Instance.virConnectOpen(v.Host);

            var capabilities = System.Xml.Linq.XDocument.Parse(h.virConnectGetCapabilities()).Root;

            Libvirt.Models.Concrete.Virtual_Machine virtuammachine = new Libvirt.Models.Concrete.Virtual_Machine();
            virtuammachine.CPU.vCpu_Count = v.Cpus;
            virtuammachine.type = Libvirt.Models.Concrete.Virtual_Machine.Domain_Types.qemu;
            virtuammachine.Metadata.name = v.Name;
            virtuammachine.Memory.memory_unit = virtuammachine.Memory.currentMemory_unit = Libvirt.Models.Concrete.Memory_Allocation.UnitTypes.MiB;
            virtuammachine.Memory.currentMemory = virtuammachine.Memory.memory = v.Ram;

            //Add a hard drive
            var harddrive = new Libvirt.Models.Concrete.Disk();
            harddrive.Device_Bus_Type = Libvirt.Models.Concrete.Disk.Disk_Bus_Types.virtio;
            harddrive.Disk_Device_Type = Libvirt.Models.Concrete.Disk.Disk_Device_Types.disk;
            harddrive.Device_Type = Libvirt.Models.Concrete.Disk.Disk_Types.volume;
            harddrive.Driver_Cache_Type = Libvirt.Models.Concrete.Disk.Driver_Cache_Types.none;
            harddrive.Driver_Type = Libvirt.Models.Concrete.Disk.Driver_Types.raw;
            harddrive.ReadOnly = false;
            harddrive.Snapshot_Type = Libvirt.Models.Concrete.Disk.Snapshot_Types._default;
            var source = new Libvirt.Models.Concrete.Device_Source_Volume();
            source.pool = v.HD_Pool;
            source.volume = v.HD_Volume;

            source.Source_Startup_Policy = Libvirt.Models.Concrete.Disk.Source_Startup_Policies.mandatory;
            harddrive.Source = source;
            virtuammachine.Drives.Add(harddrive);

            var oscdrom = new Libvirt.Models.Concrete.Disk();
            oscdrom.Device_Bus_Type = Libvirt.Models.Concrete.Disk.Disk_Bus_Types.ide;
            oscdrom.Disk_Device_Type = Libvirt.Models.Concrete.Disk.Disk_Device_Types.cdrom;
            oscdrom.Device_Type = Libvirt.Models.Concrete.Disk.Disk_Types.volume;
            oscdrom.Driver_Cache_Type = Libvirt.Models.Concrete.Disk.Driver_Cache_Types.none;
            oscdrom.Driver_Type = Libvirt.Models.Concrete.Disk.Driver_Types.raw;
            oscdrom.ReadOnly = true;
            oscdrom.Snapshot_Type = Libvirt.Models.Concrete.Disk.Snapshot_Types._default;

            var cdsource = new Libvirt.Models.Concrete.Device_Source_Volume();
            cdsource.pool = v.OS_Pool;
            cdsource.volume = v.OS_Volume;

            cdsource.Source_Startup_Policy = Libvirt.Models.Concrete.Disk.Source_Startup_Policies.optional;
            oscdrom.Source = cdsource;
            virtuammachine.Drives.Add(oscdrom);
            virtuammachine.graphics = new Libvirt.Models.Concrete.Graphics();
            virtuammachine.graphics.Graphic_Type = Libvirt.Models.Concrete.Graphics.Graphic_Types.vnc;
            virtuammachine.graphics.autoport = true;
            virtuammachine.graphics.websocket = -1;
            virtuammachine.graphics.listen = "0.0.0.0";
            virtuammachine.clock = v.Operating_System == Areas.Domain.Models.OS_Options.Windows ? Libvirt.Models.Concrete.Virtual_Machine.Clock_Types.localtime : Libvirt.Models.Concrete.Virtual_Machine.Clock_Types.utc;

            var hostcapabilities = System.Xml.Linq.XDocument.Parse(h.virConnectGetCapabilities()).Root;

            foreach(var item in hostcapabilities.Elements("guest"))
            {
                var arch = item.Element("arch");
                if (arch != null)
                {
                    var attr = arch.Attribute("name");
                    if (attr != null)
                    {
                        if(attr.Value == "x86_64")
                        {
                            var wordsize = arch.Element("wordsize");
                            if (wordsize != null)
                            {
                                if (wordsize.Value == "64")
                                {//GOT IT!!! 64 bit, x86 type.. get host capaciliies and pick best
                                    var ele = arch.Element("emulator");
                                    if (ele != null)
                                    {
                                        virtuammachine.emulator = ele.Value;
                                    }
                                    ele = arch.Element("machine");
                                    if (ele != null)
                                    {
                                        var fattr = ele.Attribute("canonical");
                                        if (fattr != null)
                                        {
                                            virtuammachine.BootLoader.machine = fattr.Value;
                                        }
                                        else
                                        {
                                            virtuammachine.BootLoader.machine = ele.Value;
                                        }

                                    }
                                }
                            }

                        }
                    }
                }
            }

            using (var domain = h.virDomainDefineXML(virtuammachine))
            {

                if (domain.IsValid)
                {
                    if (domain.virDomainCreate() == 0)
                    {
                        Debug.WriteLine("Successfully created and started the new VM!");
                    }
                    else
                    {
                        Debug.WriteLine("Successfully created the VM, but was unable to start it. Check the error logs");
                    }

                }
                else
                {
                    Debug.WriteLine("Unable to create the VM, please check the error logs!");
                }
            }
        }
 private Models.Storage_Volume_Down GetStorage_Volume_Down(Libvirt_WebManager.Areas.Storage_Pool.Models.Storage_Volume volume)
 {
     var h = GetHost(volume.Host);
     using (var p = h.virStoragePoolLookupByName(volume.Parent))
     {
         var svd = new Models.Storage_Volume_Down();
         svd.Volume = volume;
         if (p.IsValid)
         {
             Libvirt._virStoragePoolInfo info;
             p.virStoragePoolGetInfo(out info);
             svd.PoolInfo = info;
         }
         return svd;
     }
 }