Skip to content

Commit

Permalink
Add network templating to combustion base script
Browse files Browse the repository at this point in the history
Signed-off-by: Atanas Dinov <[email protected]>
  • Loading branch information
atanasdinov committed Jan 18, 2024
1 parent 7cfe0c9 commit 8fee98c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
7 changes: 6 additions & 1 deletion pkg/combustion/combustion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
19 changes: 16 additions & 3 deletions pkg/combustion/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
24 changes: 22 additions & 2 deletions pkg/combustion/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 8fee98c

Please sign in to comment.