Skip to content

Commit

Permalink
Configure combustion components
Browse files Browse the repository at this point in the history
Signed-off-by: Atanas Dinov <[email protected]>
  • Loading branch information
atanasdinov committed Nov 21, 2023
1 parent fe58bfd commit 5a882ce
Show file tree
Hide file tree
Showing 27 changed files with 526 additions and 525 deletions.
5 changes: 3 additions & 2 deletions cmd/eib/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"

"github.com/suse-edge/edge-image-builder/pkg/build"
"github.com/suse-edge/edge-image-builder/pkg/combustion"
"github.com/suse-edge/edge-image-builder/pkg/config"
"github.com/suse-edge/edge-image-builder/pkg/context"
"go.uber.org/zap"
Expand Down Expand Up @@ -50,7 +51,7 @@ func processArgs() (*config.ImageConfig, *context.Context, error) {
return nil, nil, fmt.Errorf("validating the config dir %s: %w", configDir, err)
}

ctx, err := context.NewContext(configDir, buildDir, deleteBuildDir)
ctx, err := context.NewContext(configDir, buildDir, deleteBuildDir, imageConfig)
if err != nil {
return nil, nil, fmt.Errorf("building dir structure: %w", err)
}
Expand Down Expand Up @@ -117,7 +118,7 @@ func main() {
zap.L().Fatal("CLI arguments could not be parsed", zap.Error(err))
}

builder := build.New(imageConfig, ctx)
builder := build.New(imageConfig, ctx, combustion.Configure)
if err = builder.Build(); err != nil {
zap.L().Fatal("An error occurred building the image", zap.Error(err))
}
Expand Down
64 changes: 11 additions & 53 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,31 @@ package build

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

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

type configureCombustion func(ctx *context.Context) error

type Builder struct {
imageConfig *config.ImageConfig
context *context.Context
imageConfig *config.ImageConfig
context *context.Context
configureCombustion configureCombustion
}

func New(imageConfig *config.ImageConfig, context *context.Context) *Builder {
func New(imageConfig *config.ImageConfig, ctx *context.Context, configureCombustionFunc configureCombustion) *Builder {
return &Builder{
imageConfig: imageConfig,
context: context,
imageConfig: imageConfig,
context: ctx,
configureCombustion: configureCombustionFunc,
}
}

func (b *Builder) Build() error {
var combustionScripts []string

messageScript, err := b.configureMessage()
if err != nil {
return fmt.Errorf("configuring the welcome message: %w", err)
}

combustionScripts = append(combustionScripts, messageScript)

customScripts, err := b.configureCustomScripts()
if err != nil {
return fmt.Errorf("configuring custom scripts: %w", err)
}

combustionScripts = append(combustionScripts, customScripts...)

userScript, err := b.configureUsers()
if err != nil {
return fmt.Errorf("configuring users: %w", err)
}

if userScript != "" {
combustionScripts = append(combustionScripts, userScript)
}

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

if rpmScript != "" {
combustionScripts = append(combustionScripts, rpmScript)
}

script, err := combustion.GenerateScript(combustionScripts)
if err != nil {
return fmt.Errorf("generating combustion script: %w", err)
}

if err = os.WriteFile("script", []byte(script), fileio.ExecutablePerms); err != nil {
return fmt.Errorf("writing combustion script: %w", err)
if err := b.configureCombustion(b.context); err != nil {
return fmt.Errorf("configuring combustion: %w", err)
}

switch b.imageConfig.Image.ImageType {
Expand All @@ -82,10 +44,6 @@ func (b *Builder) generateBuildDirFilename(filename string) string {
return filepath.Join(b.context.BuildDir, filename)
}

func (b *Builder) generateCombustionDirFilename(filename string) string {
return filepath.Join(b.context.CombustionDir, filename)
}

func (b *Builder) generateOutputImageFilename() string {
filename := filepath.Join(b.context.ImageConfigDir, b.imageConfig.Image.OutputImageName)
return filename
Expand Down
33 changes: 7 additions & 26 deletions pkg/build/build_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package build

import (
"os"
"path/filepath"
"testing"

Expand All @@ -9,38 +10,18 @@ import (
"github.com/suse-edge/edge-image-builder/pkg/context"
)

func TestGenerateCombustionDirFilename(t *testing.T) {
// Setup
ctx, err := context.NewContext("", "", true)
require.NoError(t, err)
defer func() {
assert.NoError(t, context.CleanUpBuildDir(ctx))
}()

builder := Builder{
context: ctx,
}

testFilename := "combustion-file.sh"

// Test
filename := builder.generateCombustionDirFilename(testFilename)

// Verify
expectedFilename := filepath.Join(ctx.CombustionDir, testFilename)
assert.Equal(t, expectedFilename, filename)
}

func TestGenerateBuildDirFilename(t *testing.T) {
// Setup
ctx, err := context.NewContext("", "", true)
tmpDir, err := os.MkdirTemp("", "eib-")
require.NoError(t, err)
defer func() {
assert.NoError(t, context.CleanUpBuildDir(ctx))
assert.NoError(t, os.RemoveAll(tmpDir))
}()

builder := Builder{
context: ctx,
context: &context.Context{
BuildDir: tmpDir,
},
}

testFilename := "build-dir-file.sh"
Expand All @@ -49,6 +30,6 @@ func TestGenerateBuildDirFilename(t *testing.T) {
filename := builder.generateBuildDirFilename(testFilename)

// Verify
expectedFilename := filepath.Join(ctx.BuildDir, testFilename)
expectedFilename := filepath.Join(builder.context.BuildDir, testFilename)
require.Equal(t, expectedFilename, filename)
}
16 changes: 9 additions & 7 deletions pkg/build/grub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import (

func TestGenerateGRUBGuestfishCommands(t *testing.T) {
// Setup
imageConfig := config.ImageConfig{
OperatingSystem: config.OperatingSystem{
KernelArgs: []string{"alpha", "beta"},
builder := Builder{
imageConfig: &config.ImageConfig{
OperatingSystem: config.OperatingSystem{
KernelArgs: []string{"alpha", "beta"},
},
},
}
builder := New(&imageConfig, nil)

// Test
commandString, err := builder.generateGRUBGuestfishCommands()
Expand All @@ -33,10 +34,11 @@ func TestGenerateGRUBGuestfishCommands(t *testing.T) {

func TestGenerateGRUBGuestfishCommandsNoArgs(t *testing.T) {
// Setup
imageConfig := config.ImageConfig{
OperatingSystem: config.OperatingSystem{},
builder := Builder{
imageConfig: &config.ImageConfig{
OperatingSystem: config.OperatingSystem{},
},
}
builder := New(&imageConfig, nil)

// Test
commandString, err := builder.generateGRUBGuestfishCommands()
Expand Down
49 changes: 26 additions & 23 deletions pkg/build/iso_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ func TestDeleteNoExistingImage(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

imageConfig := config.ImageConfig{
Image: config.Image{
OutputImageName: "not-there",
builder := Builder{
imageConfig: &config.ImageConfig{
Image: config.Image{
OutputImageName: "not-there",
},
},
context: &context.Context{
ImageConfigDir: tmpDir,
},
}
ctx := context.Context{
ImageConfigDir: tmpDir,
}
builder := New(&imageConfig, &ctx)

// Test
err = builder.deleteExistingOutputIso()
Expand All @@ -40,15 +41,16 @@ func TestDeleteExistingImage(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

imageConfig := config.ImageConfig{
Image: config.Image{
OutputImageName: "not-there",
builder := Builder{
imageConfig: &config.ImageConfig{
Image: config.Image{
OutputImageName: "not-there",
},
},
context: &context.Context{
ImageConfigDir: tmpDir,
},
}
ctx := context.Context{
ImageConfigDir: tmpDir,
}
builder := New(&imageConfig, &ctx)

_, err = os.Create(builder.generateOutputImageFilename())
require.NoError(t, err)
Expand All @@ -66,17 +68,18 @@ func TestDeleteExistingImage(t *testing.T) {

func TestCreateXorrisoCommand(t *testing.T) {
// Setup
imageConfig := config.ImageConfig{
Image: config.Image{
BaseImage: "base-image",
OutputImageName: "build-image",
builder := Builder{
imageConfig: &config.ImageConfig{
Image: config.Image{
BaseImage: "base-image",
OutputImageName: "build-image",
},
},
context: &context.Context{
ImageConfigDir: "config-dir",
CombustionDir: "combustion",
},
}
ctx := context.Context{
ImageConfigDir: "config-dir",
CombustionDir: "combustion",
}
builder := New(&imageConfig, &ctx)

// Test
cmd, logfile, err := builder.createXorrisoCommand()
Expand Down
26 changes: 0 additions & 26 deletions pkg/build/message.go

This file was deleted.

33 changes: 0 additions & 33 deletions pkg/build/message_test.go

This file was deleted.

Loading

0 comments on commit 5a882ce

Please sign in to comment.