From 9ad30765d0dfe63c6ab7c66645c295d5c6ae5e0e Mon Sep 17 00:00:00 2001 From: Paolo Chila Date: Wed, 2 Oct 2024 09:17:36 +0200 Subject: [PATCH 1/2] Fix docker image build when multiple platforms are specified --- dev-tools/mage/dockerbuilder.go | 2 +- magefile.go | 38 +++++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/dev-tools/mage/dockerbuilder.go b/dev-tools/mage/dockerbuilder.go index f99f4f1dcdb..0e0bd2078ca 100644 --- a/dev-tools/mage/dockerbuilder.go +++ b/dev-tools/mage/dockerbuilder.go @@ -46,7 +46,7 @@ func (b *dockerBuilder) Build() error { } if err := b.copyFiles(); err != nil { - return err + return fmt.Errorf("error copying files for docker variant %q: %w", b.DockerVariant, err) } if err := b.prepareBuild(); err != nil { diff --git a/magefile.go b/magefile.go index 03aecaeb620..1d4966d1654 100644 --- a/magefile.go +++ b/magefile.go @@ -1613,9 +1613,18 @@ func movePackagesToArchive(dropPath string, platformPackageSuffixes []string, pa if err := os.MkdirAll(targetDir, 0750); err != nil { fmt.Printf("warning: failed to create directory %s: %s", targetDir, err) } - if err := os.Rename(f, targetPath); err != nil { - panic(fmt.Errorf("failed renaming file: %w", err)) + if isPythonWheelPackage(f, packageVersion) { + + if err := copyFile(f, targetPath); err != nil { + panic(fmt.Errorf("failed copying file: %w", err)) + } + + } else { + if err := os.Rename(f, targetPath); err != nil { + panic(fmt.Errorf("failed renaming file: %w", err)) + } } + if mg.Verbose() { log.Printf("--- Moved dependency in archive path %s => %s\n", f, targetPath) } @@ -1625,6 +1634,31 @@ func movePackagesToArchive(dropPath string, platformPackageSuffixes []string, pa return archivePath } +func copyFile(src, dst string) error { + srcStat, err := os.Stat(src) + if err != nil { + return fmt.Errorf("stat source file %q: %w", src, err) + } + + srcF, err := os.Open(src) + if err != nil { + return fmt.Errorf("opening source file %q: %w", src, err) + } + defer srcF.Close() + + dstF, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, srcStat.Mode()|os.ModePerm) + if err != nil { + return fmt.Errorf("opening/creating destination file %q: %w", dst, err) + } + defer dstF.Close() + _, err = io.Copy(dstF, srcF) + if err != nil { + return fmt.Errorf("copying file %q to %q: %w", src, dst, err) + } + + return nil +} + func isPythonWheelPackage(f string, packageVersion string) bool { fileBaseName := filepath.Base(f) for _, spec := range manifest.ExpectedBinaries { From e1f9ad77992f3224e586aec1f7cd2b32512ac5c7 Mon Sep 17 00:00:00 2001 From: Paolo Chila Date: Wed, 2 Oct 2024 10:56:50 +0200 Subject: [PATCH 2/2] Improve logging and comments for the elastic-agent packaging process --- magefile.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/magefile.go b/magefile.go index 1d4966d1654..670920cf128 100644 --- a/magefile.go +++ b/magefile.go @@ -1537,7 +1537,7 @@ func downloadBinary(ctx context.Context, project string, packageName string, bin } compl.Add(1) - fmt.Printf("Done downloading %s\n", packageName) + fmt.Printf("Done downloading %s into %s\n", packageName, targetPath) return nil } } @@ -1590,7 +1590,8 @@ func movePackagesToArchive(dropPath string, platformPackageSuffixes []string, pa if mg.Verbose() { log.Printf("--- Evaluating moving dependency %s to archive path %s\n", f, archivePath) } - if !strings.Contains(f, packageSuffix) && !isPythonWheelPackage(f, packageVersion) { + // if the matched file name does not contain the platform suffix and it's not a platform-independent package, skip it + if !strings.Contains(f, packageSuffix) && !isPlatformIndependentPackage(f, packageVersion) { if mg.Verbose() { log.Printf("--- Skipped moving dependency %s to archive path\n", f) } @@ -1613,12 +1614,12 @@ func movePackagesToArchive(dropPath string, platformPackageSuffixes []string, pa if err := os.MkdirAll(targetDir, 0750); err != nil { fmt.Printf("warning: failed to create directory %s: %s", targetDir, err) } - if isPythonWheelPackage(f, packageVersion) { + // Platform-independent packages need to be placed in the archive sub-folders for all platforms, copy instead of moving + if isPlatformIndependentPackage(f, packageVersion) { if err := copyFile(f, targetPath); err != nil { panic(fmt.Errorf("failed copying file: %w", err)) } - } else { if err := os.Rename(f, targetPath); err != nil { panic(fmt.Errorf("failed renaming file: %w", err)) @@ -1651,6 +1652,7 @@ func copyFile(src, dst string) error { return fmt.Errorf("opening/creating destination file %q: %w", dst, err) } defer dstF.Close() + _, err = io.Copy(dstF, srcF) if err != nil { return fmt.Errorf("copying file %q to %q: %w", src, dst, err) @@ -1659,10 +1661,11 @@ func copyFile(src, dst string) error { return nil } -func isPythonWheelPackage(f string, packageVersion string) bool { +func isPlatformIndependentPackage(f string, packageVersion string) bool { fileBaseName := filepath.Base(f) for _, spec := range manifest.ExpectedBinaries { packageName := spec.GetPackageName(packageVersion, "") + // as of now only python wheels packages are platform-independent if spec.PythonWheel && (fileBaseName == packageName || fileBaseName == packageName+sha512FileExt) { return true }