Skip to content

Commit

Permalink
Merge pull request #56 from mhewedy/redesing-health-check-to-be-generic
Browse files Browse the repository at this point in the history
redesign health check to be generic
  • Loading branch information
akhiljns authored Apr 8, 2024
2 parents f2afdfb + bf7ef86 commit f084a3e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
2 changes: 1 addition & 1 deletion hypervisor/base/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Hypervisor interface {

Modify(vmName string, cpus int, mem int) error

HealthCheck(vmName string) (*string, error)
HealthCheck(vmName string) (bool, error)

GetVMProperty(vmName, property string) (*string, error)

Expand Down
27 changes: 24 additions & 3 deletions hypervisor/hypervisor.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package hypervisor

import (
"fmt"
"path/filepath"
"reflect"
"strings"
"time"

"github.com/mhewedy/vermin/db"
"github.com/mhewedy/vermin/hypervisor/base"
Expand Down Expand Up @@ -102,13 +104,32 @@ func GetVMProperty(vmName, property string) (*string, error) {
return h.GetVMProperty(vmName, property)
}

func HealthCheck(vmName string) (*string, error) {
// HealthCheck will block up to 5 minutes until the VM is healthy or will return an error otherwise
func HealthCheck(vmName string) error {
h, err := detect()
if err != nil {
return nil, err
return err
}

return h.HealthCheck(vmName)
ok, err := h.HealthCheck(vmName)
if err != nil {
return err
}
// Wait for health status to ok for up to 5 minutes
timeout := time.After(5 * time.Minute)
for !ok {
select {
case <-timeout:
return fmt.Errorf("timeout waiting for %s to be healthy", vmName)
default:
time.Sleep(10 * time.Second) // Check every 10 seconds
ok, err = h.HealthCheck(vmName)
if err != nil {
return err
}
}
}
return nil
}

func ShowGUI(vmName string) error {
Expand Down
6 changes: 3 additions & 3 deletions hypervisor/virtualbox/virtualbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ func (*virtualbox) Modify(vmName string, cpus int, mem int) error {
return nil
}

func (*virtualbox) HealthCheck(vmName string) (*string, error) {
func (*virtualbox) HealthCheck(vmName string) (bool, error) {
healthCheck, err := Instance.GetVMProperty(vmName, "status")
if err != nil {
return nil, err
return false, err
}

return healthCheck, nil
return *healthCheck == "Up", nil
}

func (*virtualbox) GetVMProperty(vmName, property string) (*string, error) {
Expand Down
23 changes: 3 additions & 20 deletions ip/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package ip

import (
"fmt"
"sort"
"strings"
"time"

"github.com/mhewedy/vermin/hypervisor"
"github.com/mhewedy/vermin/log"
"sort"
"strings"
)

type addr struct {
Expand All @@ -17,26 +15,11 @@ type addr struct {

func GetIpAddress(vmName string) (string, error) {

health, err := hypervisor.HealthCheck(vmName)
err := hypervisor.HealthCheck(vmName)
if err != nil {
return "", err
}

// Wait for health status to be "Up" for up to 5 minutes
timeout := time.After(5 * time.Minute)
for *health != "Up" {
select {
case <-timeout:
return "", fmt.Errorf("timeout waiting for VM health status to be 'Up'")
default:
time.Sleep(10 * time.Second) // Check every 10 seconds
health, err = hypervisor.HealthCheck(vmName)
if err != nil {
return "", err
}
}
}

ipAddr, err := hypervisor.GetVMProperty(vmName, "ip")
if err != nil {
return "", err
Expand Down

0 comments on commit f084a3e

Please sign in to comment.