Skip to content

Commit

Permalink
Bumped dependencies, and cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
rajch committed Jul 18, 2022
1 parent 12ffb35 commit 84d920c
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 220 deletions.
58 changes: 4 additions & 54 deletions driver-machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,63 +14,14 @@ import (
)

func machinesBaseDir() (string, error) {
return workspace.Cachesubdir("driver-vbox-machines")
return workspace.CacheSubDir("driver-vbox-machines")
}

// QualifiedMachineName returns a name in the form <clustername>-<machinename>
func (vd *Driver) QualifiedMachineName(machinename string, clustername string) string {
return clustername + "-" + machinename
}

/*ListMachines parses the list of VMs returned by
VBoxManage list vms
As of VBoxManage 6.0.8r130520, the format is:
"Matsya" {e3509073-d188-4cca-8eaf-cb9f3be7ac4a}
"Krishna" {5d9b1b16-5059-42ae-a160-e93b470f940e}
"one" {06748689-7f4e-4915-8fbf-6111596f85a2}
"two" {eee169a7-09eb-473e-96be-5d37868c5d5e}
"minikube" {5bf78b43-3240-4f50-911b-fbc111d4d085}
"Node 1" {53b82a61-ae52-44c2-86d5-4c686502dd64}
*/
func (vd *Driver) ListMachines() ([]drivercore.Machine, error) {
if !vd.validate() {
return nil, vd
}

output, err := workspace.Runwithresults(
vd.vboxmanagepath,
"list",
"vms",
)
if err != nil {
return nil, fmt.Errorf("could not get list of VMs: %v", err)
}

// TODO: Write a better parser
result := []drivercore.Machine{}
lines := strings.Split(output, "\n")
if len(lines) < 1 {
return result, nil
}

actualcount := 0
for _, value := range lines {
line := strings.Split(value, " ")
if len(line) == 2 {
result = append(result, &Machine{
driver: vd,
name: trimQuotes(line[0]),
status: drivercore.MachineStatusUnknown,
})
actualcount++
}
}

return result[:actualcount], err
}

// GetMachine returns the named machine, or an error.
// It does this by running the command:
// VBoxManage guestproperty enumerate <machinename> --patterns "/VirtualBox/GuestInfo/Net/0/*|/kutti/*|/VirtualBox/GuestInfo/OS/LoggedInUsers"
Expand Down Expand Up @@ -105,7 +56,7 @@ func (vd *Driver) DeleteMachine(machinename string, clustername string) error {
}

qualifiedmachinename := vd.QualifiedMachineName(machinename, clustername)
output, err := workspace.Runwithresults(
output, err := workspace.RunWithResults(
vd.vboxmanagepath,
"unregistervm",
qualifiedmachinename,
Expand Down Expand Up @@ -162,7 +113,7 @@ func (vd *Driver) NewMachine(machinename string, clustername string, k8sversion
return nil, err
}

l, err := workspace.Runwithresults(
l, err := workspace.RunWithResults(
vd.vboxmanagepath,
"import",
ovafile,
Expand Down Expand Up @@ -194,7 +145,7 @@ func (vd *Driver) NewMachine(machinename string, clustername string, k8sversion
}
networkname := vd.QualifiedNetworkName(clustername)

_, err = workspace.Runwithresults(
_, err = workspace.RunWithResults(
vd.vboxmanagepath,
"modifyvm",
newmachine.qname(),
Expand Down Expand Up @@ -282,7 +233,6 @@ func (vd *Driver) NewMachine(machinename string, clustername string, k8sversion

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

newmachine.status = drivercore.MachineStatusStopped

Expand Down
115 changes: 4 additions & 111 deletions driver-network.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package drivervbox

import (
"errors"
"fmt"
"strings"

"github.com/kuttiproject/drivercore"
"github.com/kuttiproject/workspace"
Expand All @@ -14,111 +12,6 @@ func (vd *Driver) QualifiedNetworkName(clustername string) string {
return clustername + "kuttinet"
}

/*ListNetworks parses the list of NAT networks returned by
VBoxManage natnetwork list
As of VBoxManage 6.0.8r130520, the format is:
NAT Networks:
Name: KubeNet
Network: 10.0.2.0/24
Gateway: 10.0.2.1
IPv6: No
Enabled: Yes
Name: NatNetwork
Network: 10.0.2.0/24
Gateway: 10.0.2.1
IPv6: No
Enabled: Yes
Name: NatNetwork1
Network: 10.0.2.0/24
Gateway: 10.0.2.1
IPv6: No
Enabled: Yes
3 networks found
Note the blank lines: one before and after
each network. If there are zero networks, the output is:
NAT Networks:
0 networks found
*/
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,
"natnetwork",
"list",
"*kuttinet",
)
if err != nil {
return nil, err
}

// TODO: write a better parser
lines := strings.Split(output, "\n")
numlines := len(lines)
if numlines < 4 {
// Bare mininum output should be
// NAT Networks:
//
// 0 networks found
//
return nil, errors.New("could not recognise VBoxManage output for natnetworks list while getting lines")
}

var numnetworks int

_, err = fmt.Sscanf(lines[numlines-2], "%d", &numnetworks)
if err != nil {
return nil, errors.New("could not recognise VBoxManage output for natnetworks list while getting count")
}

justlines := lines[2 : numlines-2]
numlines = len(justlines)

result := make([]drivercore.Network, numnetworks)

for i, j := 0, 0; i < numlines; i, j = i+7, j+1 {
result[j] = &Network{
name: justlines[i][13:],
netCIDR: justlines[i+1][13:],
}
}

return result, nil
}

// GetNetwork returns a network, or an error.
func (vd *Driver) GetNetwork(clustername string) (drivercore.Network, error) {
netname := vd.QualifiedNetworkName(clustername)

networks, err := vd.ListNetworks()
if err != nil {
return nil, err
}

for _, network := range networks {
if network.Name() == netname {
return network, nil
}
}

return nil, fmt.Errorf("network %s not found", netname)
}

// DeleteNetwork deletes a network.
// It does this by running the command:
// VBoxManage natnetwork remove --netname <networkname>
Expand All @@ -129,7 +22,7 @@ func (vd *Driver) DeleteNetwork(clustername string) error {

netname := vd.QualifiedNetworkName(clustername)

output, err := workspace.Runwithresults(
output, err := workspace.RunWithResults(
vd.vboxmanagepath,
"natnetwork",
"remove",
Expand All @@ -146,7 +39,7 @@ func (vd *Driver) DeleteNetwork(clustername string) error {
}

// Associated dhcpserver must also be deleted
output, err = workspace.Runwithresults(
output, err = workspace.RunWithResults(
vd.vboxmanagepath,
"dhcpserver",
"remove",
Expand Down Expand Up @@ -177,7 +70,7 @@ func (vd *Driver) NewNetwork(clustername string) (drivercore.Network, error) {
// Multiple VirtualBox NAT Networks can have the same IP range
// So, all Kutti networks will use the same network CIDR
// We start with dhcp enabled.
output, err := workspace.Runwithresults(
output, err := workspace.RunWithResults(
vd.vboxmanagepath,
"natnetwork",
"add",
Expand All @@ -200,7 +93,7 @@ func (vd *Driver) NewNetwork(clustername string) (drivercore.Network, error) {

// Manually create the associated DHCP server
// Hard-coding a thirty-node limit for now
output, err = workspace.Runwithresults(
output, err = workspace.RunWithResults(
vd.vboxmanagepath,
"dhcpserver",
"add",
Expand Down
2 changes: 1 addition & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (vd *Driver) validate() bool {
vd.vboxmanagepath = vbmpath

// test VBoxManage version
vbmversion, err := workspace.Runwithresults(vbmpath, "--version")
vbmversion, err := workspace.RunWithResults(vbmpath, "--version")
if err != nil {
vd.status = "Error"
vd.errormessage = err.Error()
Expand Down
10 changes: 5 additions & 5 deletions drivervbox-imagemanagement.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var ImagesSourceURL = "https://github.com/kuttiproject/driver-vbox-images/releas

var (
imagedata = &imageconfigdata{}
imageconfigmanager, _ = workspace.NewFileConfigmanager(imagesConfigFile, imagedata)
imageconfigmanager, _ = workspace.NewFileConfigManager(imagesConfigFile, imagedata)
)

type imageconfigdata struct {
Expand All @@ -41,17 +41,17 @@ func (icd *imageconfigdata) Deserialize(data []byte) error {
return err
}

func (icd *imageconfigdata) Setdefaults() {
func (icd *imageconfigdata) SetDefaults() {
icd.images = defaultimages()
}

func vboxCacheDir() (string, error) {
return workspace.Cachesubdir("driver-vbox")
return workspace.CacheSubDir("driver-vbox")
}

func vboxConfigDir() (string, error) {
//return workspace.Configsubdir("vbox")
return workspace.Configdir()
return workspace.ConfigDir()
}

func defaultimages() map[string]*Image {
Expand Down Expand Up @@ -128,7 +128,7 @@ func fetchimagelist() error {

// Load into object
tempimagedata := &imageconfigdata{}
tempconfigmanager, err := workspace.NewFileConfigmanager(tempfilename, tempimagedata)
tempconfigmanager, err := workspace.NewFileConfigManager(tempfilename, tempimagedata)
if err != nil {
return err
}
Expand Down
35 changes: 1 addition & 34 deletions drivervbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,7 @@ import (
)

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

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
// }
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module github.com/kuttiproject/driver-vbox
go 1.16

require (
github.com/kuttiproject/drivercore v0.2.0
github.com/kuttiproject/kuttilog v0.1.2
github.com/kuttiproject/workspace v0.2.2
github.com/kuttiproject/drivercore v0.3.0
github.com/kuttiproject/kuttilog v0.2.0
github.com/kuttiproject/workspace v0.3.0
)

retract (
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ github.com/kuttiproject/drivercore v0.1.3 h1:dX57IlZhYX2AZ3BVDWTF12mn35fK1/MV974
github.com/kuttiproject/drivercore v0.1.3/go.mod h1:1C1TMGfQ4u/5ltHUtl4hL7gYzXZyZQVhkOha4GuOzk0=
github.com/kuttiproject/drivercore v0.2.0 h1:RZI/azMsyI8gPidXzHPcZXqajHPdq+zaRg3W4x9ASC0=
github.com/kuttiproject/drivercore v0.2.0/go.mod h1:1C1TMGfQ4u/5ltHUtl4hL7gYzXZyZQVhkOha4GuOzk0=
github.com/kuttiproject/drivercore v0.3.0 h1:ZJK1fR4Hn8KB6D1vW2Y15FTRsW4o4188hVn7WDmC/zI=
github.com/kuttiproject/drivercore v0.3.0/go.mod h1:C4dSquHuWTbk9ITzaIZrswnw/TsqmOCkButTCk2LLbE=
github.com/kuttiproject/kuttilog v0.1.2 h1:VEqVWrR3M6RME6aoUuVwXNO3bRpHKNp0ISAP/vqncpg=
github.com/kuttiproject/kuttilog v0.1.2/go.mod h1:OO3dHpXm1/Pjlc57R4c0e/C+ZWkYlY3Fd9Ikn8xPXi4=
github.com/kuttiproject/kuttilog v0.2.0 h1:rhgqjEIX7HAGOeqyAse0Ey+thfDu/Tefvs4zV7VuPYw=
github.com/kuttiproject/kuttilog v0.2.0/go.mod h1:VWyFjqLHbs2QzqPIB2rM/7GIgoH5zD5oopbkVuiG3VI=
github.com/kuttiproject/workspace v0.2.2 h1:1eNdMooB6Oq7jq2wodbuaY+IVZSYcEuXnqY3e00s64Y=
github.com/kuttiproject/workspace v0.2.2/go.mod h1:Z4wwYslirpVHP4TTqrEX3yh/sGdinHSWa3lcMdAVPt8=
github.com/kuttiproject/workspace v0.3.0 h1:cJJU75hrkgofHIJ6W/6FLRstqg87uRfZTwvpmBdWFQI=
github.com/kuttiproject/workspace v0.3.0/go.mod h1:aoRJQEFz46NvnLLo86d6qkWYvkWPC6a8I9uiQWxdxE8=
2 changes: 1 addition & 1 deletion image.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (i *Image) Status() drivercore.ImageStatus {
return i.imageStatus
}

// Deprecated returns true if the image's version of Kubenetes is deprecated.
// Deprecated returns true if the image's version of Kubernetes is deprecated.
// New Machines should not be created from such an image.
func (i *Image) Deprecated() bool {
return i.imageDeprecated
Expand Down
2 changes: 1 addition & 1 deletion machine-commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (vh *Machine) runwithresults(execpath string, paramarray ...string) (string
}
params = append(params, paramarray...)

output, err := workspace.Runwithresults(
output, err := workspace.RunWithResults(
vh.driver.vboxmanagepath,
params...,
)
Expand Down
Loading

0 comments on commit 84d920c

Please sign in to comment.