diff --git a/debug/debug.go b/debug/debug.go deleted file mode 100644 index e214bc9..0000000 --- a/debug/debug.go +++ /dev/null @@ -1,12 +0,0 @@ -package debug - -import ( - "fmt" - "os" -) - -func Log(format string, a ...interface{}) { - if _, ok := os.LookupEnv("VERMIN_DEBUG"); ok { - fmt.Printf(format+"\n", a...) - } -} diff --git a/images/vagrant/archive_utils.go b/images/vagrant/archive_utils.go index f84f784..7bc11d6 100644 --- a/images/vagrant/archive_utils.go +++ b/images/vagrant/archive_utils.go @@ -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() diff --git a/images/vagrant/process_image.go b/images/vagrant/process_image.go index b4078e0..e2aada2 100644 --- a/images/vagrant/process_image.go +++ b/images/vagrant/process_image.go @@ -2,6 +2,7 @@ package vagrant import ( "errors" + "github.com/mhewedy/vermin/log" "github.com/mhewedy/vermin/progress" "io/ioutil" "os" @@ -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 @@ -54,7 +59,7 @@ 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 { @@ -62,7 +67,7 @@ func gunzipVagrantBox(imagePath, imageDir string) error { } defer gzipFile.Close() - return gunzip(imageDir, gzipFile) + return gunzip(gzipFile, imageDir, tarOnly) } func createOVAFile(imagePath, imageDir string) error { diff --git a/ip/ip.go b/ip/ip.go index ae5f511..d9f4d4f 100644 --- a/ip/ip.go +++ b/ip/ip.go @@ -2,8 +2,8 @@ package ip import ( "fmt" - "github.com/mhewedy/vermin/debug" "github.com/mhewedy/vermin/hypervisor" + "github.com/mhewedy/vermin/log" "strings" ) @@ -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 } @@ -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 diff --git a/ip/ping.go b/ip/ping.go index 4efe172..eadca2c 100644 --- a/ip/ping.go +++ b/ip/ping.go @@ -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" ) @@ -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) diff --git a/log/log.go b/log/log.go new file mode 100644 index 0000000..9c1971d --- /dev/null +++ b/log/log.go @@ -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...)) +}