Skip to content

Commit

Permalink
added additional rpm handling (#38)
Browse files Browse the repository at this point in the history
* added additional rpm handling

* remove random print

* reorganized rpm.go

* updates based on feedback

let me know if this is what you were meaning, I could have misinterpreted when it came to the rpm path part

* moved some logic

* updated code references to "RPM" for consistency

* update to error message based on feedback

* updated rpm tests

* removed references to "RPMDestDir" and just put the relevant dir instead

* update for linter

* update to rpm template name and change for linter

* updates based on feedback

* update to use context

* oversight

* refactoring rpm tests

* further updates to tests

* removed unnecessary setup in test

* changed build order

finishing touch, processRPMs needs to come before generateCombustionScript so that the rpm script is registered

---------

Co-authored-by: Atanas Dinov <[email protected]>
  • Loading branch information
dbw7 and atanasdinov authored Nov 17, 2023
1 parent 3962567 commit 89f1468
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 83 deletions.
8 changes: 4 additions & 4 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ func (b *Builder) Build() error {
return fmt.Errorf("configuring users: %w", err)
}

err = b.generateCombustionScript()
err = b.processRPMs()
if err != nil {
return fmt.Errorf("generating combustion script: %w", err)
return fmt.Errorf("processing RPMs: %w", err)
}

err = b.copyRPMs()
err = b.generateCombustionScript()
if err != nil {
return fmt.Errorf("copying RPMs over: %w", err)
return fmt.Errorf("generating combustion script: %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)
}

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

0 comments on commit 89f1468

Please sign in to comment.