From 7b5fd8b458774d1d6a11595f07b6df5bcabdba99 Mon Sep 17 00:00:00 2001 From: dbw7 <90856056+dbw7@users.noreply.github.com> Date: Mon, 13 Nov 2023 17:10:04 -0500 Subject: [PATCH] added additional rpm handling --- pkg/build/build.go | 4 +- pkg/build/rpm.go | 64 +++++++++-- pkg/build/rpm_test.go | 109 ++++++++++++++++++- pkg/build/scripts/rpms/10_rpm_install.sh.tpl | 7 ++ 4 files changed, 171 insertions(+), 13 deletions(-) create mode 100644 pkg/build/scripts/rpms/10_rpm_install.sh.tpl diff --git a/pkg/build/build.go b/pkg/build/build.go index f8232f62..a5f1b328 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -57,9 +57,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 { diff --git a/pkg/build/rpm.go b/pkg/build/rpm.go index 48e37d44..56caa114 100644 --- a/pkg/build/rpm.go +++ b/pkg/build/rpm.go @@ -1,11 +1,20 @@ package build import ( + _ "embed" "fmt" "os" "path/filepath" + "strings" ) +const ( + modifyRPMScriptName = "10_rpm_install.sh" +) + +//go:embed scripts/rpms/10_rpm_install.sh.tpl +var modifyRPMScript string + func (b *Builder) getRPMFileNames(rpmSourceDir string) ([]string, error) { var rpmFileNames []string @@ -27,9 +36,23 @@ func (b *Builder) getRPMFileNames(rpmSourceDir string) ([]string, error) { return rpmFileNames, nil } -func (b *Builder) copyRPMs() error { +func (b *Builder) copyRPMs(rpmSourceDir string, rpmDestDir string, rpmFileNames []string) error { + for _, rpm := range rpmFileNames { + sourcePath := filepath.Join(rpmSourceDir, rpm) + destPath := filepath.Join(rpmDestDir, rpm) + + err := copyFile(sourcePath, destPath) + if err != nil { + return fmt.Errorf("copying file %s: %w", sourcePath, err) + } + } + + return nil +} + +func (b *Builder) processRPMs() error { rpmSourceDir := filepath.Join(b.buildConfig.ImageConfigDir, "rpms") - // Only proceed with copying the RPMs if the directory exists + // Only proceed with processing the RPMs if the directory exists _, err := os.Stat(rpmSourceDir) if err != nil { if os.IsNotExist(err) { @@ -44,15 +67,38 @@ func (b *Builder) copyRPMs() error { return fmt.Errorf("getting rpm file names: %w", err) } - for _, rpm := range rpmFileNames { - sourcePath := filepath.Join(rpmSourceDir, rpm) - destPath := filepath.Join(rpmDestDir, rpm) + err = b.copyRPMs(rpmSourceDir, rpmDestDir, rpmFileNames) + if err != nil { + return fmt.Errorf("copying RPMs over: %w", err) + } - err = copyFile(sourcePath, destPath) - if err != nil { - return fmt.Errorf("copying file %s: %w", sourcePath, err) - } + err = b.writeRPMScript(rpmFileNames) + if err != nil { + return fmt.Errorf("writing the rpm install script: %w", err) } return nil } + +func (b *Builder) writeRPMScript(rpmFileNamesArray []string) error { + rpmFileNamesString := strings.Join(rpmFileNamesArray, " ") + values := struct { + RPMs string + }{ + RPMs: rpmFileNamesString, + } + + writtenFilename, err := b.writeCombustionFile(modifyRPMScriptName, modifyRPMScript, &values) + if err != nil { + return fmt.Errorf("writing rpm script %s: %w", modifyRPMScriptName, err) + } + err = os.Chmod(writtenFilename, modifyScriptMode) + if err != nil { + return fmt.Errorf("changing permissions on the rpm script %s: %w", modifyRPMScriptName, err) + } + + fmt.Println(writtenFilename, "xaksdokasd") + b.registerCombustionScript(modifyRPMScriptName) + + return nil +} diff --git a/pkg/build/rpm_test.go b/pkg/build/rpm_test.go index b5420247..8778af26 100644 --- a/pkg/build/rpm_test.go +++ b/pkg/build/rpm_test.go @@ -1,6 +1,7 @@ package build import ( + "io/fs" "os" "path/filepath" "testing" @@ -69,8 +70,11 @@ func TestCopyRPMs(t *testing.T) { file2, err := os.Create(file2Path) require.NoError(t, err) + rpmFileNames, err := builder.getRPMFileNames(rpmSourceDir) + require.NoError(t, err) + // Test - err = builder.copyRPMs() + err = builder.copyRPMs(rpmSourceDir, rpmDestDir, rpmFileNames) // Verify require.NoError(t, err) @@ -117,9 +121,110 @@ func TestCopyRPMsNoRPMDir(t *testing.T) { require.NoError(t, err) defer os.Remove(builder.eibBuildDir) + rpmSourceDir := filepath.Join(builder.buildConfig.ImageConfigDir, "rpms") + rpmDestDir := builder.combustionDir + + // Test + err = builder.copyRPMs(rpmSourceDir, rpmDestDir, nil) + + // Verify + require.NoError(t, err) +} + +func TestWriteRPMScript(t *testing.T) { + // Setup + tmpDir, err := os.MkdirTemp("", "eib-") + require.NoError(t, err) + defer os.RemoveAll(tmpDir) + + buildConfig := config.BuildConfig{ + ImageConfigDir: "../config/testdata", + } + builder := New(nil, &buildConfig) + require.NoError(t, builder.prepareBuildDir()) + + rpmSourceDir := filepath.Join(builder.buildConfig.ImageConfigDir, "rpms") + + file1Path := filepath.Join(rpmSourceDir, "rpm1.rpm") + defer os.Remove(file1Path) + file1, err := os.Create(file1Path) + require.NoError(t, err) + + file2Path := filepath.Join(rpmSourceDir, "rpm2.rpm") + defer os.Remove(file2Path) + file2, err := os.Create(file2Path) + require.NoError(t, err) + + rpmFileNames, err := builder.getRPMFileNames(rpmSourceDir) + require.NoError(t, err) + // Test - err = builder.copyRPMs() + err = builder.writeRPMScript(rpmFileNames) // Verify require.NoError(t, err) + + expectedFilename := filepath.Join(builder.combustionDir, modifyRPMScriptName) + foundBytes, err := os.ReadFile(expectedFilename) + require.NoError(t, err) + + stats, err := os.Stat(expectedFilename) + require.NoError(t, err) + assert.Equal(t, fs.FileMode(modifyScriptMode), stats.Mode()) + + foundContents := string(foundBytes) + assert.Contains(t, foundContents, "rpm1.rpm") + assert.Contains(t, foundContents, "rpm2.rpm") + + // Cleanup + assert.NoError(t, file1.Close()) + assert.NoError(t, file2.Close()) +} + +func TestProcessRPMs(t *testing.T) { + // Setup + bc := config.BuildConfig{ + ImageConfigDir: "../config/testdata", + } + builder := New(nil, &bc) + err := builder.prepareBuildDir() + require.NoError(t, err) + defer os.Remove(builder.eibBuildDir) + + rpmSourceDir := filepath.Join(builder.buildConfig.ImageConfigDir, "rpms") + rpmDestDir := builder.combustionDir + + file1Path := filepath.Join(rpmSourceDir, "rpm1.rpm") + defer os.Remove(file1Path) + file1, err := os.Create(file1Path) + require.NoError(t, err) + + file2Path := filepath.Join(rpmSourceDir, "rpm2.rpm") + defer os.Remove(file2Path) + file2, err := os.Create(file2Path) + require.NoError(t, err) + + // Test + err = builder.processRPMs() + + // Verify + require.NoError(t, err) + + _, err = os.Stat(filepath.Join(rpmDestDir, "rpm1.rpm")) + require.NoError(t, err) + + _, err = os.Stat(filepath.Join(rpmDestDir, "rpm2.rpm")) + require.NoError(t, err) + + expectedFilename := filepath.Join(rpmDestDir, modifyRPMScriptName) + foundBytes, err := os.ReadFile(expectedFilename) + require.NoError(t, err) + + foundContents := string(foundBytes) + assert.Contains(t, foundContents, "rpm1.rpm") + assert.Contains(t, foundContents, "rpm2.rpm") + + // Cleanup + assert.NoError(t, file1.Close()) + assert.NoError(t, file2.Close()) } diff --git a/pkg/build/scripts/rpms/10_rpm_install.sh.tpl b/pkg/build/scripts/rpms/10_rpm_install.sh.tpl new file mode 100644 index 00000000..01249187 --- /dev/null +++ b/pkg/build/scripts/rpms/10_rpm_install.sh.tpl @@ -0,0 +1,7 @@ +#!/bin/bash +set -euo pipefail + +# Template Fields +# RPMs - A string that contains all of the rpms present in the user created config directory, separated by spaces. + +rpm -ivh --nosignature {{.RPMs}} \ No newline at end of file