Gruntfile.coffee 10 KB

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