forked from ioos/glider-dac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglider_dac_db_sync.py
129 lines (93 loc) · 4.03 KB
/
glider_dac_db_sync.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
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
#!/usr/bin/env python
import time
import os.path
import os
import argparse
import logging
import smtplib
import subprocess
from datetime import datetime
from watchdog.events import FileSystemEventHandler, DirCreatedEvent, DirDeletedEvent, FileCreatedEvent
from watchdog.observers import Observer
from glider_dac import app, db
logging.basicConfig(level=logging.INFO,
format='[%(asctime)s | %(levelname)s] %(message)s')
logger = logging.getLogger(__name__)
class HandleDeploymentDB(FileSystemEventHandler):
def __init__(self, base):
self.base = base
def on_created(self, event):
if isinstance(event, DirCreatedEvent):
if self.base not in event.src_path:
return
rel_path = os.path.relpath(event.src_path, self.base)
# we only care about this path if it's under a user dir
# user/upload/deployment-name
path_parts = rel_path.split(os.sep)
if len(path_parts) != 3:
return
logger.info("New deployment directory: %s", rel_path)
with app.app_context():
deployment = db.Deployment.find_one({'deployment_dir':event.src_path})
if deployment is None:
deployment = db.Deployment()
usr = db.User.find_one( { 'username' : unicode(path_parts[0]) } )
if hasattr(usr, '_id'):
deployment.user_id = usr._id
deployment.name = unicode(path_parts[2])
deployment.deployment_dir = unicode(event.src_path)
deployment.updated = datetime.utcnow()
deployment.save()
elif isinstance(event, FileCreatedEvent):
if self.base not in event.src_path:
return
path_parts = os.path.split(event.src_path)
if path_parts[-1] != "wmoid.txt":
return
rel_path = os.path.relpath(event.src_path, self.base)
logger.info("New wmoid.txt in %s", rel_path)
with app.app_context():
deployment = db.Deployment.find_one({'deployment_dir':path_parts[0]})
if deployment is None:
logger.error("Cannot find deployment for %s", path_parts[0])
return
if deployment.wmo_id:
logger.info("Deployment already has wmoid %s. Updating value with new file.", deployment.wmo_id)
with open(event.src_path) as wf:
deployment.wmo_id = unicode(wf.readline().strip())
deployment.updated = datetime.utcnow()
deployment.save()
def on_deleted(self, event):
if isinstance(event, DirDeletedEvent):
if self.base not in event.src_path:
return
rel_path = os.path.relpath(event.src_path, self.base)
# we only care about this path if it's under a user dir
# user/upload/deployment-name
path_parts = rel_path.split(os.sep)
if len(path_parts) != 3:
return
logger.info("Removed deployment directory: %s", rel_path)
with app.app_context():
deployment = db.Deployment.find_one({'deployment_dir':event.src_path})
if deployment:
deployment.delete()
def main(handler):
observer = Observer()
observer.schedule(handler, path=handler.base, recursive=True)
observer.start()
logger.info("Watching user directories in %s", handler.base)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('basedir',
default=os.environ.get('DATA_ROOT', '.'),
nargs='?')
args = parser.parse_args()
base = os.path.realpath(args.basedir)
main(HandleDeploymentDB(base))