Add kickstart.user functions

This commit is contained in:
Bruno Tavares 2014-02-22 16:48:09 -03:00
parent 7ca421e645
commit d4ad417027
5 changed files with 54 additions and 1 deletions

View File

@ -0,0 +1,14 @@
# kickstart.print_with_separator separator [arguments...]
Print the list of `arguments` separated by `separator`
### Example
```bash
$ kickstart.print_with_separator , a b c
a,b,c
$ kickstart.print_with_separator '|' a b c
a|b|c
```

View File

@ -0,0 +1,12 @@
# kickstart.user.is_on_group user group
Return 0 if `user` is on `group`, and 1 if not.
### Example
```bash
$ kickstart.user.is_on_group vagrant vagrant && echo yup
yup
$ kickstart.user.is_on_group vagrant no_group || echo nope
nope
```

View File

@ -0,0 +1,8 @@
# kickstart.user.remove_group user group
Check if `user` is part of `group` and remove it from `group`
### Example
```bash
$ kickstart.user.remove_group vagrant admin
```

View File

@ -31,6 +31,14 @@ kickstart.command_exists() {
which $1 >/dev/null 2>&1
}
kickstart.print_with_separator() {
(
IFS=$1
shift
echo -e "$*"
)
}
for recipe in recipes/kickstart/*; do
source $recipe
done

View File

@ -2,7 +2,18 @@ kickstart.user.create() {
kickstart.mute "id $1" || ( useradd -m -s /bin/bash -U -p `openssl passwd -1 $2` $1 )
}
kickstart.user.is_on_group() {
grep -q $2 <(id -nG $1)
}
kickstart.user.add_group() {
kickstart.info "Adding $2 group to $1"
grep -q $2 <(groups $1) || usermod -a -G $2 $1
kickstart.user.is_on_group $1 $2 || usermod -a -G $2 $1
}
kickstart.user.remove_group() {
kickstart.info "Remove $1 from group $2"
kickstart.user.is_on_group $1 $2 && \
IFS=" " read -a groups < <(id -nG $1 | sed "s/$2//") && \
usermod -G `kickstart.print_with_separator , ${groups[*]}` $1
}