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 f1d7c53
Show file tree
Hide file tree
Showing 23 changed files with 273 additions and 285 deletions.
3 changes: 2 additions & 1 deletion 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 @@ -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
22 changes: 0 additions & 22 deletions pkg/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,6 @@ 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)
Expand Down
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.

37 changes: 20 additions & 17 deletions pkg/build/raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ import (

func TestCreateRawImageCopyCommand(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",
},
}
ctx := context.Context{
ImageConfigDir: "config-dir",
}
builder := New(&imageConfig, &ctx)

// Test
cmd := builder.createRawImageCopyCommand()
Expand All @@ -46,18 +47,20 @@ func TestWriteModifyScript(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

imageConfig := config.ImageConfig{
Image: config.Image{
OutputImageName: "output-image",
},
OperatingSystem: config.OperatingSystem{
KernelArgs: []string{"alpha", "beta"},
},
}
ctx, err := context.NewContext("config-dir", tmpDir, false)
require.NoError(t, err)

builder := New(&imageConfig, ctx)
builder := Builder{
imageConfig: &config.ImageConfig{
Image: config.Image{
OutputImageName: "output-image",
},
OperatingSystem: config.OperatingSystem{
KernelArgs: []string{"alpha", "beta"},
},
},
context: ctx,
}

// Test
err = builder.writeModifyScript()
Expand Down
37 changes: 0 additions & 37 deletions pkg/build/users.go

This file was deleted.

Loading

0 comments on commit f1d7c53

Please sign in to comment.