Skip to content

Commit

Permalink
default --isolation to "user" when running in docker
Browse files Browse the repository at this point in the history
  • Loading branch information
romkatv committed Nov 13, 2021
1 parent c4b6384 commit 5d4ae05
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
24 changes: 20 additions & 4 deletions dbg/playground
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ local -r root_dir=${ZSH_SCRIPT:a:h:h}

zmodload zsh/zutil zsh/system zsh/zselect

local -a help privileged isolation
local -a help privileged

if [[ -r /proc/1/cpuset(#qN-.) &&
"$(</proc/1/cpuset)" == /docker/[[:xdigit:]](#c64) ]]; then
local -a isolation=(--isolation user)
local -ri in_docker=1
else
local -a isolation=(--isolation docker)
local -ri in_docker=0
fi

zparseopts -D -K -F -- {h,-help}=help {p,-privileged}=privileged {I,-isolation}:=isolation

if (( $#help )); then
Expand All @@ -28,7 +38,7 @@ if (( $#help )); then
return
fi

if [[ $#isolation -ne 0 && $isolation[2] != (docker|user) ]]; then
if [[ $isolation[2] != (docker|user) ]]; then
print -ru2 -- "${ZSH_SCRIPT:t}: invalid value of --isolation: ${(q-)isolation[2]}"
return 1
fi
Expand Down Expand Up @@ -57,13 +67,17 @@ if [[ ! -t 0 || ! -t 1 || ! -t 2 ]]; then
return 1
fi

if [[ ${isolation[2]-} == user ]]; then
if [[ $isolation == user ]]; then
if (( ! ARGC )); then
print -ru2 -- "${ZSH_SCRIPT:t}: exactly one positional argument is required"
return 1
fi
if (( EUID )); then
print -ru2 -- "${ZSH_SCRIPT:t}: you must be root for --isolation user; try with sudo"
if (( in_docker )); then
print -ru2 -- "${ZSH_SCRIPT:t}: you must be root for --isolation user"
else
print -ru2 -- "${ZSH_SCRIPT:t}: you must be root for --isolation user; try with sudo"
fi
return 1
fi
local -a create_repo
Expand All @@ -72,6 +86,8 @@ if [[ ${isolation[2]-} == user ]]; then
return
fi

[[ $isolation == docker ]]

() {
local cmd
for cmd in docker tar; do
Expand Down
20 changes: 18 additions & 2 deletions zsh-bench
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ local -a standalone
local -a isolation
local -a iters=(--iters 16)
local -a git=(--git yes)

zparseopts -D -K -F -- \
{h,-help}=help \
{s,-standalone}=standalone \
Expand Down Expand Up @@ -66,6 +67,17 @@ if [[ $#scratch_dir -ne 0 && -z $scratch_dir[2] ]]; then
fi

if (( ARGC )); then
if (( ! $#isolation )); then
if [[ -r /proc/1/cpuset(#qN-.) &&
"$(</proc/1/cpuset)" == /docker/[[:xdigit:]](#c64) ]]; then
local -a isolation=(--isolation user)
local -ri in_docker=1
else
local -a isolation=(--isolation docker)
local -ri in_docker=0
fi
fi

if (( $#scratch_dir )); then
print -ru2 -- "${ZSH_SCRIPT:t}: ${scratch_dir[1]} is incompatible with positional arguments"
return 1
Expand All @@ -81,7 +93,7 @@ if (( ARGC )); then
done
} "$@"

case ${isolation[2]-docker} in
case $isolation[2] in
docker)
() {
local cmd
Expand Down Expand Up @@ -151,7 +163,11 @@ if (( ARGC )); then

user)
if (( EUID )); then
print -ru2 -- "${ZSH_SCRIPT:t}: you must be root for --isolation user; try with sudo"
if (( in_docker )); then
print -ru2 -- "${ZSH_SCRIPT:t}: you must be root for --isolation user"
else
print -ru2 -- "${ZSH_SCRIPT:t}: you must be root for --isolation user; try with sudo"
fi
return 1
fi
$self_dir/internal/benchmark-many $iters $git $standalone -- $@
Expand Down

0 comments on commit 5d4ae05

Please sign in to comment.