Gruntfile.coffee 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. fs = require "fs"
  2. spawn = require("child_process").spawn
  3. exec = require("child_process").exec
  4. rimraf = require "rimraf"
  5. Path = require "path"
  6. semver = require "semver"
  7. knox = require "knox"
  8. crypto = require "crypto"
  9. async = require "async"
  10. settings = require("settings-sharelatex")
  11. SERVICES = require("./config/services")
  12. module.exports = (grunt) ->
  13. grunt.loadNpmTasks 'grunt-bunyan'
  14. grunt.loadNpmTasks 'grunt-execute'
  15. grunt.loadNpmTasks 'grunt-available-tasks'
  16. grunt.loadNpmTasks 'grunt-concurrent'
  17. grunt.loadNpmTasks "grunt-contrib-coffee"
  18. grunt.loadNpmTasks "grunt-shell"
  19. require('load-grunt-config')(grunt)
  20. execute = {}
  21. for service in SERVICES
  22. execute[service.name] =
  23. src: "#{service.name}/app.js"
  24. grunt.initConfig
  25. execute: execute
  26. concurrent:
  27. all:
  28. tasks: ("run:#{service.name}" for service in SERVICES)
  29. options:
  30. limit: SERVICES.length
  31. logConcurrentOutput: true
  32. coffee:
  33. migrate:
  34. expand: true,
  35. flatten: false,
  36. cwd: './',
  37. src: ['./migrations/*.coffee'],
  38. dest: './',
  39. ext: '.js'
  40. options:
  41. bare:true
  42. shell:
  43. migrate:
  44. command: "./node_modules/east/bin/east migrate --adapter east-mongo --url #{settings?.mongo?.url}"
  45. availabletasks:
  46. tasks:
  47. options:
  48. filter: 'exclude',
  49. tasks: [
  50. 'concurrent'
  51. 'execute'
  52. 'bunyan'
  53. 'availabletasks'
  54. ]
  55. groups:
  56. "Run tasks": [
  57. "run"
  58. "run:all"
  59. "default"
  60. ].concat ("run:#{service.name}" for service in SERVICES)
  61. "Misc": [
  62. "help"
  63. ]
  64. "Install tasks": ("install:#{service.name}" for service in SERVICES).concat(["install:all", "install"])
  65. "Update tasks": ("update:#{service.name}" for service in SERVICES).concat(["update:all", "update"])
  66. "Checks": ["check", "check:redis", "check:latexmk", "check:s3", "check:make"]
  67. for service in SERVICES
  68. do (service) ->
  69. grunt.registerTask "install:#{service.name}", "Download and set up the #{service.name} service", () ->
  70. done = @async()
  71. Helpers.installService(service, done)
  72. grunt.registerTask 'install:all', "Download and set up all ShareLaTeX services",
  73. ["check:make"].concat(
  74. ("install:#{service.name}" for service in SERVICES)
  75. )
  76. grunt.registerTask 'install', 'install:all'
  77. grunt.registerTask 'update:all', "Checkout and update all ShareLaTeX services",
  78. ["check:make"].concat(
  79. ("update:#{service.name}" for service in SERVICES)
  80. )
  81. grunt.registerTask 'update', 'update:all'
  82. grunt.registerTask 'run', "Run all of the sharelatex processes", ['concurrent:all']
  83. grunt.registerTask 'run:all', 'run'
  84. grunt.registerTask 'help', 'Display this help list', 'availabletasks'
  85. grunt.registerTask 'default', 'run'
  86. grunt.registerTask "check:redis", "Check that redis is installed and running", () ->
  87. Helpers.checkRedis @async()
  88. grunt.registerTask "check:latexmk", "Check that latexmk is installed", () ->
  89. Helpers.checkLatexmk @async()
  90. grunt.registerTask "check:s3", "Check that Amazon S3 credentials are configured", () ->
  91. Helpers.checkS3 @async()
  92. grunt.registerTask "check:fs", "Check that local filesystem options are configured", () ->
  93. Helpers.checkFS @async()
  94. grunt.registerTask "check:aspell", "Check that aspell is installed", () ->
  95. Helpers.checkAspell @async()
  96. grunt.registerTask "check:make", "Check that make is installed", () ->
  97. Helpers.checkMake @async()
  98. grunt.registerTask "check", "Check that you have the required dependencies installed", ["check:redis", "check:latexmk", "check:s3", "check:fs", "check:aspell"]
  99. grunt.registerTask 'migrate', "compile migrations and run them", ['coffee:migrate', 'shell:migrate']
  100. Helpers =
  101. installService: (service, callback = (error) ->) ->
  102. console.log "Installing #{service.name}"
  103. Helpers.cloneGitRepo service, (error) ->
  104. return callback(error) if error?
  105. Helpers.installNpmModules service, (error) ->
  106. return callback(error) if error?
  107. Helpers.rebuildNpmModules service, (error) ->
  108. return callback(error) if error?
  109. Helpers.runGruntInstall service, (error) ->
  110. return callback(error) if error?
  111. console.log "Finished installing #{service.name}"
  112. callback()
  113. cloneGitRepo: (service, callback = (error) ->) ->
  114. repo_src = service.repo
  115. dir = service.name
  116. if !fs.existsSync(dir)
  117. proc = spawn "git", [
  118. "clone",
  119. repo_src,
  120. dir
  121. ], stdio: "inherit"
  122. proc.on "close", () ->
  123. Helpers.checkoutVersion service, callback
  124. else
  125. console.log "#{dir} already installed, skipping."
  126. callback()
  127. checkoutVersion: (service, callback = (error) ->) ->
  128. dir = service.name
  129. proc = spawn "git", ["checkout", service.version], stdio: "inherit", cwd: dir
  130. proc.on "close", () ->
  131. callback()
  132. installNpmModules: (service, callback = (error) ->) ->
  133. dir = service.name
  134. proc = spawn "npm", ["install"], stdio: "inherit", cwd: dir
  135. proc.on "close", () ->
  136. callback()
  137. # work around for https://github.com/npm/npm/issues/5400
  138. # where binary modules are not built due to bug in npm
  139. rebuildNpmModules: (service, callback = (error) ->) ->
  140. dir = service.name
  141. proc = spawn "npm", ["rebuild"], stdio: "inherit", cwd: dir
  142. proc.on "close", () ->
  143. callback()
  144. runGruntInstall: (service, callback = (error) ->) ->
  145. dir = service.name
  146. proc = spawn "grunt", ["install"], stdio: "inherit", cwd: dir
  147. proc.on "close", () ->
  148. callback()
  149. checkRedis: (callback = (error) ->) ->
  150. grunt.log.write "Checking Redis is running... "
  151. exec "redis-cli info", (error, stdout, stderr) ->
  152. if error? and error.message.match("Could not connect")
  153. grunt.log.error "FAIL. Redis is not running"
  154. return callback(error)
  155. else if error?
  156. return callback(error)
  157. else
  158. m = stdout.match(/redis_version:(.*)/)
  159. if !m?
  160. grunt.log.error "FAIL."
  161. grunt.log.error "Unknown redis version"
  162. error = new Error("Unknown redis version")
  163. else
  164. version = m[1]
  165. if semver.gte(version, "2.6.12")
  166. grunt.log.writeln "OK."
  167. grunt.log.writeln "Running Redis version #{version}"
  168. else
  169. grunt.log.error "FAIL."
  170. grunt.log.error "Redis version is too old (#{version}). Must be 2.6.12 or greater."
  171. error = new Error("Redis version is too old (#{version}). Must be 2.6.12 or greater.")
  172. callback(error)
  173. checkLatexmk: (callback = (error) ->) ->
  174. grunt.log.write "Checking latexmk is installed... "
  175. exec "latexmk --version", (error, stdout, stderr) ->
  176. if error? and error.message.match("not found")
  177. grunt.log.error "FAIL."
  178. grunt.log.errorlns """
  179. Either latexmk is not installed or is not in your PATH.
  180. latexmk comes with TexLive 2013, and must be a version from 2013 or later.
  181. If you have already have TeXLive installed, then make sure it is
  182. included in your PATH (example for 64-bit linux):
  183. export PATH=$PATH:/usr/local/texlive/2014/bin/x86_64-linux/
  184. This is a not a fatal error, but compiling will not work without latexmk.
  185. """
  186. return callback(error)
  187. else if error?
  188. return callback(error)
  189. else
  190. m = stdout.match(/Version (.*)/)
  191. if !m?
  192. grunt.log.error "FAIL."
  193. grunt.log.error "Unknown latexmk version"
  194. error = new Error("Unknown latexmk version")
  195. else
  196. version = m[1]
  197. if semver.gte(version + ".0", "4.39.0")
  198. grunt.log.writeln "OK."
  199. grunt.log.writeln "Running latexmk version #{version}"
  200. else
  201. grunt.log.error "FAIL."
  202. grunt.log.errorlns """
  203. latexmk version is too old (#{version}). Must be 4.39 or greater.
  204. This is a not a fatal error, but compiling will not work without latexmk
  205. """
  206. error = new Error("latexmk is too old")
  207. callback(error)
  208. checkAspell: (callback = (error) ->) ->
  209. grunt.log.write "Checking aspell is installed... "
  210. exec "aspell dump dicts", (error, stdout, stderr) ->
  211. if error? and error.message.match("not found")
  212. grunt.log.error "FAIL."
  213. grunt.log.errorlns """
  214. Either aspell is not installed or is not in your PATH.
  215. On Ubuntu you can install aspell with:
  216. sudo apt-get install aspell
  217. Or on a mac:
  218. brew install aspell
  219. This is not a fatal error, but the spell-checker will not work without aspell
  220. """
  221. return callback(error)
  222. else if error?
  223. return callback(error)
  224. else
  225. grunt.log.writeln "OK."
  226. grunt.log.writeln "The following spell check dictionaries are available:"
  227. grunt.log.write stdout
  228. callback()
  229. callback(error)
  230. checkS3: (callback = (error) ->) ->
  231. Settings = require "settings-sharelatex"
  232. if Settings.filestore.backend==""
  233. grunt.log.writeln "No backend specified. Assuming Amazon S3"
  234. Settings.filestore.backend = "s3"
  235. if Settings.filestore.backend=="s3"
  236. grunt.log.write "Checking S3 credentials... "
  237. try
  238. client = knox.createClient({
  239. key: Settings.filestore.s3.key
  240. secret: Settings.filestore.s3.secret
  241. bucket: Settings.filestore.stores.user_files
  242. })
  243. catch e
  244. grunt.log.error "FAIL."
  245. grunt.log.errorlns """
  246. Please configure your Amazon S3 credentials in config/settings.development.coffee
  247. Amazon S3 (Simple Storage Service) is a cloud storage service provided by
  248. Amazon. ShareLaTeX uses S3 for storing binary files like images. You can
  249. sign up for an account and find out more at:
  250. http://aws.amazon.com/s3/
  251. """
  252. return callback()
  253. client.getFile "does-not-exist", (error, response) ->
  254. unless response? and response.statusCode == 404
  255. grunt.log.error "FAIL."
  256. grunt.log.errorlns """
  257. Could not connect to Amazon S3. Please check your credentials.
  258. """
  259. else
  260. grunt.log.writeln "OK."
  261. callback()
  262. else
  263. grunt.log.writeln "Filestore other than S3 configured. Not checking S3."
  264. callback()
  265. checkFS: (callback = (error) ->) ->
  266. Settings = require "settings-sharelatex"
  267. if Settings.filestore.backend=="fs"
  268. grunt.log.write "Checking FS configuration... "
  269. fs = require("fs")
  270. fs.exists Settings.filestore.stores.user_files, (exists) ->
  271. if exists
  272. grunt.log.writeln "OK."
  273. else
  274. grunt.log.error "FAIL."
  275. grunt.log.errorlns """
  276. Could not find directory "#{Settings.filestore.stores.user_files}".
  277. Please check your configuration.
  278. """
  279. callback()
  280. else
  281. grunt.log.writeln "Filestore other than FS configured. Not checking FS."
  282. callback()
  283. checkMake: (callback = (error) ->) ->
  284. grunt.log.write "Checking make is installed... "
  285. exec "make --version", (error, stdout, stderr) ->
  286. if error? and error.message.match("not found")
  287. grunt.log.error "FAIL."
  288. grunt.log.errorlns """
  289. Either make is not installed or is not in your path.
  290. On Ubuntu you can install make with:
  291. sudo apt-get install build-essential
  292. """
  293. return callback(error)
  294. else if error?
  295. return callback(error)
  296. else
  297. grunt.log.write "OK."
  298. return callback()