Skip to content

Commit

Permalink
fix #36
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy committed Dec 11, 2020
1 parent 4e10361 commit 7e223e3
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 32 deletions.
12 changes: 0 additions & 12 deletions debug/debug.go

This file was deleted.

17 changes: 11 additions & 6 deletions images/vagrant/archive_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,19 @@ func tarFiles(w io.Writer, base string, files []os.FileInfo) error {
return nil
}

func gunzip(baseDir string, gzipStream io.Reader) error {
func gunzip(gzipStream io.Reader, baseDir string, tarOnly bool) error {

uncompressedStream, err := gzip.NewReader(gzipStream)
if err != nil {
return errImageIsNotGzipped
}
var tarReader *tar.Reader

tarReader := tar.NewReader(uncompressedStream)
if tarOnly {
tarReader = tar.NewReader(gzipStream)
} else {
uncompressedStream, err := gzip.NewReader(gzipStream)
if err != nil {
return errImageIsNotGzipped
}
tarReader = tar.NewReader(uncompressedStream)
}

for true {
header, err := tarReader.Next()
Expand Down
17 changes: 11 additions & 6 deletions images/vagrant/process_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vagrant

import (
"errors"
"github.com/mhewedy/vermin/log"
"github.com/mhewedy/vermin/progress"
"io/ioutil"
"os"
Expand All @@ -24,11 +25,15 @@ func ProcessImage(imagePath string) error {

imageDir := path.Dir(imagePath)

if err := gunzipVagrantBox(imagePath, imageDir); err != nil {
if err == errImageIsNotGzipped {
return nil
if err := gunzipVagrantBox(imagePath, imageDir, false); err != nil {
if err != errImageIsNotGzipped {
return err
}

log.Info("cannot ungzip image %s, try untaring only...", imagePath)
if err = gunzipVagrantBox(imagePath, imageDir, true); err != nil {
return err
}
return err
}

// remove the downloaded file
Expand All @@ -54,15 +59,15 @@ func ProcessImage(imagePath string) error {
})
}

func gunzipVagrantBox(imagePath, imageDir string) error {
func gunzipVagrantBox(imagePath, imageDir string, tarOnly bool) error {

gzipFile, err := os.Open(imagePath)
if err != nil {
return err
}
defer gzipFile.Close()

return gunzip(imageDir, gzipFile)
return gunzip(gzipFile, imageDir, tarOnly)
}

func createOVAFile(imagePath, imageDir string) error {
Expand Down
10 changes: 4 additions & 6 deletions ip/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package ip

import (
"fmt"
"github.com/mhewedy/vermin/debug"
"github.com/mhewedy/vermin/hypervisor"
"github.com/mhewedy/vermin/log"
"strings"
)

Expand All @@ -24,12 +24,11 @@ func Find(vmName string, purge bool) (string, error) {
return "", err
}

debug.Log("found mac: %s for vm: %s", mac, vmName)

log.Debug("found mac: %s for vm: %s", mac, vmName)
var pong bool

if purge {
debug.Log("purge=1, purging...")
log.Debug("purge=1, purging...")
if err := ping(); err != nil {
return "", err
}
Expand All @@ -38,8 +37,7 @@ func Find(vmName string, purge bool) (string, error) {

for {
arp, err := getArpTable()

debug.Log("here's the arp table: %s ", arp)
log.Debug("here's the arp table: %s ", arp)

if err != nil {
return "", err
Expand Down
4 changes: 2 additions & 2 deletions ip/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package ip

import (
"github.com/mhewedy/vermin/cmd"
"github.com/mhewedy/vermin/debug"
"github.com/mhewedy/vermin/hypervisor"
"github.com/mhewedy/vermin/hypervisor/base"
"github.com/mhewedy/vermin/log"
"sync"
)

Expand All @@ -14,7 +14,7 @@ func ping() error {
if err != nil {
return err
}
debug.Log("subnet: %v", subnet)
log.Debug("subnet: %v", subnet)

var wg sync.WaitGroup
wg.Add(subnet.Len)
Expand Down
28 changes: 28 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package log

import (
"fmt"
"github.com/mhewedy/vermin/db"
"os"
"path/filepath"
"time"
)

func Debug(format string, a ...interface{}) {
if _, ok := os.LookupEnv("VERMIN_DEBUG"); ok {
Info(format, a)
}
}

func Info(format string, a ...interface{}) {
year, month, _ := time.Now().Date()
logFilePath := filepath.Join(db.BaseDir, fmt.Sprintf("log_%d-%d.log", year, month))

f, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return
}
defer f.Close()

_, _ = f.WriteString(fmt.Sprintf(format+"\n", a...))
}

0 comments on commit 7e223e3

Please sign in to comment.