Skip to content

Commit

Permalink
Suppress unnecessary box-related log messages
Browse files Browse the repository at this point in the history
Store `boxInstalled` status in package `box` to avoid unnecessary
queries to VBoxManage when the VM is not installed
  • Loading branch information
mplattu committed Sep 13, 2023
1 parent 1d6d37a commit cd9b309
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
39 changes: 35 additions & 4 deletions src/naksu/box/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const (
boxLowMemoryLimit = 8192 - 1024 // 8G minus 1G for display adapter
)

var boxInstalled bool

func calculateBoxCPUs() (int, error) {
detectedCores, err := host.GetCPUCoreCount()
if err != nil {
Expand Down Expand Up @@ -253,11 +255,12 @@ func StartEnvironmentStatusUpdate(environmentStatus *constants.EnvironmentStatus
for {
<-ticker.C

boxInstalled, boxInstalledErr := Installed()
if boxInstalledErr != nil {
log.Error("Could not query whether VM is installed: %v", boxInstalledErr)
boxInstalledStatus, err := Installed()
if err != nil {
log.Error("Could not query whether VM is installed: %v", err)
} else {
environmentStatus.BoxInstalled = (boxInstalledErr == nil) && boxInstalled
boxInstalled = (err == nil) && boxInstalledStatus
environmentStatus.BoxInstalled = boxInstalled
}

boxRunning, boxRunningErr := Running()
Expand All @@ -282,6 +285,10 @@ func Installed() (bool, error) {
}

func Running() (bool, error) {
if !boxInstalled {
return false, nil
}

isRunning, err := vboxmanage.IsVMRunning(boxName)

if err != nil {
Expand All @@ -293,11 +300,19 @@ func Running() (bool, error) {

// GetType returns the box type (e.g. "digabi/ktp-qa") of the current VM
func GetType() string {
if !boxInstalled {
return ""
}

return vboxmanage.GetVMProperty(boxName, "boxType")
}

// GetTypeLegend returns an user-readable type legend of the current VM
func GetTypeLegend() string {
if !boxInstalled {
return "-"
}

if TypeIsAbitti() {
return xlate.Get("Abitti server")
}
Expand Down Expand Up @@ -328,21 +343,37 @@ func TypeIsMatriculationExam() bool {

// GetVersion returns the version string (e.g. "SERVER7108X v69") of the current VM
func GetVersion() string {
if !boxInstalled {
return ""
}

return vboxmanage.GetVMProperty(boxName, "boxVersion")
}

// getDiskUUID returns the VirtualBox UUID for the image of the current VM
func getDiskUUID() string {
if !boxInstalled {
return ""
}

return vboxmanage.GetVMInfoByRegexp(boxName, "\"SATA Controller-ImageUUID-0-0\"=\"(.*?)\"")
}

// GetDiskLocation returns the full path of the current VM disk image.
func GetDiskLocation() string {
if !boxInstalled {
return ""
}

return vboxmanage.GetVMInfoByRegexp(boxName, "\"SATA Controller-0-0\"=\"(.*)\"")
}

// GetLogDir returns the full path of VirtualBox log directory
func GetLogDir() string {
if !boxInstalled {
return ""
}

return vboxmanage.GetVMInfoByRegexp(boxName, "LogFldr=\"(.*)\"")
}

Expand Down
15 changes: 6 additions & 9 deletions src/naksu/box/vboxmanage/vboxmanage.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,21 +313,18 @@ func IsVMRunning(vmName string) (bool, error) {

// IsVMInstalled returns true if given VM has been installed
func IsVMInstalled(vmName string) (bool, error) {
rawVMInfo, err := RunCommandWithoutLogging([]string{"showvminfo", "--machinereadable", vmName})
rawVMInfo, err := RunCommandWithoutLogging([]string{"list", "vms"})

if err != nil {
if strings.Contains(rawVMInfo, vBoxManageOutputNoVMInstalled) {
log.Debug("vboxmanage.IsVMInstalled: Box is not installed")

return false, nil
}

// Other error, return it to the caller
return false, err
}

// We got the showvminfo all right, so the machine is installed
return true, nil
if strings.Contains(rawVMInfo, fmt.Sprintf("\"%s\"", vmName)) {
return true, nil
}

return false, nil
}

// IsIstalled returns true if VBoxManage has been installed
Expand Down
10 changes: 7 additions & 3 deletions src/naksu/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,15 +466,15 @@ func mainUIStatusHandler(currentMainUIStatus mainUIStatusType) { //nolint:gocycl
func checkAbittiUpdate() (bool, string) {
availAbittiVersion := ""

currentBoxVersion := box.GetVersion()

boxInstalled, err := box.Installed()

if err != nil {
log.Error("Could not detect whether VM is installed: %v", err)
}

if (err == nil && !boxInstalled) || box.TypeIsAbitti() {
currentBoxVersion := box.GetVersion()

availAbittiVersion, err = download.GetAvailableVersion(constants.AbittiVersionURL)
if err == nil && currentBoxVersion != availAbittiVersion {
return true, availAbittiVersion
Expand Down Expand Up @@ -1198,7 +1198,11 @@ func RunUI() error { // nolint:whitespace
}
}

log.Debug("Currently installed box: %s %s", box.GetVersion(), box.GetType())
if environmentStatus.BoxInstalled {
log.Debug("Currently installed box is version '%s', type '%s'", box.GetVersion(), box.GetType())
} else {
log.Debug("There is no box installed")
}

logdelivery.DeleteLogCopyFiles()

Expand Down

0 comments on commit cd9b309

Please sign in to comment.