summaryrefslogtreecommitdiff
path: root/server/pkg
diff options
context:
space:
mode:
authorVictor Shyba <victor.shyba@gmail.com>2015-09-17 18:30:07 -0300
committerVictor Shyba <victor.shyba@gmail.com>2015-09-28 16:52:53 -0300
commitb065492f35006c3d108965b2b50144e080fbe678 (patch)
tree66d5e236fcd0b29ecc0064facb6d0d2197ec3e51 /server/pkg
parentf7ff2e014e25b5c201f4e6209549518b53fc36b2 (diff)
[feat] script for user db creation
Added a simple script for user db creation and design docs creation. It uses a netrc from /etc/couchdb/couchdb-admin.netrc and same validator used on couch.py for database names.
Diffstat (limited to 'server/pkg')
-rwxr-xr-xserver/pkg/create-user-db42
1 files changed, 42 insertions, 0 deletions
diff --git a/server/pkg/create-user-db b/server/pkg/create-user-db
new file mode 100755
index 00000000..edcb8a82
--- /dev/null
+++ b/server/pkg/create-user-db
@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+import os
+import sys
+import netrc
+import argparse
+from leap.soledad.common.couch import CouchDatabase
+from leap.soledad.common.couch import is_db_name_valid
+
+
+description = """
+Creates a user database.
+This is meant to be used by Soledad Server.
+"""
+parser = argparse.ArgumentParser(description=description)
+parser.add_argument('dbname', metavar='user-d34db33f', type=str,
+ help='database name on the format user-{uuid4}')
+NETRC_PATH = '/etc/couchdb/couchdb-admin.netrc'
+
+
+def url_for_db(dbname):
+ if not os.path.exists(NETRC_PATH):
+ print ('netrc not found in %s' % NETRC_PATH)
+ sys.exit(1)
+ parsed_netrc = netrc.netrc(NETRC_PATH)
+ host, (login, _, password) = parsed_netrc.hosts.items()[0]
+ url = ('http://%(login)s:%(password)s@%(host)s:5984/%(dbname)s' % {
+ 'login':login,
+ 'password':password,
+ 'host':host,
+ 'dbname':dbname})
+ return url
+
+
+if __name__ == '__main__':
+ args = parser.parse_args()
+ if not is_db_name_valid(args.dbname):
+ print ("Invalid name! %s" % args.dbname)
+ sys.exit(1)
+ url = url_for_db(args.dbname)
+ db = CouchDatabase.open_database(url=url, create=True,
+ replica_uid=None, ensure_ddocs=True)
+ print ('success! Created %s, replica_uid: %s' % (db._dbname, db.replica_uid))