2016-10-19 06:39:40 -04:00
|
|
|
coffee = require("coffee-script")
|
2014-02-12 05:21:20 -05:00
|
|
|
fs = require "fs"
|
|
|
|
spawn = require("child_process").spawn
|
2014-02-22 07:08:03 -05:00
|
|
|
exec = require("child_process").exec
|
2014-02-12 07:11:58 -05:00
|
|
|
rimraf = require "rimraf"
|
2014-02-12 07:15:47 -05:00
|
|
|
Path = require "path"
|
2014-02-22 07:08:03 -05:00
|
|
|
semver = require "semver"
|
2014-02-22 10:02:21 -05:00
|
|
|
knox = require "knox"
|
2014-08-15 10:05:07 -04:00
|
|
|
crypto = require "crypto"
|
|
|
|
async = require "async"
|
2015-02-10 12:25:13 -05:00
|
|
|
settings = require("settings-sharelatex")
|
2016-09-20 12:07:52 -04:00
|
|
|
_ = require("underscore")
|
2015-02-10 12:25:13 -05:00
|
|
|
|
2014-02-12 05:21:20 -05:00
|
|
|
|
2016-04-20 20:57:19 -04:00
|
|
|
SERVICES = require("./config/services")
|
2014-02-12 05:21:20 -05:00
|
|
|
|
2014-02-08 09:44:47 -05:00
|
|
|
module.exports = (grunt) ->
|
|
|
|
grunt.loadNpmTasks 'grunt-bunyan'
|
|
|
|
grunt.loadNpmTasks 'grunt-execute'
|
|
|
|
grunt.loadNpmTasks 'grunt-available-tasks'
|
|
|
|
grunt.loadNpmTasks 'grunt-concurrent'
|
2015-01-28 12:50:39 -05:00
|
|
|
grunt.loadNpmTasks "grunt-contrib-coffee"
|
2015-02-10 12:25:13 -05:00
|
|
|
grunt.loadNpmTasks "grunt-shell"
|
2015-01-28 12:50:39 -05:00
|
|
|
|
2016-11-23 10:32:44 -05:00
|
|
|
grunt.task.loadTasks "./tasks"
|
2014-02-08 09:44:47 -05:00
|
|
|
|
2014-02-13 07:37:47 -05:00
|
|
|
execute = {}
|
2014-03-05 08:39:21 -05:00
|
|
|
for service in SERVICES
|
2014-02-13 07:37:47 -05:00
|
|
|
execute[service.name] =
|
|
|
|
src: "#{service.name}/app.js"
|
|
|
|
|
2014-02-08 09:44:47 -05:00
|
|
|
grunt.initConfig
|
2014-02-13 07:37:47 -05:00
|
|
|
execute: execute
|
2014-02-08 09:44:47 -05:00
|
|
|
|
|
|
|
concurrent:
|
|
|
|
all:
|
2014-02-13 07:37:47 -05:00
|
|
|
tasks: ("run:#{service.name}" for service in SERVICES)
|
2014-02-08 09:44:47 -05:00
|
|
|
options:
|
2014-02-13 07:37:47 -05:00
|
|
|
limit: SERVICES.length
|
2014-02-08 09:44:47 -05:00
|
|
|
logConcurrentOutput: true
|
2015-01-28 12:50:39 -05:00
|
|
|
coffee:
|
2016-06-09 11:59:57 -04:00
|
|
|
migrate:
|
2015-01-28 12:50:39 -05:00
|
|
|
expand: true,
|
|
|
|
flatten: false,
|
|
|
|
cwd: './',
|
|
|
|
src: ['./migrations/*.coffee'],
|
|
|
|
dest: './',
|
|
|
|
ext: '.js'
|
|
|
|
options:
|
|
|
|
bare:true
|
|
|
|
|
2015-02-10 12:25:13 -05:00
|
|
|
shell:
|
|
|
|
migrate:
|
2015-02-26 07:13:56 -05:00
|
|
|
command: "./node_modules/east/bin/east migrate --adapter east-mongo --url #{settings?.mongo?.url}"
|
2014-02-08 09:44:47 -05:00
|
|
|
|
|
|
|
availabletasks:
|
|
|
|
tasks:
|
|
|
|
options:
|
2014-02-12 05:21:20 -05:00
|
|
|
filter: 'exclude',
|
|
|
|
tasks: [
|
|
|
|
'concurrent'
|
|
|
|
'execute'
|
|
|
|
'bunyan'
|
|
|
|
'availabletasks'
|
|
|
|
]
|
|
|
|
groups:
|
|
|
|
"Run tasks": [
|
|
|
|
"run"
|
|
|
|
"run:all"
|
|
|
|
"default"
|
2014-02-13 07:37:47 -05:00
|
|
|
].concat ("run:#{service.name}" for service in SERVICES)
|
2014-02-12 05:21:20 -05:00
|
|
|
"Misc": [
|
|
|
|
"help"
|
|
|
|
]
|
2016-08-15 08:39:34 -04:00
|
|
|
"Install tasks": ("install:#{service.name}" for service in SERVICES).concat(["install:all", "install"])
|
2014-02-12 07:11:58 -05:00
|
|
|
"Update tasks": ("update:#{service.name}" for service in SERVICES).concat(["update:all", "update"])
|
2016-09-21 07:08:11 -04:00
|
|
|
"Checks": ["check", "check:redis", "check:latexmk", "check:s3", "check:make", "check:mongo"]
|
2014-02-08 09:44:47 -05:00
|
|
|
|
2014-03-05 08:39:21 -05:00
|
|
|
for service in SERVICES
|
2014-02-12 06:18:25 -05:00
|
|
|
do (service) ->
|
|
|
|
grunt.registerTask "install:#{service.name}", "Download and set up the #{service.name} service", () ->
|
|
|
|
done = @async()
|
2014-08-19 08:14:47 -04:00
|
|
|
Helpers.installService(service, done)
|
2014-02-12 06:18:25 -05:00
|
|
|
|
2016-06-09 11:59:57 -04:00
|
|
|
|
2016-08-05 07:51:37 -04:00
|
|
|
|
2014-02-23 06:45:08 -05:00
|
|
|
grunt.registerTask 'install:all', "Download and set up all ShareLaTeX services",
|
2016-09-21 07:08:11 -04:00
|
|
|
[].concat(
|
2014-02-23 06:53:46 -05:00
|
|
|
("install:#{service.name}" for service in SERVICES)
|
2016-08-15 08:39:34 -04:00
|
|
|
)
|
2016-06-23 12:13:12 -04:00
|
|
|
|
2014-02-12 06:24:13 -05:00
|
|
|
grunt.registerTask 'install', 'install:all'
|
2016-06-23 12:13:12 -04:00
|
|
|
|
2014-02-23 06:45:08 -05:00
|
|
|
grunt.registerTask 'update:all', "Checkout and update all ShareLaTeX services",
|
2014-02-23 06:53:46 -05:00
|
|
|
["check:make"].concat(
|
|
|
|
("update:#{service.name}" for service in SERVICES)
|
|
|
|
)
|
2014-02-12 06:24:13 -05:00
|
|
|
grunt.registerTask 'update', 'update:all'
|
2014-02-08 09:44:47 -05:00
|
|
|
grunt.registerTask 'run', "Run all of the sharelatex processes", ['concurrent:all']
|
2014-02-08 16:52:45 -05:00
|
|
|
grunt.registerTask 'run:all', 'run'
|
2014-02-08 09:44:47 -05:00
|
|
|
|
2014-02-12 06:24:13 -05:00
|
|
|
grunt.registerTask 'help', 'Display this help list', 'availabletasks'
|
2014-02-08 09:44:47 -05:00
|
|
|
grunt.registerTask 'default', 'run'
|
|
|
|
|
2014-02-22 07:08:03 -05:00
|
|
|
grunt.registerTask "check:redis", "Check that redis is installed and running", () ->
|
2016-09-20 12:07:52 -04:00
|
|
|
Helpers.checkRedisConnect @async()
|
2016-08-05 07:51:37 -04:00
|
|
|
|
2016-09-21 07:08:11 -04:00
|
|
|
grunt.registerTask "check:mongo", "Check that mongo is installed", () ->
|
2016-09-20 12:07:52 -04:00
|
|
|
Helpers.checkMongoConnect @async()
|
|
|
|
|
2016-09-21 07:08:11 -04:00
|
|
|
grunt.registerTask "check", "Check that you have the required dependencies installed", ["check:redis", "check:mongo", "check:make"]
|
2014-05-15 12:45:24 -04:00
|
|
|
|
2016-09-21 07:08:11 -04:00
|
|
|
grunt.registerTask "check:make", "Check that make is installed", () ->
|
|
|
|
Helpers.checkMake @async()
|
2015-01-28 12:50:39 -05:00
|
|
|
|
2016-11-23 10:32:44 -05:00
|
|
|
grunt.registerTask 'migrate', "compile migrations and run them", ["coffee:migrate", 'shell:migrate']
|
2015-01-28 12:50:39 -05:00
|
|
|
|
|
|
|
|
2014-02-22 07:08:03 -05:00
|
|
|
Helpers =
|
2014-08-19 08:14:47 -04:00
|
|
|
installService: (service, callback = (error) ->) ->
|
2016-08-05 12:35:46 -04:00
|
|
|
console.log "Installing #{service.name}"
|
2014-08-19 08:14:47 -04:00
|
|
|
Helpers.cloneGitRepo service, (error) ->
|
2014-02-12 05:21:20 -05:00
|
|
|
return callback(error) if error?
|
2014-08-19 08:14:47 -04:00
|
|
|
Helpers.installNpmModules service, (error) ->
|
2014-02-12 05:21:20 -05:00
|
|
|
return callback(error) if error?
|
2014-11-24 11:13:17 -05:00
|
|
|
Helpers.rebuildNpmModules service, (error) ->
|
2014-02-22 07:08:03 -05:00
|
|
|
return callback(error) if error?
|
2014-11-24 11:13:17 -05:00
|
|
|
Helpers.runGruntInstall service, (error) ->
|
|
|
|
return callback(error) if error?
|
2016-08-05 12:35:46 -04:00
|
|
|
console.log "Finished installing #{service.name}"
|
2014-11-24 11:13:17 -05:00
|
|
|
callback()
|
2014-02-12 05:21:20 -05:00
|
|
|
|
2014-02-22 07:08:03 -05:00
|
|
|
|
2014-08-19 08:14:47 -04:00
|
|
|
cloneGitRepo: (service, callback = (error) ->) ->
|
|
|
|
repo_src = service.repo
|
|
|
|
dir = service.name
|
2014-02-22 07:08:03 -05:00
|
|
|
if !fs.existsSync(dir)
|
2014-08-19 08:14:47 -04:00
|
|
|
proc = spawn "git", [
|
|
|
|
"clone",
|
|
|
|
repo_src,
|
|
|
|
dir
|
|
|
|
], stdio: "inherit"
|
2014-02-22 07:08:03 -05:00
|
|
|
proc.on "close", () ->
|
2016-06-23 12:13:12 -04:00
|
|
|
Helpers.checkoutVersion service, callback
|
2014-02-22 07:08:03 -05:00
|
|
|
else
|
|
|
|
console.log "#{dir} already installed, skipping."
|
|
|
|
callback()
|
|
|
|
|
2016-06-23 12:13:12 -04:00
|
|
|
checkoutVersion: (service, callback = (error) ->) ->
|
|
|
|
dir = service.name
|
2016-10-19 06:30:10 -04:00
|
|
|
grunt.log.write "checking out #{service.name} #{service.version}"
|
2016-06-23 12:13:12 -04:00
|
|
|
proc = spawn "git", ["checkout", service.version], stdio: "inherit", cwd: dir
|
|
|
|
proc.on "close", () ->
|
|
|
|
callback()
|
|
|
|
|
2014-11-24 11:13:17 -05:00
|
|
|
|
2014-08-19 08:14:47 -04:00
|
|
|
installNpmModules: (service, callback = (error) ->) ->
|
|
|
|
dir = service.name
|
2014-02-22 07:08:03 -05:00
|
|
|
proc = spawn "npm", ["install"], stdio: "inherit", cwd: dir
|
2014-02-12 05:21:20 -05:00
|
|
|
proc.on "close", () ->
|
|
|
|
callback()
|
2014-11-24 11:13:17 -05:00
|
|
|
|
|
|
|
# work around for https://github.com/npm/npm/issues/5400
|
|
|
|
# where binary modules are not built due to bug in npm
|
|
|
|
rebuildNpmModules: (service, callback = (error) ->) ->
|
|
|
|
dir = service.name
|
|
|
|
proc = spawn "npm", ["rebuild"], stdio: "inherit", cwd: dir
|
|
|
|
proc.on "close", () ->
|
|
|
|
callback()
|
|
|
|
|
2014-08-19 10:10:50 -04:00
|
|
|
runGruntInstall: (service, callback = (error) ->) ->
|
|
|
|
dir = service.name
|
2014-02-22 07:08:03 -05:00
|
|
|
proc = spawn "grunt", ["install"], stdio: "inherit", cwd: dir
|
2014-02-12 05:21:20 -05:00
|
|
|
proc.on "close", () ->
|
|
|
|
callback()
|
|
|
|
|
2016-09-21 07:08:11 -04:00
|
|
|
checkMake: (callback = (error) ->) ->
|
|
|
|
grunt.log.write "Checking make is installed... "
|
|
|
|
exec "make --version", (error, stdout, stderr) ->
|
|
|
|
if error? and error.message.match("not found")
|
|
|
|
grunt.log.error "FAIL."
|
|
|
|
grunt.log.errorlns """
|
|
|
|
Either make is not installed or is not in your path.
|
|
|
|
|
|
|
|
On Ubuntu you can install make with:
|
|
|
|
|
|
|
|
sudo apt-get install build-essential
|
|
|
|
|
|
|
|
"""
|
|
|
|
return callback(error)
|
|
|
|
else if error?
|
|
|
|
return callback(error)
|
|
|
|
else
|
|
|
|
grunt.log.write "OK."
|
|
|
|
return callback()
|
2016-09-20 12:07:52 -04:00
|
|
|
checkMongoConnect: (callback = (error) ->) ->
|
|
|
|
grunt.log.write "Checking can connect to mongo"
|
|
|
|
mongojs = require("mongojs")
|
|
|
|
db = mongojs.connect(settings.mongo.url, ["tags"])
|
|
|
|
db.runCommand { ping: 1 }, (err, res) ->
|
|
|
|
if !err and res.ok
|
|
|
|
grunt.log.write "OK."
|
|
|
|
return callback()
|
|
|
|
db.on 'error', (err)->
|
|
|
|
err = "Can not connect to mongodb"
|
|
|
|
grunt.log.error "FAIL."
|
|
|
|
grunt.log.errorlns """
|
2016-09-22 07:30:43 -04:00
|
|
|
!!!!!!!!!!!!!! MONGO ERROR !!!!!!!!!!!!!!
|
|
|
|
|
2016-09-20 12:07:52 -04:00
|
|
|
ShareLaTeX can not talk to the mongdb instance
|
|
|
|
|
|
|
|
Check the mongodb instance is running and accessible on env var SHARELATEX_MONGO_URL
|
2016-09-22 07:30:43 -04:00
|
|
|
|
|
|
|
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
2016-09-20 12:07:52 -04:00
|
|
|
"""
|
2016-09-21 07:43:58 -04:00
|
|
|
throw new Error("Can not connect to Mongodb")
|
2016-09-20 12:07:52 -04:00
|
|
|
return callback(err)
|
|
|
|
|
|
|
|
checkRedisConnect: (callback = (error) ->) ->
|
|
|
|
grunt.log.write "Checking can connect to redis\n"
|
|
|
|
rclient = require("redis").createClient(settings.redis.web)
|
|
|
|
|
|
|
|
rclient.ping (err, res) ->
|
|
|
|
if !err?
|
|
|
|
grunt.log.write "OK."
|
|
|
|
else
|
2016-09-21 07:43:58 -04:00
|
|
|
throw new Error("Can not connect to redis")
|
2016-09-20 12:07:52 -04:00
|
|
|
return callback()
|
|
|
|
errorHandler = _.once (err)->
|
|
|
|
err = "Can not connect to redis"
|
|
|
|
grunt.log.error "FAIL."
|
|
|
|
grunt.log.errorlns """
|
2016-09-22 07:30:43 -04:00
|
|
|
!!!!!!!!!!!!!! REDIS ERROR !!!!!!!!!!!!!!
|
|
|
|
|
2016-09-20 12:07:52 -04:00
|
|
|
ShareLaTeX can not talk to the redis instance
|
|
|
|
|
2016-11-03 11:13:02 -04:00
|
|
|
Check the redis instance is running and accessible on env var SHARELATEX_REDIS_HOST
|
2016-09-20 12:07:52 -04:00
|
|
|
|
2016-09-22 07:30:43 -04:00
|
|
|
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
2016-09-20 12:07:52 -04:00
|
|
|
"""
|
2016-09-21 07:43:58 -04:00
|
|
|
throw new Error("Can not connect to redis")
|
2016-09-20 12:07:52 -04:00
|
|
|
return callback(err)
|
|
|
|
rclient.on 'error', errorHandler
|
2016-10-19 06:39:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
|