Gruntfile.coffee 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. coffee = require("coffee-script")
  2. fs = require "fs"
  3. spawn = require("child_process").spawn
  4. exec = require("child_process").exec
  5. rimraf = require "rimraf"
  6. Path = require "path"
  7. semver = require "semver"
  8. knox = require "knox"
  9. crypto = require "crypto"
  10. async = require "async"
  11. settings = require("settings-sharelatex")
  12. _ = require("underscore")
  13. SERVICES = require("./config/services")
  14. module.exports = (grunt) ->
  15. grunt.loadNpmTasks 'grunt-bunyan'
  16. grunt.loadNpmTasks 'grunt-execute'
  17. grunt.loadNpmTasks 'grunt-available-tasks'
  18. grunt.loadNpmTasks 'grunt-concurrent'
  19. grunt.loadNpmTasks "grunt-contrib-coffee"
  20. grunt.loadNpmTasks "grunt-shell"
  21. execute = {}
  22. for service in SERVICES
  23. execute[service.name] =
  24. src: "#{service.name}/app.js"
  25. grunt.initConfig
  26. execute: execute
  27. concurrent:
  28. all:
  29. tasks: ("run:#{service.name}" for service in SERVICES)
  30. options:
  31. limit: SERVICES.length
  32. logConcurrentOutput: true
  33. coffee:
  34. migrate:
  35. expand: true,
  36. flatten: false,
  37. cwd: './',
  38. src: ['./migrations/*.coffee'],
  39. dest: './',
  40. ext: '.js'
  41. options:
  42. bare:true
  43. shell:
  44. migrate:
  45. command: "./node_modules/east/bin/east migrate --adapter east-mongo --url #{settings?.mongo?.url}"
  46. availabletasks:
  47. tasks:
  48. options:
  49. filter: 'exclude',
  50. tasks: [
  51. 'concurrent'
  52. 'execute'
  53. 'bunyan'
  54. 'availabletasks'
  55. ]
  56. groups:
  57. "Run tasks": [
  58. "run"
  59. "run:all"
  60. "default"
  61. ].concat ("run:#{service.name}" for service in SERVICES)
  62. "Misc": [
  63. "help"
  64. ]
  65. "Install tasks": ("install:#{service.name}" for service in SERVICES).concat(["install:all", "install"])
  66. "Update tasks": ("update:#{service.name}" for service in SERVICES).concat(["update:all", "update"])
  67. "Checks": ["check", "check:redis", "check:latexmk", "check:s3", "check:make", "check:mongo"]
  68. for service in SERVICES
  69. do (service) ->
  70. grunt.registerTask "install:#{service.name}", "Download and set up the #{service.name} service", () ->
  71. done = @async()
  72. Helpers.installService(service, done)
  73. grunt.registerTask 'install:all', "Download and set up all ShareLaTeX services",
  74. [].concat(
  75. ("install:#{service.name}" for service in SERVICES)
  76. )
  77. grunt.registerTask 'install', 'install:all'
  78. grunt.registerTask 'update:all', "Checkout and update all ShareLaTeX services",
  79. ["check:make"].concat(
  80. ("update:#{service.name}" for service in SERVICES)
  81. )
  82. grunt.registerTask 'update', 'update:all'
  83. grunt.registerTask 'run', "Run all of the sharelatex processes", ['concurrent:all']
  84. grunt.registerTask 'run:all', 'run'
  85. grunt.registerTask 'help', 'Display this help list', 'availabletasks'
  86. grunt.registerTask 'default', 'run'
  87. grunt.registerTask "check:redis", "Check that redis is installed and running", () ->
  88. Helpers.checkRedisConnect @async()
  89. grunt.registerTask "check:mongo", "Check that mongo is installed", () ->
  90. Helpers.checkMongoConnect @async()
  91. grunt.registerTask "check", "Check that you have the required dependencies installed", ["check:redis", "check:mongo", "check:make"]
  92. grunt.registerTask "check:make", "Check that make is installed", () ->
  93. Helpers.checkMake @async()
  94. grunt.registerTask 'migrate', "compile migrations and run them", ['coffee:migrate', 'shell:migrate']
  95. Helpers =
  96. installService: (service, callback = (error) ->) ->
  97. console.log "Installing #{service.name}"
  98. Helpers.cloneGitRepo service, (error) ->
  99. return callback(error) if error?
  100. Helpers.installNpmModules service, (error) ->
  101. return callback(error) if error?
  102. Helpers.rebuildNpmModules service, (error) ->
  103. return callback(error) if error?
  104. Helpers.runGruntInstall service, (error) ->
  105. return callback(error) if error?
  106. console.log "Finished installing #{service.name}"
  107. callback()
  108. cloneGitRepo: (service, callback = (error) ->) ->
  109. repo_src = service.repo
  110. dir = service.name
  111. if !fs.existsSync(dir)
  112. proc = spawn "git", [
  113. "clone",
  114. repo_src,
  115. dir
  116. ], stdio: "inherit"
  117. proc.on "close", () ->
  118. Helpers.checkoutVersion service, callback
  119. else
  120. console.log "#{dir} already installed, skipping."
  121. callback()
  122. checkoutVersion: (service, callback = (error) ->) ->
  123. dir = service.name
  124. grunt.log.write "checking out #{service.name} #{service.version}"
  125. proc = spawn "git", ["checkout", service.version], stdio: "inherit", cwd: dir
  126. proc.on "close", () ->
  127. callback()
  128. installNpmModules: (service, callback = (error) ->) ->
  129. dir = service.name
  130. proc = spawn "npm", ["install"], stdio: "inherit", cwd: dir
  131. proc.on "close", () ->
  132. callback()
  133. # work around for https://github.com/npm/npm/issues/5400
  134. # where binary modules are not built due to bug in npm
  135. rebuildNpmModules: (service, callback = (error) ->) ->
  136. dir = service.name
  137. proc = spawn "npm", ["rebuild"], stdio: "inherit", cwd: dir
  138. proc.on "close", () ->
  139. callback()
  140. runGruntInstall: (service, callback = (error) ->) ->
  141. dir = service.name
  142. proc = spawn "grunt", ["install"], stdio: "inherit", cwd: dir
  143. proc.on "close", () ->
  144. callback()
  145. checkMake: (callback = (error) ->) ->
  146. grunt.log.write "Checking make is installed... "
  147. exec "make --version", (error, stdout, stderr) ->
  148. if error? and error.message.match("not found")
  149. grunt.log.error "FAIL."
  150. grunt.log.errorlns """
  151. Either make is not installed or is not in your path.
  152. On Ubuntu you can install make with:
  153. sudo apt-get install build-essential
  154. """
  155. return callback(error)
  156. else if error?
  157. return callback(error)
  158. else
  159. grunt.log.write "OK."
  160. return callback()
  161. checkMongoConnect: (callback = (error) ->) ->
  162. grunt.log.write "Checking can connect to mongo"
  163. mongojs = require("mongojs")
  164. db = mongojs.connect(settings.mongo.url, ["tags"])
  165. db.runCommand { ping: 1 }, (err, res) ->
  166. if !err and res.ok
  167. grunt.log.write "OK."
  168. return callback()
  169. db.on 'error', (err)->
  170. err = "Can not connect to mongodb"
  171. grunt.log.error "FAIL."
  172. grunt.log.errorlns """
  173. !!!!!!!!!!!!!! MONGO ERROR !!!!!!!!!!!!!!
  174. ShareLaTeX can not talk to the mongdb instance
  175. Check the mongodb instance is running and accessible on env var SHARELATEX_MONGO_URL
  176. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  177. """
  178. throw new Error("Can not connect to Mongodb")
  179. return callback(err)
  180. checkRedisConnect: (callback = (error) ->) ->
  181. grunt.log.write "Checking can connect to redis\n"
  182. rclient = require("redis").createClient(settings.redis.web)
  183. rclient.ping (err, res) ->
  184. if !err?
  185. grunt.log.write "OK."
  186. else
  187. throw new Error("Can not connect to redis")
  188. return callback()
  189. errorHandler = _.once (err)->
  190. err = "Can not connect to redis"
  191. grunt.log.error "FAIL."
  192. grunt.log.errorlns """
  193. !!!!!!!!!!!!!! REDIS ERROR !!!!!!!!!!!!!!
  194. ShareLaTeX can not talk to the redis instance
  195. Check the redis instance is running and accessible on env var SHARELATEX_REDIS_HOST
  196. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  197. """
  198. throw new Error("Can not connect to redis")
  199. return callback(err)
  200. rclient.on 'error', errorHandler
  201. require('load-grunt-config')(grunt)