This repository has been archived by the owner on Jun 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathreindex_tokens.py
100 lines (82 loc) · 3.04 KB
/
reindex_tokens.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from elasticsearch_dsl import DocType, String, Date, Integer, Boolean, Float, Ip, GeoPoint, Keyword, Index
from elasticsearch_dsl.connections import connections
import time
import os
### If you need to restore the tokens index to the previous schema run the following from a python interactive shell:
### from reindex_tokens import *
### restore_tokens()
INDEX_NAME = 'tokens'
BACKUP_INDEX_NAME = 'tokens_backup'
ES_NODES = os.getenv('CIF_ES_NODES', '127.0.0.1:9200')
connections.create_connection(hosts=ES_NODES)
class TokenBackup(DocType):
username = Keyword()
token = Keyword()
expires = Date()
read = Boolean()
write = Boolean()
revoked = Boolean()
acl = Keyword()
groups = Keyword()
admin = Boolean()
last_activity_at = Date()
class Meta:
index = BACKUP_INDEX_NAME
class Token(DocType):
username = Keyword()
token = Keyword()
expires = Date()
read = Boolean()
write = Boolean()
revoked = Boolean()
acl = Keyword()
groups = Keyword()
admin = Boolean()
last_activity_at = Date()
class Meta:
index = INDEX_NAME
def reindex_tokens():
TokenBackup.init()
connections.create_connection(hosts=ES_NODES)
backup_results = connections.get_connection().reindex(body={"source": {"index": INDEX_NAME}, "dest": {"index": BACKUP_INDEX_NAME}}, request_timeout=3600)
if backup_results.get('created') + backup_results.get('updated') == backup_results.get('total'):
Index(INDEX_NAME).delete()
else:
return ('Tokens did not backup properly')
time.sleep(1)
Token.init()
reindex_results = connections.get_connection().reindex(body={"source": {"index": BACKUP_INDEX_NAME}, "dest": {"index": INDEX_NAME}}, request_timeout=3600)
if reindex_results.get('created') + reindex_results.get('updated') == reindex_results.get('total'):
return ('Tokens reindexed successfully!')
else:
return ('Tokens did not reindex from backup properly')
def restore_tokens():
connections.create_connection(hosts=ES_NODES)
Index(INDEX_NAME).delete()
class Token(DocType):
username = String()
token = String()
expires = Date()
read = Boolean()
write = Boolean()
revoked = Boolean()
acl = String()
groups = String()
admin = Boolean()
last_activity_at = Date()
class Meta:
index = INDEX_NAME
Token.init()
reindex_results = connections.get_connection().reindex(body={"source": {"index": BACKUP_INDEX_NAME}, "dest": {"index": INDEX_NAME}}, request_timeout=3600)
if reindex_results.get('created') + reindex_results.get('updated') == reindex_results.get('total'):
return ('Tokens restored to previous schema successfully!')
else:
return ('Tokens did not restore from backup properly')
def main():
results = reindex_tokens()
if results == 'Tokens reindexed successfully!':
print("Tokens reindexed successfully!")
else:
print("Tokens did not reindex properly")
if __name__ == '__main__':
main()