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

Generate checksums during package #679

Merged
merged 5 commits into from
Aug 12, 2021
Merged
Changes from 1 commit
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
45 changes: 42 additions & 3 deletions tools/packager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"archive/tar"
"bytes"
"compress/gzip"
"crypto/sha256"
"encoding/hex"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -47,15 +50,12 @@ func doPackage() error {
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Failed to open -archivePath %s", archivePath))
}
defer f.Close()

zw := gzip.NewWriter(f)
defer zw.Close()

tw := archive.NewNormalizingTarWriter(tar.NewWriter(zw))
tw.WithUID(0)
tw.WithGID(0)
defer tw.Close()

templateContents, err := ioutil.ReadFile(descriptorPath)
if err != nil {
Expand Down Expand Up @@ -112,6 +112,45 @@ func doPackage() error {
return errors.Wrap(err, "Failed to write dir to archive")
}

err = tw.Close()
if err != nil {
return errors.Wrap(err, "Failed to close tar writer")
}

err = zw.Close()
if err != nil {
return errors.Wrap(err, "Failed to close gzip writer")
}

err = f.Close()
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Failed to close -archivePath %s", archivePath))
}

f, err = os.OpenFile(archivePath, os.O_RDONLY, 0777)
natalieparellano marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Failed to open -archivePath %s", archivePath))
}
defer f.Close()

h := sha256.New()
if _, err = io.Copy(h, f); err != nil {
return errors.Wrap(err, fmt.Sprintf("Failed to calculate sha256 of -archivePath %s", archivePath))
}

hashFileName := archivePath + ".sha256"
hashFile, err := os.OpenFile(hashFileName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0777)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Failed to open %s", hashFileName))
}
defer hashFile.Close()

sha := hex.EncodeToString(h.Sum(nil))
_, err = hashFile.Write([]byte(sha))
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Failed to write sha256:%s to %s", h.Sum(nil), hashFileName))
}

return nil
}

Expand Down