Skip to content

Commit

Permalink
#11 allow create vagrant images - polishing code
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy committed Aug 20, 2020
1 parent a168ae8 commit a6ecb6e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 70 deletions.
10 changes: 5 additions & 5 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ const (
)

var (
ImagesDir = getVerminDir() + string(os.PathSeparator) + "images"
VMsBaseDir = getVerminDir() + string(os.PathSeparator) + "vms"
ImagesDir = filepath.Join(getVerminDir(), "images")
VMsBaseDir = filepath.Join(getVerminDir(), "vms")
)

func GetImageFilePath(imageName string) string {
return ImagesDir + string(os.PathSeparator) + imageName + ".ova"
return filepath.Join(ImagesDir, imageName+".ova")
}

func GetVMPath(vm string) string {
return VMsBaseDir + string(os.PathSeparator) + vm
return filepath.Join(VMsBaseDir, vm)
}

func getVerminDir() string {
dir, err := os.UserHomeDir()
if err != nil {
log.Fatal("cannot obtain user home dir")
}
return dir + string(os.PathSeparator) + ".vermin"
return filepath.Join(dir, ".vermin")
}

func GetUsername(vmName string) string {
Expand Down
3 changes: 2 additions & 1 deletion db/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
)

type Box struct {
Expand Down Expand Up @@ -62,7 +63,7 @@ func GetBoxInfo(vm string) (*Box, error) {
}

func getDiskSizeInGB(vm string, hdLocation string) string {
stat, err := os.Stat(GetVMPath(vm) + string(os.PathSeparator) + hdLocation)
stat, err := os.Stat(filepath.Join(GetVMPath(vm), hdLocation))
if err != nil {
return ""
}
Expand Down
65 changes: 65 additions & 0 deletions vagrant/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package vagrant

import (
"github.com/mhewedy/vermin/command"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
)

func ProcessImage(imagePath string) error {

imageDir := path.Dir(imagePath)

// gunzip the downloaded file
// TODO change from using tar command to golang code
if err := command.Tar("xzf", imagePath, "-C", imageDir).Run(); err != nil {
return err
}

// remove the downloaded file
if err := os.Remove(imagePath); err != nil {
return err
}

// get ovf, vmdk FileInfo
infos, err := ioutil.ReadDir(imageDir)
if err != nil {
return err
}
var ovaFileInfo, vmdkFileInfo os.FileInfo
for _, info := range infos {
if strings.HasSuffix(info.Name(), ".ovf") {
ovaFileInfo = info
}
if strings.HasSuffix(info.Name(), ".vmdk") {
vmdkFileInfo = info
}
}

// create ova by TARing (ovf and vmdk)
file, err := os.Create(imagePath)
if err != nil {
return err
}
defer file.Close()

if err := tarFiles(file, imageDir, []os.FileInfo{ovaFileInfo, vmdkFileInfo}); err != nil {
return err
}

// remove all files except ova
return filepath.Walk(imageDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && !strings.HasSuffix(info.Name(), ".ova") {
if err := os.Remove(path); err != nil {
return err
}
}
return nil
})
}
64 changes: 0 additions & 64 deletions vagrant/vagrant.go
Original file line number Diff line number Diff line change
@@ -1,71 +1,7 @@
package vagrant

import (
"github.com/mhewedy/vermin/command"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
)

func GetImageURL(image string) (string, error) {
// TODO
// get json and parse it to return download URL
return "https://vagrantcloud.com/hashicorp/boxes/bionic64/versions/1.0.282/providers/virtualbox.box", nil
}

func ProcessImage(imagePath string) error {

imageDir := path.Dir(imagePath)

// gunzip the downloaded file
// TODO change from command to golang code
if err := command.Tar("xzf", imagePath, "-C", imageDir).Run(); err != nil {
return err
}

// remove the downloaded file
if err := os.Remove(imagePath); err != nil {
return err
}

// get ovf, vmdk FileInfo
infos, err := ioutil.ReadDir(imageDir)
if err != nil {
return err
}
var ovaFileInfo, vmdkFileInfo os.FileInfo
for _, info := range infos {
if strings.HasSuffix(info.Name(), ".ovf") {
ovaFileInfo = info
}
if strings.HasSuffix(info.Name(), ".vmdk") {
vmdkFileInfo = info
}
}

// create ova by TARing (ovf and vmdk)
file, err := os.Create(imagePath)
if err != nil {
return err
}
defer file.Close()

if err := tarFiles(file, imageDir, []os.FileInfo{ovaFileInfo, vmdkFileInfo}); err != nil {
return err
}

// remove all files except ova
return filepath.Walk(imageDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && !strings.HasSuffix(info.Name(), ".ova") {
if err := os.Remove(path); err != nil {
return err
}
}
return nil
})
}

0 comments on commit a6ecb6e

Please sign in to comment.