-
Notifications
You must be signed in to change notification settings - Fork 1
/
init_user.py
executable file
·85 lines (76 loc) · 3.01 KB
/
init_user.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3.5
import json
import sys, getopt
import rethinkdb as r
import threading
from myslice import db
from myslice.db.user import User
from myslicelib.query import q
from myslice.web.controllers.login import crypt_password
from pprint import pprint
from myslice.services.workers.users import syncUsers
from tornado import gen, escape
@gen.coroutine
def main(argv):
try:
opts, args = getopt.getopt(argv,"he:P:k:p:s",["email=","password=","private_key=","public_key=","sync="])
except getopt.GetoptError:
print('init_user.py -e <email> -P <password> -k <private_key path> -p <public_key path> -s <synchronize with Registry>')
sys.exit(2)
if(len(opts)<4):
print('Missing parameters:')
print('init_user.py -e <email> -P <password> -k <private_key path> -p <public_key path> -s <synchronize with Registry>')
sys.exit(2)
sync = False
for opt, arg in opts:
if opt == '-h':
print('init_user.py -e <email> -P <password> -k <private_key path> -p <public_key path> -s <synchronize with Registry>')
sys.exit()
elif opt in ("-e", "--email"):
email = arg
elif opt in ("-P", "--password"):
password = crypt_password(arg)
elif opt in ("-k", "--private_key"):
f = open(arg, 'r')
private_key = f.read()
elif opt in ("-p", "--public_key"):
f = open(arg, 'r')
public_key = f.read()
elif opt in ("-s", "--sync"):
if arg.lower() in ["true", "yes", "y"]:
sync = True
dbconnection = db.connect()
lock = threading.Lock()
# Synchronize the users from SFA Registry into the DB
if sync:
print("sync user %s" % email)
syncUsers(lock, email=email, job=False)
else:
try:
# Get the user from SFA Registry
print("get user %s from SFA Reg" % email)
remote_user = q(User).filter('email', email).get().first()
pprint(remote_user)
# merge fields from script with remote_user
remote_user.setAttribute('password', password)
remote_user.setAttribute('private_key', private_key)
remote_user.setAttribute('generate_keys', False)
remote_user.setAttribute('public_key', public_key)
remote_user.setAttribute('keys', [public_key])
r.db('myslice').table('users').insert(remote_user.dict()).run(dbconnection)
result = r.db('myslice').table('users').get(remote_user.id).run(dbconnection)
#result = remote_user.save(dbconnection)
if result:
print("User saved")
else:
print("Error during save")
pprint(result)
# if user has private key
# update its Credentials
#if 'private_key' in updated_user:
# updated_user = update_credentials(updated_user)
except Exception as e:
import traceback
traceback.print_exc()
if __name__ == '__main__':
main(sys.argv[1:])