2015-08-06 14:46:44 -04:00
|
|
|
settings = require "settings-sharelatex"
|
|
|
|
child_process = require "child_process"
|
|
|
|
mongoUri = require "mongo-uri";
|
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
AWS = require 'aws-sdk'
|
|
|
|
fs = require 'fs'
|
2015-08-09 14:47:47 -04:00
|
|
|
S3S = require 's3-streams'
|
2015-08-14 18:19:54 -04:00
|
|
|
{db, ObjectId} = require "./mongojs"
|
|
|
|
JSONStream = require "JSONStream"
|
|
|
|
ReadlineStream = require "readline-stream"
|
2015-08-06 14:46:44 -04:00
|
|
|
|
|
|
|
module.exports = MongoAWS =
|
|
|
|
|
2015-08-14 18:19:54 -04:00
|
|
|
bulkLimit: 10
|
|
|
|
|
2015-08-06 14:46:44 -04:00
|
|
|
archiveDocHistory: (project_id, doc_id, callback = (error) ->) ->
|
2015-08-14 18:19:54 -04:00
|
|
|
query = {
|
|
|
|
doc_id: ObjectId(doc_id)
|
|
|
|
expiresAt: {$exists : false}
|
|
|
|
}
|
|
|
|
|
|
|
|
AWS.config.update {
|
|
|
|
accessKeyId: settings.filestore.s3.key
|
|
|
|
secretAccessKey: settings.filestore.s3.secret
|
|
|
|
}
|
|
|
|
|
|
|
|
upload = S3S.WriteStream new AWS.S3(), {
|
|
|
|
"Bucket": settings.filestore.stores.user_files,
|
|
|
|
"Key": project_id+"/changes-"+doc_id
|
|
|
|
}
|
|
|
|
|
|
|
|
db.docHistory.find(query)
|
|
|
|
.pipe JSONStream.stringify()
|
|
|
|
.pipe upload
|
|
|
|
.on 'finish', () ->
|
|
|
|
return callback(null)
|
|
|
|
|
|
|
|
unArchiveDocHistory: (project_id, doc_id, callback = (error) ->) ->
|
|
|
|
|
|
|
|
AWS.config.update {
|
|
|
|
accessKeyId: settings.filestore.s3.key
|
|
|
|
secretAccessKey: settings.filestore.s3.secret
|
|
|
|
}
|
|
|
|
|
|
|
|
download = S3S.ReadStream new AWS.S3(), {
|
|
|
|
"Bucket": settings.filestore.stores.user_files,
|
|
|
|
"Key": project_id+"/changes-"+doc_id
|
|
|
|
}, {
|
|
|
|
encoding: "utf8"
|
|
|
|
}
|
|
|
|
|
|
|
|
lineStream = new ReadlineStream();
|
|
|
|
ops = []
|
|
|
|
|
|
|
|
download
|
|
|
|
.on 'open', (obj) ->
|
|
|
|
return 1
|
|
|
|
.pipe lineStream
|
|
|
|
.on 'data', (line) ->
|
|
|
|
if line.length > 2
|
|
|
|
ops.push(JSON.parse(line))
|
2015-08-14 18:58:38 -04:00
|
|
|
if ops.length == MongoAWS.bulkLimit
|
|
|
|
MongoAWS.handleBulk ops.slice(0), () ->
|
|
|
|
ops.splice(0,ops.length)
|
2015-08-14 18:19:54 -04:00
|
|
|
.on 'end', () ->
|
|
|
|
MongoAWS.handleBulk ops, callback
|
|
|
|
.on 'error', (err) ->
|
|
|
|
return callback(err)
|
|
|
|
|
|
|
|
handleBulk: (ops, cb) ->
|
|
|
|
bulk = db.docHistory.initializeUnorderedBulkOp();
|
|
|
|
|
|
|
|
for op in ops
|
|
|
|
op._id = ObjectId(op._id)
|
|
|
|
op.doc_id = ObjectId(op.doc_id)
|
|
|
|
op.project_id = ObjectId(op.project_id)
|
|
|
|
bulk.find({_id:op._id}).upsert().updateOne(op)
|
|
|
|
|
|
|
|
bulk.execute (err, result) ->
|
|
|
|
if err?
|
|
|
|
logger.error err:err, "error bulking ReadlineStream"
|
|
|
|
else
|
2015-08-14 18:58:38 -04:00
|
|
|
logger.log count:ops.length, result:result, "bulked ReadlineStream"
|
2015-08-14 18:19:54 -04:00
|
|
|
cb(err)
|
|
|
|
|
|
|
|
|
|
|
|
archiveDocHistoryExternal: (project_id, doc_id, callback = (error) ->) ->
|
2015-08-06 16:09:36 -04:00
|
|
|
MongoAWS.mongoExportDocHistory doc_id, (error, filepath) ->
|
2015-08-09 14:47:47 -04:00
|
|
|
MongoAWS.s3upStream project_id, doc_id, filepath, callback
|
2015-08-09 16:50:15 -04:00
|
|
|
#delete temp file?
|
2015-08-06 14:46:44 -04:00
|
|
|
|
2015-08-14 18:19:54 -04:00
|
|
|
|
|
|
|
unArchiveDocHistoryExternal: (project_id, doc_id, callback = (error) ->) ->
|
2015-08-09 14:47:47 -04:00
|
|
|
MongoAWS.s3downStream project_id, doc_id, (error, filepath) ->
|
2015-08-06 16:09:36 -04:00
|
|
|
if error == null
|
|
|
|
MongoAWS.mongoImportDocHistory filepath, callback
|
2015-08-09 16:50:15 -04:00
|
|
|
#delete temp file?
|
2015-08-06 16:09:36 -04:00
|
|
|
else
|
|
|
|
callback
|
|
|
|
|
|
|
|
mongoExportDocHistory: (doc_id, callback = (error, filepath) ->) ->
|
2015-08-06 14:46:44 -04:00
|
|
|
uriData = mongoUri.parse(settings.mongo.url);
|
2015-08-06 16:09:36 -04:00
|
|
|
filepath = settings.path.dumpFolder + '/' + doc_id + '.jsonUp'
|
2015-08-06 14:46:44 -04:00
|
|
|
|
|
|
|
args = []
|
|
|
|
args.push '-h'
|
|
|
|
args.push uriData.hosts[0]
|
|
|
|
args.push '-d'
|
|
|
|
args.push uriData.database
|
|
|
|
args.push '-c'
|
|
|
|
args.push 'docHistory'
|
|
|
|
args.push '-q'
|
2015-08-14 14:07:16 -04:00
|
|
|
args.push "{doc_id: ObjectId('#{doc_id}') , expiresAt: {$exists : false} }"
|
2015-08-06 14:46:44 -04:00
|
|
|
args.push '-o'
|
|
|
|
args.push filepath
|
|
|
|
|
|
|
|
proc = child_process.spawn "mongoexport", args
|
|
|
|
|
|
|
|
proc.on "error", callback
|
|
|
|
|
|
|
|
stderr = ""
|
|
|
|
proc.stderr.on "data", (chunk) -> stderr += chunk.toString()
|
|
|
|
|
|
|
|
proc.on "close", (code) ->
|
|
|
|
if code == 0
|
|
|
|
return callback(null,filepath)
|
|
|
|
else
|
|
|
|
return callback(new Error("mongodump failed: #{stderr}"),null)
|
|
|
|
|
2015-08-06 16:09:36 -04:00
|
|
|
mongoImportDocHistory: (filepath, callback = (error) ->) ->
|
|
|
|
|
|
|
|
uriData = mongoUri.parse(settings.mongo.url);
|
|
|
|
|
|
|
|
args = []
|
|
|
|
args.push '-h'
|
|
|
|
args.push uriData.hosts[0]
|
|
|
|
args.push '-d'
|
|
|
|
args.push uriData.database
|
|
|
|
args.push '-c'
|
|
|
|
args.push 'docHistory'
|
|
|
|
args.push '--file'
|
|
|
|
args.push filepath
|
|
|
|
|
|
|
|
proc = child_process.spawn "mongoimport", args
|
|
|
|
|
|
|
|
proc.on "error", callback
|
|
|
|
|
|
|
|
stderr = ""
|
|
|
|
proc.stderr.on "data", (chunk) -> stderr += chunk.toString()
|
|
|
|
|
|
|
|
proc.on "close", (code) ->
|
|
|
|
if code == 0
|
|
|
|
return callback(null,filepath)
|
|
|
|
else
|
|
|
|
return callback(new Error("mongodump failed: #{stderr}"),null)
|
|
|
|
|
2015-08-09 14:47:47 -04:00
|
|
|
s3upStream: (project_id, doc_id, filepath, callback = (error) ->) ->
|
2015-08-06 14:46:44 -04:00
|
|
|
|
|
|
|
AWS.config.update {
|
|
|
|
accessKeyId: settings.filestore.s3.key
|
|
|
|
secretAccessKey: settings.filestore.s3.secret
|
|
|
|
}
|
2015-08-09 14:47:47 -04:00
|
|
|
|
|
|
|
upload = S3S.WriteStream new AWS.S3(), {
|
|
|
|
"Bucket": settings.filestore.stores.user_files,
|
|
|
|
"Key": project_id+"/changes-"+doc_id
|
2015-08-06 14:46:44 -04:00
|
|
|
}
|
|
|
|
|
2015-08-09 14:47:47 -04:00
|
|
|
fs.createReadStream(filepath)
|
|
|
|
.on 'open', (obj) ->
|
|
|
|
return 1
|
|
|
|
.pipe(upload)
|
|
|
|
.on 'finish', () ->
|
|
|
|
return callback(null)
|
|
|
|
.on 'error', (err) ->
|
|
|
|
return callback(err)
|
2015-08-06 14:46:44 -04:00
|
|
|
|
2015-08-09 14:47:47 -04:00
|
|
|
s3downStream: (project_id, doc_id, callback = (error, filepath) ->) ->
|
2015-08-06 16:09:36 -04:00
|
|
|
|
|
|
|
filepath = settings.path.dumpFolder + '/' + doc_id + '.jsonDown'
|
|
|
|
|
|
|
|
AWS.config.update {
|
|
|
|
accessKeyId: settings.filestore.s3.key
|
|
|
|
secretAccessKey: settings.filestore.s3.secret
|
|
|
|
}
|
2015-08-09 14:47:47 -04:00
|
|
|
|
|
|
|
download = S3S.ReadStream new AWS.S3(), {
|
2015-08-06 16:09:36 -04:00
|
|
|
"Bucket": settings.filestore.stores.user_files,
|
|
|
|
"Key": project_id+"/changes-"+doc_id
|
|
|
|
}
|
|
|
|
|
2015-08-09 14:47:47 -04:00
|
|
|
download
|
|
|
|
.on 'open', (obj) ->
|
|
|
|
return 1
|
|
|
|
.pipe(fs.createWriteStream(filepath))
|
|
|
|
.on 'finish', () ->
|
|
|
|
return callback(null, filepath)
|
|
|
|
.on 'error', (err) ->
|
|
|
|
return callback(err, null)
|