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

added additional rpm handling #38

Merged
merged 19 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
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
4 changes: 2 additions & 2 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ func (b *Builder) Build() error {
return fmt.Errorf("generating combustion script: %w", err)
}

err = b.copyRPMs()
err = b.processRPMs()
if err != nil {
return fmt.Errorf("copying RPMs over: %w", err)
return fmt.Errorf("processing RPMs: %w", err)
}

switch b.imageConfig.Image.ImageType {
Expand Down
99 changes: 79 additions & 20 deletions pkg/build/rpm.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,57 @@
package build

import (
_ "embed"
"fmt"
"os"
"path/filepath"

"strings"

"github.com/suse-edge/edge-image-builder/pkg/fileio"
)

func (b *Builder) getRPMFileNames(rpmSourceDir string) ([]string, error) {
const (
modifyRPMScriptName = "10-rpm-install.sh"
)

//go:embed scripts/rpms/10-rpm-install.sh.tpl
var modifyRPMScript string

func (b *Builder) processRPMs() error {
rpmSourceDir, err := b.generateRPMPath()
if err != nil {
return fmt.Errorf("generating RPM path: %w", err)
}
// Only proceed with processing the RPMs if the directory exists
if rpmSourceDir == "" {
return nil
}

rpmFileNames, err := getRPMFileNames(rpmSourceDir)
if err != nil {
return fmt.Errorf("getting RPM file names: %w", err)
}

err = copyRPMs(rpmSourceDir, b.context.CombustionDir, rpmFileNames)
if err != nil {
return fmt.Errorf("copying RPMs over: %w", err)
atanasdinov marked this conversation as resolved.
Show resolved Hide resolved
}

err = b.writeRPMScript(rpmFileNames)
if err != nil {
return fmt.Errorf("writing the RPM install script %s: %w", modifyRPMScriptName, err)
}

return nil
}

func getRPMFileNames(rpmSourceDir string) ([]string, error) {
var rpmFileNames []string

rpms, err := os.ReadDir(rpmSourceDir)
if err != nil {
return nil, fmt.Errorf("reading rpm source dir: %w", err)
return nil, fmt.Errorf("reading RPM source dir: %w", err)
}

for _, rpmFile := range rpms {
Expand All @@ -23,38 +61,59 @@ func (b *Builder) getRPMFileNames(rpmSourceDir string) ([]string, error) {
}

if len(rpmFileNames) == 0 {
return nil, fmt.Errorf("no rpms found")
return nil, fmt.Errorf("no RPMs found")
}

return rpmFileNames, nil
}

func (b *Builder) copyRPMs() error {
rpmSourceDir := filepath.Join(b.context.ImageConfigDir, "rpms")
// Only proceed with copying the RPMs if the directory exists
_, err := os.Stat(rpmSourceDir)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("checking for rpm directory at %s: %w", rpmSourceDir, err)
func copyRPMs(rpmSourceDir string, rpmDestDir string, rpmFileNames []string) error {
if rpmDestDir == "" {
return fmt.Errorf("RPM destination directory cannot be empty")
}
rpmDestDir := b.context.CombustionDir

rpmFileNames, err := b.getRPMFileNames(rpmSourceDir)
if err != nil {
return fmt.Errorf("getting rpm file names: %w", err)
}

for _, rpm := range rpmFileNames {
sourcePath := filepath.Join(rpmSourceDir, rpm)
destPath := filepath.Join(rpmDestDir, rpm)

err = fileio.CopyFile(sourcePath, destPath)
err := fileio.CopyFile(sourcePath, destPath)
if err != nil {
return fmt.Errorf("copying file %s: %w", sourcePath, err)
}
}

return nil
}

func (b *Builder) writeRPMScript(rpmFileNames []string) error {
values := struct {
RPMs string
}{
RPMs: strings.Join(rpmFileNames, " "),
}

writtenFilename, err := b.writeCombustionFile(modifyRPMScriptName, modifyRPMScript, &values)
if err != nil {
return fmt.Errorf("writing RPM script: %w", err)
}
err = os.Chmod(writtenFilename, modifyScriptMode)
if err != nil {
return fmt.Errorf("adjusting permissions: %w", err)
}

b.registerCombustionScript(modifyRPMScriptName)

return nil
}

func (b *Builder) generateRPMPath() (string, error) {
rpmSourceDir := filepath.Join(b.context.ImageConfigDir, "rpms")
_, err := os.Stat(rpmSourceDir)
if err != nil {
if os.IsNotExist(err) {
return "", nil
}
return "", fmt.Errorf("checking for RPM directory at %s: %w", rpmSourceDir, err)
}

return rpmSourceDir, nil
}
Loading