added script destroy-user-db
authorelijah <elijah@riseup.net>
Thu, 24 Sep 2015 23:56:11 +0000 (16:56 -0700)
committerelijah <elijah@riseup.net>
Thu, 24 Sep 2015 23:56:11 +0000 (16:56 -0700)
destroy-all-test-users [moved from ruby/destroy-all-test-users with 100% similarity]
destroy-all-user-dbs [moved from ruby/destroy-all-user-dbs with 100% similarity]
destroy-user-db [new file with mode: 0755]
lib/leap.rb [new file with mode: 0644]
lib/leap/server.rb [new file with mode: 0644]
lib/leap/users.rb [new file with mode: 0644]

diff --git a/destroy-user-db b/destroy-user-db
new file mode 100755 (executable)
index 0000000..fdf917b
--- /dev/null
@@ -0,0 +1,47 @@
+#!/usr/bin/ruby
+
+require_relative "lib/leap"
+
+def main()
+  parse_opts
+  server   = LEAP::Server.new
+  users_db = LEAP::Users.new(server)
+  record   = users_db.find_by_login($username)
+  if record.nil?
+    puts "Username #{$username} does not exist."
+    exit 1
+  end
+  dbname   = "user-#{record.id}"
+  begin
+    server.database(dbname).delete!
+    puts "Deleted DB #{dbname}"
+    exit 0
+  rescue RestClient::ResourceNotFound
+    puts "DB does not exist: #{dbname}"
+    exit 1
+  end
+end
+
+def parse_opts()
+  $username = nil
+  while cmd = ARGV.shift
+    case cmd
+      when "--username"
+        $username = ARGV.shift
+      else
+        usage
+    end
+  end
+  unless $username
+    usage
+  end
+end
+
+def usage()
+  puts "#{$0} COMMAND"
+  puts "Commands:"
+  puts "  --username USERNAME - destroy the user db corresponding to USERNAME."
+  exit(1)
+end
+
+main()
\ No newline at end of file
diff --git a/lib/leap.rb b/lib/leap.rb
new file mode 100644 (file)
index 0000000..39d2004
--- /dev/null
@@ -0,0 +1,2 @@
+require_relative('leap/server.rb')
+require_relative('leap/users.rb')
\ No newline at end of file
diff --git a/lib/leap/server.rb b/lib/leap/server.rb
new file mode 100644 (file)
index 0000000..bc60455
--- /dev/null
@@ -0,0 +1,11 @@
+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
+  end
+end
\ No newline at end of file
diff --git a/lib/leap/users.rb b/lib/leap/users.rb
new file mode 100644 (file)
index 0000000..14c9c4e
--- /dev/null
@@ -0,0 +1,36 @@
+require 'couchrest'
+
+module LEAP
+  class Users < CouchRest::Database
+
+    def initialize(server)
+      super(server, 'users')
+    end
+
+    def find_by_login(login)
+      record = self.view('User/by_login',
+        :reduce => false,
+        :startkey => login,
+        :endkey => login
+      )['rows'].first
+      if record
+        return self.get(record['id'])
+      end
+    end
+
+    def all_logins
+      self.view('User/by_login',
+        :reduce => false
+      )['rows'].map {|row|
+        row['key']
+      }
+    end
+
+    def all_active_ids
+    end
+
+    def all_ids
+    end
+
+  end
+end