Skip to content

Commit 7f6b42a

Browse files
Generate checksum file for components (#604)
* generating checksum? * yaml output * Update dev-tools/mage/common.go Co-authored-by: Michel Laterman <[email protected]> * review * ioutil removal from magefile Co-authored-by: Michel Laterman <[email protected]>
1 parent 2679c82 commit 7f6b42a

File tree

3 files changed

+84
-13
lines changed

3 files changed

+84
-13
lines changed

dev-tools/mage/common.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -754,22 +754,29 @@ func VerifySHA256(file string, hash string) error {
754754
// CreateSHA512File computes the sha512 sum of the specified file the writes
755755
// a sidecar file containing the hash and filename.
756756
func CreateSHA512File(file string) error {
757+
computedHash, err := GetSHA512Hash(file)
758+
if err != nil {
759+
return err
760+
}
761+
out := fmt.Sprintf("%v %v", computedHash, filepath.Base(file))
762+
763+
//nolint:gosec // permissions are correct
764+
return os.WriteFile(file+".sha512", []byte(out), 0644)
765+
}
766+
func GetSHA512Hash(file string) (string, error) {
757767
f, err := os.Open(file)
758768
if err != nil {
759-
return errors.Wrap(err, "failed to open file for sha512 summing")
769+
return "", errors.Wrap(err, "failed to open file for sha512 summing")
760770
}
761771
defer f.Close()
762772

763773
sum := sha512.New()
764774
if _, err := io.Copy(sum, f); err != nil {
765-
return errors.Wrap(err, "failed reading from input file")
775+
return "", errors.Wrap(err, "failed reading from input file")
766776
}
767777

768778
computedHash := hex.EncodeToString(sum.Sum(nil))
769-
out := fmt.Sprintf("%v %v", computedHash, filepath.Base(file))
770-
771-
//nolint:gosec // permissions are correct
772-
return ioutil.WriteFile(file+".sha512", []byte(out), 0644)
779+
return computedHash, nil
773780
}
774781

775782
// Mage executes mage targets in the specified directory.

dev-tools/mage/pkgtypes.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -906,10 +906,6 @@ func addFileToTar(ar *tar.Writer, baseDir string, pkgFile PackageFile) error {
906906
header.Mode = int64(0755)
907907
}
908908

909-
if strings.Contains(info.Name(), "disabled") {
910-
log.Println(">>>>>", info.Name(), pkgFile.ConfigMode, "matches", configFilePattern.MatchString(info.Name()), "or", componentConfigFilePattern.MatchString(info.Name()))
911-
}
912-
913909
if filepath.IsAbs(pkgFile.Target) {
914910
baseDir = ""
915911
}

magefile.go

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import (
3939
_ "github.com/elastic/elastic-agent/dev-tools/mage/target/integtest/notests"
4040
// mage:import
4141
"github.com/elastic/elastic-agent/dev-tools/mage/target/test"
42+
43+
"gopkg.in/yaml.v2"
4244
)
4345

4446
const (
@@ -52,6 +54,7 @@ const (
5254
configFile = "elastic-agent.yml"
5355
agentDropPath = "AGENT_DROP_PATH"
5456
specSuffix = ".spec.yml" // TODO: change after beat ignores yml config
57+
checksumFilename = "checksum.yml"
5558
)
5659

5760
// Aliases for commands required by master makefile
@@ -373,7 +376,7 @@ func AssembleDarwinUniversal() error {
373376
cmd := "lipo"
374377

375378
if _, err := exec.LookPath(cmd); err != nil {
376-
return fmt.Errorf("'%s' is required to assemble the universal binary: %w",
379+
return fmt.Errorf("%q is required to assemble the universal binary: %w",
377380
cmd, err)
378381
}
379382

@@ -437,7 +440,7 @@ func requiredPackagesPresent(basePath, beat, version string, requiredPackages []
437440
path := filepath.Join(basePath, "build", "distributions", packageName)
438441

439442
if _, err := os.Stat(path); err != nil {
440-
fmt.Printf("Package '%s' does not exist on path: %s\n", packageName, path)
443+
fmt.Printf("Package %q does not exist on path: %s\n", packageName, path)
441444
return false
442445
}
443446
}
@@ -794,6 +797,7 @@ func packageAgent(requiredPackages []string, packagingFn func()) {
794797
panic(err)
795798
}
796799

800+
checksums := make(map[string]string)
797801
for _, f := range files {
798802
options := copy.Options{
799803
OnSymlink: func(_ string) copy.SymlinkAction {
@@ -814,9 +818,16 @@ func packageAgent(requiredPackages []string, packagingFn func()) {
814818
specName = specName[:idx]
815819
}
816820

817-
if err := devtools.Copy(filepath.Join("specs", specName+specSuffix), filepath.Join(versionedDropPath, specName+specSuffix)); err != nil {
821+
checksum, err := copyComponentSpecs(specName, versionedDropPath)
822+
if err != nil {
818823
panic(err)
819824
}
825+
826+
checksums[specName+specSuffix] = checksum
827+
}
828+
829+
if err := appendComponentChecksums(versionedDropPath, checksums); err != nil {
830+
panic(err)
820831
}
821832
}
822833

@@ -827,6 +838,44 @@ func packageAgent(requiredPackages []string, packagingFn func()) {
827838
mg.Deps(CrossBuild, CrossBuildGoDaemon)
828839
mg.SerialDeps(devtools.Package, TestPackages)
829840
}
841+
func copyComponentSpecs(componentName, versionedDropPath string) (string, error) {
842+
sourceSpecFile := filepath.Join("specs", componentName+specSuffix)
843+
targetPath := filepath.Join(versionedDropPath, componentName+specSuffix)
844+
err := devtools.Copy(sourceSpecFile, targetPath)
845+
if err != nil {
846+
return "", errors.Wrapf(err, "failed copying spec file %q to %q", sourceSpecFile, targetPath)
847+
}
848+
849+
// compute checksum
850+
return devtools.GetSHA512Hash(sourceSpecFile)
851+
}
852+
853+
func appendComponentChecksums(versionedDropPath string, checksums map[string]string) error {
854+
// for each spec file checksum calculate binary checksum as well
855+
for file := range checksums {
856+
if !strings.HasSuffix(file, specSuffix) {
857+
continue
858+
}
859+
860+
componentFile := strings.TrimSuffix(file, specSuffix)
861+
hash, err := devtools.GetSHA512Hash(filepath.Join(versionedDropPath, componentFile))
862+
if errors.Is(err, os.ErrNotExist) {
863+
fmt.Printf(">>> Computing hash for %q failed: file not present\n", componentFile)
864+
continue
865+
} else if err != nil {
866+
return err
867+
}
868+
869+
checksums[componentFile] = hash
870+
}
871+
872+
content, err := yamlChecksum(checksums)
873+
if err != nil {
874+
return err
875+
}
876+
877+
return os.WriteFile(filepath.Join(versionedDropPath, checksumFilename), content, 0644)
878+
}
830879

831880
func movePackagesToArchive(dropPath string, requiredPackages []string) string {
832881
archivePath := filepath.Join(dropPath, "archives")
@@ -959,3 +1008,22 @@ func injectBuildVars(m map[string]string) {
9591008
m[k] = v
9601009
}
9611010
}
1011+
1012+
func yamlChecksum(checksums map[string]string) ([]byte, error) {
1013+
filesMap := make(map[string][]checksumFile)
1014+
files := make([]checksumFile, 0, len(checksums))
1015+
for file, checksum := range checksums {
1016+
files = append(files, checksumFile{
1017+
Name: file,
1018+
Checksum: checksum,
1019+
})
1020+
}
1021+
1022+
filesMap["files"] = files
1023+
return yaml.Marshal(filesMap)
1024+
}
1025+
1026+
type checksumFile struct {
1027+
Name string `yaml:"name"`
1028+
Checksum string `yaml:"sha512"`
1029+
}

0 commit comments

Comments
 (0)