diff options
Diffstat (limited to 'src/realmd/doc/examples/realmd-cim')
-rw-r--r-- | src/realmd/doc/examples/realmd-cim | 245 |
1 files changed, 245 insertions, 0 deletions
diff --git a/src/realmd/doc/examples/realmd-cim b/src/realmd/doc/examples/realmd-cim new file mode 100644 index 0000000..90ae9d5 --- /dev/null +++ b/src/realmd/doc/examples/realmd-cim @@ -0,0 +1,245 @@ +#!/usr/bin/python + +import sys +import os +import optparse +import urlparse +import pywbem + +#---------------------------------------------------------------------- + +def do_list(conn, options, args): + realms = conn.EnumerateInstances('LMI_RealmdKerberosRealm') + + print "%d realms" % (len(realms)) + + for realm in realms: + if options.verbose > 1: + # Very verbose, dump all properties + property_names = sorted(realm.keys()) + for name in property_names: + value = realm[name] + print " %s: %s" % (name, value) + + print realm['RealmName'] + print " type: kerberos" + print " realm-name: %s" % realm['RealmName'] + print " domain-name: %s" % realm['DomainName'] + + is_configured = True + configured = realm['Configured'] + if not configured: + configured = "no" + is_configured = False + elif configured == "KerberosMembership": + configured = "kerberos-member" + + print " configured: %s" % configured + + for detail in zip(realm['DetailNames'], realm['DetailValues']): + print " %s: %s" % (detail[0], detail[1]) + + if is_configured: + print " login-formats: %s" % ", ".join(realm['LoginFormats']) + print " login-policy: %s" % realm['LoginPolicy'] + print " permitted-logins: %s" % ", ".join(realm['PermittedLogins']) + + print + +def do_join(conn, options, args): + # Validate arguments + if len(args) != 3: + raise ValueError("You must supply exacly 3 arguments (user, password, domain)") + + user, password, domain = args + + if (options.verbose): + print "Joining domain: %s" % domain + + try: + realmd_service_instance_name = conn.EnumerateInstanceNames('LMI_RealmdService')[0] + except Exception, e: + raise ValueError("could not obtain realmd service") + + try: + retval, outparams = conn.InvokeMethod("JoinDomain", realmd_service_instance_name, + Domain=domain, + User=user, + Password=password) + except Exception, e: + raise ValueError("Join failed (%s)" % (e)) + + + +def do_leave(conn, options, args): + # Validate arguments + if len(args) != 3: + raise ValueError("You must supply exacly 3 arguments (user, password, domain)") + + user, password, domain = args + + if (options.verbose): + print "Leave domain: %s" % domain + + try: + realmd_service_instance_name = conn.EnumerateInstanceNames('LMI_RealmdService')[0] + except Exception, e: + raise ValueError("could not obtain realmd service") + + try: + retval, outparams = conn.InvokeMethod("LeaveDomain", realmd_service_instance_name, + Domain=domain, + User=user, + Password=password) + except Exception, e: + raise ValueError("Leave failed (%s)" % (e)) + + + +def do_discover(conn, options, args): + # Validate arguments + if len(args) != 1: + raise ValueError("You must supply exactly 1 domain.") + + domain = args[0] + + if (options.verbose): + print "Discovering domain: %s" % domain + + try: + realmd_service_instance_name = conn.EnumerateInstanceNames('LMI_RealmdService')[0] + except Exception, e: + raise ValueError("could not obtain realmd service") + + try: + retval, outparams = conn.InvokeMethod("Discover", realmd_service_instance_name, + Target=domain) + except Exception, e: + raise ValueError("Join failed (%s)" % (e)) + + realm_refs = outparams['DiscoveredRealms'] + + print "%d Discovered" % len(realm_refs) + for realm_ref in realm_refs: + #print realm_ref + realm = conn.GetInstance(realm_ref) + print realm['RealmName'] + print " Name: %s" % realm['RealmName'] + print " Configured: %s" % realm['Configured'] + print " Supported Interfaces: %s" % ", ".join(realm['SupportedInterfaces']) + for detail in zip(realm['DetailNames'], realm['DetailValues']): + print " %s: %s" % (detail[0], detail[1]) + print " login-formats: %s" % ", ".join(realm['LoginFormats']) + print " login-policy: %s" % realm['LoginPolicy'] + print " permitted-logins: %s" % ", ".join(realm['PermittedLogins']) + + + +#---------------------------------------------------------------------- + +def main(): + + actions = {'list': do_list, + 'discover': do_discover, + 'join': do_join, + 'leave': do_leave, + } + + usage =''' + %%prog [options] <action> <arg> ... + + %%prog [options] list + %%prog [options] discover domain + %%prog [options] join user password domain + %%prog [options] leave user password domain + + Available Actions: %(actions)s + ''' % {'actions': ", ".join(sorted(actions.keys()))} + + # Set-up defaults + + default_cimom_port = 5989 + default_url = os.environ.get("LMI_CIMOM_URL", "https://localhost:5989") + + parsed_default_url = urlparse.urlparse(default_url) + + if parsed_default_url.port: + default_port = parsed_default_url.port or default_cimom_port + + default_username = os.environ.get("LMI_CIMOM_USERNAME", "root") + default_password = os.environ.get("LMI_CIMOM_PASSWORD", "") + + # Set-up arg parser + parser = optparse.OptionParser(usage=usage) + + parser.add_option('-c', '--url', dest='url', default=default_url, + help='CIMOM URL or hostname to connect to') + + parser.add_option('-u', '--username', dest='username', default=default_username, + help='Username for CIMOM authentication') + + parser.add_option('-p', '--password', dest='password', default=default_password, + help='Password for CIMOM authentication') + + parser.add_option('-v', '--verbose', dest='verbose', default=0, + action='count', + help='Turn on verbose output, increases verbosity level by one each time specified') + + options, args = parser.parse_args() + + # Validate arguments + + try: + action = args.pop(0) + except IndexError: + print >>sys.stderr, "You must supply an action to execute" + parser.print_help() + return 1 + + try: + action_func = actions[action] + except KeyError: + print >>sys.stderr, "Unknown action (%s)" % (action) + parser.print_help() + return 1 + + # Get CIMOM URL + + parsed_url = urlparse.urlparse(options.url) + + if not parsed_url.netloc: + # Handle case where URL was bare hostname + parsed_url = urlparse.urlparse('//' + options.url) + + scheme = 'https' + hostname = parsed_url.hostname + port = parsed_url.port or default_port + + url = urlparse.urlunparse((scheme, "%s:%d" % (hostname, port), '', None, None, None)) + + # Connect to CIMOM + + if (options.verbose): + print "Connecting to: %s" % url + + try: + conn = pywbem.WBEMConnection(url, (options.username, options.password)) + except Exception, e: + print >>sys.stderr, "Unable to connect to %s (%s)" % (options.url, e) + return 1 + + + # Execute action + + try: + action_func(conn, options, args) + except Exception, e: + print >>sys.stderr, "%s failed: %s" % (action, e) + return 1 + + return 0 + +#---------------------------------------------------------------------- + +if __name__ == "__main__": + sys.exit(main()) |