diff --git a/lib/core.sh b/lib/core.sh index c1280f6..8d5acf0 100644 --- a/lib/core.sh +++ b/lib/core.sh @@ -83,10 +83,8 @@ PROOT_LINK=http://static.proot.me/proot-${ARCH} SH="/bin/sh --login" CHROOT=${JUJU_HOME}/usr/bin/arch-chroot -TRUE=${JUJU_HOME}/usr/bin/true ID="${JUJU_HOME}/usr/bin/id -u" - ################################# MAIN FUNCTIONS ############################## function is_juju_installed(){ @@ -174,33 +172,27 @@ function run_juju_as_root(){ JUJU_ENV=1 ${CHROOT} $JUJU_HOME /usr/bin/bash -c "mkdir -p /run/lock && $(_define_chroot_args "$@")" } +function _run_proot(){ + if ! JUJU_ENV=1 ${@} + then + info "Trying execute proot with PROOT_NO_SECCOMP=1 " + JUJU_ENV=1 PROOT_NO_SECCOMP=1 ${@} + fi +} + function _run_juju_with_proot(){ - local proot_bin=${PROOT} - if ! ${proot_bin} ${TRUE} &> /dev/null - then - if PROOT_NO_SECCOMP=1 ${proot_bin} ${TRUE} &> /dev/null - then - export PROOT_NO_SECCOMP=1 - else - proot_bin=${PROOT_COMPAT} - if PROOT_NO_SECCOMP=1 ${proot_bin} ${TRUE} &> /dev/null - then - export PROOT_NO_SECCOMP=1 - else - die "Proot cannot be executed." - fi - fi - fi - - [ "$(${proot_bin} ${ID} 2> /dev/null )" == "0" ] && \ + [ "$(${ID} 2> /dev/null )" == "0" ] && \ die "You cannot access with root privileges. Use --root option instead." - JUJU_ENV=1 ${proot_bin} $@ - local ret=$? - export -n PROOT_NO_SECCOMP - - return $ret + if ! _run_proot ${PROOT} ${@} + then + info "Trying to execute proot compat binary" + if ! _run_proot ${PROOT_COMPAT} ${@} + then + die "Proot cannot be executed: Try to use juju -p \"-k 3.10\"" + fi + fi } diff --git a/tests/test_core.sh b/tests/test_core.sh index afae1a6..d433933 100755 --- a/tests/test_core.sh +++ b/tests/test_core.sh @@ -108,8 +108,6 @@ function test_run_juju_as_user_proot_args(){ install_mini_juju run_juju_as_user "--help" "" 1> /dev/null is_equal $? 0 || return 1 - run_juju_as_user "--helps" "" &> /dev/null - is_equal $? 1 || return 1 mkdir $JUJU_TEMPDIR/newdir touch $JUJU_TEMPDIR/newdir/newfile @@ -117,24 +115,57 @@ function test_run_juju_as_user_proot_args(){ is_equal $? 0 || return 1 export -f _run_juju_with_proot + export -f _run_proot + export -f info export PROOT - export TRUE + export PROOT_COMPAT + ID="/usr/bin/echo 1" bash -ic "_run_juju_with_proot --helps" &> /dev/null + is_equal $? 1 || return 1 + export -n _run_juju_with_proot + export -n _run_proot + export -n info + export -n PROOT + export -n PROOT_COMPAT +} + +function test_run_juju_with_proot_with_compat(){ + install_mini_juju + PROOT="/usr/bin/true" + PROOT_COMPAT="/usr/bin/false" + _run_juju_with_proot "" "" 1> /dev/null + is_equal $? 0 || return 1 + + PROOT="/usr/bin/false" + PROOT_COMPAT="/usr/bin/true" + _run_juju_with_proot "" "" 1> /dev/null + is_equal $? 0 || return 1 + + export -f _run_juju_with_proot + export -f _run_proot + export -f info + PROOT="/usr/bin/false" PROOT_COMPAT="/usr/bin/false" ID="/usr/bin/echo 1" bash -ic "_run_juju_with_proot --helps" &> /dev/null + is_equal $? 1 || return 1 + export -n _run_juju_with_proot + export -n _run_proot + export -n info +} + +function test_run_juju_with_proot_as_root(){ + export -f _run_juju_with_proot ID="/usr/bin/echo 0" bash -ic "_run_juju_with_proot" &> /dev/null is_equal $? 1 || return 1 export -n _run_juju_with_proot unset _run_juju_with_proot - export -n PROOT - export -n TRUE } -function test_run_juju_as_user_seccomp(){ - install_mini_juju - PROOT="" - local output=$(_run_juju_with_proot "" "env" | grep "PROOT_NO_SECCOMP") +function test_run_proot_seccomp(){ + local output=$(_run_proot "env" | grep "^PROOT_NO_SECCOMP") is_equal $output "" || return 1 - TRUE="/usr/bin/false" - local output=$(_run_juju_with_proot "" "env" | grep "PROOT_NO_SECCOMP") + envv(){ + env | grep "^PROOT_NO_SECCOMP" + } + local output=$(_run_proot "envv" | grep "^PROOT_NO_SECCOMP") is_equal $output "PROOT_NO_SECCOMP=1" || return 1 } @@ -160,7 +191,6 @@ function test_nested_juju(){ is_equal $? 1 || return 1 } - for func in $(declare -F | grep test_ | awk '{print $3}' | xargs) do set_up