|
1 |
| -import optparse |
2 |
| -import pymongo |
3 |
| -import uuid |
| 1 | +import argparse |
4 | 2 | import base64
|
| 3 | +from pymongo import MongoClient |
| 4 | +import uuid |
| 5 | +import logging |
5 | 6 |
|
6 |
| -from ..auth import get_auth, authenticate |
7 | 7 | from ..hooks import get_mongodb_uri
|
8 |
| -from .utils import setup_logging |
| 8 | +from .utils import do_db_auth |
| 9 | +from arctic.arctic import Arctic |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
9 | 12 |
|
10 | 13 |
|
11 | 14 | def main():
|
12 |
| - usage = """usage: %prog [options] username ... |
| 15 | + usage = """arctic_create_user --host research [--db mongoose_user] [--write] user |
13 | 16 |
|
14 |
| - Create the user's personal Arctic database, and adds them, read-only |
15 |
| - to the central admin database. |
| 17 | + Creates the user's personal Arctic mongo database |
| 18 | + Or add a user to an existing Mongo Database. |
16 | 19 | """
|
17 |
| - setup_logging() |
18 |
| - parser = optparse.OptionParser(usage=usage) |
19 |
| - parser.add_option("--host", default='localhost', help="Hostname, or clustername. Default: localhost") |
20 |
| - parser.add_option("--password", dest="password", default=None, help="Password. Default: random") |
21 |
| - parser.add_option("--admin-write", dest="admin", action='store_false', default=True, |
22 |
| - help="Give write access to the admin DB. Default: False") |
23 |
| - parser.add_option("--dryrun", "-n", dest="dryrun", action="store_true", help="Don't really do anything", default=False) |
24 |
| - parser.add_option("--verbose", "-v", dest="verbose", action="store_true", help="Print some commentary", default=False) |
25 |
| - parser.add_option("--nodb", dest="nodb", help="Don't create a 'personal' database", action="store_true", default=False) |
26 |
| - |
27 |
| - (opts, args) = parser.parse_args() |
28 | 20 |
|
29 |
| - c = pymongo.MongoClient(get_mongodb_uri(opts.host)) |
30 |
| - credentials = get_auth(opts.host, 'admin', 'admin') |
31 |
| - if not credentials: |
32 |
| - raise ValueError("You have no admin credentials for instance '%s'" % (opts.host)) |
| 21 | + parser = argparse.ArgumentParser(usage=usage) |
| 22 | + parser.add_argument("--host", default='localhost', help="Hostname, or clustername. Default: localhost") |
| 23 | + parser.add_argument("--db", default=None, help="Database to add user on. Default: mongoose_<user>") |
| 24 | + parser.add_argument("--password", default=None, help="Password. Default: random") |
| 25 | + parser.add_argument("--write", action='store_true', default=False, help="Used for granting write access to someone else's DB") |
| 26 | + parser.add_argument("users", nargs='+', help="Users to add.") |
33 | 27 |
|
34 |
| - if not authenticate(c.admin, credentials.user, credentials.password): |
35 |
| - raise ValueError("Failed to authenticate to '%s' as '%s'" % (opts.host, credentials.user)) |
| 28 | + args = parser.parse_args() |
36 | 29 |
|
37 |
| - for user in args: |
| 30 | + c = MongoClient(get_mongodb_uri(args.host)) |
38 | 31 |
|
39 |
| - p = opts.password |
| 32 | + if not do_db_auth(args.host, c, args.db if args.db else 'admin'): |
| 33 | + logger.error("Failed to authenticate to '%s'. Check your admin password!" % (args.host)) |
| 34 | + return |
40 | 35 |
|
| 36 | + for user in args.users: |
| 37 | + write_access = args.write |
| 38 | + p = args.password |
41 | 39 | if p is None:
|
42 | 40 | p = base64.b64encode(uuid.uuid4().bytes).replace('/', '')[:12]
|
| 41 | + db = args.db |
| 42 | + if not db: |
| 43 | + # Users always have write access to their database |
| 44 | + write_access = True |
| 45 | + db = Arctic.DB_PREFIX + '_' + user |
| 46 | + |
| 47 | + # Add the user to the database |
| 48 | + c[db].add_user(user, p, read_only=not write_access) |
| 49 | + |
| 50 | + logger.info("Granted: {user} [{permission}] to {db}".format(user=user, |
| 51 | + permission='WRITE' if write_access else 'READ', |
| 52 | + db=db)) |
| 53 | + logger.info("User creds: {db}/{user}/{password}".format(user=user, |
| 54 | + host=args.host, |
| 55 | + db=db, |
| 56 | + password=p, |
| 57 | + )) |
43 | 58 |
|
44 |
| - if not opts.dryrun: |
45 |
| - if opts.verbose: |
46 |
| - print "Adding user %s to DB %s" % (user, opts.host) |
47 |
| - if not opts.nodb: |
48 |
| - if opts.verbose: |
49 |
| - print "Adding database arctic_%s to DB %s" % (user, opts.host) |
50 |
| - c['arctic_' + user].add_user(user, p) |
51 |
| - c.admin.add_user(user, p, read_only=opts.admin) |
52 |
| - else: |
53 |
| - print "DRYRUN: add user %s readonly %s nodb %s" % (user, opts.admin, opts.nodb) |
54 |
| - |
55 |
| - if not opts.password: |
56 |
| - print "%-16s %s" % (user, p) |
57 | 59 |
|
58 | 60 | if __name__ == '__main__':
|
59 | 61 | main()
|
0 commit comments