2014-06-23 14:29:37 -04:00
|
|
|
# This is written in coffeescript. Run this using coffee, not node. (Line to yield nice warnings on node. :))
|
|
|
|
return console.log '------------------------------------------------- \n
|
|
|
|
|
|
|
|
Commandline utility written in Coffeescript to run all updates, download latest database and install it after you git pushed. \n
|
|
|
|
Params: \n
|
|
|
|
\t--skipupdates skips npm and bower update \n
|
|
|
|
\t--dldb: download the latest database (Over 300 mb!) \n
|
|
|
|
\t--resetdb: to reset the database and load dump from tmp. Will need a downloaded database or --dbdownload specified \n
|
|
|
|
\t--mongopath <.path/to/mongo>: to specify mongodb folder if not set in PATH. \n
|
|
|
|
\t--help: Yo fund this one already. \n
|
|
|
|
\n
|
|
|
|
May need an initial npm install upfront if newly checked out. \n
|
|
|
|
|
|
|
|
' if '--help' in process.argv
|
|
|
|
|
2014-09-13 22:55:01 -04:00
|
|
|
#TODO: MD5 Verification, using http://54.91.159.37/dump.md5 using digest stream https://github.com/jeffbski/digest-stream
|
|
|
|
dbDump = 'http://54.91.159.37/dump.tar.gz' # Don't change this unless you know what you're doing
|
2014-06-16 19:57:26 -04:00
|
|
|
dbLocalPath = '../temp'
|
|
|
|
|
|
|
|
fs = require 'fs'
|
|
|
|
tar = require 'tar'
|
|
|
|
spawn = require('child_process').spawn
|
|
|
|
http = require 'http'
|
|
|
|
fs = require 'fs'
|
|
|
|
zlib = require 'zlib'
|
|
|
|
Deferred = require 'JQDeferred'
|
|
|
|
|
|
|
|
#TODO: Could kill current coco server here.
|
|
|
|
|
|
|
|
mongopath = ''
|
|
|
|
useNext = false
|
|
|
|
for path in process.argv
|
|
|
|
if useNext
|
|
|
|
mongopath = path
|
|
|
|
break
|
2014-06-23 14:29:37 -04:00
|
|
|
useNext = path is '--mongopath'
|
2014-06-16 19:57:26 -04:00
|
|
|
mongopath += '/bin/' if mongopath.length
|
|
|
|
mongopath += 'mongodb' # mongodb is in path.
|
|
|
|
|
|
|
|
run = (proc, args) ->
|
|
|
|
deferred = Deferred()
|
|
|
|
spawned = spawn proc, args
|
2014-06-30 22:16:26 -04:00
|
|
|
spawned.stdout.on 'data', (data) -> process.stdout.write data
|
|
|
|
spawned.stderr.on 'data', (data) -> process.stderr.write data
|
|
|
|
spawned.on 'exit', (code) ->
|
|
|
|
console.log proc + ' exited with code ' + code
|
2014-06-23 14:29:37 -04:00
|
|
|
# unless code is null doesn't seem to work
|
|
|
|
# deferred.reject()
|
|
|
|
deferred.resolve code
|
2014-06-30 22:16:26 -04:00
|
|
|
spawned.on 'error', (code, error) ->
|
|
|
|
console.error proc + ' failed!'
|
2014-06-23 14:29:37 -04:00
|
|
|
deferred.reject()
|
2014-06-16 19:57:26 -04:00
|
|
|
deferred.promise()
|
|
|
|
|
|
|
|
removeDir = (path) ->
|
|
|
|
if fs.existsSync(path)
|
|
|
|
fs.readdirSync(path).forEach (file) ->
|
2014-06-30 22:16:26 -04:00
|
|
|
current = path + '/' + file
|
2014-06-16 19:57:26 -04:00
|
|
|
if fs.lstatSync(current).isDirectory() # recurse
|
|
|
|
removeDir current
|
|
|
|
else # delete file
|
|
|
|
fs.unlinkSync current
|
|
|
|
fs.rmdirSync path
|
|
|
|
|
2014-06-17 19:40:06 -04:00
|
|
|
resetDB = ->
|
|
|
|
deferred = Deferred()
|
2014-06-30 22:16:26 -04:00
|
|
|
console.log 'Dropping Database'
|
|
|
|
mongodrop = run 'mongo', ['coco', '--eval', 'db.dropDatabase()']
|
|
|
|
mongodrop.fail -> console.error 'Error occurred while dropping mongo. Make sure CoCo\'s MongoDB is running.'
|
2014-06-23 14:29:37 -04:00
|
|
|
mongodrop.done ->
|
2014-06-30 22:16:26 -04:00
|
|
|
console.log 'Restoring from dump.'
|
|
|
|
mongorestore = run 'mongorestore', [dbLocalPath]
|
2014-06-23 14:29:37 -04:00
|
|
|
mongorestore.always = deferred.resolve()
|
2014-06-17 19:40:06 -04:00
|
|
|
deferred.promise()
|
|
|
|
|
|
|
|
downloadDB = ->
|
|
|
|
deferred = Deferred()
|
2014-06-16 19:57:26 -04:00
|
|
|
#mongoose = require 'mongoose'
|
|
|
|
# TODO: What if mongo is not running?
|
2014-06-30 22:16:26 -04:00
|
|
|
console.log 'Downloading Database dump. It\'s big. This may take a while...'
|
2014-06-16 19:57:26 -04:00
|
|
|
request = http.get dbDump, (response)->
|
2014-06-17 19:51:25 -04:00
|
|
|
unzip = response.pipe(zlib.createGunzip()).pipe(tar.Extract(path: dbLocalPath))
|
2014-06-16 20:33:52 -04:00
|
|
|
# Log download
|
2014-06-16 19:57:26 -04:00
|
|
|
currentChunk = 0
|
|
|
|
cur = 0
|
|
|
|
len = parseInt(response.headers['content-length'], 10)
|
|
|
|
total = len / 1048576 #1048576 - bytes in 1Megabyte
|
|
|
|
response.on 'data', (chunk) ->
|
|
|
|
cur += chunk.length
|
2014-06-30 22:16:26 -04:00
|
|
|
console.log 'DB dump download received chunk ' + currentChunk++ + ', ' + (100.0 * cur / len).toFixed(2) + '% finished of ' + total.toFixed(0) + ' mb'
|
|
|
|
unzip.on('data', -> console.log 'Unpacking zip...')
|
|
|
|
unzip.on('error', (err) -> console.log 'An error occurred while downloading DB Dump: ' + err)
|
2014-06-16 20:33:52 -04:00
|
|
|
unzip.on 'end', ->
|
2014-06-30 22:16:26 -04:00
|
|
|
console.log 'Finished downloading.'
|
2014-06-17 19:40:06 -04:00
|
|
|
deferred.resolve()
|
|
|
|
deferred.promise()
|
2014-06-16 19:57:26 -04:00
|
|
|
|
2014-06-17 19:40:06 -04:00
|
|
|
installUpdates = ->
|
|
|
|
deferred = Deferred()
|
2014-06-30 22:16:26 -04:00
|
|
|
npm = if process.platform is 'win32' then 'npm.cmd' else 'npm'
|
2014-06-16 19:57:26 -04:00
|
|
|
npminstall = run npm, ['update']
|
|
|
|
npminstall.done ->
|
|
|
|
bowerinstall = run 'bower', ['update']
|
2014-06-17 19:40:06 -04:00
|
|
|
deferred.resolve()
|
|
|
|
deferred.promise()
|
|
|
|
|
|
|
|
cleanUpTmp = ->
|
|
|
|
removeDir dbLocalPath
|
|
|
|
|
|
|
|
unless '--skipupdates' in process.argv
|
|
|
|
installUpdates()
|
|
|
|
|
|
|
|
if '--resetdb' in process.argv
|
2014-06-23 14:29:37 -04:00
|
|
|
if '--dldb' in process.argv
|
|
|
|
downloadDB().done ->
|
2014-06-17 19:40:06 -04:00
|
|
|
resetDB().done ->
|
|
|
|
cleanUpTmp() if '--cleanup' in process.argv
|
2014-06-23 14:29:37 -04:00
|
|
|
else
|
|
|
|
resetDB().done ->
|
|
|
|
cleanUpTmp() if '--cleanup' in process.argv
|
|
|
|
else if '--dldb' in process.argv
|
|
|
|
downloadDB()
|
2014-06-16 19:57:26 -04:00
|
|
|
|
2014-06-17 19:40:06 -04:00
|
|
|
# TODO: Could advice to start SCOCODE.bat et al. here
|