Merge pull request #383 from asteven/type/__group

rewrite __group type
This commit is contained in:
Nico Schottelius 2015-02-10 22:52:02 +01:00
commit 86e81b19ac
8 changed files with 95 additions and 93 deletions

View file

@ -1,2 +0,0 @@
- delete groups

View file

@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
# #
# 2011 Steven Armstrong (steven-cdist at armstrong.cc) # 2011-2015 Steven Armstrong (steven-cdist at armstrong.cc)
# #
# This file is part of cdist. # This file is part of cdist.
# #

View file

@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
# #
# 2011 Steven Armstrong (steven-cdist at armstrong.cc) # 2011-2015 Steven Armstrong (steven-cdist at armstrong.cc)
# #
# This file is part of cdist. # This file is part of cdist.
# #
@ -22,7 +22,6 @@
# #
name=$__object_id name=$__object_id
os_version="$($__explorer/os_version)"
os="$($__explorer/os)" os="$($__explorer/os)"
if [ "$os" = "freebsd" ]; then if [ "$os" = "freebsd" ]; then
@ -30,13 +29,4 @@ if [ "$os" = "freebsd" ]; then
exit 0 exit 0
fi fi
case "$os_version" in
"Red Hat Enterprise Linux Server release "[45]*|"CentOS release "[45]*)
# TODO: find a way to get this information
echo "$os_version does not have getent gshadow"
;;
*)
getent gshadow "$name" || true getent gshadow "$name" || true
;;
esac

View file

@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
# #
# 2011 Steven Armstrong (steven-cdist at armstrong.cc) # 2011-2015 Steven Armstrong (steven-cdist at armstrong.cc)
# 2011 Nico Schottelius (nico-cdist at schottelius.org) # 2011 Nico Schottelius (nico-cdist at schottelius.org)
# #
# This file is part of cdist. # This file is part of cdist.
@ -23,91 +23,86 @@
# #
name="$__object_id" name="$__object_id"
os_version="$(cat "$__global/explorer/os_version")"
os="$(cat "$__global/explorer/os")" os="$(cat "$__global/explorer/os")"
state="$(cat "$__object/parameter/state")"
cd "$__object/parameter"
# Use short option names for portability
shorten_property() {
case "$1" in
gid) echo "-g";;
password) echo "-p";;
system) echo "-r";;
esac
}
if [ "$state" = "present" ]; then
case "$os" in
freebsd)
supported_add_properties="gid"
supported_change_properties="gid"
;;
*)
supported_add_properties="gid password system"
supported_change_properties="gid password"
;;
esac
if grep -q "^${name}:" "$__object/explorer/group"; then if grep -q "^${name}:" "$__object/explorer/group"; then
for property in $(ls .); do # change existing
new_value="$(cat "$property")" for property in $supported_change_properties; do
# argument to pass the groupmod command for this property (exceptions if [ -f "$__object/parameter/$property" ]; then
# are made in the case statement below) new_value="$(cat "$__object/parameter/$property")"
proparg="--$property" unset current_value
case "$property" in case "$property" in
password) password)
if [ "$os" = "freebsd" ]; then current_value="$(awk -F: '{ print $2 }' "$__object/explorer/gshadow")"
echo "group/$name: FreeBSD doesn't support password modification" >&2
exit 1
fi
case "$os_version" in
"Red Hat Enterprise Linux Server release "[45]*|"CentOS release "[45]*)
# TODO: Use gpasswd? Need to fix gshadow explorer first.
echo "group/$name: '$os_version' groupmod does not support password modification" >&2
exit 1
;;
esac
current_value="$(awk -F: '{ print $2 }' < "$__object/explorer/gshadow")"
;; ;;
gid) gid)
# set to -g to support older redhat/centos current_value="$(awk -F: '{ print $3 }' "$__object/explorer/group")"
proparg="-g"
current_value="$(awk -F: '{ print $3 }' < "$__object/explorer/group")"
;; ;;
esac esac
if [ "$new_value" != "$current_value" ]; then if [ "$new_value" != "$current_value" ]; then
set -- "$@" "$proparg" \"$new_value\" set -- "$@" "$(shorten_property $property)" \'$new_value\'
echo change $property $new_value $current_value >> "$__messages_out" echo change $property $new_value $current_value >> "$__messages_out"
fi fi
fi
done done
if [ $# -gt 0 ]; then if [ $# -gt 0 ]; then
echo mod >> "$__messages_out"
case $os in
freebsd)
echo pw group mod "$@" "$name"
;;
*)
echo groupmod "$@" "$name"
;;
esac
fi
else
echo add >> "$__messages_out"
for property in $(ls .); do
new_value="$(cat "$property")"
if [ "$os" = "freebsd" ]; then if [ "$os" = "freebsd" ]; then
case $property in echo pw groupmod "$@" "$name"
gid)
proparg="-g"
;;
password)
echo "group/$name: FreeBSD doesn't support password setting" >&2
exit 1
;;
*)
# The type has been updated to support more properties than it knows how to handle for FreeBSD
# tell the user about this.
echo "Currently unknown property: $property" >&2
exit 1
;;
esac
else else
proparg="--$property" echo groupmod "$@" "$name"
fi
echo mod >> "$__messages_out"
fi
else
# create new
for property in $supported_change_properties; do
if [ -f "$__object/parameter/$property" ]; then
new_value="$(cat "$__object/parameter/$property")"
if [ -z "$new_value" ]; then
# Boolean parameters have no value
set -- "$@" "$(shorten_property $property)"
else
set -- "$@" "$(shorten_property $property)" \'$new_value\'
fi
fi fi
set -- "$@" "$proparg" \"$new_value\"
echo set $property $new_value >> "$__messages_out"
done done
if [ "$os" = "freebsd" ]; then
case $os in
freebsd)
echo pw groupadd "$@" "$name" echo pw groupadd "$@" "$name"
;; else
*)
echo groupadd "$@" "$name" echo groupadd "$@" "$name"
;;
esac
fi fi
fi
else
# delete existing
if grep -q "^${name}:" "$__object/explorer/group"; then
if [ "$os" = "freebsd" ]; then
echo pw groupdel "$name"
else
echo groupdel "$name"
fi
echo remove >> "$__messages_out"
fi
fi

View file

@ -20,18 +20,28 @@ None.
OPTIONAL PARAMETERS OPTIONAL PARAMETERS
------------------- -------------------
state::
absent or present, defaults to present
gid:: gid::
see groupmod(8) see groupmod(8)
password:: password::
see above see above
BOOLEAN PARAMETERS
------------------
system::
see groupadd(8), apply only on group creation
MESSAGES MESSAGES
-------- --------
mod:: mod::
group is modified group is modified
add:: add::
New group added New group added
remove::
group is removed
change <property> <new_value> <current_value>:: change <property> <new_value> <current_value>::
Changed group property from current_value to new_value Changed group property from current_value to new_value
set <property> <new_value>:: set <property> <new_value>::
@ -45,6 +55,12 @@ EXAMPLES
# Create a group 'foobar' with operating system default settings # Create a group 'foobar' with operating system default settings
__group foobar __group foobar
# Remove the 'foobar' group
__group foobar --state absent
# Create a system group 'myservice' with operating system default settings
__group myservice --system
# Same but with a specific gid # Same but with a specific gid
__group foobar --gid 1234 __group foobar --gid 1234
@ -60,5 +76,5 @@ SEE ALSO
COPYING COPYING
------- -------
Copyright \(C) 2011 Steven Armstrong. Free use of this software is Copyright \(C) 2011-2015 Steven Armstrong. Free use of this software is
granted under the terms of the GNU General Public License version 3 (GPLv3). granted under the terms of the GNU General Public License version 3 (GPLv3).

View file

@ -0,0 +1 @@
system

View file

@ -0,0 +1 @@
present

View file

@ -1,2 +1,3 @@
gid gid
password password
state