mirror of
https://github.com/mudler/luet.git
synced 2025-08-10 11:42:15 +00:00
Allow to finalizer to specify entrypoint command
In this way, finalizer in strict environment can override the default shell used to run commands. The shell keyword is a list, as it needs to contain the full command + args.
This commit is contained in:
parent
333b9cc023
commit
ee0fe1a86a
@ -26,23 +26,37 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type LuetFinalizer struct {
|
type LuetFinalizer struct {
|
||||||
|
Shell []string `json:"shell"`
|
||||||
Install []string `json:"install"`
|
Install []string `json:"install"`
|
||||||
Uninstall []string `json:"uninstall"` // TODO: Where to store?
|
Uninstall []string `json:"uninstall"` // TODO: Where to store?
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *LuetFinalizer) RunInstall(s *System) error {
|
func (f *LuetFinalizer) RunInstall(s *System) error {
|
||||||
for _, c := range f.Install {
|
var cmd string
|
||||||
if s.Target == "/" {
|
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)
|
for _, c := range f.Install {
|
||||||
cmd := exec.Command("sh", "-c", c)
|
toRun := append(args, c)
|
||||||
|
Info("Executing finalizer on ", s.Target, cmd, toRun)
|
||||||
|
if s.Target == "/" {
|
||||||
|
cmd := exec.Command(cmd, toRun...)
|
||||||
stdoutStderr, err := cmd.CombinedOutput()
|
stdoutStderr, err := cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "Failed running command: "+string(stdoutStderr))
|
return errors.Wrap(err, "Failed running command: "+string(stdoutStderr))
|
||||||
}
|
}
|
||||||
Info(string(stdoutStderr))
|
Info(string(stdoutStderr))
|
||||||
} else {
|
} 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()
|
err := b.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "Failed running command ")
|
return errors.Wrap(err, "Failed running command ")
|
||||||
|
4
tests/fixtures/finalizer_custom/build.yaml
vendored
Normal file
4
tests/fixtures/finalizer_custom/build.yaml
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
image: "alpine"
|
||||||
|
unpack: true
|
||||||
|
steps:
|
||||||
|
- apk update && apk add bash
|
3
tests/fixtures/finalizer_custom/definition.yaml
vendored
Normal file
3
tests/fixtures/finalizer_custom/definition.yaml
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
category: "seed"
|
||||||
|
name: "alpine"
|
||||||
|
version: "2.0"
|
5
tests/fixtures/finalizer_custom/finalize.yaml
vendored
Normal file
5
tests/fixtures/finalizer_custom/finalize.yaml
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
shell:
|
||||||
|
- bash
|
||||||
|
- -c
|
||||||
|
install:
|
||||||
|
- echo "$BASH" > /tmp/foo
|
@ -1,2 +1,2 @@
|
|||||||
install:
|
install:
|
||||||
- touch /tmp/foo
|
- echo "$0" > /tmp/foo
|
@ -61,6 +61,7 @@ testInstall() {
|
|||||||
assertEquals 'install test successfully' "$installst" "0"
|
assertEquals 'install test successfully' "$installst" "0"
|
||||||
assertTrue 'package installed' "[ -e '$tmpdir/testrootfs/bin/busybox' ]"
|
assertTrue 'package installed' "[ -e '$tmpdir/testrootfs/bin/busybox' ]"
|
||||||
assertTrue 'finalizer runs' "[ -e '$tmpdir/testrootfs/tmp/foo' ]"
|
assertTrue 'finalizer runs' "[ -e '$tmpdir/testrootfs/tmp/foo' ]"
|
||||||
|
assertEquals 'finalizer printed used shell' "$(cat $tmpdir/testrootfs/tmp/foo)" 'sh'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
76
tests/integration/07_finalizer_custom.sh
Executable file
76
tests/integration/07_finalizer_custom.sh
Executable file
@ -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 <<EOF > $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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user