Skip to content

Commit

Permalink
chore: improve architecture detection and logic
Browse files Browse the repository at this point in the history
  • Loading branch information
M0Rf30 committed Sep 30, 2024
1 parent 9b54872 commit 8a1ab77
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 129 deletions.
35 changes: 17 additions & 18 deletions pkg/apk/apk.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (a *Apk) BuildPackage(artifactsPath string) error {
// It initializes the apkDir, cleans up any existing directory, creates the necessary packer directory,
// and generates the APKBUILD and post-installation script files. The method returns an error if any step fails.
func (a *Apk) PrepareFakeroot(_ string) error {
a.PKGBUILD.ArchComputed = APKArchs[a.PKGBUILD.ArchComputed]
a.apkDir = filepath.Join(a.PKGBUILD.StartDir, "apk")

if err := utils.RemoveAll(a.apkDir); err != nil {
Expand Down Expand Up @@ -62,24 +63,22 @@ func (a *Apk) PrepareFakeroot(_ string) error {
// It takes a string parameter `artifactsPath` which specifies the path where the artifacts are located.
// It returns an error if there was an error during the installation process.
func (a *Apk) Install(artifactsPath string) error {
for _, arch := range a.PKGBUILD.Arch {
pkgName := a.PKGBUILD.PkgName + "-" +
a.PKGBUILD.PkgVer +
"-" +
"r" + a.PKGBUILD.PkgRel +
"-" +
arch +
".apk"

pkgFilePath := filepath.Join(artifactsPath, a.PKGBUILD.PkgName, arch, pkgName)

if err := utils.Exec(true,
"apk",
"add",
"--allow-untrusted",
pkgFilePath); err != nil {
return err
}
pkgName := a.PKGBUILD.PkgName + "-" +
a.PKGBUILD.PkgVer +
"-" +
"r" + a.PKGBUILD.PkgRel +
"-" +
a.PKGBUILD.ArchComputed +
".apk"

pkgFilePath := filepath.Join(artifactsPath, a.PKGBUILD.PkgName, a.PKGBUILD.ArchComputed, pkgName)

if err := utils.Exec(true,
"apk",
"add",
"--allow-untrusted",
pkgFilePath); err != nil {
return err
}

return nil
Expand Down
21 changes: 17 additions & 4 deletions pkg/apk/constants.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
package apk

var buildEnvironmentDeps = []string{
"alpine-sdk",
}
var (
APKArchs = map[string]string{
"x86_64": "x86_64",
"i686": "x86",
"aarch64": "aarch64",
"armv7h": "armv7h",
"armv6h": "armv6h",
"any": "all",
}

buildEnvironmentDeps = []string{
"alpine-sdk",
}
)

const postInstall = `
{{- if .PreInst}}
Expand Down Expand Up @@ -51,7 +62,9 @@ epoch={{.Epoch}}
pkgver={{.PkgVer}}
pkgrel={{.PkgRel}}
pkgdesc="{{.PkgDesc}}"
arch="all"
{{- if .ArchComputed}}
arch="{{.ArchComputed}}"
{{- end}}
{{- if .Depends}}
depends="
{{ range .Depends }}{{ . }}
Expand Down
4 changes: 2 additions & 2 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ var (

DistroToPackageManager = map[string]string{
"alpine": "alpine",
"arch": "pacman",
"amazon": "redhat",
"fedora": "redhat",
"arch": "pacman",
"centos": "redhat",
"debian": "debian",
"fedora": "redhat",
"oracle": "redhat",
"rocky": "redhat",
"ubuntu": "debian",
Expand Down
4 changes: 2 additions & 2 deletions pkg/dpkg/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ Version: {{ if .Epoch}}{{ .Epoch }}:{{ end }}{{.PkgVer}}
{{- if .PkgRel}}-{{ .PkgRel }}{{- end }}
Section: {{.Section}}
Priority: {{.Priority}}
{{- with .Arch}}
Architecture: {{join .}}
{{- if .ArchComputed}}
Architecture: {{.ArchComputed}}
{{- end }}
{{- /* Optional fields */ -}}
{{- if .Maintainer}}
Expand Down
73 changes: 30 additions & 43 deletions pkg/dpkg/dpkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,12 @@ type Deb struct {
PKGBUILD *pkgbuild.PKGBUILD
}

// getArch updates the architecture field in the Deb struct.
//
// It iterates over the architecture values in the PKGBUILD field of the Deb struct
// and replaces them with the corresponding values from the DebArchs map.
func (d *Deb) getArch() {
for index, arch := range d.PKGBUILD.Arch {
d.PKGBUILD.Arch[index] = DebArchs[arch]
}
}

// createTarZst creates a compressed tar.zst archive from the specified source directory.
// It takes the source directory and the output file path as arguments and returns an error if any occurs.
func createTarZst(sourceDir, outputFile string) error {
// Retrieve the list of files from the source directory on disk.
// The map specifies that the files should be read from the sourceDir
// and the output path in the archive should be empty.
files, err := archiver.FilesFromDisk(nil, map[string]string{
sourceDir + string(os.PathSeparator): "",
})
Expand All @@ -61,13 +56,10 @@ func createTarZst(sourceDir, outputFile string) error {
return format.Archive(context.Background(), out, files)
}

// createConfFiles creates the conffiles file in the Deb package.
//
// It iterates over the Backup field of the PKGBUILD struct and adds each name
// to the data string. The data string is then written to the conffiles file
// located at the debDir path.
//
// Returns an error if there was a problem creating or writing to the file.
// createConfFiles creates the configuration files for the Debian package.
// It generates a file located at the debDir path containing the backup
// files specified in the PKGBUILD. Returns an error if there was a
// problem creating or writing to the file.
func (d *Deb) createConfFiles() error {
if len(d.PKGBUILD.Backup) == 0 {
return nil
Expand All @@ -88,6 +80,10 @@ func (d *Deb) createConfFiles() error {
return utils.CreateWrite(path, data)
}

// createCopyrightFile generates a copyright file for the Debian package.
// It checks if there is a license specified in the PKGBUILD and creates
// the copyright file accordingly. Returns an error if there was an
// issue creating the file.
func (d *Deb) createCopyrightFile() error {
if len(d.PKGBUILD.License) == 0 {
return nil
Expand All @@ -99,13 +95,10 @@ func (d *Deb) createCopyrightFile() error {
return d.PKGBUILD.CreateSpec(copyrightFilePath, tmpl)
}

// createDebconfFile creates a debconf file with the given variable and name.
//
// Parameters:
// - variable: the variable used to create the debconf asset.
// - name: the name of the debconf asset.
//
// Return type: error.
// createDebconfFile creates a debconf file with the given variable and
// name. It takes parameters for the variable used to create the debconf
// asset and the name of the debconf asset. Returns an error if there
// was an issue during the creation.
func (d *Deb) createDebconfFile(name, variable string) error {
if variable == "" {
return nil
Expand All @@ -118,9 +111,8 @@ func (d *Deb) createDebconfFile(name, variable string) error {
}

// createScripts generates and writes the scripts for the Deb package.
//
// It takes no parameters.
// It returns an error if there was an issue generating or writing the scripts.
// It takes no parameters and returns an error if there was an issue
// generating or writing the scripts.
func (d *Deb) createScripts() error {
scripts := map[string]string{
"preinst": d.PKGBUILD.PreInst,
Expand Down Expand Up @@ -153,18 +145,15 @@ func (d *Deb) createScripts() error {
}

// createDeb generates Deb package files from the given artifact path.
//
// It takes a string parameter `artifactPath` which represents the path where the
// Deb package files will be generated.
//
// The function returns an error if there was an issue generating the Deb package
// files.
// It takes a string parameter `artifactPath` which represents the path
// where the Deb package files will be generated. The function returns
// an error if there was an issue generating the Deb package files.
func (d *Deb) createDeb(artifactPath, control, data string) error {
// Create the .deb package
artifactFilePath := filepath.Join(artifactPath,
fmt.Sprintf("%s_%s-%s_%s.deb",
d.PKGBUILD.PkgName, d.PKGBUILD.PkgVer, d.PKGBUILD.PkgRel,
d.PKGBUILD.Arch[0]))
d.PKGBUILD.ArchComputed))

cleanFilePath := filepath.Clean(artifactFilePath)
debianBinary := []byte(binaryContent)
Expand Down Expand Up @@ -346,8 +335,8 @@ func (d *Deb) BuildPackage(artifactsPath string) error {
// It retrieves architecture and release information, cleans up the debDir, creates necessary
// resources, and strips binaries. The method returns an error if any step fails.
func (d *Deb) PrepareFakeroot(_ string) error {
d.getArch()
d.getRelease()
d.PKGBUILD.ArchComputed = DebArchs[d.PKGBUILD.ArchComputed]

if err := utils.RemoveAll(d.debDir); err != nil {
return err
Expand All @@ -365,15 +354,13 @@ func (d *Deb) PrepareFakeroot(_ string) error {
}

func (d *Deb) Install(artifactsPath string) error {
for _, arch := range d.PKGBUILD.Arch {
artifactFilePath := filepath.Join(artifactsPath,
fmt.Sprintf("%s_%s-%s_%s.deb",
d.PKGBUILD.PkgName, d.PKGBUILD.PkgVer, d.PKGBUILD.PkgRel,
arch))
artifactFilePath := filepath.Join(artifactsPath,
fmt.Sprintf("%s_%s-%s_%s.deb",
d.PKGBUILD.PkgName, d.PKGBUILD.PkgVer, d.PKGBUILD.PkgRel,
d.PKGBUILD.ArchComputed))

if err := utils.Exec(false, "", "apt-get", "install", "-y", artifactFilePath); err != nil {
return err
}
if err := utils.Exec(false, "", "apt-get", "install", "-y", artifactFilePath); err != nil {
return err
}

return nil
Expand Down
7 changes: 2 additions & 5 deletions pkg/pacman/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,8 @@ epoch={{.Epoch}}
pkgver={{.PkgVer}}
pkgrel={{.PkgRel}}
pkgdesc="{{.PkgDesc}}"
{{- if .Arch}}
arch=(
{{ range .Arch }}"{{ . }}"
{{ end }}
)
{{- if .ArchComputed}}
arch=({{.ArchComputed}})
{{- end }}
{{- if .Depends}}
depends=(
Expand Down
34 changes: 16 additions & 18 deletions pkg/pacman/pacman.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,22 @@ func (p *Pacman) PrepareFakeroot(artifactsPath string) error {
// artifactsPath: the path where the package artifacts are located.
// error: an error if the installation fails.
func (p *Pacman) Install(artifactsPath string) error {
for _, arch := range p.PKGBUILD.Arch {
pkgName := p.PKGBUILD.PkgName + "-" +
p.PKGBUILD.PkgVer +
"-" +
p.PKGBUILD.PkgRel +
"-" +
arch +
".pkg.tar.zst"

pkgFilePath := filepath.Join(artifactsPath, pkgName)

if err := utils.Exec(false, "",
"pacman",
"-U",
"--noconfirm",
pkgFilePath); err != nil {
return err
}
pkgName := p.PKGBUILD.PkgName + "-" +
p.PKGBUILD.PkgVer +
"-" +
p.PKGBUILD.PkgRel +
"-" +
p.PKGBUILD.ArchComputed +
".pkg.tar.zst"

pkgFilePath := filepath.Join(artifactsPath, pkgName)

if err := utils.Exec(false, "",
"pacman",
"-U",
"--noconfirm",
pkgFilePath); err != nil {
return err
}

return nil
Expand Down
26 changes: 26 additions & 0 deletions pkg/pkgbuild/pkgbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var Verbose bool
// templating and other rpm/deb descriptors.
type PKGBUILD struct {
Arch []string
ArchComputed string
Backup []string
Build string
Codename string
Expand Down Expand Up @@ -340,6 +341,31 @@ func (pkgBuild *PKGBUILD) parseDirective(input string) (string, int, error) {
return key, priority, nil
}

// ValidateArchitecture checks if the architecture specified in the PKGBUILD
// is supported. If the architecture is "any", it sets the computed architecture
// to "any". If the architecture is not "any", it checks if the current architecture
// is in the list of supported architectures. If the current architecture is not
// supported, it logs a fatal error with the package name. Finally, it sets the
// computed architecture to the current architecture if it is supported.
func (pkgBuild *PKGBUILD) ValidateArchitecture() {
isSupported := utils.Contains(pkgBuild.Arch, "any")
if isSupported {
pkgBuild.ArchComputed = "any"

return
}

currentArch := utils.GetArchitecture()

isSupported = utils.Contains(pkgBuild.Arch, currentArch)
if !isSupported {
utils.Logger.Fatal("unsupported architecture",
utils.Logger.Args("pkgname", pkgBuild.PkgName))
}

pkgBuild.ArchComputed = currentArch
}

// setMainFolders sets the main folders for the PKGBUILD.
//
// It takes no parameters.
Expand Down
8 changes: 5 additions & 3 deletions pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (mpc *MultipleProject) BuildAll() error {
}

if !NoBuild {
if err := mpc.createPackages(proj); err != nil {
if err := mpc.createPackage(proj); err != nil {
return err
}
}
Expand Down Expand Up @@ -249,11 +249,11 @@ func (mpc *MultipleProject) checkPkgsRange(fromPkgName, toPkgName string) {
}
}

// createPackages creates packages for the MultipleProject.
// createPackage creates packages for the MultipleProject.
//
// It takes a pointer to a MultipleProject as a receiver and a pointer to a Project as a parameter.
// It returns an error.
func (mpc *MultipleProject) createPackages(proj *Project) error {
func (mpc *MultipleProject) createPackage(proj *Project) error {
if mpc.Output != "" {
absOutput, err := filepath.Abs(mpc.Output)
if err != nil {
Expand Down Expand Up @@ -337,6 +337,8 @@ func (mpc *MultipleProject) populateProjects(distro, release, path string) error
return err
}

pkgbuildFile.ValidateArchitecture()

packageManager = packer.GetPackageManager(pkgbuildFile, distro)

proj := &Project{
Expand Down
2 changes: 1 addition & 1 deletion pkg/project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pkgdesc__fedora="Http file server written with Go for Fedora"
pkgdesc__rocky="Http file server written with Go for Rocky"
pkgdesc__ubuntu="Http file server written with Go for Ubuntu"
maintainer="Example <[email protected]>"
arch=("all")
arch=("x86_64")
license=("GPL-3.0-only")
section="utils"
priority="optional"
Expand Down
4 changes: 2 additions & 2 deletions pkg/rpm/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ Epoch: {{.Epoch}}
{{- end }}
Version: {{.PkgVer}}
Release: {{.PkgRel}}
{{- with .Arch}}
BuildArch: {{join .}}
{{- if .ArchComputed}}
BuildArch: {{.ArchComputed}}
{{- end }}
{{- if .Section}}
Group: {{.Section}}
Expand Down
Loading

0 comments on commit 8a1ab77

Please sign in to comment.