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

Remove raw image on rebuild #339

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

* [#319](https://github.com/suse-edge/edge-image-builder/issues/319) - Combustion fails when combustion directory content is larger than half of the RAM of the system
* [#233](https://github.com/suse-edge/edge-image-builder/issues/233) - Use different Helm chart sources for development and production builds
* [#337](https://github.com/suse-edge/edge-image-builder/issues/337) - Re-running raw builds should remove the previous built image
* [#95](https://github.com/suse-edge/edge-image-builder/issues/95) - Compressed images are not supported

---
Expand Down
9 changes: 9 additions & 0 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ func (b *Builder) generateBaseImageFilename() string {
return filename
}

func (b *Builder) deleteExistingOutputImage() error {
outputFilename := b.generateOutputImageFilename()
err := os.Remove(outputFilename)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("error deleting file %s: %w", outputFilename, err)
}
return nil
}

func SetupBuildDirectory(rootDir string) (string, error) {
timestamp := time.Now().Format("Jan02_15-04-05")
buildDir := filepath.Join(rootDir, fmt.Sprintf("build-%s", timestamp))
Expand Down
55 changes: 55 additions & 0 deletions pkg/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,58 @@ func TestGenerateBuildDirFilename(t *testing.T) {
expectedFilename := filepath.Join(builder.context.BuildDir, testFilename)
require.Equal(t, expectedFilename, filename)
}

func TestDeleteNoExistingImage(t *testing.T) {
// Setup
tmpDir, err := os.MkdirTemp("", "eib-")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

builder := Builder{
context: &image.Context{
ImageConfigDir: tmpDir,
ImageDefinition: &image.Definition{
Image: image.Image{
OutputImageName: "not-there",
},
},
},
}

// Test
err = builder.deleteExistingOutputImage()

// Verify
require.NoError(t, err)
}

func TestDeleteExistingImage(t *testing.T) {
// Setup
tmpDir, err := os.MkdirTemp("", "eib-")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

builder := Builder{
context: &image.Context{
ImageConfigDir: tmpDir,
ImageDefinition: &image.Definition{
Image: image.Image{
OutputImageName: "not-there",
},
},
},
}

_, err = os.Create(builder.generateOutputImageFilename())
require.NoError(t, err)

// Test
err = builder.deleteExistingOutputImage()

// Verify
require.NoError(t, err)

_, err = os.Stat(builder.generateOutputImageFilename())
require.Error(t, err)
require.True(t, os.IsNotExist(err))
}
11 changes: 1 addition & 10 deletions pkg/build/iso.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var extractIsoTemplate string
var rebuildIsoTemplate string

func (b *Builder) buildIsoImage() error {
if err := b.deleteExistingOutputIso(); err != nil {
if err := b.deleteExistingOutputImage(); err != nil {
return fmt.Errorf("deleting existing ISO image: %w", err)
}

Expand Down Expand Up @@ -96,15 +96,6 @@ func (b *Builder) rebuildIso() error {
return nil
}

func (b *Builder) deleteExistingOutputIso() error {
outputFilename := b.generateOutputImageFilename()
err := os.Remove(outputFilename)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("error deleting file %s: %w", outputFilename, err)
}
return nil
}

func (b *Builder) writeIsoScript(templateContents, outputFilename string) error {
scriptName := filepath.Join(b.context.BuildDir, outputFilename)
isoExtractPath := filepath.Join(b.context.BuildDir, isoExtractDir)
Expand Down
55 changes: 0 additions & 55 deletions pkg/build/iso_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,61 +38,6 @@ func setupContext(t *testing.T) (ctx *image.Context, teardown func()) {
}
}

func TestDeleteNoExistingImage(t *testing.T) {
// Setup
tmpDir, err := os.MkdirTemp("", "eib-")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

builder := Builder{
context: &image.Context{
ImageConfigDir: tmpDir,
ImageDefinition: &image.Definition{
Image: image.Image{
OutputImageName: "not-there",
},
},
},
}

// Test
err = builder.deleteExistingOutputIso()

// Verify
require.NoError(t, err)
}

func TestDeleteExistingImage(t *testing.T) {
// Setup
tmpDir, err := os.MkdirTemp("", "eib-")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

builder := Builder{
context: &image.Context{
ImageConfigDir: tmpDir,
ImageDefinition: &image.Definition{
Image: image.Image{
OutputImageName: "not-there",
},
},
},
}

_, err = os.Create(builder.generateOutputImageFilename())
require.NoError(t, err)

// Test
err = builder.deleteExistingOutputIso()

// Verify
require.NoError(t, err)

_, err = os.Stat(builder.generateOutputImageFilename())
require.Error(t, err)
require.True(t, os.IsNotExist(err))
}

func TestWriteIsoScript_Extract(t *testing.T) {
// Setup
ctx, teardown := setupContext(t)
Expand Down
4 changes: 4 additions & 0 deletions pkg/build/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const (
var modifyRawImageTemplate string

func (b *Builder) buildRawImage() error {
if err := b.deleteExistingOutputImage(); err != nil {
return fmt.Errorf("deleting existing RAW image: %w", err)
}

cmd := b.createRawImageCopyCommand()
if err := cmd.Run(); err != nil {
return fmt.Errorf("copying the base image %s to the output image location %s: %w",
Expand Down
Loading