-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadd_host.py
More file actions
executable file
·28 lines (21 loc) · 969 Bytes
/
add_host.py
File metadata and controls
executable file
·28 lines (21 loc) · 969 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python
"""Utility script to add a new authorized host."""
import logging
import pymongo
import argparse
import datetime
logging.basicConfig()
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('site_id', help='site_id to be added to authd instances')
arg_parser.add_argument('--db_uri', help='DB uri', default='mongodb://127.0.0.1/central')
args = arg_parser.parse_args()
log = logging.getLogger('add_host')
kwargs = dict(tz_aware=True)
db_client = pymongo.MongoReplicaSetClient(args.db_uri, **kwargs) if 'replicaSet' in args.db_uri else pymongo.MongoClient(args.db_uri, **kwargs)
db = db_client.get_default_database()
# is there already a host entry for this site_id?
if not db.instances.find_one({'_id': args.site_id}):
db.instances.insert({'_id': args.site_id, 'date_added': datetime.datetime.now()})
log.info('entry created for host %s' % args.site_id)
else:
log.info('entry already exists for host %s' % args.site_id)