diff options
Diffstat (limited to 'pykolab/cli')
-rw-r--r-- | pykolab/cli/__init__.py | 371 |
1 files changed, 341 insertions, 30 deletions
diff --git a/pykolab/cli/__init__.py b/pykolab/cli/__init__.py index 6a13c14..298335e 100644 --- a/pykolab/cli/__init__.py +++ b/pykolab/cli/__init__.py @@ -72,7 +72,14 @@ class Cli(object): action_action = action_components.pop() exec("from pykolab.cli import action_%s" %(action_domain)) if hasattr("action_%s" %(action_domain), "%s" %(action_action)): - exec("result = action_%s.%s(%r)" %(action_domain,action_action,conf.cli_args)) + exec( + "result = action_%s.%s(%r)" %( + action_domain, + action_action, + conf.cli_args + ) + ) + except IndexError, e: self.no_command() except ImportError, e: @@ -114,7 +121,9 @@ class Cli(object): ## def action_add_domain(self): - log.info(_("TODO: Figure out where the domain should actually be added.")) + log.info( + _("TODO: Figure out where the domain should actually be added.") + ) domainname = conf.cli_args.pop(0) @@ -135,9 +144,15 @@ class Cli(object): ] attrs['associatedDomain'] = ['%s' %(domainname)] domainname_components = domainname.split('.') - attrs['inetDomainBaseDN'] = ['dc=%s,dc=%s' %(domainname_components[0],domainname_components[1])] + attrs['inetDomainBaseDN'] = [ + 'dc=%s,dc=%s' %( + domainname_components[0], + domainname_components[1] + ) + ] - log.info(_("TODO: Prompt for organization name/description. For now, use domain name.")) + # TODO: Prompt for organization name/description. For now, use domain + # name. attrs['o'] = ['%s' %(domainname)] go_ahead = True @@ -145,16 +160,26 @@ class Cli(object): if conf.cli_keywords.review: ldif_writer = ldif.LDIFWriter(sys.stdout) ldif_writer.unparse(dn,attrs) - if not utils.ask_confirmation(_("Please ACK or NACK the above LDIF:"), default="y", all_inclusive_no=True): + if not utils.ask_confirmation( + _("Please ACK or NACK the above LDIF:"), + default="y", + all_inclusive_no=True + ): + go_ahead = False if go_ahead: - # Convert our dict to nice syntax for the add-function using modlist-module + # Convert our dict to nice syntax for the add-function using + # modlist-module _ldif = addModlist(attrs) + # TODO: Use auth # Now build an ldap connection and execute the motherf. ldap_con = ldap.initialize(conf.get('ldap', 'uri')) - ldap_con.bind_s(conf.get('ldap', 'bind_dn'), conf.get('ldap', 'bind_pw')) + ldap_con.bind_s( + conf.get('ldap', 'bind_dn'), + conf.get('ldap', 'bind_pw') + ) # The try/except should actually be in check_add_domain try: @@ -187,8 +212,12 @@ class Cli(object): dn = "associateddomain=%s,cn=kolab,cn=config" %(domainname) + # TODO: Use auth ldap_con = ldap.initialize(conf.get('ldap', 'uri')) - ldap_con.bind_s(conf.get('ldap', 'bind_dn'), conf.get('ldap', 'bind_pw')) + ldap_con.bind_s( + conf.get('ldap', 'bind_dn'), + conf.get('ldap', 'bind_pw') + ) # The try/except should actually be in check_del_domain() try: @@ -232,40 +261,80 @@ class Cli(object): # TODO: /etc/imapd.conf is not the definitive location for the # imapd.conf configuration file. - partition_proc = subprocess.Popen(['grep', '^partition', '/etc/imapd.conf'], stdout=subprocess.PIPE) - - partitions = [x.split(':')[1].strip() for x in partition_proc.communicate()[0].split('\n') if len(x.split(':')) > 1] + partition_proc = subprocess.Popen( + ['grep', '^partition', '/etc/imapd.conf'], + stdout=subprocess.PIPE + ) + + partitions = [ + x.split(':')[1].strip() + for x in partition_proc.communicate()[0].split('\n') + if len(x.split(':')) > 1 + ] # TODO: ctl_mboxlist is not necessarily in this location. ctl_mboxlist_args = [ '/usr/lib/cyrus-imapd/ctl_mboxlist', '-d' ] - ctl_mboxlist = subprocess.Popen(ctl_mboxlist_args, stdout=subprocess.PIPE) + ctl_mboxlist = subprocess.Popen( + ctl_mboxlist_args, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE + ) + + mboxlist_proc = subprocess.Popen( + ['grep', '-E', '\s*%s\s*.*i.*p.*' %(user)], + stdin=ctl_mboxlist.stdout, + stdout=subprocess.PIPE + ) - mboxlist_proc = subprocess.Popen(['grep', '-E', '\s*%s\s*.*i.*p.*' %(user)], stdin=ctl_mboxlist.stdout, stdout=subprocess.PIPE) ctl_mboxlist.stdout.close() + # TODO: Handle errors from ctl_mboxlist process (stderr) mboxlist_output = mboxlist_proc.communicate()[0] + zipper_args = [ 'zip', '-r', '%s.zip' %(user) ] directories = [] for mbox_internal in mboxlist_output.split('\n'): if len(mbox_internal.split('\t')[0].split('!')) > 1: domain = mbox_internal.split('\t')[0].split('!')[0] - mailbox = '/'.join(mbox_internal.split('\t')[0].split('!')[1].split('.')[1:]) - - zipper_args = [ - 'zip', - '-r', - '%s.zip' %(user) - ] + mailbox = '/'.join( + mbox_internal.split( + '\t' + )[0].split( + '!' + )[1].split( + '.' + )[1:] + ) for partition in partitions: - if os.path.isdir('%s/domain/%s/%s/%s/user/%s/' %(partition,domain[0],domain,user[0],mailbox)): - directories.append('%s/domain/%s/%s/%s/user/%s/' %(partition,domain[0],domain,user[0],mailbox)) - else: - log.debug(_('%s/domain/%s/%s/%s/user/%s/ is not a directory') %(partition,domain[0],domain,user[0],mailbox), level=5) + mbox_dir = '%s/domain/%s/%s/%s/user/%s/' %( + partition, + domain[0], + domain, + user[0], + mailbox + ) + + if os.path.isdir(mbox_dir): + directories.append(mbox_dir) - zipper_output = subprocess.Popen(zipper_args + directories, stdout=subprocess.PIPE).communicate()[0] - print >> sys.stderr, _("ZIP file at %s.zip") %(user) + else: + log.debug( + _('%s is not a directory') %(mbox_dir), + level=5 + ) + + if not len(directories) == 0: + zipper_output = subprocess.Popen( + zipper_args + directories, + stdout=subprocess.PIPE + ).communicate()[0] + + print >> sys.stderr, _("ZIP file at %s.zip") %(user) + else: + print >> sys.stderr, _("No directories found for user %s") %(user) + sys.exit(1) def action_list_deleted(self): """ @@ -287,11 +356,17 @@ class Cli(object): # TODO: Take a hint in --quiet, and otherwise print out a nice table # with headers and such. for domain,domain_aliases in domains: - print _("Primary domain: %s - Secondary domain(s): %s") %(domain, ', '.join(domain_aliases)) + if len(domain_aliases) > 0: + print _("Primary domain: %s - Secondary domain(s): %s") %( + domain, + ', '.join(domain_aliases) + ) + else: + print _("Primary domain: %s") %(domain) def action_list_mailbox(self): """ - List a mailbox + List mailboxes """ try: searches = [ conf.cli_args.pop(0) ] @@ -314,12 +389,18 @@ class Cli(object): start_time = time.time() domains = auth.list_domains() end_time = time.time() - log.debug(_("Found %d domains in %d seconds") %(len(domains),(end_time-start_time)), level=8) + log.debug( + _("Found %d domains in %d seconds") %( + len(domains), + (end_time-start_time) + ), + level=8 + ) all_folders = [] for primary_domain,secondary_domains in domains: - #print "Running for domain %s" %(primary_domain) + log.debug(_("Running for domain %s") %(primary_domain), level=8) auth.connect(primary_domain) start_time = time.time() auth.synchronize(primary_domain, secondary_domains) @@ -329,6 +410,236 @@ class Cli(object): %(primary_domain, (end_time-start_time)) ) + def action_telemetry_examine_command_issue_id(self): + from pykolab import telemetry + + db = telemetry.init_db() + + try: + wanted = conf.cli_args.pop(0) + except: + log.error(_("Unspecified command issue identifier")) + sys.exit(1) + + command_issue = db.query( + telemetry.TelemetryCommandIssue + ).filter_by( + id=wanted + ).first() + + if command_issue == None: + log.error(_("Invalid command issue identifier")) + sys.exit(1) + + session = db.query( + telemetry.TelemetrySession + ).filter_by( + id=command_issue.session_id + ).first() + + if session == None: + log.error(_("Invalid session identifier")) + sys.exit(1) + + user = db.query( + telemetry.TelemetryUser + ).filter_by( + id=session.user_id + ).first() + + server = db.query( + telemetry.TelemetryServer + ).filter_by( + id=session.server_id + ).first() + + print _("Session by %s on server %s") %(user.sasl_username,server.fqdn) + + command_issues = db.query( + telemetry.TelemetryCommandIssue + ).filter_by( + session_id=session.id + ) + + for _command_issue in command_issues: + command = db.query( + telemetry.TelemetryCommand + ).filter_by( + id=_command_issue.command_id + ).first() + + command_arg = db.query( + telemetry.TelemetryCommandArg + ).filter_by( + id=_command_issue.command_arg_id + ).first() + + if command_issue.id == _command_issue.id: + print "=========" + + print "Client(%d): %s %s %s" %( + _command_issue.id, + _command_issue.command_tag, + command.command, + command_arg.command_arg + ) + + server_responses = db.query( + telemetry.TelemetryServerResponse + ).filter_by( + command_issue_id=_command_issue.id + ) + + for server_response in server_responses: + server_response_lines = server_response.response.split('\n'); + + for server_response_line in server_response_lines: + print "Server(%d): %s" %( + server_response.id, + server_response_line + ) + + if command_issue.id == _command_issue.id: + print "=========" + + def action_telemetry_examine_session(self, session_id=None): + from pykolab import telemetry + + db = telemetry.init_db() + + wanted = False + + if session_id == None: + try: + wanted = conf.cli_args.pop(0) + except: + log.error(_("Unspecified session identifier")) + sys.exit(1) + + if not wanted: + wanted = session_id + + session_wanted = None + + try: + _wanted = (int)(wanted) + session_wanted = _wanted + except: + user_wanted = wanted + + if not session_wanted == None: + session = db.query( + telemetry.TelemetrySession + ).filter_by( + id=session_wanted + ).first() + + if session == None: + log.error(_("Invalid session identifier")) + sys.exit(1) + + user = db.query( + telemetry.TelemetryUser + ).filter_by( + id=session.user_id + ).first() + + server = db.query( + telemetry.TelemetryServer + ).filter_by( + id=session.server_id + ).first() + + else: + user = db.query( + telemetry.TelemetryUser + ).filter_by( + sasl_username=user_wanted + ).first() + + sessions = db.query( + telemetry.TelemetrySession + ).filter_by( + user_id=user.id + ).order_by( + telemetry.TelemetrySession.start + ) + + for session in sessions: + self.action_telemetry_examine_session(session_id=session.id) + + return + + print _("Session by %s on server %s") %(user.sasl_username,server.fqdn) + + command_issues = db.query( + telemetry.TelemetryCommandIssue + ).filter_by( + session_id=session.id + ) + + for command_issue in command_issues: + command = db.query( + telemetry.TelemetryCommand + ).filter_by( + id=command_issue.command_id + ).first() + + command_arg = db.query( + telemetry.TelemetryCommandArg + ).filter_by( + id=command_issue.command_arg_id + ).first() + + print "Client(%d): %s %s %s" %( + command_issue.id, + command_issue.command_tag, + command.command, + command_arg.command_arg + ) + + server_responses = db.query( + telemetry.TelemetryServerResponse + ).filter_by( + command_issue_id=command_issue.id + ) + + for server_response in server_responses: + server_response_lines = server_response.response.split('\n'); + for server_response_line in server_response_lines: + print "Server(%d): %s" %( + server_response.id, + server_response_line + ) + + def action_telemetry_expire_sessions(self): + from pykolab import telemetry + telemetry.expire_sessions() + + def action_telemetry_list_sessions(self): + from pykolab import telemetry + + db = telemetry.init_db() + + sessions = db.query( + telemetry.TelemetrySession + ).order_by( + telemetry.telemetry_session_table.c.start + ) + + for session in sessions: + user = db.query( + telemetry.TelemetryUser + ).filter_by( + id=session.user_id + ).first() + + print _("Session for user %s started at %s with ID %s") %( + user.sasl_username, + session.start, + session.id + ) + def action_undelete_mailbox(self): """ Undelete mailbox |