Skip to content

Commit

Permalink
Combustion Cleanup (#514)
Browse files Browse the repository at this point in the history
* cleanup combustion
  • Loading branch information
dbw7 authored Aug 1, 2024
1 parent 8b5622d commit 43e03be
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Added ability to build aarch64 images on an aarch64 host machine
* Added caching for container images
* Added built image name output to build command
* Leftover combustion artifacts are now removed on first boot

## API

Expand Down
37 changes: 37 additions & 0 deletions pkg/combustion/cleanup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package combustion

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

"github.com/suse-edge/edge-image-builder/pkg/fileio"
"github.com/suse-edge/edge-image-builder/pkg/image"
"github.com/suse-edge/edge-image-builder/pkg/log"
"go.uber.org/zap"
)

const (
cleanupScriptName = "cleanup-combustion.sh"
cleanupComponentName = "cleanup"
)

//go:embed templates/cleanup-combustion.sh
var cleanupScript string

func configureCleanup(ctx *image.Context) ([]string, error) {
if ctx.ImageDefinition.Image.ImageType != image.TypeRAW {
log.AuditComponentSkipped(cleanupComponentName)
zap.S().Info("skipping cleanup component, image type is not raw")
return nil, nil
}

cleanupScriptFilename := filepath.Join(ctx.CombustionDir, cleanupScriptName)
if err := os.WriteFile(cleanupScriptFilename, []byte(cleanupScript), fileio.ExecutablePerms); err != nil {
return nil, fmt.Errorf("writing cleanup files script %s: %w", cleanupScriptName, err)
}

log.AuditComponentSuccessful(cleanupComponentName)
return []string{cleanupScriptName}, nil
}
52 changes: 52 additions & 0 deletions pkg/combustion/cleanup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package combustion

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

"github.com/suse-edge/edge-image-builder/pkg/image"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestConfigureCleanupRaw(t *testing.T) {
// Setup
ctx, teardown := setupContext(t)
defer teardown()
ctx.ImageDefinition.Image.ImageType = image.TypeRAW

// Test
scriptNames, err := configureCleanup(ctx)

// Verify
require.NoError(t, err)

assert.Equal(t, []string{cleanupScriptName}, scriptNames)

// -- Combustion Script
expectedCombustionScript := filepath.Join(ctx.CombustionDir, cleanupScriptName)
contents, err := os.ReadFile(expectedCombustionScript)
require.NoError(t, err)
assert.Contains(t, string(contents), "rm -r /artefacts")
}

func TestConfigureCleanupISO(t *testing.T) {
// Setup
ctx, teardown := setupContext(t)
defer teardown()
ctx.ImageDefinition.Image.ImageType = image.TypeISO

// Test
scriptNames, err := configureCleanup(ctx)

// Verify
require.NoError(t, err)

assert.NotEqual(t, []string{cleanupScriptName}, scriptNames)

// -- Combustion Script
expectedCombustionScript := filepath.Join(ctx.CombustionDir, cleanupScriptName)
assert.NoFileExists(t, expectedCombustionScript)
}
7 changes: 7 additions & 0 deletions pkg/combustion/combustion.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ func (c *Combustion) Configure(ctx *image.Context) error {
combustionScripts = append(combustionScripts, scripts...)
}

// We manually add the cleanup component as to always make sure it is last
s, err := configureCleanup(ctx)
if err != nil {
return fmt.Errorf("configuring cleanup component %q: %w", cleanupComponentName, err)
}
combustionScripts = append(combustionScripts, s...)

var networkScript string
if isComponentConfigured(ctx, networkConfigDir) {
networkScript = networkConfigScriptName
Expand Down
8 changes: 8 additions & 0 deletions pkg/combustion/templates/cleanup-combustion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
set -euo pipefail

rm -r /combustion

if test -d /artefacts; then
rm -r /artefacts
fi

0 comments on commit 43e03be

Please sign in to comment.