diff --git a/pkg/combustion/combustion.go b/pkg/combustion/combustion.go index 3bd985a9..3d1fcf61 100644 --- a/pkg/combustion/combustion.go +++ b/pkg/combustion/combustion.go @@ -99,7 +99,12 @@ func Configure(ctx *image.Context) error { combustionScripts = append(combustionScripts, scripts...) } - script, err := assembleScript(combustionScripts) + var networkScript string + if isComponentConfigured(ctx, networkConfigDir) { + networkScript = networkConfigScriptName + } + + script, err := assembleScript(combustionScripts, networkScript) if err != nil { return fmt.Errorf("assembling script: %w", err) } diff --git a/pkg/combustion/script.go b/pkg/combustion/script.go index 39c533ce..f8fe75c7 100644 --- a/pkg/combustion/script.go +++ b/pkg/combustion/script.go @@ -5,15 +5,28 @@ import ( "fmt" "slices" "strings" + + "github.com/suse-edge/edge-image-builder/pkg/template" ) -//go:embed templates/script-base.sh +//go:embed templates/script-base.sh.tpl var combustionScriptBase string -func assembleScript(scripts []string) (string, error) { +func assembleScript(scripts []string, networkScript string) (string, error) { b := new(strings.Builder) - _, err := b.WriteString(combustionScriptBase) + values := struct { + NetworkScript string + }{ + NetworkScript: networkScript, + } + + data, err := template.Parse("combustion-base", combustionScriptBase, values) + if err != nil { + return "", fmt.Errorf("parsing combustion base template: %w", err) + } + + _, err = b.WriteString(data) if err != nil { return "", fmt.Errorf("writing script base: %w", err) } diff --git a/pkg/combustion/script_test.go b/pkg/combustion/script_test.go index ca1c7d54..d084b796 100644 --- a/pkg/combustion/script_test.go +++ b/pkg/combustion/script_test.go @@ -7,10 +7,30 @@ import ( "github.com/stretchr/testify/require" ) -func TestAssembleScript(t *testing.T) { - script, err := assembleScript([]string{"foo.sh", "bar.sh", "baz.sh"}) +func TestAssembleScript_DynamicNetwork(t *testing.T) { + script, err := assembleScript([]string{"foo.sh", "bar.sh", "baz.sh"}, "") require.NoError(t, err) + assert.Contains(t, script, "# combustion: network") + assert.NotContains(t, script, "# combustion: prepare network") + + assert.NotContains(t, script, `if [ "${1-}" = "--prepare" ]; then`) + assert.NotContains(t, script, "./configure-network.sh") + + // alphabetic ordering + assert.Contains(t, script, "./bar.sh\n./baz.sh\n./foo.sh") +} + +func TestAssembleScript_StaticNetwork(t *testing.T) { + script, err := assembleScript([]string{"foo.sh", "bar.sh", "baz.sh"}, "configure-network.sh") + require.NoError(t, err) + + assert.Contains(t, script, "# combustion: prepare network") + assert.NotContains(t, script, "# combustion: network") + + assert.Contains(t, script, `if [ "${1-}" = "--prepare" ]; then`) + assert.Contains(t, script, "./configure-network.sh") + // alphabetic ordering assert.Contains(t, script, "./bar.sh\n./baz.sh\n./foo.sh") } diff --git a/pkg/combustion/templates/script-base.sh b/pkg/combustion/templates/script-base.sh.tpl similarity index 52% rename from pkg/combustion/templates/script-base.sh rename to pkg/combustion/templates/script-base.sh.tpl index 4696736c..1cb65e57 100644 --- a/pkg/combustion/templates/script-base.sh +++ b/pkg/combustion/templates/script-base.sh.tpl @@ -1,7 +1,17 @@ #!/bin/bash -# combustion: network set -euo pipefail +{{ if .NetworkScript -}} +# combustion: prepare network + +if [ "${1-}" = "--prepare" ]; then + ./{{ .NetworkScript }} + exit 0 +fi +{{- else -}} +# combustion: network +{{- end }} + # Redirect output to the console exec > >(exec tee -a /dev/tty0) 2>&1