2025-01-08 22:42:17 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (cluster *Cluster) Init(pve ProxmoxClient) {
|
|
|
|
cluster.pve = pve
|
|
|
|
}
|
|
|
|
|
2025-01-20 21:42:13 +00:00
|
|
|
func (cluster *Cluster) Sync() error {
|
|
|
|
// aquire lock on cluster, release on return
|
|
|
|
cluster.lock.Lock()
|
|
|
|
defer cluster.lock.Unlock()
|
|
|
|
|
2025-01-08 22:42:17 +00:00
|
|
|
cluster.Hosts = make(map[string]*Host)
|
|
|
|
|
|
|
|
// get all nodes
|
|
|
|
nodes, err := cluster.pve.Nodes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// for each node:
|
|
|
|
for _, hostName := range nodes {
|
|
|
|
// rebuild node
|
2025-01-20 21:42:13 +00:00
|
|
|
err := cluster.RebuildHost(hostName)
|
2025-01-08 22:42:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-01-20 21:42:13 +00:00
|
|
|
// get a node in the cluster
|
|
|
|
func (cluster *Cluster) GetHost(hostName string) (*Host, error) {
|
|
|
|
host_ch := make(chan *Host)
|
|
|
|
err_ch := make(chan error)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// aquire cluster lock
|
|
|
|
cluster.lock.Lock()
|
|
|
|
defer cluster.lock.Unlock()
|
|
|
|
// get host
|
|
|
|
host, ok := cluster.Hosts[hostName]
|
|
|
|
if !ok {
|
|
|
|
host_ch <- nil
|
|
|
|
err_ch <- fmt.Errorf("%s not in cluster", hostName)
|
|
|
|
}
|
|
|
|
// aquire host lock to wait in case of a concurrent write
|
|
|
|
host.lock.Lock()
|
|
|
|
defer host.lock.Unlock()
|
|
|
|
|
|
|
|
host_ch <- host
|
|
|
|
err_ch <- nil
|
|
|
|
}()
|
|
|
|
|
|
|
|
host := <-host_ch
|
|
|
|
err := <-err_ch
|
|
|
|
return host, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cluster *Cluster) RebuildHost(hostName string) error {
|
2025-01-08 22:42:17 +00:00
|
|
|
host, err := cluster.pve.Node(hostName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2025-01-20 21:42:13 +00:00
|
|
|
|
|
|
|
// aquire lock on host, release on return
|
|
|
|
host.lock.Lock()
|
|
|
|
defer host.lock.Unlock()
|
|
|
|
|
|
|
|
cluster.Hosts[hostName] = host
|
2025-01-08 22:42:17 +00:00
|
|
|
|
|
|
|
// get node's VMs
|
|
|
|
vms, err := host.VirtualMachines()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2025-01-20 21:42:13 +00:00
|
|
|
|
2025-01-08 22:42:17 +00:00
|
|
|
}
|
|
|
|
for _, vmid := range vms {
|
|
|
|
err := host.RebuildVM(vmid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get node's CTs
|
|
|
|
cts, err := host.Containers()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, vmid := range cts {
|
|
|
|
err := host.RebuildCT(vmid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-01-20 21:42:13 +00:00
|
|
|
func (host *Host) GetInstance(vmid uint) (*Instance, error) {
|
|
|
|
instance_ch := make(chan *Instance)
|
|
|
|
err_ch := make(chan error)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// aquire host lock
|
|
|
|
host.lock.Lock()
|
|
|
|
defer host.lock.Unlock()
|
|
|
|
// get instance
|
|
|
|
instance, ok := host.Instances[vmid]
|
|
|
|
if !ok {
|
|
|
|
instance_ch <- nil
|
|
|
|
err_ch <- fmt.Errorf("vmid %d not in host %s", vmid, host.Name)
|
|
|
|
}
|
|
|
|
// aquire instance lock to wait in case of a concurrent write
|
|
|
|
instance.lock.Lock()
|
|
|
|
defer instance.lock.Unlock()
|
|
|
|
|
|
|
|
instance_ch <- instance
|
|
|
|
err_ch <- nil
|
|
|
|
}()
|
|
|
|
|
|
|
|
instance := <-instance_ch
|
|
|
|
err := <-err_ch
|
|
|
|
return instance, err
|
|
|
|
}
|
|
|
|
|
2025-01-08 22:42:17 +00:00
|
|
|
func (host *Host) RebuildVM(vmid uint) error {
|
|
|
|
instance, err := host.VirtualMachine(vmid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2025-01-20 21:42:13 +00:00
|
|
|
// aquire lock on instance, release on return
|
|
|
|
instance.lock.Lock()
|
|
|
|
defer instance.lock.Unlock()
|
|
|
|
|
|
|
|
host.Instances[vmid] = instance
|
2025-01-08 22:42:17 +00:00
|
|
|
|
|
|
|
for volid := range instance.configDisks {
|
|
|
|
instance.RebuildVolume(host, volid)
|
|
|
|
}
|
|
|
|
|
|
|
|
for netid := range instance.configNets {
|
|
|
|
instance.RebuildNet(netid)
|
|
|
|
}
|
|
|
|
|
2025-01-10 01:08:44 +00:00
|
|
|
for deviceid := range instance.configHostPCIs {
|
2025-01-20 21:42:13 +00:00
|
|
|
instance.RebuildDevice(host, deviceid)
|
2025-01-10 01:08:44 +00:00
|
|
|
}
|
|
|
|
|
2025-01-08 22:42:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (host *Host) RebuildCT(vmid uint) error {
|
|
|
|
instance, err := host.Container(vmid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2025-01-20 21:42:13 +00:00
|
|
|
// aquire lock on instance, release on return
|
|
|
|
instance.lock.Lock()
|
|
|
|
defer instance.lock.Unlock()
|
|
|
|
|
|
|
|
host.Instances[vmid] = instance
|
2025-01-08 22:42:17 +00:00
|
|
|
|
|
|
|
for volid := range instance.configDisks {
|
|
|
|
instance.RebuildVolume(host, volid)
|
|
|
|
}
|
|
|
|
|
|
|
|
for netid := range instance.configNets {
|
|
|
|
instance.RebuildNet(netid)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (instance *Instance) RebuildVolume(host *Host, volid string) error {
|
|
|
|
volumeDataString := instance.configDisks[volid]
|
|
|
|
|
2025-01-20 21:42:13 +00:00
|
|
|
volume, _, _, err := GetVolumeInfo(host, volumeDataString)
|
2025-01-08 22:42:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2025-01-20 21:42:13 +00:00
|
|
|
instance.Volumes[volid] = volume
|
2025-01-08 22:42:17 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (instance *Instance) RebuildNet(netid string) error {
|
|
|
|
net := instance.configNets[netid]
|
|
|
|
idnum, err := strconv.ParseUint(strings.TrimPrefix(netid, "net"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
netinfo, err := GetNetInfo(net)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-01-20 21:42:13 +00:00
|
|
|
instance.Nets[uint(idnum)] = netinfo
|
2025-01-08 22:42:17 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-01-20 21:42:13 +00:00
|
|
|
func (instance *Instance) RebuildDevice(host *Host, deviceid string) error {
|
2025-01-10 01:08:44 +00:00
|
|
|
instanceDevice, ok := instance.configHostPCIs[deviceid]
|
|
|
|
if !ok { // if device does not exist
|
|
|
|
return fmt.Errorf("%s not found in devices", deviceid)
|
|
|
|
}
|
|
|
|
|
|
|
|
hostDeviceBusID := strings.Split(instanceDevice, ",")[0]
|
|
|
|
|
|
|
|
instanceDeviceBusID, err := strconv.ParseUint(strings.TrimPrefix(deviceid, "hostpci"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if DeviceBusIDIsSuperDevice(hostDeviceBusID) {
|
2025-01-14 04:44:18 +00:00
|
|
|
devices := []*Device{}
|
|
|
|
for k, v := range host.Devices {
|
|
|
|
if DeviceBusIDIsSubDevice(k, hostDeviceBusID) {
|
|
|
|
v.Reserved = true
|
|
|
|
devices = append(devices, v)
|
|
|
|
}
|
2025-01-10 01:08:44 +00:00
|
|
|
}
|
2025-01-14 04:44:18 +00:00
|
|
|
instance.Devices[uint(instanceDeviceBusID)] = devices
|
2025-01-10 01:08:44 +00:00
|
|
|
} else {
|
2025-01-14 04:44:18 +00:00
|
|
|
devices := []*Device{}
|
|
|
|
v := host.Devices[hostDeviceBusID]
|
2025-01-10 20:47:33 +00:00
|
|
|
v.Reserved = true
|
2025-01-14 04:44:18 +00:00
|
|
|
instance.Devices[uint(instanceDeviceBusID)] = devices
|
2025-01-10 01:08:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|