Introduce kickstart.user.exec

This commit is contained in:
Bruno Tavares 2014-03-03 11:10:03 -03:00
parent 8216d790fa
commit 5f420e6f24
2 changed files with 31 additions and 0 deletions

View File

@ -0,0 +1,18 @@
# kickstart.user.exec user command
Execute `command` as `user`.
It makes kickstart functions available to be used.
To use variables, like `~`, quote it, otherwise it will be resolved by the current user.
### Example
```bash
$ kickstart.user.exec vagrant whoami
vagrant
$ kickstart.user.exec vagrant kickstart.os
Ubuntu
$ kickstart.user.exec vagrant 'cd ~; pwd'
/home/vagrant
```

View File

@ -17,3 +17,16 @@ kickstart.user.remove_group() {
IFS=" " read -a groups < <(id -nG $1 | sed "s/$2//") && \
usermod -G `kickstart.print_with_separator , ${groups[*]}` $1
}
kickstart.user.exec() {
local user=$1
shift
local command=$(cat <<COMMAND
eval "$(kickstart infect)"
$@
COMMAND
)
kickstart.info "Running \"$@\" as $user"
su $user - bash -c "$command"
}