added cleanup-user-dbs
authorelijah <elijah@riseup.net>
Tue, 29 Sep 2015 21:22:15 +0000 (14:22 -0700)
committerelijah <elijah@riseup.net>
Tue, 29 Sep 2015 21:22:15 +0000 (14:22 -0700)
cleanup-user-dbs [new file with mode: 0755]
lib/leap/server.rb
lib/leap/users.rb

diff --git a/cleanup-user-dbs b/cleanup-user-dbs
new file mode 100755 (executable)
index 0000000..8b09472
--- /dev/null
@@ -0,0 +1,37 @@
+#!/usr/bin/ruby
+
+#
+# This script will destroy every per-user storage database
+# where the corresponding user record does not exist.
+#
+# This should be run regularly by cron to clean up old storage dbs.
+#
+# This script is inefficient, but hopefully we will not be using couchdb
+# by the time that becomes a problem.
+#
+
+require_relative "lib/leap"
+
+server      = LEAP::Server.new
+users_db    = LEAP::Users.new(server)
+user_ids    = users_db.all_ids.inject({}) {|h,i| h[i] = true; h}
+storage_dbs = server.storage_dbs
+
+begin
+  storage_dbs.each do |db|
+    user_id = db.sub(/^user-/,'')
+    unless user_ids[user_id]
+      begin
+        server.database(db).delete!
+        puts "Deleted storage DB `#{db}`."
+      rescue RestClient::ResourceNotFound
+        puts "Storage DB `#{db}` does not exist."
+      end
+    end
+  end
+rescue Exception => exc
+  STDERR.puts "ERROR: unhandled exception, #{exc}."
+  exit(1)
+end
+
+exit(0)
\ No newline at end of file
index bc60455..05d76e3 100644 (file)
@@ -2,10 +2,22 @@ require 'couchrest'
 
 module LEAP
   class Server < CouchRest::Server
+
     def initialize
       netrc = File.read('/root/.netrc').split(' ')
       auth = "%{username}:%{password}@" % {username: netrc[3], password: netrc[5]}
       super("http://#{auth}localhost:5984")
     end
+
+    #
+    # returns an array of the names of all the per-user storage
+    # databases.
+    #
+    def storage_dbs
+      self.databases.select { |db_name|
+        db_name =~ /^user-[a-f0-9]{32}$/
+      }
+    end
+
   end
 end
\ No newline at end of file
index 14c9c4e..b9dc12c 100644 (file)
@@ -30,6 +30,7 @@ module LEAP
     end
 
     def all_ids
+      self.all_docs["rows"].map {|row| row["id"]}
     end
 
   end