From: Michael Orlitzky Date: Sun, 6 Oct 2013 22:55:50 +0000 (-0400) Subject: Get prune working, at least in a simple case. X-Git-Tag: 0.0.1~80 X-Git-Url: http://gitweb.michael.orlitzky.com/?p=mailshears.git;a=commitdiff_plain;h=4e7ef12d3d47aa7ec68419de46c51269c40eb158;ds=inline Get prune working, at least in a simple case. --- diff --git a/bin/mailshears b/bin/mailshears index cb8ee41..441708e 100755 --- a/bin/mailshears +++ b/bin/mailshears @@ -118,79 +118,4 @@ plugin_class.includers.each do |plugin_class_includer| # *target arguments as well, to turn ARGV back into an array. runner.run(plugin, *ARGV) - puts "" -end - - - -Kernel.exit(0) - - -begin - # Get a list of domains from the Postfixadmin database. - db_domains = pgadb.get_domains_from_db() -rescue DatabaseError => e - puts "There was an error connecting to the database: #{e.to_s}" - Kernel.exit(ExitCodes::DATABASE_ERROR) -end - -begin - # And the accounts. - db_accounts = pgadb.get_accounts_from_db() -rescue DatabaseError => e - puts "There was an error connecting to the database: #{e.to_s}" - Kernel.exit(ExitCodes::DATABASE_ERROR) -end - - -Plugin.includers.each do |plugin_class_includer| - plugin = plugin_class_includer.new() - - begin - leftover_domains = plugin.get_leftover_domains(db_domains) - rescue StandardError => e - puts "There was an error retrieving domains from the filesystem: #{e.to_s}" - Kernel.exit(ExitCodes::FILESYSTEM_ERROR) - end - - begin - leftover_accounts = plugin.get_leftover_accounts(db_accounts) - rescue StandardError => e - puts "There was an error retrieving accounts from the filesystem: #{e.to_s}" - Kernel.exit(ExitCodes::FILESYSTEM_ERROR) - end - - if leftover_domains.size > 0 or leftover_accounts.size > 0 - puts make_header(plugin_class.to_s()) - - leftover_domains.each do |domain| - puts "Found: #{domain} (#{plugin.describe_domain(domain)})" - end - - leftover_accounts.each do |account| - puts "Found: #{account} (#{plugin.describe_account(account)})" - end - - if cfg.i_mean_business - # We have to delete the accounts before the domain, - # otherwise they'd already be gone. - leftover_accounts.each do |account| - # Get the description before we delete the domain. - # This can still fail if the account's domain is gone. - account_description = plugin.describe_account(account) - plugin.delete_account(account) - puts "Removed: #{account} (#{account_description})" - end - - leftover_domains.each do |domain| - # Get the description before we delete the domain. - domain_description = plugin.describe_domain(domain) - plugin.delete_domain(domain) - puts "Removed: #{domain} (#{domain_description})" - end - end - - puts "" - end - end diff --git a/lib/prune/plugins/postfixadmin.rb b/lib/prune/plugins/postfixadmin.rb index d085489..62e5647 100644 --- a/lib/prune/plugins/postfixadmin.rb +++ b/lib/prune/plugins/postfixadmin.rb @@ -1,24 +1,10 @@ -require 'pg' - require 'prune/prune_plugin' require 'rm/plugins/postfixadmin' class PostfixadminPrune < PostfixadminRm - - include PrunePlugin - - #def get_leftover_domains(db_domains) - # AgenDAV doesn't have a concept of domains. We could parse the - # usernames to see what domains are present, but the point is - # moot: all leftover accounts will be pruned anyway. - #return [] -#end - - - #def get_leftover_accounts(db_accounts) - # Get a list of all users who have logged in to AgenDAV. - #ad_accounts = self.list_users() - # return ad_accounts - db_accounts - #end - + # We don't need the ability to remove "left over" accounts or + # domains, since "left over" is with respect to what's in + # PostfixAdmin. In other words, the other plugins check themselves + # against PostfixAdmin, it doesn't make sense to check PostfixAdmin + # against itself. end diff --git a/lib/prune/plugins/roundcube.rb b/lib/prune/plugins/roundcube.rb index 77eacfc..1ad8cdb 100644 --- a/lib/prune/plugins/roundcube.rb +++ b/lib/prune/plugins/roundcube.rb @@ -18,7 +18,7 @@ class RoundcubePrune < RoundcubeRm def get_leftover_accounts(db_accounts) # Get a list of all users who have logged in to Roundcube. - rc_accounts = self.list__users() + rc_accounts = self.list_users() return rc_accounts - db_accounts end diff --git a/lib/prune/prune_dummy_runner.rb b/lib/prune/prune_dummy_runner.rb index 8e9fcd9..4274bef 100644 --- a/lib/prune/prune_dummy_runner.rb +++ b/lib/prune/prune_dummy_runner.rb @@ -1,10 +1,32 @@ require 'common/runner' +# This is always needed, regardless of which plugin is running. +require 'prune/plugins/postfixadmin' + class PruneDummyRunner include Runner def run(plugin) - puts "Not implemented" + # We don't want to check the PostfixAdmin database against itself. + return if plugin.class == PostfixadminPrune + + pfa = PostfixadminPrune.new() + + db_users = pfa.list_users() + db_domains = pfa.list_domains() + + leftover_users = plugin.get_leftover_accounts(db_users) + leftover_domains = plugin.get_leftover_domains(db_domains) + + leftover_users.each do |user| + user_description = plugin.describe_account(user) + report(plugin, "Would remove user: #{user} (#{user_description})") + end + + leftover_domains.each do |domain| + domain_description = plugin.describe_domain(domain) + report(plugin, "Would remove domain: #{domain} (#{domain_description})") + end end end diff --git a/lib/prune/prune_runner.rb b/lib/prune/prune_runner.rb index 789ed57..74455d6 100644 --- a/lib/prune/prune_runner.rb +++ b/lib/prune/prune_runner.rb @@ -1,10 +1,35 @@ require 'common/runner' +# This is always needed, regardless of which plugin is running. +require 'prune/plugins/postfixadmin' + class PruneRunner include Runner def run(plugin) - puts "Not implemented" + # We don't want to check the PostfixAdmin database against itself. + return if plugin.class == PostfixadminPrune + + pfa = PostfixadminPrune.new() + + db_users = pfa.list_users() + db_domains = pfa.list_domains() + + leftover_users = plugin.get_leftover_accounts(db_users) + leftover_domains = plugin.get_leftover_domains(db_domains) + + leftover_users.each do |user| + user_description = plugin.describe_account(user) + plugin.delete_account(user) + report(plugin, "Removed user: #{user} (#{user_description})") + end + + leftover_domains.each do |domain| + domain_description = plugin.describe_domain(domain) + plugin.delete_domain(domain) + report(plugin, "Removed domain: #{domain} (#{domain_description})") + end + end end diff --git a/lib/rm/rm_dummy_runner.rb b/lib/rm/rm_dummy_runner.rb index 9bdd13f..091d328 100644 --- a/lib/rm/rm_dummy_runner.rb +++ b/lib/rm/rm_dummy_runner.rb @@ -6,14 +6,11 @@ class RmDummyRunner def run(plugin, *targets) targets.each do |target| if target.include?('@') then - puts "Would remove account: #{target}" - # TODO: remove from postfixadmin as well. + user_description = plugin.describe_account(target) + report(plugin, "Would remove user: #{user} (#{user_description})") else - usernames = plugin.get_domain_usernames(target) - usernames.each { |u| run(plugin, u) } - - puts "Would remove domain: #{target}" - # TODO: remove from postfixadmin as well. + domain_description = plugin.describe_domain(target) + report(plugin, "Would remove domain: #{domain} (#{domain_description})") end end