|
a/scripts/restore_project.py |
|
b/scripts/restore_project.py |
|
... |
|
... |
15 |
MONGO_HOME=os.environ.get('MONGO_HOME', '/usr')
|
15 |
MONGO_HOME=os.environ.get('MONGO_HOME', '/usr')
|
16 |
MONGO_DUMP=os.path.join(MONGO_HOME, 'bin/mongodump')
|
16 |
MONGO_DUMP=os.path.join(MONGO_HOME, 'bin/mongodump')
|
17 |
MONGO_RESTORE=os.path.join(MONGO_HOME, 'bin/mongorestore')
|
17 |
MONGO_RESTORE=os.path.join(MONGO_HOME, 'bin/mongorestore')
|
18 |
|
18 |
|
19 |
def main():
|
19 |
def main():
|
20 |
if len(sys.argv) != 3:
|
20 |
if len(sys.argv) != 4:
|
21 |
log.error('Usage: %s <dirname> <new_shortname>', sys.argv[0])
|
21 |
log.error('Usage: %s <dirname> <new_shortname> <new_unix_group_name>', sys.argv[0])
|
22 |
return
|
22 |
return 2
|
23 |
dirname = sys.argv[1]
|
23 |
dirname = sys.argv[1]
|
24 |
new_pname = sys.argv[2]
|
24 |
new_pname = sys.argv[2]
|
|
|
25 |
new_ug_name = sys.argv[3]
|
25 |
restore_project(dirname, new_pname)
|
26 |
return restore_project(dirname, new_pname, new_ug_name)
|
26 |
|
27 |
|
27 |
def restore_project(dirname, new_shortname=None):
|
28 |
def restore_project(dirname, new_shortname, new_unix_group_name):
|
28 |
log.info('Reloading %s into %s', dirname, new_shortname)
|
29 |
log.info('Reloading %s into %s', dirname, new_shortname)
|
29 |
with open(os.path.join(dirname, 'project.json')) as fp:
|
30 |
with open(os.path.join(dirname, 'project.json')) as fp:
|
30 |
project_doc = json.load(fp, object_hook=object_hook)
|
31 |
project_doc = json.load(fp, object_hook=object_hook)
|
31 |
project = M.Project.query.get(_id=project_doc['_id'])
|
32 |
project = M.Project.query.get(_id=project_doc['_id'])
|
32 |
st = state(project)
|
33 |
st = state(project)
|
33 |
st.document = instrument(project_doc, DocumentTracker(st))
|
34 |
st.document = instrument(project_doc, DocumentTracker(st))
|
34 |
if project is None:
|
35 |
if project is None:
|
35 |
log.fatal('Project not found')
|
36 |
log.fatal('Project not found')
|
36 |
return
|
37 |
return 2
|
37 |
dump_path = os.path.join(dirname, project.database)
|
38 |
dump_path = os.path.join(dirname, project.database)
|
38 |
with open(os.path.join(dirname, 'project.json')) as fp:
|
39 |
with open(os.path.join(dirname, 'project.json')) as fp:
|
39 |
project_doc = json.load(fp, object_hook=object_hook)
|
40 |
project_doc = json.load(fp, object_hook=object_hook)
|
40 |
st = state(project)
|
41 |
st = state(project)
|
41 |
st.document = instrument(project_doc, DocumentTracker(st))
|
42 |
st.document = instrument(project_doc, DocumentTracker(st))
|
42 |
project.shortname = new_shortname
|
43 |
project.shortname = new_shortname
|
|
|
44 |
project.set_tool_data('sfx', 'unix_group_name', new_unix_group_name)
|
43 |
project.database = 'project:' + new_shortname.replace('/', ':').replace('-', '_')
|
45 |
project.database = 'project:' + new_shortname.replace('/', ':').replace('-', '_')
|
44 |
project.deleted = False
|
46 |
project.deleted = False
|
45 |
conn = M.main_doc_session.bind.conn
|
47 |
conn = M.main_doc_session.bind.conn
|
46 |
if project.database in conn.database_names():
|
48 |
if project.database in conn.database_names():
|
47 |
raw_input('''Warning: database %s is already populated. If you do NOT want
|
49 |
raw_input('''Warning: database %s is already populated. If you do NOT want
|
|
... |
|
... |
51 |
os.system('%s --db %s %s' % (
|
53 |
os.system('%s --db %s %s' % (
|
52 |
MONGO_RESTORE, project.database, dump_path))
|
54 |
MONGO_RESTORE, project.database, dump_path))
|
53 |
session(project).flush()
|
55 |
session(project).flush()
|
54 |
reindex= ReindexCommand('reindex')
|
56 |
reindex= ReindexCommand('reindex')
|
55 |
reindex.run(['--project', new_shortname])
|
57 |
reindex.run(['--project', new_shortname])
|
|
|
58 |
return 0
|
56 |
|
59 |
|
57 |
if __name__ == '__main__':
|
60 |
if __name__ == '__main__':
|
58 |
main()
|
61 |
sys.exit(main())
|