Gruntfile.coffee 10 KB

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