Hey list. Here's my current version of a users and groups management
module for Func. Calling it 'management' at this point might be a bit of
an overstatement - at this point, it only provides for informational
queries about the users and groups present on the minion systems, but
methods to allow for creation and modification related tasks are going
to be coming soon. Enjoy. Please feel free to respond with any thoughts
or comments you might have. The file can also be found at:
http://matrix.senecac.on.ca/~gjmasseau/func/0.2/users.py
Gregory Masseau.
<code follows>
# Gregory Masseau <gjmasseau@xxxxxxxxxxxxxxxxxxx>
#
# This software may be freely redistributed under the terms of the GNU
# general public license.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
"""
User management module for func.
"""
import func_module
import sub_process
class UsersModule(func_module.FuncModule):
version = "0.2"
api_version = "0.2"
description = "Just getting started, only has informational query
methods so far."
def exists(self, user):
"""
Checks to see if a given user exists on the target system(s).
"""
import pwd
try:
if pwd.getpwnam(user):
return True
except KeyError:
return False
def exists_uid(self, uid):
"""
Checks to see if a given uid exists on the target system(s).
"""
import pwd
try:
if pwd.getpwuid(int(uid)):
return True
except KeyError:
return False
def list(self):
"""
Lists all users.
"""
import pwd
return tuple([ elem[0] for elem in pwd.getpwall() ])
def list_uid(self):
"""
Lists all UIDs..
"""
import pwd
return tuple([ str(elem[2]) for elem in pwd.getpwall() ])
def info(self, user):
"""
Returns user info (as a tuple) or false for a specified user on the
target system(s).
"""
import pwd
try:
if pwd.getpwname(user):
info = tuple(pwd.getpwname(user))
return info
except KeyError:
return False
def info_gid(self, gid):
"""
Returns user info (as a tuple) or false for a specified user (by
GID) on the target system(s).
"""
import pwd
try:
if pwd.getpwuid(user):
info = tuple(pwd.getpwuid(user))
return info
except KeyError:
return False
def inventory(self):
"""
Returns user info for all users.
"""
import pwd
return tuple([
(elem[0],elem[1],str(elem[2]),str(elem[3]),elem[4],elem[5],elem[6]) for
elem in pwd.getpwall() ])
def username_to_uid(self, user):
"""
Takes a username and converts it to the matching UID.
"""
import pwd
try:
username = str(tuple(pwd.getpwnam(user))[2])
return username
except KeyError:
return False
def uid_to_username(self, uid):
"""
Takes a UID and converts it to the matching username.
"""
import pwd
try:
user = tuple(pwd.getpwuid(int(uid)))[0]
return user
except KeyError:
return False
def grp_exists(self, group):
"""
Checks to see if a given group exists on the target system(s).
"""
import grp
try:
if grp.getgrnam(group):
return True
except KeyError:
return False
def grp_exists_gid(self, gid):
"""
Checks to see if a given gid exists on the target system(s).
"""
import grp
try:
if grp.getgrgid(int(gid)):
return True
except KeyError:
return False
def grp_list(self):
"""
Lists all groups.
"""
import grp
return tuple([ elem[0] for elem in grp.getgrall() ])
def grp_list_gid(self):
"""
Lists all GIDs.
"""
import grp
return tuple([ str(elem[2]) for elem in grp.getgrall() ])
def grp_info(self, group):
"""
Returns group info (as a tuple) or false for a specified group on
the target system(s).
"""
import grp
try:
if grp.getgrnam(group):
info = tuple(grp.getgrnam(group))
return info
except KeyError:
return False
def grp_info_gid(self, gid):
"""
Returns group info (as a tuple) or false for a specified group (by
GID) on the target system(s).
"""
import grp
try:
if grp.getgruid(gid):
info = tuple(grp.getgruid(gid))
return info
except KeyError:
return False
def grp_inventory(self):
"""
Returns group info for all users.
"""
import grp
return tuple([ (elem[0],elem[1],str(elem[2]),str(elem[3])) for elem
in grp.getgrall() ])
def groupname_to_gid(self, group):
"""
Takes a groupname and converts it to the matching gid.
"""
import grp
try:
groupname = str(tuple(grp.getgrnam(group))[2])
return groupname
except KeyError:
return False
def gid_to_groupname(self, gid):
"""
Takes a gid and converts it to the matching groupname.
"""
import grp
try:
group = tuple(grp.getgrgid(int(gid)))[0]
return group
except KeyError:
return False
_______________________________________________
Func-list mailing list
Func-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/func-list