Skip to content

Commit

Permalink
Implemented delayed validation, and added FetchWithProgress to Image.
Browse files Browse the repository at this point in the history
  • Loading branch information
rajch committed Jul 15, 2022
1 parent 53c8bbc commit 12ffb35
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 59 deletions.
42 changes: 29 additions & 13 deletions driver-machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ As of VBoxManage 6.0.8r130520, the format is:
*/
func (vd *Driver) ListMachines() ([]drivercore.Machine, error) {
if !vd.validate() {
return nil, vd
}

output, err := workspace.Runwithresults(
vd.vboxmanagepath,
"list",
Expand Down Expand Up @@ -72,6 +76,10 @@ func (vd *Driver) ListMachines() ([]drivercore.Machine, error) {
// VBoxManage guestproperty enumerate <machinename> --patterns "/VirtualBox/GuestInfo/Net/0/*|/kutti/*|/VirtualBox/GuestInfo/OS/LoggedInUsers"
// and parsing the enumerated properties.
func (vd *Driver) GetMachine(machinename string, clustername string) (drivercore.Machine, error) {
if !vd.validate() {
return nil, vd
}

machine := &Machine{
driver: vd,
name: machinename,
Expand All @@ -92,6 +100,10 @@ func (vd *Driver) GetMachine(machinename string, clustername string) (drivercore
// It does this by running the command:
// VBoxManage unregistervm "<hostname>" --delete
func (vd *Driver) DeleteMachine(machinename string, clustername string) error {
if !vd.validate() {
return vd
}

qualifiedmachinename := vd.QualifiedMachineName(machinename, clustername)
output, err := workspace.Runwithresults(
vd.vboxmanagepath,
Expand Down Expand Up @@ -122,9 +134,13 @@ var ipRegex, _ = regexp.Compile(`^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-
// In the second case, if the caller does not actually want the machine, they should
// call DeleteMachine afterwards.
func (vd *Driver) NewMachine(machinename string, clustername string, k8sversion string) (drivercore.Machine, error) {
if !vd.validate() {
return nil, vd
}

qualifiedmachinename := vd.QualifiedMachineName(machinename, clustername)

kuttilog.Println(2, "Importing image...")
kuttilog.Println(kuttilog.Info, "Importing image...")

ovafile, err := imagepathfromk8sversion(k8sversion)
if err != nil {
Expand Down Expand Up @@ -169,7 +185,7 @@ func (vd *Driver) NewMachine(machinename string, clustername string, k8sversion
}

// Attach newly created VM to NAT Network
kuttilog.Println(2, "Attaching host to network...")
kuttilog.Println(kuttilog.Info, "Attaching host to network...")
newmachine := &Machine{
driver: vd,
name: machinename,
Expand All @@ -195,7 +211,7 @@ func (vd *Driver) NewMachine(machinename string, clustername string, k8sversion
}

// Start the host
kuttilog.Println(2, "Starting host...")
kuttilog.Println(kuttilog.Info, "Starting host...")
err = newmachine.Start()
if err != nil {
return newmachine, err
Expand All @@ -205,19 +221,19 @@ func (vd *Driver) NewMachine(machinename string, clustername string, k8sversion

// Change the name
for renameretries := 1; renameretries < 4; renameretries++ {
kuttilog.Printf(2, "Renaming host (attempt %v/3)...", renameretries)
kuttilog.Printf(kuttilog.Info, "Renaming host (attempt %v/3)...", renameretries)
err = renamemachine(newmachine, machinename)
if err == nil {
break
}
kuttilog.Printf(2, "Failed. Waiting %v seconds before retry...", renameretries*10)
kuttilog.Printf(kuttilog.Info, "Failed. Waiting %v seconds before retry...", renameretries*10)
time.Sleep(time.Duration(renameretries*10) * time.Second)
}

if err != nil {
return newmachine, err
}
kuttilog.Println(2, "Host renamed.")
kuttilog.Println(kuttilog.Info, "Host renamed.")

// Save the IP Address
// The first IP address should be DHCP-assigned, and therefore start with
Expand All @@ -227,7 +243,7 @@ func (vd *Driver) NewMachine(machinename string, clustername string, k8sversion
// times.
ipSet := false
for ipretries := 1; ipretries < 4; ipretries++ {
kuttilog.Printf(2, "Fetching IP address (attempt %v/3)...", ipretries)
kuttilog.Printf(kuttilog.Info, "Fetching IP address (attempt %v/3)...", ipretries)

var ipaddress string
ipprops := []string{propIPAddress, propIPAddress2, propIPAddress3}
Expand All @@ -243,28 +259,28 @@ func (vd *Driver) NewMachine(machinename string, clustername string, k8sversion
}
}

if kuttilog.V(4) {
kuttilog.Printf(4, "value of property %v is %v, and present is %v.", ipprop, ipaddr, present)
kuttilog.Printf(4, "Regex match is %v, and prefix match is %v.", ipRegex.MatchString(ipaddr), strings.HasPrefix(ipaddr, ipNetAddr))
if kuttilog.V(kuttilog.Debug) {
kuttilog.Printf(kuttilog.Debug, "value of property %v is %v, and present is %v.", ipprop, ipaddr, present)
kuttilog.Printf(kuttilog.Debug, "Regex match is %v, and prefix match is %v.", ipRegex.MatchString(ipaddr), strings.HasPrefix(ipaddr, ipNetAddr))
}
}

if ipaddress != "" {
kuttilog.Printf(2, "Obtained IP address '%v'", ipaddress)
kuttilog.Printf(kuttilog.Info, "Obtained IP address '%v'", ipaddress)
newmachine.setproperty(propSavedIPAddress, ipaddress)
ipSet = true
break
}

kuttilog.Printf(2, "Failed. Waiting %v seconds before retry...", ipretries*10)
kuttilog.Printf(kuttilog.Info, "Failed. Waiting %v seconds before retry...", ipretries*10)
time.Sleep(time.Duration(ipretries*10) * time.Second)
}

if !ipSet {
kuttilog.Printf(0, "Error: Failed to get IP address. You may have to delete this node and recreate it manually.")
}

kuttilog.Println(2, "Stopping host...")
kuttilog.Println(kuttilog.Info, "Stopping host...")
newmachine.Stop()
// newhost.WaitForStateChange(25)

Expand Down
12 changes: 12 additions & 0 deletions driver-network.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ each network. If there are zero networks, the output is:
*/
func (vd *Driver) ListNetworks() ([]drivercore.Network, error) {
if !vd.validate() {
return nil, vd
}

// The default pattern for all our network names is "*kuttinet"
output, err := workspace.Runwithresults(
vd.vboxmanagepath,
Expand Down Expand Up @@ -119,6 +123,10 @@ func (vd *Driver) GetNetwork(clustername string) (drivercore.Network, error) {
// It does this by running the command:
// VBoxManage natnetwork remove --netname <networkname>
func (vd *Driver) DeleteNetwork(clustername string) error {
if !vd.validate() {
return vd
}

netname := vd.QualifiedNetworkName(clustername)

output, err := workspace.Runwithresults(
Expand Down Expand Up @@ -160,6 +168,10 @@ func (vd *Driver) DeleteNetwork(clustername string) error {
// NewNetwork creates a new VirtualBox NAT network.
// It uses the CIDR common to all Kutti networks, and is dhcp-enabled at start.
func (vd *Driver) NewNetwork(clustername string) (drivercore.Network, error) {
if !vd.validate() {
return nil, vd
}

netname := vd.QualifiedNetworkName(clustername)

// Multiple VirtualBox NAT Networks can have the same IP range
Expand Down
44 changes: 44 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package drivervbox

import (
"fmt"

"github.com/kuttiproject/workspace"
)

const (
driverName = "vbox"
driverDescription = "Kutti driver for VirtualBox 6.0 and above"
Expand All @@ -18,6 +24,7 @@ var DefaultNetCIDR = "192.168.125.0/24"
// Driver implements the drivercore.Driver interface for VirtualBox.
type Driver struct {
vboxmanagepath string
validated bool
status string
errormessage string
}
Expand All @@ -42,12 +49,49 @@ func (vd *Driver) UsesNATNetworking() bool {
return true
}

func (vd *Driver) validate() bool {
if vd.validated {
return true
}

// find VBoxManage tool and set it
vbmpath, err := findvboxmanage()
if err != nil {
vd.status = "Error"
vd.errormessage = err.Error()
return false
}
vd.vboxmanagepath = vbmpath

// test VBoxManage version
vbmversion, err := workspace.Runwithresults(vbmpath, "--version")
if err != nil {
vd.status = "Error"
vd.errormessage = err.Error()
return false
}
var majorversion int
_, err = fmt.Sscanf(vbmversion, "%d", &majorversion)
if err != nil || majorversion < 6 {
err = fmt.Errorf("unsupported VBoxManage version %v. 6.0 and above are supported", vbmversion)
vd.status = "Error"
vd.errormessage = err.Error()
return false
}

vd.status = "Ready"
vd.validated = true
return true
}

// Status returns current driver status
func (vd *Driver) Status() string {
vd.validate()
return vd.status
}

func (vd *Driver) Error() string {
vd.validate()
if vd.status != "Error" {
return ""
}
Expand Down
2 changes: 1 addition & 1 deletion drivervbox-findvboxmanage.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !windows
//go:build !windows

package drivervbox

Expand Down
2 changes: 1 addition & 1 deletion drivervbox-findvboxmanage_windows.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build windows
//go:build windows

package drivervbox

Expand Down
69 changes: 33 additions & 36 deletions drivervbox.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,44 @@
package drivervbox

import (
"fmt"

"github.com/kuttiproject/drivercore"
"github.com/kuttiproject/workspace"
)

func init() {
driver := newvboxdriver()
driver := &Driver{} //newvboxdriver()

drivercore.RegisterDriver(driverName, driver)
}

func newvboxdriver() *Driver {
result := &Driver{}

// find VBoxManage tool and set it
vbmpath, err := findvboxmanage()
if err != nil {
result.status = "Error"
result.errormessage = err.Error()
return result
}

result.vboxmanagepath = vbmpath

// test VBoxManage version
vbmversion, err := workspace.Runwithresults(vbmpath, "--version")
if err != nil {
result.status = "Error"
result.errormessage = err.Error()
return result
}
var majorversion int
_, err = fmt.Sscanf(vbmversion, "%d", &majorversion)
if err != nil || majorversion < 6 {
err = fmt.Errorf("unsupported VBoxManage version %v. 6.0 and above are supported", vbmversion)
result.status = "Error"
result.errormessage = err.Error()
return result
}

result.status = "Ready"
return result
}
// func newvboxdriver() *Driver {
// result := &Driver{}

// // find VBoxManage tool and set it
// vbmpath, err := findvboxmanage()
// if err != nil {
// result.status = "Error"
// result.errormessage = err.Error()
// return result
// }

// result.vboxmanagepath = vbmpath

// // test VBoxManage version
// vbmversion, err := workspace.Runwithresults(vbmpath, "--version")
// if err != nil {
// result.status = "Error"
// result.errormessage = err.Error()
// return result
// }
// var majorversion int
// _, err = fmt.Sscanf(vbmversion, "%d", &majorversion)
// if err != nil || majorversion < 6 {
// err = fmt.Errorf("unsupported VBoxManage version %v. 6.0 and above are supported", vbmversion)
// result.status = "Error"
// result.errormessage = err.Error()
// return result
// }

// result.status = "Ready"
// return result
// }
Loading

0 comments on commit 12ffb35

Please sign in to comment.