-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmongoUtil.py
More file actions
168 lines (120 loc) · 5.36 KB
/
mongoUtil.py
File metadata and controls
168 lines (120 loc) · 5.36 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
b'This script requires python 3.4'
"""
Connect to NERSC mongoDB sever and returns handles to the
different collections
Author: Jochen Thaeder <jmthader@lbl.gov>
"""
import sys, os, datetime
import pymongo
from pymongo import MongoClient
##############################################
# -- GLOBAL CONSTANTS
MONGO_SERVER = 'mongodb01.nersc.gov'
MONGO_DB_NAME = 'STAR_XROOTD'
ADMIN_USER = 'STAR_XROOTD_admin'
READONLY_USER = 'STAR_XROOTD_ro'
COLLECTION_INDICES = {'HPSS_Files': 'fileFullPath', 'HPSS_PicoDsts': 'filePath', 'XRD_DataServers': 'nodeName',
'XRD_PicoDsts': 'filePath', 'XRD_PicoDsts_brokenLink': 'nodeFilePath',
'XRD_PicoDsts_corrupt': 'nodeFilePath', 'XRD_PicoDsts_noHPSS': 'nodeFilePath',
'Stage_From_HPSS': 'fileFullPath', 'Stage_To_XRD': 'fileFullPath'
}
##############################################
# -- Check for a proper Python Version
if sys.version[0:3] < '3.0':
print ('Python version 3.0 or greater required (found: {0}).'.format(sys.version[0:5]))
sys.exit(-1)
if pymongo.__version__[0:3] < '3.0':
print ('pymongo version 3.0 or greater required (found: {0}).'.format(pymongo.__version__[0:5]))
sys.exit(-1)
# ----------------------------------------------------------------------------------
class mongoDbUtil:
"""Class to connect to mongoDB and perform actions."""
# _________________________________________________________
def __init__(self, args, userSwitch = 'user'):
self.args = args
# -- Get the password form env
if userSwitch == "admin":
self.user = ADMIN_USER
self.password = os.getenv('STAR_XROOTD_ad', 'empty')
else:
self.user = READONLY_USER
self.password = os.getenv('STAR_XROOTD_ro', 'empty')
if self.password == 'empty':
print("Password for user {0} at database {1} has not been supplied".format(self.user, MONGO_DB_NAME))
sys.exit(-1)
self.today = datetime.datetime.today().strftime('%Y-%m-%d')
# -- Connect
self._connectDB()
# _________________________________________________________
def _connectDB(self):
"""Connect to the NERSC mongoDB using pymongo."""
self.client = MongoClient('mongodb://{0}:{1}@{2}/{3}'.format(self.user, self.password,
MONGO_SERVER, MONGO_DB_NAME))
self.db = self.client[MONGO_DB_NAME]
# print ("Existing collections:", self.db.collection_names(include_system_collections = False))
# _________________________________________________________
def close(self):
"""Close conenction to the NERSC mongoDB using pymongo."""
self.client.close()
self.db = ""
# _________________________________________________________
def getCollection(self, collectionName = 'HPSS_Files'):
"""Get collection and set index."""
collection = self.db[collectionName]
try:
collection.create_index([(COLLECTION_INDICES[collectionName], pymongo.ASCENDING)], unique=True)
except KeyError:
#print ("Warning: Collection", collectionName, "not known. Index not created.")
pass
return collection
# _________________________________________________________
def dropCollection(self, collectionName):
"""Drop collection."""
self.db[collectionName].drop()
# _________________________________________________________
def checkProcessLock(self, fieldName):
"""Check process lock."""
collLock = self.getCollection("Process_Locks")
docLock = collLock.find_one({'unique': 'unique'})
if not docLock:
return False
if not fieldName in docLock.keys():
return False
return docLock[fieldName]
# _________________________________________________________
def checkSetProcessLock(self, fieldName):
"""Check process lock and set if False.
Returns True if already locked.
"""
if self.checkProcessLock(fieldName):
return True
else:
self.setProcessLock(fieldName)
return False
# _________________________________________________________
def _setProcessLock(self, fieldName, state):
"""Set process lock state."""
collLock = self.getCollection("Process_Locks")
collLock.find_one_and_update({'unique': 'unique'},
{'$set': {fieldName: state}})
return
# _________________________________________________________
def setProcessLock(self, fieldName):
"""Set process lock - active."""
self._setProcessLock(fieldName, True)
return
# _________________________________________________________
def unsetProcessLock(self, fieldName):
"""Set process lock - inactive."""
self._setProcessLock(fieldName, False)
return
# ----------------------------------------------------------------------------------
# ____________________________________________________________________________
def main():
"""Initialize and run,"""
print("mongoDbUtil main")
# ----------------------------------------------------------------------------------
if __name__ == "__main__":
"""Call main."""
sys.exit(main())