Ejemplo n.º 1
0
Archivo: Virsh.cs Proyecto: pk8est/Antd
        public IEnumerable <VirtualMachineInfo> GetVmList()
        {
            var vms = new List <VirtualMachineInfo>();
            var res = _bash.Execute("virsh list --all | sed '1,2d'");

            if (res.Length < 1)
            {
                return(vms);
            }
            var virshVms = res.Split(new[] { Environment.NewLine }, 3, StringSplitOptions.RemoveEmptyEntries);

            foreach (var i in virshVms)
            {
                var info = i.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                var vm   = new VirtualMachineInfo {
                    Id     = info[0],
                    Domain = info[1],
                    State  = info[2],
                };
                var vnc = GetVmVncAddress(vm.Domain);
                vm.VncIp   = vnc.Key;
                vm.VncPort = vnc.Value;
                vms.Add(vm);
            }
            return(vms);
        }
Ejemplo n.º 2
0
        public VirtualMachineInfo GetVirtualMachineInfo(string vmName)
        {
            ConnectToSession();
            VirtualMachine vm = GetVirtualMachineByName(vmName);

            if (vm == null)
            {
                throw new ArgumentNullException("VM not found");
            }

            VirtualMachineInfo vmInfo = new VirtualMachineInfo();

            vmInfo.Name         = vm.Name;
            vmInfo.VirtualDisks = new List <VirtualDiskInfo>();
            vmInfo.CdDisks      = new List <CdDiskInfo>();

            if (vm.Guest?.Disk != null)
            {
                foreach (GuestDiskInfo diskInfo in vm.Guest?.Disk)
                {
                    vmInfo.VirtualDisks.Add(new VirtualDiskInfo()
                    {
                        Name      = diskInfo.ToString(),
                        Path      = diskInfo.DiskPath,
                        Capacity  = diskInfo.Capacity,
                        FreeSpace = diskInfo.FreeSpace
                    });
                }
            }

            return(vmInfo);
        }
        // GET: api/VirtualMachines/5
        public IHttpActionResult GetVirtualMachines(string id)
        {
            _vmWareService = new VmWareClient(serviceUrl, userName, password);
            VirtualMachineInfo vmInfo = _vmWareService.GetVirtualMachineInfo(id);

            if (vmInfo == null)
            {
                return(NotFound());
            }
            return(Ok(vmInfo));
        }
Ejemplo n.º 4
0
 private static void VmApiTest()
 {
     try
     {
         VmWareClient       service = new VmWareClient(serviceUrl, userName, password);
         List <string>      vms     = service.GetVirtualMachines();
         VirtualMachineInfo vmInfo  = service.GetVirtualMachineInfo(vms.SingleOrDefault());
     }
     catch (Exception)
     {
         throw;
     }
 }
        // GET api/VirtualMachines/name
        public IHttpActionResult GetVirtualMachineInfo(string id)
        {
            try
            {
                string sessionId  = GetCookie(VSphereAccountController.SessionId);
                string serviceUrl = GetCookie(VSphereAccountController.ServiceUrl);

                _vmWareClient = new VmWareClient(serviceUrl, sessionId);
                VirtualMachineInfo vmInfo = _vmWareClient.GetVirtualMachineInfo(id);
                if (vmInfo == null)
                {
                    return(NotFound());
                }
                return(Ok(vmInfo));
            }
            catch (Exception)
            {
                return(BadRequest());
            }
        }
Ejemplo n.º 6
0
        private List <VirtualMachineInfo> GetVirtualMachinesForDeployment(Deployment deployment, HostedService cloudService)
        {
            var result = new List <VirtualMachineInfo>();

            foreach (var roleInstance in deployment.RoleInstanceList)
            {
                var role = deployment.RoleList.FirstOrDefault(r => string.Equals(r.RoleName, roleInstance.RoleName, StringComparison.OrdinalIgnoreCase));
                if (role != null && role.RoleType == "PersistentVMRole")
                {
                    var virtualMachineInfo = new VirtualMachineInfo()
                    {
                        Name             = roleInstance.InstanceName,
                        CloudServiceName = cloudService.ServiceName,
                        Status           = (VirtualMachineStatus)Enum.Parse(typeof(VirtualMachineStatus), roleInstance.InstanceStatus)
                    };
                    result.Add(virtualMachineInfo);
                }
            }

            return(result);
        }