@@ -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
4446const (
@@ -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
831880func 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