diff --git a/pkg/installer/finalizer.go b/pkg/installer/finalizer.go index d7bb00c5..c0fc89fb 100644 --- a/pkg/installer/finalizer.go +++ b/pkg/installer/finalizer.go @@ -26,23 +26,37 @@ import ( ) type LuetFinalizer struct { + Shell []string `json:"shell"` Install []string `json:"install"` Uninstall []string `json:"uninstall"` // TODO: Where to store? } func (f *LuetFinalizer) RunInstall(s *System) error { - for _, c := range f.Install { - if s.Target == "/" { + var cmd string + var args []string + if len(f.Shell) == 0 { + // Default to sh otherwise + cmd = "sh" + args = []string{"-c"} + } else { + cmd = f.Shell[0] + if len(f.Shell) > 1 { + args = f.Shell[1:] + } + } - Info("finalizer on / :", "sh", "-c", c) - cmd := exec.Command("sh", "-c", c) + for _, c := range f.Install { + toRun := append(args, c) + Info("Executing finalizer on ", s.Target, cmd, toRun) + if s.Target == "/" { + cmd := exec.Command(cmd, toRun...) stdoutStderr, err := cmd.CombinedOutput() if err != nil { return errors.Wrap(err, "Failed running command: "+string(stdoutStderr)) } Info(string(stdoutStderr)) } else { - b := box.NewBox("sh", []string{"-c", c}, s.Target, false, true, true) + b := box.NewBox(cmd, toRun, s.Target, false, true, true) err := b.Run() if err != nil { return errors.Wrap(err, "Failed running command ") diff --git a/tests/fixtures/finalizer_custom/build.yaml b/tests/fixtures/finalizer_custom/build.yaml new file mode 100644 index 00000000..9258e4cf --- /dev/null +++ b/tests/fixtures/finalizer_custom/build.yaml @@ -0,0 +1,4 @@ +image: "alpine" +unpack: true +steps: +- apk update && apk add bash \ No newline at end of file diff --git a/tests/fixtures/finalizer_custom/definition.yaml b/tests/fixtures/finalizer_custom/definition.yaml new file mode 100644 index 00000000..88a562e4 --- /dev/null +++ b/tests/fixtures/finalizer_custom/definition.yaml @@ -0,0 +1,3 @@ +category: "seed" +name: "alpine" +version: "2.0" diff --git a/tests/fixtures/finalizer_custom/finalize.yaml b/tests/fixtures/finalizer_custom/finalize.yaml new file mode 100644 index 00000000..4ae70d73 --- /dev/null +++ b/tests/fixtures/finalizer_custom/finalize.yaml @@ -0,0 +1,5 @@ +shell: +- bash +- -c +install: +- echo "$BASH" > /tmp/foo \ No newline at end of file diff --git a/tests/fixtures/finalizers/alpine/finalize.yaml b/tests/fixtures/finalizers/alpine/finalize.yaml index 62824e2f..2e46c23e 100644 --- a/tests/fixtures/finalizers/alpine/finalize.yaml +++ b/tests/fixtures/finalizers/alpine/finalize.yaml @@ -1,2 +1,2 @@ install: -- touch /tmp/foo \ No newline at end of file +- echo "$0" > /tmp/foo \ No newline at end of file diff --git a/tests/integration/07_finalizer.sh b/tests/integration/07_finalizer.sh index 858b66ea..7dbfcf34 100755 --- a/tests/integration/07_finalizer.sh +++ b/tests/integration/07_finalizer.sh @@ -61,6 +61,7 @@ testInstall() { assertEquals 'install test successfully' "$installst" "0" assertTrue 'package installed' "[ -e '$tmpdir/testrootfs/bin/busybox' ]" assertTrue 'finalizer runs' "[ -e '$tmpdir/testrootfs/tmp/foo' ]" + assertEquals 'finalizer printed used shell' "$(cat $tmpdir/testrootfs/tmp/foo)" 'sh' } diff --git a/tests/integration/07_finalizer_custom.sh b/tests/integration/07_finalizer_custom.sh new file mode 100755 index 00000000..4bb9b3af --- /dev/null +++ b/tests/integration/07_finalizer_custom.sh @@ -0,0 +1,76 @@ +#!/bin/bash + +export LUET_NOLOCK=true + +oneTimeSetUp() { +export tmpdir="$(mktemp -d)" +} + +oneTimeTearDown() { + rm -rf "$tmpdir" +} + +testBuild() { + mkdir $tmpdir/testbuild + luet build --tree "$ROOT_DIR/tests/fixtures/finalizer_custom" --destination $tmpdir/testbuild --compression gzip --all > /dev/null + buildst=$? + assertEquals 'builds successfully' "$buildst" "0" + assertTrue 'create package' "[ -e '$tmpdir/testbuild/alpine-seed-2.0.package.tar.gz' ]" +} + +testRepo() { + assertTrue 'no repository' "[ ! -e '$tmpdir/testbuild/repository.yaml' ]" + luet create-repo --tree "$ROOT_DIR/tests/fixtures/finalizer_custom" \ + --output $tmpdir/testbuild \ + --packages $tmpdir/testbuild \ + --name "test" \ + --descr "Test Repo" \ + --urls $tmpdir/testrootfs \ + --type disk > /dev/null + + createst=$? + assertEquals 'create repo successfully' "$createst" "0" + assertTrue 'create repository' "[ -e '$tmpdir/testbuild/repository.yaml' ]" +} + +testConfig() { + mkdir $tmpdir/testrootfs + cat < $tmpdir/luet.yaml +general: + debug: true +system: + rootfs: $tmpdir/testrootfs + database_path: "/" + database_engine: "boltdb" +repositories: + - name: "main" + type: "disk" + enable: true + urls: + - "$tmpdir/testbuild" +EOF + luet config --config $tmpdir/luet.yaml + res=$? + assertEquals 'config test successfully' "$res" "0" +} + +testInstall() { + luet install --config $tmpdir/luet.yaml seed/alpine + #luet install --config $tmpdir/luet.yaml test/c-1.0 > /dev/null + installst=$? + assertEquals 'install test successfully' "$installst" "0" + assertTrue 'package installed' "[ -e '$tmpdir/testrootfs/bin/busybox' ]" + assertTrue 'finalizer runs' "[ -e '$tmpdir/testrootfs/tmp/foo' ]" + assertContains 'finalizer printed used shell' "$(cat $tmpdir/testrootfs/tmp/foo)" '/bin/bash' +} + + +testCleanup() { + luet cleanup --config $tmpdir/luet.yaml + installst=$? + assertEquals 'install test successfully' "$installst" "0" +} + +# Load shUnit2. +. "$ROOT_DIR/tests/integration/shunit2"/shunit2 +