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

Start to include arch in packaging targets + packaging commands #1198

Merged
merged 4 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion pkg/packagekit/assets/distribution.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<bundle CFBundleShortVersionString="{{.Version}}" CFBundleVersion="{{.Version}}" id="com.{{.Identifier}}.launcher" path="usr/local/{{.Identifier}}/Kolide.app"/>
</bundle-version>
</pkg-ref>
<options customize="never" require-scripts="false" hostArchitectures="x86_64,arm64"/>
<options customize="never" require-scripts="false" hostArchitectures="{{.HostArchitectures}}"/>
<choices-outline>
<line choice="default">
<line choice="com.{{.Identifier}}.launcher"/>
Expand Down
12 changes: 12 additions & 0 deletions pkg/packagekit/package_fpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
type fpmOptions struct {
outputType outputType
replaces []string
arch string
}

type FpmOpt func(*fpmOptions)
Expand Down Expand Up @@ -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()
Expand All @@ -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
}
Expand All @@ -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",
}
Expand Down
37 changes: 26 additions & 11 deletions pkg/packagekit/package_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
}

Expand Down Expand Up @@ -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()

Expand All @@ -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 {
Expand Down Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion pkg/packagekit/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
2 changes: 0 additions & 2 deletions pkg/packagekit/wix/wix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/packaging/packaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions pkg/packaging/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Target struct {
Init InitFlavor
Package PackageFlavor
Platform PlatformFlavor
Arch ArchFlavor
}

type InitFlavor string
Expand Down Expand Up @@ -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, "-")
Expand All @@ -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)
}

James-Pickett marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

Expand Down