diff options
author | Peter Hatina <phatina@redhat.com> | 2012-09-17 14:57:05 +0200 |
---|---|---|
committer | Peter Hatina <phatina@redhat.com> | 2012-09-17 15:07:25 +0200 |
commit | 1ce8eec0c9ba658db869cbfff2095c481ebd6b64 (patch) | |
tree | 89964cbb24a4c3bd2307563152ccdf073a9d430a | |
parent | 32f093b4db0b2a5337448c4df2b94c2634deaa86 (diff) | |
download | openlmi-providers-1ce8eec0c9ba658db869cbfff2095c481ebd6b64.tar.gz openlmi-providers-1ce8eec0c9ba658db869cbfff2095c481ebd6b64.tar.xz openlmi-providers-1ce8eec0c9ba658db869cbfff2095c481ebd6b64.zip |
cli-tools, cura-user: introduce useradd
-rwxr-xr-x | cli-tools/cura-user.py | 112 | ||||
-rw-r--r-- | cli-tools/cura/cura_client_user.py | 43 |
2 files changed, 150 insertions, 5 deletions
diff --git a/cli-tools/cura-user.py b/cli-tools/cura-user.py index 98e1964..e229fb6 100755 --- a/cli-tools/cura-user.py +++ b/cli-tools/cura-user.py @@ -14,6 +14,7 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, see <http://www.gnu.org/licenses/>. +import os import sys from cura.cura_options import CuraBasicOptions @@ -22,10 +23,56 @@ from cura.cura_client_user import CuraUserClient class CuraUserOptions(CuraBasicOptions): def __init__(self): + script_name = os.path.basename(__file__) super(self.__class__, self).__init__( "Available actions:\n" - " list-users, list-groups, group-members\n") - self.m_parser.set_usage("Usage: %prog [options] action [group]") + " list-users, list-groups, group-members\n" + " useradd\n\n" + "Group-members:\n" + " " + script_name + " [options] group-members groupName\n\n" + "Useradd:\n" + " " + script_name + " [options] useradd newUsername\n\n") + self.m_parser.set_usage("Usage: %prog [options] action") + self.m_parser.add_option("-s", "--shell", + action = "store", + dest = "shell", + help = "login shell of the new account") + self.m_parser.add_option("-r", "--system-account", + action = "store_true", + dest = "system_account", + help = "create a system account") + self.m_parser.add_option("-m", "--create-home", + action = "store_true", + dest = "create_home", + help = "create the user's home directory") + self.m_parser.add_option("-M", "--no-create-home", + action = "store_false", + dest = "create_home", + help = "do not create the user's home directory") + self.m_parser.add_option("-N", "--no-user-group", + action = "store_false", + dest = "create_group", + help = "do not create a group with the same name as the user") + self.m_parser.add_option("-g", "--gid", + action = "store", + dest = "gid", + help = "name or ID of the primary group of the new account") + self.m_parser.add_option("", "--gecos", + action = "store", + dest = "gecos", + help = "GECOS information for new user") + self.m_parser.add_option("-d", "--home-dir", + action = "store", + dest = "home_dir", + help = "home directory of the new account") + self.m_parser.add_option("-n", "--new-password", + action = "store", + dest = "new_password", + help = "password for the new account") + self.m_parser.add_option("-i", "--uid", + action = "store", + dest = "uid", + help = "user ID of the new account") @property def good(self): @@ -33,6 +80,8 @@ class CuraUserOptions(CuraBasicOptions): return False elif self.m_pos_options[0] == "group-members": return len(self.m_pos_options) == 2 + elif self.m_pos_options[0] == "useradd": + return len(self.m_pos_options) == 2 return len(self.m_pos_options) == 1 @property @@ -43,13 +92,52 @@ class CuraUserOptions(CuraBasicOptions): def group(self): return self.m_pos_options[1] if self.good and self.m_pos_options[1] else "" + @property + def newUsername(self): + return self.m_pos_options[1] if self.good and self.m_pos_options[1] else "" + + @property + def shell(self): + return self.m_options.shell if self.good and self.m_options.shell else "" + + @property + def systemAccount(self): + return self.m_options.system_account if self.good and self.m_options.system_account else False + + @property + def createHome(self): + return self.m_options.create_home in (None, True) if self.good else False + + @property + def createGroup(self): + return self.m_options.create_group in (None, True) if self.good else True + + @property + def gid(self): + return self.m_options.gid if self.good and self.m_options.gid else "" + + @property + def gecos(self): + return self.m_options.gecos if self.good and self.m_options.gecos else "" + + @property + def homeDir(self): + return self.m_options.home_dir if self.good and self.m_options.home_dir else "" + + @property + def newPassword(self): + return self.m_options.new_password if self.good and self.m_options.new_password else "" + + @property + def uid(self): + return self.m_options.uid if self.good and self.m_options.uid else "" + if __name__ == "__main__": options = CuraUserOptions() options.parse(sys.argv) if not options.good: sys.stderr.write( - "Wrong tool usage. Run " + __file__ + - " --help for detailed help.\n") + "Wrong tool usage. Run " + __file__ + " --help for detailed help.\n") sys.exit(1) client_failed = False @@ -66,13 +154,27 @@ if __name__ == "__main__": if client_action == "group-members": (rval, rparam) = client.listGroupMembers(options.group) + elif client_action == "useradd": + (rval, rparam) = client.userAdd( + options.newUsername, + shell = options.shell, + systemAccount = options.systemAccount, + createHome = options.createHome, + createGroup = options.createGroup, + homeDir = options.homeDir, + uid = options.uid, + gid = options.gid, + password = options.newPassword, + gecos = options.gecos) elif client_action in actions: (rval, rparam) = actions[client_action]() else: sys.stderr.write("No such action to perform!\n") sys.exit(1) - if not rval: + if rval: + sys.stdout.write("%s: %s\n" % (client_hostname, rparam if rparam else "ok")) + else: sys.stderr.write("%s: %s\n" % (client_hostname, rparam)) client_failed = True diff --git a/cli-tools/cura/cura_client_user.py b/cli-tools/cura/cura_client_user.py index b0b21fc..3f92dc3 100644 --- a/cli-tools/cura/cura_client_user.py +++ b/cli-tools/cura/cura_client_user.py @@ -22,6 +22,8 @@ class CuraUserClient(): GROUP_CLASS_NAME = "LMI_Group" MEMBER_OF_GROUP_CLASS_NAME = "LMI_MemberOfGroup" ASSIGNED_IDENTITY_CLASS_NAME = "LMI_AssignedAccountIdentity" + ACCOUNT_MANAGEMENT_SERVICE_CLASS_NAME = "LMI_AccountManagementService" + COMPUTER_SYSTEM_CLASS_NAME = "CIM_ComputerSystem" def __init__(self, hostname, username = "", password = ""): self.m_hostname = hostname @@ -53,6 +55,12 @@ class CuraUserClient(): def __getGroupInstances(self): return self.__getInstances(CuraUserClient.GROUP_CLASS_NAME) + def __getAccountManagementInstance(self): + return self.__getInstances(CuraUserClient.ACCOUNT_MANAGEMENT_SERVICE_CLASS_NAME) + + def __getComputerSystemInstance(self): + return self.__getInstances(CuraUserClient.COMPUTER_SYSTEM_CLASS_NAME) + def __getMemberGroups(self, inst): members = [] identities = self.m_cliconn.Associators(inst.path, @@ -109,3 +117,38 @@ class CuraUserClient(): for m in members: sys.stdout.write("%s\n" % m) return (True, "") + + def userAdd(self, user_name, **params): + (inst_management, rparam) = self.__getAccountManagementInstance() + if not inst_management: + return (False, rparam) + # We are using the first and only instance present. + # For KVM_ComputerSystem, there has be some CLI option added. + inst_management = inst_management[0] + (inst_computer_system, rparam) = self.__getComputerSystemInstance() + if not inst_computer_system: + return (False, rparam) + user_params = { "Name" : user_name } + user_params["DontCreateHome"] = not params["createHome"] + user_params["System"] = inst_computer_system[0].path + user_params["DontCreateGroup"] = not params["createGroup"] + if params["shell"]: + user_params["Shell"] = params["shell"] + if params["systemAccount"]: + user_params["SystemAccount"] = params["systemAccount"] + if params["gid"]: + user_params["GID"] = pywbem.Uint32(params["gid"]) + if params["gecos"]: + user_params["GECOS"] = params["gecos"] + if params["homeDir"]: + user_params["HomeDirectory"] = params["homeDir"] + if params["password"]: + user_params["Password"] = params["password"] + if params["uid"]: + user_params["UID"] = pywbem.Uint32(params["uid"]) + try: + (account, identity) = self.m_cliconn.InvokeMethod( + "CreateAccount", inst_management.path, **user_params) + except pywbem.CIMError, e: + return (False, e.args[1]) + return (account != None, "") |