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