From 7731af9a74e094acfee0370859b857ac96206dd1 Mon Sep 17 00:00:00 2001 From: Rebecca Mahany-Horton Date: Tue, 16 May 2023 15:55:11 -0400 Subject: [PATCH 1/2] Start to include arch in packaging targets + packaging commands --- pkg/packagekit/assets/distribution.dist | 2 +- pkg/packagekit/package_fpm.go | 12 ++++++++ pkg/packagekit/package_pkg.go | 37 +++++++++++++++++-------- pkg/packagekit/package_test.go | 2 +- pkg/packagekit/wix/wix.go | 2 -- pkg/packaging/packaging.go | 10 +++---- pkg/packaging/target.go | 22 +++++++++++++++ 7 files changed, 67 insertions(+), 20 deletions(-) diff --git a/pkg/packagekit/assets/distribution.dist b/pkg/packagekit/assets/distribution.dist index f63923ad1..cb1ff82d6 100644 --- a/pkg/packagekit/assets/distribution.dist +++ b/pkg/packagekit/assets/distribution.dist @@ -6,7 +6,7 @@ - + diff --git a/pkg/packagekit/package_fpm.go b/pkg/packagekit/package_fpm.go index dd0daeb1e..520fa8ec7 100644 --- a/pkg/packagekit/package_fpm.go +++ b/pkg/packagekit/package_fpm.go @@ -30,6 +30,7 @@ const ( type fpmOptions struct { outputType outputType replaces []string + arch string } type FpmOpt func(*fpmOptions) @@ -67,6 +68,12 @@ func WithReplaces(r []string) FpmOpt { } } +func WithArch(arch string) FpmOpt { + return func(f *fpmOptions) { + f.arch = arch + } +} + func PackageFPM(ctx context.Context, w io.Writer, po *PackageOptions, fpmOpts ...FpmOpt) error { ctx, span := trace.StartSpan(ctx, "packagekit.PackageRPM") defer span.End() @@ -81,6 +88,10 @@ func PackageFPM(ctx context.Context, w io.Writer, po *PackageOptions, fpmOpts .. return errors.New("Missing output type") } + if f.arch == "" { + return errors.New("missing architecture") + } + if err := isDirectory(po.Root); err != nil { return err } @@ -99,6 +110,7 @@ func PackageFPM(ctx context.Context, w io.Writer, po *PackageOptions, fpmOpts .. "-t", string(f.outputType), "-n", fmt.Sprintf("%s-%s", po.Name, po.Identifier), "-v", po.Version, + "-a", f.arch, "-p", filepath.Join("/out", outputFilename), "-C", "/pkgsrc", } diff --git a/pkg/packagekit/package_pkg.go b/pkg/packagekit/package_pkg.go index 6feda972c..165a08cc8 100644 --- a/pkg/packagekit/package_pkg.go +++ b/pkg/packagekit/package_pkg.go @@ -22,7 +22,7 @@ import ( //go:embed assets/distribution.dist var distributionTemplate []byte -func PackagePkg(ctx context.Context, w io.Writer, po *PackageOptions) error { +func PackagePkg(ctx context.Context, w io.Writer, po *PackageOptions, arch string) error { ctx, span := trace.StartSpan(ctx, "packagekit.PackagePkg") defer span.End() @@ -43,7 +43,7 @@ func PackagePkg(ctx context.Context, w io.Writer, po *PackageOptions) error { return fmt.Errorf("running pkgbuild: %w", err) } - if err := runProductbuild(ctx, flatPkgPath, distributionPkgPath, po); err != nil { + if err := runProductbuild(ctx, flatPkgPath, distributionPkgPath, arch, po); err != nil { return fmt.Errorf("running productbuild: %w", err) } @@ -163,7 +163,7 @@ func runPkbuild(ctx context.Context, outputPath string, po *PackageOptions) erro // package. It does this by execing productbuild. // // See https://github.com/kolide/launcher/issues/407 and associated links -func runProductbuild(ctx context.Context, flatPkgPath, distributionPkgPath string, po *PackageOptions) error { +func runProductbuild(ctx context.Context, flatPkgPath, distributionPkgPath string, arch string, po *PackageOptions) error { ctx, span := trace.StartSpan(ctx, "packagekit.runProductbuild") defer span.End() @@ -178,15 +178,17 @@ func runProductbuild(ctx context.Context, flatPkgPath, distributionPkgPath strin defer fh.Close() var templateData = struct { - Title string - Identifier string - Version string - PkgName string + Title string + Identifier string + Version string + PkgName string + HostArchitectures string }{ - Title: po.Title, - Identifier: po.Identifier, - Version: po.Version, - PkgName: filepath.Base(flatPkgPath), + Title: po.Title, + Identifier: po.Identifier, + Version: po.Version, + PkgName: filepath.Base(flatPkgPath), + HostArchitectures: hostArchitectures(arch), } t, err := template.New("distribution").Parse(string(distributionTemplate)) if err != nil { @@ -235,3 +237,16 @@ func runProductbuild(ctx context.Context, flatPkgPath, distributionPkgPath strin return nil } + +func hostArchitectures(arch string) string { + switch arch { + case "universal": + return "x86_64,arm64" + case "amd64": + return "x86_64" + case "arm64": + return "arm64" + default: + return "x86_64,arm64" + } +} diff --git a/pkg/packagekit/package_test.go b/pkg/packagekit/package_test.go index d812482f2..275d988a6 100644 --- a/pkg/packagekit/package_test.go +++ b/pkg/packagekit/package_test.go @@ -36,7 +36,7 @@ func TestPackageTrivial(t *testing.T) { err = PackageFPM(context.TODO(), io.Discard, po, AsRPM()) require.NoError(t, err) - err = PackagePkg(context.TODO(), io.Discard, po) + err = PackagePkg(context.TODO(), io.Discard, po, "universal") require.NoError(t, err) } diff --git a/pkg/packagekit/wix/wix.go b/pkg/packagekit/wix/wix.go index 260950151..8a0f720ea 100644 --- a/pkg/packagekit/wix/wix.go +++ b/pkg/packagekit/wix/wix.go @@ -78,14 +78,12 @@ func WithBuildDir(path string) WixOpt { func WithDocker(image string) WixOpt { return func(wo *wixTool) { wo.dockerImage = image - } } func WithUI() WixOpt { return func(wo *wixTool) { wo.ui = true - } } diff --git a/pkg/packaging/packaging.go b/pkg/packaging/packaging.go index 9f4fce52d..425e58393 100644 --- a/pkg/packaging/packaging.go +++ b/pkg/packaging/packaging.go @@ -388,24 +388,24 @@ func (p *PackageOptions) makePackage(ctx context.Context) error { switch { case p.target.Package == Deb: - if err := packagekit.PackageFPM(ctx, p.packageWriter, p.packagekitops, packagekit.AsDeb(), packagekit.WithReplaces(oldPackageNames)); err != nil { + if err := packagekit.PackageFPM(ctx, p.packageWriter, p.packagekitops, packagekit.AsDeb(), packagekit.WithReplaces(oldPackageNames), packagekit.WithArch(string(p.target.Arch))); err != nil { return fmt.Errorf("packaging, target %s: %w", p.target.String(), err) } case p.target.Package == Rpm: - if err := packagekit.PackageFPM(ctx, p.packageWriter, p.packagekitops, packagekit.AsRPM(), packagekit.WithReplaces(oldPackageNames)); err != nil { + if err := packagekit.PackageFPM(ctx, p.packageWriter, p.packagekitops, packagekit.AsRPM(), packagekit.WithReplaces(oldPackageNames), packagekit.WithArch(string(p.target.Arch))); err != nil { return fmt.Errorf("packaging, target %s: %w", p.target.String(), err) } case p.target.Package == Tar: - if err := packagekit.PackageFPM(ctx, p.packageWriter, p.packagekitops, packagekit.AsTar(), packagekit.WithReplaces(oldPackageNames)); err != nil { + if err := packagekit.PackageFPM(ctx, p.packageWriter, p.packagekitops, packagekit.AsTar(), packagekit.WithReplaces(oldPackageNames), packagekit.WithArch(string(p.target.Arch))); err != nil { return fmt.Errorf("packaging, target %s: %w", p.target.String(), err) } case p.target.Package == Pacman: - if err := packagekit.PackageFPM(ctx, p.packageWriter, p.packagekitops, packagekit.AsPacman(), packagekit.WithReplaces(oldPackageNames)); err != nil { + if err := packagekit.PackageFPM(ctx, p.packageWriter, p.packagekitops, packagekit.AsPacman(), packagekit.WithReplaces(oldPackageNames), packagekit.WithArch(string(p.target.Arch))); err != nil { return fmt.Errorf("packaging, target %s: %w", p.target.String(), err) } case p.target.Package == Pkg: - if err := packagekit.PackagePkg(ctx, p.packageWriter, p.packagekitops); err != nil { + if err := packagekit.PackagePkg(ctx, p.packageWriter, p.packagekitops, string(p.target.Arch)); err != nil { return fmt.Errorf("packaging, target %s: %w", p.target.String(), err) } case p.target.Package == Msi: diff --git a/pkg/packaging/target.go b/pkg/packaging/target.go index 8e1e18c4c..2594d51ac 100644 --- a/pkg/packaging/target.go +++ b/pkg/packaging/target.go @@ -12,6 +12,7 @@ type Target struct { Init InitFlavor Package PackageFlavor Platform PlatformFlavor + Arch ArchFlavor } type InitFlavor string @@ -51,6 +52,20 @@ const ( var knownPackageFlavors = [...]PackageFlavor{Pkg, Tar, Deb, Rpm, Msi, Pacman} +type ArchFlavor string + +const ( + Arm64 ArchFlavor = "arm64" + Amd64 ArchFlavor = "amd64" + Universal ArchFlavor = "universal" // Darwin only +) + +var defaultArchMap = map[PlatformFlavor]ArchFlavor{ + Darwin: Universal, + Windows: Amd64, + Linux: Amd64, +} + // Parse parses a string in the form platform-init-package and sets the target accordingly. func (t *Target) Parse(s string) error { components := strings.Split(s, "-") @@ -70,6 +85,13 @@ func (t *Target) Parse(s string) error { return err } + // For now, set the default arch according to the given platform + if defaultArch, ok := defaultArchMap[t.Platform]; ok { + t.Arch = defaultArch + } else { + return fmt.Errorf("cannot select default arch for unknown platform %s", t.Platform) + } + return nil } From 135252604a19f5ca01ad7709968a3ba6af4ef33d Mon Sep 17 00:00:00 2001 From: Rebecca Mahany-Horton Date: Fri, 19 May 2023 09:45:15 -0400 Subject: [PATCH 2/2] Reduce nesting --- pkg/packaging/target.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/packaging/target.go b/pkg/packaging/target.go index 2594d51ac..35e352989 100644 --- a/pkg/packaging/target.go +++ b/pkg/packaging/target.go @@ -86,11 +86,11 @@ func (t *Target) Parse(s string) error { } // For now, set the default arch according to the given platform - if defaultArch, ok := defaultArchMap[t.Platform]; ok { - t.Arch = defaultArch - } else { + defaultArch, ok := defaultArchMap[t.Platform] + if !ok { return fmt.Errorf("cannot select default arch for unknown platform %s", t.Platform) } + t.Arch = defaultArch return nil }