Gruntfile.coffee 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. grunt.registerTask "build_deb", "Build an installable .deb file from the current directory", () ->
  104. Helpers.buildDeb @async()
  105. Helpers =
  106. installService: (repo_src, dir, callback = (error) ->) ->
  107. Helpers.cloneGitRepo repo_src, dir, (error) ->
  108. return callback(error) if error?
  109. Helpers.installNpmModules dir, (error) ->
  110. return callback(error) if error?
  111. Helpers.runGruntInstall dir, (error) ->
  112. return callback(error) if error?
  113. callback()
  114. updateService: (dir, callback = (error) ->) ->
  115. Helpers.updateGitRepo dir, (error) ->
  116. return callback(error) if error?
  117. Helpers.installNpmModules dir, (error) ->
  118. return callback(error) if error?
  119. Helpers.runGruntInstall dir, (error) ->
  120. return callback(error) if error?
  121. callback()
  122. cloneGitRepo: (repo_src, dir, callback = (error) ->) ->
  123. if !fs.existsSync(dir)
  124. proc = spawn "git", ["clone", repo_src, dir], stdio: "inherit"
  125. proc.on "close", () ->
  126. callback()
  127. else
  128. console.log "#{dir} already installed, skipping."
  129. callback()
  130. updateGitRepo: (dir, callback = (error) ->) ->
  131. proc = spawn "git", ["checkout", "master"], cwd: dir, stdio: "inherit"
  132. proc.on "close", () ->
  133. proc = spawn "git", ["pull"], cwd: dir, stdio: "inherit"
  134. proc.on "close", () ->
  135. callback()
  136. installNpmModules: (dir, callback = (error) ->) ->
  137. proc = spawn "npm", ["install"], stdio: "inherit", cwd: dir
  138. proc.on "close", () ->
  139. callback()
  140. installConfig: (callback = (error) ->) ->
  141. if !fs.existsSync("config/settings.development.coffee")
  142. grunt.log.writeln "Copying example config into config/settings.development.coffee"
  143. exec "cp config/settings.development.coffee.example config/settings.development.coffee", (error, stdout, stderr) ->
  144. callback(error)
  145. else
  146. grunt.log.writeln "Config file already exists. Skipping."
  147. callback()
  148. runGruntInstall: (dir, callback = (error) ->) ->
  149. proc = spawn "grunt", ["install"], stdio: "inherit", cwd: dir
  150. proc.on "close", () ->
  151. callback()
  152. checkRedis: (callback = (error) ->) ->
  153. grunt.log.write "Checking Redis is running... "
  154. exec "redis-cli info", (error, stdout, stderr) ->
  155. if error? and error.message.match("Could not connect")
  156. grunt.log.error "FAIL. Redis is not running"
  157. return callback(error)
  158. else if error?
  159. return callback(error)
  160. else
  161. m = stdout.match(/redis_version:(.*)/)
  162. if !m?
  163. grunt.log.error "FAIL."
  164. grunt.log.error "Unknown redis version"
  165. error = new Error("Unknown redis version")
  166. else
  167. version = m[1]
  168. if semver.gte(version, "2.6.12")
  169. grunt.log.writeln "OK."
  170. grunt.log.writeln "Running Redis version #{version}"
  171. else
  172. grunt.log.error "FAIL."
  173. grunt.log.error "Redis version is too old (#{version}). Must be 2.6.12 or greater."
  174. error = new Error("Redis version is too old (#{version}). Must be 2.6.12 or greater.")
  175. callback(error)
  176. checkLatexmk: (callback = (error) ->) ->
  177. grunt.log.write "Checking latexmk is installed... "
  178. exec "latexmk --version", (error, stdout, stderr) ->
  179. if error? and error.message.match("command not found")
  180. grunt.log.error "FAIL."
  181. grunt.log.errorlns """
  182. Either latexmk is not installed or is not in your PATH.
  183. latexmk comes with TexLive 2013, and must be a version from 2013 or later.
  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. checkS3: (callback = (error) ->) ->
  209. Settings = require "settings-sharelatex"
  210. if Settings.filestore.backend==""
  211. grunt.log.writeln "No backend specified. Assuming Amazon S3"
  212. Settings.filestore.backend = "s3"
  213. if Settings.filestore.backend=="s3"
  214. grunt.log.write "Checking S3 credentials... "
  215. try
  216. client = knox.createClient({
  217. key: Settings.filestore.s3.key
  218. secret: Settings.filestore.s3.secret
  219. bucket: Settings.filestore.stores.user_files
  220. })
  221. catch e
  222. grunt.log.error "FAIL."
  223. grunt.log.errorlns """
  224. Please configure your Amazon S3 credentials in config/settings.development.coffee
  225. Amazon S3 (Simple Storage Service) is a cloud storage service provided by
  226. Amazon. ShareLaTeX uses S3 for storing binary files like images. You can
  227. sign up for an account and find out more at:
  228. http://aws.amazon.com/s3/
  229. """
  230. return callback()
  231. client.getFile "does-not-exist", (error, response) ->
  232. unless response? and response.statusCode == 404
  233. grunt.log.error "FAIL."
  234. grunt.log.errorlns """
  235. Could not connect to Amazon S3. Please check your credentials.
  236. """
  237. else
  238. grunt.log.write "OK."
  239. callback()
  240. else
  241. grunt.log.writeln "Filestore other than S3 configured. Not checking S3."
  242. callback()
  243. checkFS: (callback = (error) ->) ->
  244. Settings = require "settings-sharelatex"
  245. if Settings.filestore.backend=="fs"
  246. grunt.log.write "Checking FS configuration..."
  247. fs = require("fs")
  248. fs.exists Settings.filestore.stores.user_files, (exists) ->
  249. if exists
  250. grunt.log.write "OK."
  251. else
  252. grunt.log.error "FAIL."
  253. grunt.log.errorlns """
  254. Could not find directory "#{Settings.filestore.stores.user_files}".
  255. Please check your configuration.
  256. """
  257. else
  258. grunt.log.writeln "Filestore other than FS configured. Not checking FS."
  259. callback()
  260. checkMake: (callback = (error) ->) ->
  261. grunt.log.write "Checking make is installed... "
  262. exec "make --version", (error, stdout, stderr) ->
  263. if error? and error.message.match("command not found")
  264. grunt.log.error "FAIL."
  265. grunt.log.errorlns """
  266. Either make is not installed or is not in your path.
  267. On Ubuntu you can install make with:
  268. sudo apt-get install build-essential
  269. """
  270. return callback(error)
  271. else if error?
  272. return callback(error)
  273. else
  274. grunt.log.write "OK."
  275. return callback()
  276. buildDeb: (callback = (error) ->) ->
  277. # TODO: filestore uses local 'uploads' directory, not configurable in settings
  278. command = ["-s", "dir", "-t", "deb", "-n", "sharelatex", "-v", "0.0.1", "--verbose"]
  279. command.push(
  280. "--maintainer", "ShareLaTeX <team@sharelatex.com>"
  281. "--config-files", "/etc/sharelatex/settings.coffee",
  282. "--directories", "/var/data/sharelatex"
  283. "--directories", "/var/log/sharelatex"
  284. )
  285. command.push(
  286. "--depends", "redis-server > 2.6.12"
  287. "--depends", "mongodb-10gen > 2.4.0"
  288. "--depends", "nodejs > 0.10.0"
  289. )
  290. template = fs.readFileSync("package/upstart/sharelatex-template").toString()
  291. for service in SERVICES
  292. fs.writeFileSync "package/upstart/sharelatex-#{service.name}", template.replace(/SERVICE/g, service.name)
  293. command.push(
  294. "--deb-upstart", "package/upstart/sharelatex-#{service.name}"
  295. )
  296. after_install_script = """
  297. #!/bin/sh
  298. sudo adduser --system --group --home /var/www/sharelatex --no-create-home sharelatex
  299. mkdir -p /var/log/sharelatex
  300. chown sharelatex:sharelatex /var/log/sharelatex
  301. """
  302. for dir in ["user_files", "uploads", "compiles", "cache", "dump"]
  303. after_install_script += """
  304. mkdir -p /var/data/sharelatex/#{dir}
  305. chown sharelatex:sharelatex /var/data/sharelatex/#{dir}
  306. """
  307. for service in SERVICES
  308. after_install_script += "service sharelatex-#{service.name} restart\n"
  309. fs.writeFileSync "package/scripts/after_install.sh", after_install_script
  310. command.push("--after-install", "package/scripts/after_install.sh")
  311. command.push("--exclude", "'**/.git'")
  312. for path in ["filestore/user_files", "filestore/uploads", "clsi/cache", "clsi/compiles"]
  313. command.push "--exclude", path
  314. for service in SERVICES
  315. command.push "#{service.name}=/var/www/sharelatex/"
  316. command.push(
  317. "package/config/settings.coffee=/etc/sharelatex/settings.coffee"
  318. )
  319. console.log "fpm " + command.join(" ")
  320. proc = spawn "fpm", command, stdio: "inherit"
  321. proc.on "close", (code) ->
  322. if code != 0
  323. callback(new Error("exit code: #{code}"))
  324. else
  325. callback()