various improvements
Some checks are pending
CI / Lint (push) Waiting to run
CI / Test (push) Waiting to run
CI / End-to-End (push) Waiting to run
CI / Publish Docker (push) Blocked by required conditions
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-09-30 12:01:39 +10:00
parent 6f5d21fa71
commit ea63ffa178
11 changed files with 146 additions and 31 deletions

View File

@@ -6,7 +6,10 @@ import (
"errors"
"fmt"
"log/slog"
"strings"
"time"
"vctp/db/queries"
"vctp/internal/utils"
"vctp/internal/vcenter"
"github.com/vmware/govmomi/vim25/mo"
@@ -17,9 +20,10 @@ import (
func (c *CronTask) RunVcenterPoll(ctx context.Context, logger *slog.Logger) error {
var matchFound bool
// TODO - reload settings in case vcenter list has changed
// reload settings in case vcenter list has changed
c.Settings.ReadYMLSettings()
for _, url := range c.Settings.Settings.VcenterAddresses {
for _, url := range c.Settings.Values.Settings.VcenterAddresses {
c.Logger.Debug("connecting to vcenter", "url", url)
vc := vcenter.New(c.Logger, c.VcCreds)
vc.Login(url)
@@ -43,9 +47,17 @@ func (c *CronTask) RunVcenterPoll(ctx context.Context, logger *slog.Logger) erro
// Iterate VMs from vcenter and see if they were in the database
for _, vm := range vms {
matchFound = false
// Skip any vCLS VMs
if strings.HasPrefix(vm.Name(), "vCLS-") {
c.Logger.Debug("Skipping internal VM", "vm_name", vm.Name())
continue
}
// TODO - should we compare the UUID as well?
for _, dbvm := range results {
if dbvm.VmId.String == vm.Reference().Value {
c.Logger.Debug("Found match for VM", "name", dbvm.Name, "id", dbvm.VmId.String)
c.Logger.Debug("Found match for VM", "vm_name", dbvm.Name, "id", dbvm.VmId.String)
matchFound = true
break
}
@@ -61,6 +73,9 @@ func (c *CronTask) RunVcenterPoll(ctx context.Context, logger *slog.Logger) erro
// retrieve VM properties and insert into inventory
c.AddVmToInventory(vmObj, vc, ctx)
// add sleep to slow down mass VM additions
utils.SleepWithContext(ctx, (10 * time.Millisecond))
}
}
c.Logger.Debug("Finished checking vcenter", "url", url)
@@ -190,6 +205,7 @@ func (c *CronTask) AddVmToInventory(vmObject *mo.VirtualMachine, vc *vcenter.Vce
ProvisionedDisk: sql.NullFloat64{Float64: totalDiskGB, Valid: totalDiskGB > 0},
Folder: sql.NullString{String: folderPath, Valid: folderPath != ""},
ResourcePool: sql.NullString{String: rpName, Valid: rpName != ""},
VmUuid: sql.NullString{String: vmObject.Config.Uuid, Valid: vmObject.Config.Uuid != ""},
SrmPlaceholder: srmPlaceholder,
IsTemplate: isTemplate,
PoweredOn: poweredOn,

View File

@@ -23,6 +23,7 @@ func (c *CronTask) RunVmCheck(ctx context.Context, logger *slog.Logger) error {
poweredOn string
folderPath string
rpName string
vmUuid string
)
dateCmp := time.Now().AddDate(0, 0, -1).Unix()
@@ -60,6 +61,7 @@ func (c *CronTask) RunVmCheck(ctx context.Context, logger *slog.Logger) error {
totalDiskGB = 0
isTemplate = "FALSE"
folderPath = ""
vmUuid = ""
} else {
c.Logger.Debug("found VM")
srmPlaceholder = "FALSE" // Default assumption
@@ -69,6 +71,8 @@ func (c *CronTask) RunVmCheck(ctx context.Context, logger *slog.Logger) error {
if vmObject.Config != nil {
numRam = vmObject.Config.Hardware.MemoryMB
numVcpus = vmObject.Config.Hardware.NumCPU
vmUuid = vmObject.Config.Uuid
var totalDiskBytes int64
// Calculate the total disk allocated in GB
@@ -148,6 +152,7 @@ func (c *CronTask) RunVmCheck(ctx context.Context, logger *slog.Logger) error {
ProvisionedDisk: sql.NullFloat64{Float64: totalDiskGB, Valid: totalDiskGB > 0},
Folder: sql.NullString{String: folderPath, Valid: folderPath != ""},
ResourcePool: sql.NullString{String: rpName, Valid: rpName != ""},
VmUuid: sql.NullString{String: vmUuid, Valid: vmUuid != ""},
SrmPlaceholder: srmPlaceholder,
IsTemplate: isTemplate,
PoweredOn: poweredOn,

View File

@@ -11,6 +11,6 @@ import (
type CronTask struct {
Logger *slog.Logger
Database db.Database
Settings settings.SettingsYML
Settings *settings.Settings
VcCreds *vcenter.VcenterLogin
}