Skip to content

Commit

Permalink
Merge pull request #52 from akhiljns/healthBasedCheck
Browse files Browse the repository at this point in the history
health based check before polling vm
  • Loading branch information
mhewedy authored Apr 8, 2024
2 parents 93972cc + 419c088 commit f2afdfb
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 21 deletions.
7 changes: 3 additions & 4 deletions hypervisor/base/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type Hypervisor interface {

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

HealthCheck(vmName string) (*string, error)

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

ShowGUI(vmName string) error
Expand Down Expand Up @@ -85,10 +87,7 @@ func NewSubnet(anIp, netmask string) (*Subnet, error) {

func (c *Subnet) HasNext() bool {
max := c.start + uint32(c.Len)
if c.current < max {
return true
}
return false
return c.current < max
}

func (c *Subnet) Next() *Subnet {
Expand Down
9 changes: 9 additions & 0 deletions hypervisor/hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ func GetVMProperty(vmName, property string) (*string, error) {
return h.GetVMProperty(vmName, property)
}

func HealthCheck(vmName string) (*string, error) {
h, err := detect()
if err != nil {
return nil, err
}

return h.HealthCheck(vmName)
}

func ShowGUI(vmName string) error {
h, err := detect()
if err != nil {
Expand Down
45 changes: 30 additions & 15 deletions hypervisor/virtualbox/virtualbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import (

const (
ipProperty = "/VirtualBox/GuestInfo/Net/0/V4/IP"
vmStatus = "/VirtualBox/GuestInfo/Net/0/Status"
)

var propertyMap = map[string]string{
"ip": ipProperty,
"ip": ipProperty,
"status": vmStatus,
}

var Instance = &virtualbox{}
Expand Down Expand Up @@ -129,36 +131,37 @@ func (*virtualbox) Modify(vmName string, cpus int, mem int) error {
return nil
}

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

return healthCheck, nil
}

func (*virtualbox) GetVMProperty(vmName, property string) (*string, error) {
prop, ok := propertyMap[property]
if !ok {
return nil, fmt.Errorf("property %s not found", property)
}

guestProperty, err := vboxManage("guestproperty",
"enumerate",
vmName).Call()

guestProperty, err := vboxManage("guestproperty", "enumerate", vmName).Call()
if err != nil {
return nil, err
}

time.Sleep(30 * time.Second)

index := strings.Index(guestProperty, prop)
if index == -1 {
return nil, fmt.Errorf("IP address property not found")
return nil, fmt.Errorf(property + " property not found")
}

quoteIndex := strings.Index(guestProperty[index:], "'")
if quoteIndex == -1 {
return nil, fmt.Errorf("unexpected format")
propertyValue, err := getValueFromProperty(guestProperty, index)
if err != nil {
return nil, err
}

// Extract the IP address between single quotes
ipAddress := guestProperty[index+quoteIndex+1 : index+quoteIndex+1+strings.Index(guestProperty[index+quoteIndex+1:], "'")]

return &ipAddress, nil
return propertyValue, nil
}

func (*virtualbox) ShowGUI(vmName string) error {
Expand Down Expand Up @@ -291,3 +294,15 @@ func (*virtualbox) GetSubnet() (*base.Subnet, error) {

return base.NewSubnet(bridgeInfo[0], bridgeInfo[1])
}

func getValueFromProperty(guestProperty string, index int) (*string, error) {
quoteIndex := strings.Index(guestProperty[index:], "'")
if quoteIndex == -1 {
return nil, fmt.Errorf("unexpected format")
}

// Extract the IP address between single quotes
value := guestProperty[index+quoteIndex+1 : index+quoteIndex+1+strings.Index(guestProperty[index+quoteIndex+1:], "'")]

return &value, nil
}
5 changes: 3 additions & 2 deletions images/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package images

import (
"fmt"
"github.com/mhewedy/vermin/images/vagrant"
"github.com/schollz/progressbar/v3"
"io"
"io/ioutil"
"net/http"
"os"
"strings"

"github.com/mhewedy/vermin/images/vagrant"
"github.com/schollz/progressbar/v3"
)

type image struct {
Expand Down
22 changes: 22 additions & 0 deletions ip/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"sort"
"strings"
"time"

"github.com/mhewedy/vermin/hypervisor"
"github.com/mhewedy/vermin/log"
Expand All @@ -15,6 +16,27 @@ type addr struct {
}

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

health, 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 f2afdfb

Please sign in to comment.