From d4ad41702789c709fbf4ec260e028162438231d2 Mon Sep 17 00:00:00 2001 From: Bruno Tavares Date: Sat, 22 Feb 2014 16:48:09 -0300 Subject: [PATCH] Add kickstart.user functions --- docs/kickstart/print_with_separator.md | 14 ++++++++++++++ docs/kickstart/user/is_on_group.md | 12 ++++++++++++ docs/kickstart/user/remove_group.md | 8 ++++++++ kickstart/recipes/kickstart.sh | 8 ++++++++ kickstart/recipes/kickstart/user.sh | 13 ++++++++++++- 5 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 docs/kickstart/print_with_separator.md create mode 100644 docs/kickstart/user/is_on_group.md create mode 100644 docs/kickstart/user/remove_group.md diff --git a/docs/kickstart/print_with_separator.md b/docs/kickstart/print_with_separator.md new file mode 100644 index 0000000..093f95a --- /dev/null +++ b/docs/kickstart/print_with_separator.md @@ -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 +``` diff --git a/docs/kickstart/user/is_on_group.md b/docs/kickstart/user/is_on_group.md new file mode 100644 index 0000000..1a2c184 --- /dev/null +++ b/docs/kickstart/user/is_on_group.md @@ -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 +``` diff --git a/docs/kickstart/user/remove_group.md b/docs/kickstart/user/remove_group.md new file mode 100644 index 0000000..260acb6 --- /dev/null +++ b/docs/kickstart/user/remove_group.md @@ -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 +``` diff --git a/kickstart/recipes/kickstart.sh b/kickstart/recipes/kickstart.sh index 2fc089d..a27fa81 100644 --- a/kickstart/recipes/kickstart.sh +++ b/kickstart/recipes/kickstart.sh @@ -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 diff --git a/kickstart/recipes/kickstart/user.sh b/kickstart/recipes/kickstart/user.sh index 1bb95fe..d6564e7 100644 --- a/kickstart/recipes/kickstart/user.sh +++ b/kickstart/recipes/kickstart/user.sh @@ -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 }