Skip to content

Commit

Permalink
VMs will be created inside kutti cache directory
Browse files Browse the repository at this point in the history
Also, slightly faster file copying.
  • Loading branch information
rajch committed Jun 9, 2022
1 parent dd89584 commit 53c8bbc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
23 changes: 23 additions & 0 deletions driver-machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package drivervbox
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
Expand All @@ -12,6 +13,10 @@ import (
"github.com/kuttiproject/workspace"
)

func machinesBaseDir() (string, error) {
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
Expand Down Expand Up @@ -113,6 +118,9 @@ var ipRegex, _ = regexp.Compile(`^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-
// The first imports from an .ova file (easiest way to get fully configured VM), while
// setting the VM name. The second connects the first network interface card to
// the NAT network.
// This function may return nil and an error, or a Machine and an error.
// 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) {
qualifiedmachinename := vd.QualifiedMachineName(machinename, clustername)

Expand All @@ -127,6 +135,17 @@ func (vd *Driver) NewMachine(machinename string, clustername string, k8sversion
return nil, fmt.Errorf("could not retrieve image %s: %v", ovafile, err)
}

machinebasedir, err := machinesBaseDir()
if err != nil {
return nil, err
}

// Risk: convert path to absolute
absmachinebasedir, err := filepath.Abs(machinebasedir)
if err != nil {
return nil, err
}

l, err := workspace.Runwithresults(
vd.vboxmanagepath,
"import",
Expand All @@ -139,6 +158,10 @@ func (vd *Driver) NewMachine(machinename string, clustername string, k8sversion
"0",
"--group",
"/"+clustername,
"--vsys",
"0",
"--basefolder",
absmachinebasedir,
)

if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion drivervbox-imagemanagement.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ func imagepathfromk8sversion(k8sversion string) (string, error) {
}

func addfromfile(k8sversion string, filepath string, checksum string) error {
kuttilog.Println(kuttilog.Info, "Checking image validity...")
filechecksum, err := workspace.ChecksumFile(filepath)
if err != nil {
return err
}

if filechecksum != checksum {
kuttilog.Printf(kuttilog.Debug, "checksum for file %v failed.\nWanted: %v\nGot : %v\n", filepath, checksum, filechecksum)
return errors.New("file is not valid")
}

Expand All @@ -87,7 +89,10 @@ func addfromfile(k8sversion string, filepath string, checksum string) error {
return err
}

err = workspace.CopyFile(filepath, localfilepath, 1000, true)
kuttilog.Println(kuttilog.Info, "Copying image to local cache...")
// A 128 KiB buffer should help
const BUFSIZE = 131072
err = workspace.CopyFile(filepath, localfilepath, BUFSIZE, true)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion drivervbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// to use for the test.
const (
TESTK8SVERSION = "1.23"
TESTK8SCHECKSUM = "8b18b91c670b62e91a0b179753a1c1b778ea5e1ffa300ca1309ec7098d8dbbc3"
TESTK8SCHECKSUM = "2c9a6a24175f5589200b269f75f4043f4e97ed1ec7088f2d14a37a689624d770"
)

func TestDriverVBox(t *testing.T) {
Expand Down

0 comments on commit 53c8bbc

Please sign in to comment.