public async Task <int> UploadVmTemplate(
            string name,
            Stream fileStream,
            long length,
            Hypervisor hypervisor,
            VmTemplate vmTemplate,
            Action <double> callback = null)
        {
            var          safeFileName = vmTemplate.Id + ".ova";
            const string baseDir      = "/root/uploads";
            var          dirName      = "VmTemplate_" + vmTemplate.Id;
            var          dirPath      = string.Join("/", baseDir, dirName);
            var          filePath     = string.Join("/", dirPath, safeFileName);
            var          node         = await ProxmoxManager.GetPrimaryHypervisorNode(hypervisor);

            var api = ProxmoxManager.GetProxmoxApi(node);
            int vmId;

            // Upload File
            using (var sftp = new SftpClient(GetConnectionInfoFromHypervisor(hypervisor)))
            {
                sftp.Connect();
                try
                {
                    sftp.CreateDirectory(dirPath);
                    sftp.ChangeDirectory(dirPath);
                    sftp.UploadFile(fileStream, filePath, true, progress =>
                    {
                        if (callback != null)
                        {
                            callback((double)progress / length * 100);
                        }
                    });
                    using (var ssh = new SshClient(GetConnectionInfoFromHypervisor(hypervisor)))
                    {
                        ssh.Connect();
                        await ExtractOva(ssh, filePath, dirPath);

                        vmId = await CreateVmAndImportDisk(name, ssh, sftp, api, dirPath);

                        var unusedDisk = await api.GetVmUnusedDisk(vmId);

                        await api.SetVmScsi0(vmId, unusedDisk);

                        await api.ConvertVmToTemplate(vmId);

                        ssh.Disconnect();
                    }
                    Cleanup(sftp, dirPath);
                }
                catch (Exception)
                {
                    Cleanup(sftp, dirPath);
                    throw;
                }
                sftp.Disconnect();
            }

            return(vmId);
        }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            Badge.OnModelCreating(builder);
            User.OnModelCreating(builder);

            // module section
            Module.OnModelCreating(builder);
            Lab.OnModelCreating(builder);
            LabVm.OnModelCreating(builder);
            UserModule.OnModelCreating(builder);
            // user module section
            UserModule.OnModelCreating(builder);
            UserLab.OnModelCreating(builder);
            UserLabVm.OnModelCreating(builder);
            // hypervisor
            Hypervisor.OnModelCreating(builder);
            HypervisorNode.OnModelCreating(builder);
            // configure many to many relationship
            UserUserModule.OnModelCreating(builder);
            ContactEmail.OnModelCreating(builder);
            BridgeInstance.OnModelCreating(builder);
            VmInterfaceTemplate.OnModelCreating(builder);
            VmInterfaceInstance.OnModelCreating(builder);
            BridgeTemplate.OnModelCreating(builder);
            VmTemplate.OnModelCreating(builder);
        }
Beispiel #3
0
        public ActionResult Edit(Hypervisor hypervisor)
        {
            if (ModelState.IsValid)
            {
                hypervisor.LastUpdated = DateTime.Now;

                if (hypervisor.Inactive)
                {
                    if (hypervisor.InactiveDate == null)
                    {
                        hypervisor.InactiveDate = hypervisor.LastUpdated;
                    }
                }
                else
                {
                    if (hypervisor.InactiveDate != null)
                    {
                        hypervisor.InactiveDate = null;
                    }
                }

                this.db.Hypervisors.Attach(hypervisor);
                this.db.ObjectStateManager.ChangeObjectState(hypervisor, EntityState.Modified);
                this.db.SaveChanges();
                return(this.RedirectToAction("Details", new { id = hypervisor.Id }));
            }

            ViewBag.HypervisorGroup_Id_Select           = new SelectList(this.db.HypervisorGroups.OrderBy(x => x.Name), "Id", "Name", hypervisor.HypervisorGroup_Id);
            ViewBag.HypervisorType_Id_Select            = new SelectList(this.db.HypervisorTypes.OrderBy(x => x.Name), "Id", "Name", hypervisor.HypervisorType_Id);
            ViewBag.HypervisorWorkloadProfile_Id_Select = new SelectList(this.db.HypervisorWorkloadProfiles.OrderBy(x => x.Name), "Id", "Name", hypervisor.HypervisorWorkloadProfile_Id);
            return(this.View(hypervisor));
        }
Beispiel #4
0
 public Hypervisor Update(Hypervisor hv)
 {
     using (dbc = DAL.Instance.GetDb()){
         dbc.Update <Hypervisor>(hv);
     }
     return(hv);
 }
Beispiel #5
0
        public async Task <HypervisorNode> GetLeastLoadedHyperVisorNode(Hypervisor hypervisor, int estimatedMemoryUsedMb)
        {
            long requiredMemoryBytes = estimatedMemoryUsedMb * 1024 * 1204;
            // only support one cluster right now

            var hypervisorNodes = _context.HypervisorNodes
                                  .Where(n => n.HypervisorId == hypervisor.Id)
                                  .Include(n => n.Hypervisor)
                                  .ToList();
            var api  = GetProxmoxApi(hypervisorNodes.First());
            var list = new List <KeyValuePair <NodeStatus, HypervisorNode> >();

            foreach (var hypervisorNode in hypervisorNodes)
            {
                var nodeStatus = await api.GetNodeStatus(hypervisorNode);

                list.Add(new KeyValuePair <NodeStatus, HypervisorNode>(nodeStatus, hypervisorNode));
            }

            list = list.Where(p => p.Key.MemoryUsage.Free > requiredMemoryBytes).ToList();
            list.Sort((s1, s2) => (int)((s1.Key.CpuUsage - s2.Key.CpuUsage) * 100));
            if (list.Count == 0)
            {
                throw new NoHypervisorAvailableException();
            }

            return(list.First().Value);
        }
 public async Task TestConnect(Hypervisor hypervisor)
 {
     using (var ssh = new SshClient(GetConnectionInfoFromHypervisor(hypervisor)))
     {
         ssh.Connect();
         ssh.Disconnect();
     }
 }
Beispiel #7
0
        public ActionResult DeleteConfirmed(Guid id)
        {
            Hypervisor hypervisor = this.db.Hypervisors.Single(h => h.Id == id);

            this.db.Hypervisors.DeleteObject(hypervisor);
            this.db.SaveChanges();
            return(this.RedirectToAction("Index"));
        }
        public ConnectionInfo GetConnectionInfoFromHypervisor(Hypervisor hypervisor)
        {
            var host     = hypervisor.Host;
            var username = hypervisor.UserName;
            var password = ProxmoxManager.GetProxmoxPassword(hypervisor);

            return(new ConnectionInfo(host, username, new PasswordAuthenticationMethod(username, password)));
        }
Beispiel #9
0
        public override string Second(string input)
        {
            Hypervisor hv = new Hypervisor();

            hv.Boot(input);
            hv.Execute();
            return(hv.Alus[1].SendCount.ToString());
        }
Beispiel #10
0
 public Hypervisor Save(Hypervisor hv)
 {
     hv.Id = IdManager.GetId();
     using (dbc = DAL.Instance.GetDb()){
         dbc.Insert <Hypervisor>(hv);
     }
     return(hv);
 }
Beispiel #11
0
 public Hypervisor GetById(int id)
 {
     using (dbc = DAL.Instance.GetDb()){
         Hypervisor hv = dbc.Select <Hypervisor>(h => h.Id == id)[0];
         hv.Password = PasswordManager.Get(hv.PasswordId);
         return(hv);
     }
 }
        /// <summary>
        /// Initializes a new instance of the
        /// <see cref="HypervisorListIndexViewModel"/> class.
        /// </summary>
        /// <param name="model">The Hypervisor to reference.</param>
        /// <param name="activeVirtualMachineCount">The number of active
        /// Virtual Machines related to the Hypervisor.</param>
        private HypervisorListIndexViewModel(Hypervisor model, int activeVirtualMachineCount)
            : base(model)
        {
            // Properties from the entity, notes and tags.
            this.ReadEntityProperties(model);

            // Related entities.
            this.ActiveVirtualMachineCount = activeVirtualMachineCount;
        }
Beispiel #13
0
    void Awake()
    {
        if (instance != null)
        {
            Destroy(gameObject);
            return;
        }

        instance = this;
        DontDestroyOnLoad(gameObject);
    }
Beispiel #14
0
 private void SendToQueue()
 {
     if (FirmwareVersion == 2)
     {
         if (HasRegValue(_SEND))
         {
             Hypervisor.Send(this, GetRegValue(_SEND));
             SendCount++;
             RemoveRegValue(_SEND);
         }
     }
 }
        /// <summary>
        /// Initializes a new instance of the
        /// <see cref="HypervisorListIndexViewModel"/> class.
        /// </summary>
        /// <param name="model">The Hypervisor to reference.</param>
        public HypervisorListIndexViewModel(Hypervisor model)
            : base(model)
        {
            // Properties from the entity, notes and tags.
            this.ReadEntityProperties(model);

            // Related entities.
            this.ActiveVirtualMachineCount = model.VirtualMachines
                                             .Where(z => !z.Inactive)
                                             .GroupBy(x => x.Id)
                                             .Select(x => x.FirstOrDefault())
                                             .Count();
        }
Beispiel #16
0
        public ActionResult Edit(Guid id)
        {
            Hypervisor hypervisor = this.db.Hypervisors.Single(h => h.Id == id);

            if (hypervisor == null)
            {
                return(this.HttpNotFound());
            }

            ViewBag.HypervisorGroup_Id_Select           = new SelectList(this.db.HypervisorGroups.OrderBy(x => x.Name), "Id", "Name", hypervisor.HypervisorGroup_Id);
            ViewBag.HypervisorType_Id_Select            = new SelectList(this.db.HypervisorTypes.OrderBy(x => x.Name), "Id", "Name", hypervisor.HypervisorType_Id);
            ViewBag.HypervisorWorkloadProfile_Id_Select = new SelectList(this.db.HypervisorWorkloadProfiles.OrderBy(x => x.Name), "Id", "Name", hypervisor.HypervisorWorkloadProfile_Id);
            return(this.View(hypervisor));
        }
        public override async Task Run(AddHypervisorOptions options)
        {
            var hypervisor = new Hypervisor
            {
                Host     = options.Host,
                UserName = options.UserName,
                NoVncUrl = options.NoVncUrl,
                Password = Cryptography.EncryptString(options.Password, _encryptionKey)
            };

            _context.Hypervisors.Add(hypervisor);
            await _context.SaveChangesAsync();

            System.Console.WriteLine("Added Hypervisor:");
            System.Console.WriteLine(JsonConvert.SerializeObject(hypervisor));
        }
Beispiel #18
0
        private async Task <Hypervisor> WriteHypervisorToDbAsync(string hypervisor, string backupTask)
        {
            var hyperV = await FindHypervisorByName(hypervisor);

            if (hyperV == null)
            {
                hyperV            = new Hypervisor();
                hyperV.Name       = hypervisor.ToLower();
                hyperV.BackupTask = backupTask;
                _context.Hypervisors.Add(hyperV);

                await _context.SaveChangesAsync();
            }

            return(hyperV);
        }
        /// <summary>
        /// Populates properties on the view model from an instance of the
        /// Hypervisor class.
        /// </summary>
        /// <param name="model">The Hypervisor to reference.</param>
        private void ReadEntityProperties(Hypervisor model)
        {
            // Properties from the entity.
            this.Memory            = model.Memory;
            this.ProcessorCores    = model.ProcessorCores;
            this.CreatedDate       = model.CreatedDate;
            this.LastUpdated       = model.LastUpdated;
            this.InactiveDate      = model.InactiveDate;
            this.IPAddress         = model.IPAddress;
            this.Status            = model.Status;
            this.SiteLocation      = model.SiteLocation;
            this.HyperVisorVersion = model.HyperVisorVersion;

            // Related entities.
            this.HypervisorType = model.HypervisorType;

            // TODO: Remove Hypervisor Workload Profile.
            this.HypervisorWorkloadProfile = model.HypervisorWorkloadProfile;

            if (model.HypervisorGroup != null)
            {
                this.HypervisorGroup = new HypervisorGroupReferenceViewModel(model.HypervisorGroup);
            }
            else
            {
                this.HypervisorGroup = null;
            }

            // IHasCapacity Members.
            this.TotalCapacity       = model.TotalCapacity;
            this.UsedCapacity        = model.UsedCapacity;
            this.UsedCapacityPercent = model.UsedCapacityPercent;

            // Notes and Tags.
            this.Notes = model.Notes
                         .OrderByDescending(x => x.CreatedAt)
                         .AsEnumerable()
                         .Select(x => new NoteDetailsViewModel(x))
                         .ToList();

            this.Tags = model.TagsMetas
                        .OrderBy(x => x.Tag.Name)
                        .AsEnumerable()
                        .Select(x => new TagDetailsViewModel(x))
                        .ToList();
        }
Beispiel #20
0
        public ActionResult Create(Hypervisor hypervisor)
        {
            if (ModelState.IsValid)
            {
                hypervisor.Id          = Guid.NewGuid();
                hypervisor.CreatedDate = DateTime.Now;
                hypervisor.LastUpdated = hypervisor.CreatedDate;

                this.db.Hypervisors.AddObject(hypervisor);
                this.db.SaveChanges();
                return(this.RedirectToAction("Details", new { id = hypervisor.Id }));
            }

            ViewBag.HypervisorGroup_Id_Select           = new SelectList(this.db.HypervisorGroups.OrderBy(x => x.Name), "Id", "Name", hypervisor.HypervisorGroup_Id);
            ViewBag.HypervisorType_Id_Select            = new SelectList(this.db.HypervisorTypes.OrderBy(x => x.Name), "Id", "Name", hypervisor.HypervisorType_Id);
            ViewBag.HypervisorWorkloadProfile_Id_Select = new SelectList(this.db.HypervisorWorkloadProfiles.OrderBy(x => x.Name), "Id", "Name", hypervisor.HypervisorWorkloadProfile_Id);
            return(this.View(hypervisor));
        }
Beispiel #21
0
            public Alu(Hypervisor hypervisor, int aluRef, string input, int firmwareVersion = 1)
            {
                Hypervisor      = hypervisor;
                AluRef          = aluRef;
                FirmwareVersion = firmwareVersion;
                Registers       = new Dictionary <string, long>();
                SendCount       = 0;
                Ptr             = 0;
                ReceiveQueue    = new Queue <long>();

                var lines = GetLines(input);

                Instructions = new Instruction[lines.Length];
                foreach (var line in lines)
                {
                    Instructions[Ptr++] = Instruction.GetInstruction(line, firmwareVersion);
                }
                Ptr = 0;
            }
        /// <summary>
        /// Determines the best Hypervisor from the List of managed Hypervisors for the Vm <paramref name="vm"/> and adds the Vm to it.
        /// </summary>
        /// <param name="vm">The Vm that should be added to one of the managed Hypervisors</param>
        public void AddVm(Vm vm)
        {
            // Find the Hypervisor, where adding the Vm leads to the best result (equally distributed load).
            Hypervisor bestCandidate = FindBestHypervisor(vm);

            if (bestCandidate != null)
            {
                // update number of free Hypervisors (Hypervisors without any Vm)
                if (bestCandidate.CurrentLoadAbsolute == 0)
                {
                    _numFreeHypervisors--;
                }

                // Add the Vm to the Hypervisor.
                bestCandidate.AddVm(vm);
            }
            else
            {
                // No Hypervisor with enough capacity for the Vm found.
                Console.WriteLine($"WARNING - not enough free capacity for adding Vm {vm.Id}");
            }
        }
        /// <summary>
        /// Populates properties on the view model from an instance of the
        /// Hypervisor class.
        /// </summary>
        /// <param name="model">The Hypervisor to reference.</param>
        private void ReadEntityProperties(Hypervisor model)
        {
            // Related entities.
            this.HypervisorType = model.HypervisorType;

            if (model.HypervisorGroup != null)
            {
                this.HypervisorGroup = new HypervisorGroupReferenceViewModel(model.HypervisorGroup);
            }
            else
            {
                this.HypervisorGroup = null;
            }

            // IHasCapacity Members.
            this.TotalCapacity       = model.TotalCapacity;
            this.UsedCapacity        = model.UsedCapacity;
            this.UsedCapacityPercent = model.UsedCapacityPercent;

            // Notes and Tags.
            this.Notes = new NoteCollectionListViewModel(model.Notes);
            this.Tags  = new TagCollectionListViewModel(model.TagsMetas);
        }
        /// <summary>
        /// Finds the best matching Hypervisor for a Vm out of the List of managed Hypervisors.
        /// This is done by assigning the Vm to each Hypervisor consecutively and calculating the average deviation from the average load
        /// this would cause. Then the Hypervisor that causes the best result (lowest overall deviation) will be picked.
        /// </summary>
        /// <param name="vm">The Vm for which the best matching Hypervisor should be found.</param>
        /// <returns></returns>
        private Hypervisor FindBestHypervisor(Vm vm)
        {
            Hypervisor result = null;

            // temporary array to calculate the overall-deviation for each candidate without changing the Hypervisors current load.
            double[] tmpLoad         = _managedHypervisors.Select(hv => hv.CurrentLoadPercent).ToArray();
            double   lowestDeviation = 100;

            for (var i = 0; i < _managedHypervisors.Count; i++)
            {
                var hv = _managedHypervisors[i];

                // Only if the remaining capacity is large enough for the new Vm
                if (hv.CheckIfVmFits(vm))
                {
                    // calculate the new load of the Hypervisor after adding this Vm.
                    tmpLoad[i] = hv.CalcLoadAfterAddingVm(vm);
                    // Calculate the average load of all (non empty) hypervisors,
                    // and the average deviation from the average load for all Hypervisors.
                    var tmpAverage          = tmpLoad.Sum() / tmpLoad.Where(l => l > 0).Count();
                    var tmpAverageDeviation = tmpLoad.Select(l => Math.Abs(tmpAverage - l)).Sum() / tmpLoad.Length;
                    // restore the load to its original value for the next iteration.
                    tmpLoad[i] = hv.CurrentLoadPercent;

                    // Find the lowest overall deviation. If the deviation caused by this Hypervisor is equal to the
                    // lowest deviation so far, the Hypervisor with the lower load after adding the Vm is chosen.
                    if ((tmpAverageDeviation < lowestDeviation) ||
                        (tmpAverageDeviation == lowestDeviation && result.CalcLoadAfterAddingVm(vm) > hv.CalcLoadAfterAddingVm(vm)))
                    {
                        lowestDeviation = tmpAverageDeviation;
                        result          = hv;
                    }
                }
            }

            return(result);
        }
Beispiel #25
0
        private async Task WriteVirtualMachinesToDbAsync(Hypervisor hyperV, List <string> virtualMachines)
        {
            if (virtualMachines == null || virtualMachines.Count == 0)
            {
                return;
            }

            if (hyperV.VirtualMachines != null && hyperV.VirtualMachines.Count > 0)
            {
                var machinesInDb = hyperV.VirtualMachines.Select(m => m.Name);
                virtualMachines = virtualMachines.Except(machinesInDb, StringComparer.OrdinalIgnoreCase)
                                  .ToList();
            }

            foreach (var machine in virtualMachines)
            {
                var vm = new VirtualMachine();
                vm.Hypervisor = hyperV;
                vm.Name       = machine;
                _context.VirtualMachines.Add(vm);
            }

            await _context.SaveChangesAsync();
        }
Beispiel #26
0
 public void DeleteHypervisor(Hypervisor hv)
 {
     new DAL.HypervisorDAO().Delete(hv);
 }
Beispiel #27
0
 public async Task <HypervisorNode> GetPrimaryHypervisorNode(Hypervisor hypervisor)
 {
     return(await _context.HypervisorNodes
            .Where(n => n.HypervisorId == hypervisor.Id && n.Primary)
            .FirstAsync());
 }
Beispiel #28
0
 public string GetProxmoxPassword(Hypervisor hypervisor)
 {
     return(Cryptography.DecryptString(hypervisor.Password, _encryptionKey));
 }
 /// <summary>
 /// Initializes a new instance of the
 /// <see cref="HypervisorReferenceViewModel" /> class.
 /// </summary>
 /// <param name="model">The Hypervisor to
 /// reference.</param>
 public HypervisorReferenceViewModel(Hypervisor model)
 {
     this.Id       = model.Id;
     this.Name     = model.Name;
     this.Inactive = model.Inactive;
 }
 /// <summary>
 /// Initializes a new instance of the
 /// <see cref="HypervisorDetailsViewModel"/> class.
 /// </summary>
 /// <param name="model">The Hypervisor to reference.</param>
 public HypervisorDetailsViewModel(Hypervisor model)
     : base(model)
 {
     // Properties from the entity, notes and tags.
     this.ReadEntityProperties(model);
 }