Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use zip library to create zip file #61

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions cmd/aks-periscope/aks-periscope.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package main

import (
"archive/zip"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"sync"

Expand Down Expand Up @@ -123,19 +127,47 @@ func zipAndExport(exporter interfaces.Exporter) error {
return err
}

sourcePathOnHost := "/var/log/aks-periscope/" + strings.Replace(creationTimeStamp, ":", "-", -1) + "/" + hostName
zipFileOnHost := sourcePathOnHost + "/" + hostName + ".zip"
sourcePathOnHost := fmt.Sprintf("/var/log/aks-periscope/%s/%s", strings.Replace(creationTimeStamp, ":", "-", -1), hostName)
zipFileOnHost := fmt.Sprintf("%s/%s.zip", sourcePathOnHost, hostName)
zipFileOnContainer := strings.TrimPrefix(zipFileOnHost, "/var/log")

_, err = utils.RunCommandOnHost("zip", "-r", zipFileOnHost, sourcePathOnHost)
if err != nil {
return err
if err = createZip(zipFileOnHost, sourcePathOnHost); err != nil {
return fmt.Errorf("create zip: %w", err)
}

err = exporter.Export([]string{zipFileOnContainer})
return exporter.Export([]string{zipFileOnContainer})
}

func createZip(sourcePath, destinationPath string) error {
destinationFile, err := os.Create(sourcePath)
if err != nil {
return err
return fmt.Errorf("file %s cannot be created: %w", sourcePath, err)
}

return nil
w := zip.NewWriter(destinationFile)
defer w.Close()

return filepath.Walk(sourcePath, func(filePath string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if err != nil {
return err
}

relPath := strings.TrimPrefix(filePath, filepath.Dir(sourcePath))
zipFile, err := w.Create(relPath)
if err != nil {
return err
}

fsFile, err := os.Open(filePath)
if err != nil {
return err
}

_, err = io.Copy(zipFile, fsFile)

return err
})
}