From 84d920cd1d9da0d66ea4275fc6b94db5ddb6d836 Mon Sep 17 00:00:00 2001 From: Raj Chaudhuri Date: Mon, 18 Jul 2022 16:31:14 +0530 Subject: [PATCH] Bumped dependencies, and cleaned up --- driver-machine.go | 58 ++--------------- driver-network.go | 115 ++-------------------------------- driver.go | 2 +- drivervbox-imagemanagement.go | 10 +-- drivervbox.go | 35 +---------- go.mod | 6 +- go.sum | 6 ++ image.go | 2 +- machine-commands.go | 2 +- machine-properties.go | 6 +- machine.go | 14 ++--- 11 files changed, 36 insertions(+), 220 deletions(-) diff --git a/driver-machine.go b/driver-machine.go index f4c97df..3ba7153 100644 --- a/driver-machine.go +++ b/driver-machine.go @@ -14,7 +14,7 @@ import ( ) func machinesBaseDir() (string, error) { - return workspace.Cachesubdir("driver-vbox-machines") + return workspace.CacheSubDir("driver-vbox-machines") } // QualifiedMachineName returns a name in the form - @@ -22,55 +22,6 @@ func (vd *Driver) QualifiedMachineName(machinename string, clustername string) s 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 --patterns "/VirtualBox/GuestInfo/Net/0/*|/kutti/*|/VirtualBox/GuestInfo/OS/LoggedInUsers" @@ -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, @@ -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, @@ -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(), @@ -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 diff --git a/driver-network.go b/driver-network.go index fc4be3c..0d227d2 100644 --- a/driver-network.go +++ b/driver-network.go @@ -1,9 +1,7 @@ package drivervbox import ( - "errors" "fmt" - "strings" "github.com/kuttiproject/drivercore" "github.com/kuttiproject/workspace" @@ -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 @@ -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", @@ -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", @@ -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", @@ -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", diff --git a/driver.go b/driver.go index 07ce8b1..55560fc 100644 --- a/driver.go +++ b/driver.go @@ -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() diff --git a/drivervbox-imagemanagement.go b/drivervbox-imagemanagement.go index 1b01319..83b5a1c 100644 --- a/drivervbox-imagemanagement.go +++ b/drivervbox-imagemanagement.go @@ -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 { @@ -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 { @@ -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 } diff --git a/drivervbox.go b/drivervbox.go index 7fafde1..bbaa15a 100644 --- a/drivervbox.go +++ b/drivervbox.go @@ -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 -// } diff --git a/go.mod b/go.mod index b4ea490..a6875bc 100644 --- a/go.mod +++ b/go.mod @@ -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 ( diff --git a/go.sum b/go.sum index e01089f..3d2e05b 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/image.go b/image.go index 26ef136..a48cec4 100644 --- a/image.go +++ b/image.go @@ -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 diff --git a/machine-commands.go b/machine-commands.go index 447426f..88775cc 100644 --- a/machine-commands.go +++ b/machine-commands.go @@ -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..., ) diff --git a/machine-properties.go b/machine-properties.go index e596dfe..2ad823c 100644 --- a/machine-properties.go +++ b/machine-properties.go @@ -36,7 +36,7 @@ var propMap = map[string]func(*Machine, string){ } func (vh *Machine) getproperty(propname string) (string, bool) { - output, err := workspace.Runwithresults( + output, err := workspace.RunWithResults( vh.driver.vboxmanagepath, "guestproperty", "get", @@ -57,7 +57,7 @@ func (vh *Machine) getproperty(propname string) (string, bool) { } func (vh *Machine) setproperty(propname string, value string) error { - _, err := workspace.Runwithresults( + _, err := workspace.RunWithResults( vh.driver.vboxmanagepath, "guestproperty", "set", @@ -80,7 +80,7 @@ func (vh *Machine) setproperty(propname string, value string) error { } func (vh *Machine) unsetproperty(propname string) error { - _, err := workspace.Runwithresults( + _, err := workspace.RunWithResults( vh.driver.vboxmanagepath, "guestproperty", "unset", diff --git a/machine.go b/machine.go index e2ae1c5..7bdf300 100644 --- a/machine.go +++ b/machine.go @@ -68,7 +68,7 @@ func (vh *Machine) SSHAddress() string { // and therefore its status will not change. // See WaitForStateChange(). func (vh *Machine) Start() error { - output, err := workspace.Runwithresults( + output, err := workspace.RunWithResults( vh.driver.vboxmanagepath, "startvm", vh.qname(), @@ -90,7 +90,7 @@ func (vh *Machine) Start() error { // and therefore its status will not change. // See WaitForStateChange(). func (vh *Machine) Stop() error { - _, err := workspace.Runwithresults( + _, err := workspace.RunWithResults( vh.driver.vboxmanagepath, "controlvm", vh.qname(), @@ -114,7 +114,7 @@ func (vh *Machine) Stop() error { // VBoxManage controlvm poweroff // This operation will set the status to drivercore.MachineStatusStopped. func (vh *Machine) ForceStop() error { - _, err := workspace.Runwithresults( + _, err := workspace.RunWithResults( vh.driver.vboxmanagepath, "controlvm", vh.qname(), @@ -141,7 +141,7 @@ func (vh *Machine) ForceStop() error { // WaitForStateChange should be called after a call to Start, before // any other operation. From observation, it should not be called _before_ Stop. func (vh *Machine) WaitForStateChange(timeoutinseconds int) { - workspace.Runwithresults( + workspace.RunWithResults( vh.driver.vboxmanagepath, "guestproperty", "wait", @@ -179,7 +179,7 @@ func (vh *Machine) ForwardPort(hostport int, machineport int) error { machineport, ) - _, err := workspace.Runwithresults( + _, err := workspace.RunWithResults( vh.driver.vboxmanagepath, "natnetwork", "modify", @@ -208,7 +208,7 @@ func (vh *Machine) ForwardPort(hostport int, machineport int) error { // This driver writes the rule name as "Node Port ". func (vh *Machine) UnforwardPort(machineport int) error { rulename := vh.forwardingrulename(machineport) - _, err := workspace.Runwithresults( + _, err := workspace.RunWithResults( vh.driver.vboxmanagepath, "natnetwork", "modify", @@ -283,7 +283,7 @@ func (vh *Machine) ExecuteCommand(command drivercore.PredefinedCommand, params . } func (vh *Machine) get() error { - output, err := workspace.Runwithresults( + output, err := workspace.RunWithResults( vh.driver.vboxmanagepath, "guestproperty", "enumerate",