forked from hyperledger/indy-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clear_node.py
executable file
·77 lines (64 loc) · 1.94 KB
/
clear_node.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
#!/usr/bin/env python3
import shutil
import os
import pyorient
import argparse
import re
def pathList(*pathes):
return {os.path.expanduser(path) for path in pathes}
TARGET_DIRS = pathList(
"~/.sovrin",
"~/.plenum"
)
WHITE_LIST = pathList(
"~/.sovrin/sovrin_config.py",
"~/.plenum/plenum_config.py",
"~/.sovrin/.*/role",
"~/.plenum/.*/role",
"~/.sovrin/.*log"
)
ORIENTDB_HOST = "localhost"
ORIENTDB_PORT = 2424
ORIENTDB_USER = "root"
ORIENTDB_PASSWORD = "password"
def clean_orientdb():
client = pyorient.OrientDB(ORIENTDB_HOST, ORIENTDB_PORT)
client.connect(ORIENTDB_USER, ORIENTDB_PASSWORD)
names = [n for n in client.db_list().oRecordData['databases'].keys()]
for nm in names:
try:
client.db_drop(nm)
except:
continue
def clean_files(full):
if full:
for dir in TARGET_DIRS:
if os.path.exists(dir):
shutil.rmtree(dir)
return
files_to_keep = [re.compile(pattern) for pattern in WHITE_LIST]
def isOk(path):
return any(pattern.match(path) for pattern in files_to_keep)
for dir in TARGET_DIRS:
for root, dirs, files in os.walk(dir):
if not isOk(root):
for file in files:
fullName = os.path.join(root, file)
if not isOk(fullName):
os.remove(fullName)
if not os.listdir(root):
os.rmdir(root)
else:
dirs[:] = []
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Removes node data and configuration')
parser.add_argument('--full',
action="store_true",
help="remove configs and logs too")
args = parser.parse_args()
clean_files(args.full)
try:
clean_orientdb()
except Exception as ex:
print("Cleaning of data from orientdb failed:", ex)