public async Task LinkVmToBridges(UserLab userLab, UserLabVm vm, ProxmoxApi api, HypervisorNode node)
 {
     foreach (var templateInterface in vm.LabVm.TemplateInterfaces)
     {
         var bridge = userLab.BridgeInstances.First(b => b.BridgeTemplate == templateInterface.BridgeTemplate);
         await api.AddBridgeToVm(vm.ProxmoxVmId, bridge.HypervisorInterfaceId, templateInterface.InterfaceNumber, node.Name);
     }
 }
예제 #2
0
        public void Setup()
        {
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json", false, true);

            IConfiguration configuration = builder.Build();
            var            apiSection    = configuration.GetSection("Api");

            _hypervisorNode = new HypervisorNode
            {
                Name       = apiSection["Node"],
                Hypervisor = new Hypervisor
                {
                    Host     = apiSection["Host"],
                    UserName = apiSection["Username"],
                    Password = apiSection["Password"]
                }
            };
            _vmId  = int.Parse(apiSection["VmId"]);
            client = new ProxmoxApi(_hypervisorNode, apiSection["Password"]);
        }
        public async Task <int> CreateVmAndImportDisk(string name, SshClient ssh, SftpClient sftp, ProxmoxApi api, string dirPath)
        {
            var      files   = sftp.ListDirectory(dirPath).ToList();
            SftpFile vmdk    = files.First(f => f.GetExtension() == "vmdk");
            SftpFile ovfFile = files.First(f => f.GetExtension() == "ovf");

            Debug.Assert(vmdk != null, "vmdk != null");
            Debug.Assert(ovfFile != null, "ovfFile != null");
            await using var stream = new MemoryStream();
            sftp.DownloadFile(ovfFile.FullName, stream);
            stream.Position  = 0;
            using var reader = new StreamReader(stream);
            var ovf = ParseOvf(await reader.ReadToEndAsync());

            var vmId = await api.CreateVm(name.ToSafeId(), ovf.MemorySizeMb);

            var result = ssh.RunCommand($"qm importdisk {vmId} {vmdk.FullName} nasapp -format qcow2");

            if (result.Error != "")
            {
                throw new Exception("Failed to import disk! Error: " + result.Error);
            }

            return(vmId);
        }
        public async Task LinkVmToBridgesAndAddInstances(UserLab userLab, UserLabVm vm, ProxmoxApi api, HypervisorNode node)
        {
            await LinkVmToBridges(userLab, vm, api, node);

            foreach (var templateInterface in vm.LabVm.TemplateInterfaces)
            {
                var bridge = userLab.BridgeInstances.First(b => b.BridgeTemplate == templateInterface.BridgeTemplate);
                vm.InterfaceInstances.Add(new VmInterfaceInstance
                {
                    BridgeInstance = bridge,
                    Template       = templateInterface
                });
            }
        }