Gruntfile.coffee 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. name: "chat"
  28. repo: "https://github.com/sharelatex/chat-sharelatex.git"
  29. }, {
  30. name: "tags"
  31. repo: "https://github.com/sharelatex/tags-sharelatex.git"
  32. }]
  33. module.exports = (grunt) ->
  34. grunt.loadNpmTasks 'grunt-bunyan'
  35. grunt.loadNpmTasks 'grunt-execute'
  36. grunt.loadNpmTasks 'grunt-available-tasks'
  37. grunt.loadNpmTasks 'grunt-concurrent'
  38. execute = {}
  39. for service in SERVICES
  40. execute[service.name] =
  41. src: "#{service.name}/app.js"
  42. grunt.initConfig
  43. execute: execute
  44. concurrent:
  45. all:
  46. tasks: ("run:#{service.name}" for service in SERVICES)
  47. options:
  48. limit: SERVICES.length
  49. logConcurrentOutput: true
  50. availabletasks:
  51. tasks:
  52. options:
  53. filter: 'exclude',
  54. tasks: [
  55. 'concurrent'
  56. 'execute'
  57. 'bunyan'
  58. 'availabletasks'
  59. ]
  60. groups:
  61. "Run tasks": [
  62. "run"
  63. "run:all"
  64. "default"
  65. ].concat ("run:#{service.name}" for service in SERVICES)
  66. "Misc": [
  67. "help"
  68. ]
  69. "Install tasks": ("install:#{service.name}" for service in SERVICES).concat(["install:all", "install", "install:config"])
  70. "Update tasks": ("update:#{service.name}" for service in SERVICES).concat(["update:all", "update"])
  71. "Config tasks": ["install:config"]
  72. "Checks": ["check", "check:redis", "check:latexmk", "check:s3", "check:make"]
  73. for service in SERVICES
  74. do (service) ->
  75. grunt.registerTask "install:#{service.name}", "Download and set up the #{service.name} service", () ->
  76. done = @async()
  77. Helpers.installService(service.repo, service.name, done)
  78. grunt.registerTask "update:#{service.name}", "Checkout and update the #{service.name} service", () ->
  79. done = @async()
  80. Helpers.updateService(service.name, done)
  81. grunt.registerTask "run:#{service.name}", "Run the ShareLaTeX #{service.name} service", ["bunyan", "execute:#{service.name}"]
  82. grunt.registerTask 'install:config', "Copy the example config into the real config", () ->
  83. Helpers.installConfig @async()
  84. grunt.registerTask 'install:all', "Download and set up all ShareLaTeX services",
  85. ["check:make"].concat(
  86. ("install:#{service.name}" for service in SERVICES)
  87. ).concat(["install:config"])
  88. grunt.registerTask 'install', 'install:all'
  89. grunt.registerTask 'update:all', "Checkout and update all ShareLaTeX services",
  90. ["check:make"].concat(
  91. ("update:#{service.name}" for service in SERVICES)
  92. )
  93. grunt.registerTask 'update', 'update:all'
  94. grunt.registerTask 'run', "Run all of the sharelatex processes", ['concurrent:all']
  95. grunt.registerTask 'run:all', 'run'
  96. grunt.registerTask 'help', 'Display this help list', 'availabletasks'
  97. grunt.registerTask 'default', 'run'
  98. grunt.registerTask "check:redis", "Check that redis is installed and running", () ->
  99. Helpers.checkRedis @async()
  100. grunt.registerTask "check:latexmk", "Check that latexmk is installed", () ->
  101. Helpers.checkLatexmk @async()
  102. grunt.registerTask "check:s3", "Check that Amazon S3 credentials are configured", () ->
  103. Helpers.checkS3 @async()
  104. grunt.registerTask "check:fs", "Check that local filesystem options are configured", () ->
  105. Helpers.checkFS @async()
  106. grunt.registerTask "check:make", "Check that make is installed", () ->
  107. Helpers.checkMake @async()
  108. grunt.registerTask "check", "Check that you have the required dependencies installed", ["check:redis", "check:latexmk", "check:s3", "check:fs"]
  109. grunt.registerTask "build_deb", "Build an installable .deb file from the current directory", () ->
  110. Helpers.buildDeb @async()
  111. Helpers =
  112. installService: (repo_src, dir, callback = (error) ->) ->
  113. Helpers.cloneGitRepo repo_src, 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. updateService: (dir, callback = (error) ->) ->
  121. Helpers.updateGitRepo dir, (error) ->
  122. return callback(error) if error?
  123. Helpers.installNpmModules dir, (error) ->
  124. return callback(error) if error?
  125. Helpers.runGruntInstall dir, (error) ->
  126. return callback(error) if error?
  127. callback()
  128. cloneGitRepo: (repo_src, dir, callback = (error) ->) ->
  129. if !fs.existsSync(dir)
  130. proc = spawn "git", ["clone", repo_src, dir], stdio: "inherit"
  131. proc.on "close", () ->
  132. callback()
  133. else
  134. console.log "#{dir} already installed, skipping."
  135. callback()
  136. updateGitRepo: (dir, callback = (error) ->) ->
  137. proc = spawn "git", ["checkout", "master"], cwd: dir, stdio: "inherit"
  138. proc.on "close", () ->
  139. proc = spawn "git", ["pull"], cwd: dir, stdio: "inherit"
  140. proc.on "close", () ->
  141. callback()
  142. installNpmModules: (dir, callback = (error) ->) ->
  143. proc = spawn "npm", ["install"], stdio: "inherit", cwd: dir
  144. proc.on "close", () ->
  145. callback()
  146. installConfig: (callback = (error) ->) ->
  147. if !fs.existsSync("config/settings.development.coffee")
  148. grunt.log.writeln "Copying example config into config/settings.development.coffee"
  149. exec "cp config/settings.development.coffee.example config/settings.development.coffee", (error, stdout, stderr) ->
  150. callback(error)
  151. else
  152. grunt.log.writeln "Config file already exists. Skipping."
  153. callback()
  154. runGruntInstall: (dir, callback = (error) ->) ->
  155. proc = spawn "grunt", ["install"], stdio: "inherit", cwd: dir
  156. proc.on "close", () ->
  157. callback()
  158. checkRedis: (callback = (error) ->) ->
  159. grunt.log.write "Checking Redis is running... "
  160. exec "redis-cli info", (error, stdout, stderr) ->
  161. if error? and error.message.match("Could not connect")
  162. grunt.log.error "FAIL. Redis is not running"
  163. return callback(error)
  164. else if error?
  165. return callback(error)
  166. else
  167. m = stdout.match(/redis_version:(.*)/)
  168. if !m?
  169. grunt.log.error "FAIL."
  170. grunt.log.error "Unknown redis version"
  171. error = new Error("Unknown redis version")
  172. else
  173. version = m[1]
  174. if semver.gte(version, "2.6.12")
  175. grunt.log.writeln "OK."
  176. grunt.log.writeln "Running Redis version #{version}"
  177. else
  178. grunt.log.error "FAIL."
  179. grunt.log.error "Redis version is too old (#{version}). Must be 2.6.12 or greater."
  180. error = new Error("Redis version is too old (#{version}). Must be 2.6.12 or greater.")
  181. callback(error)
  182. checkLatexmk: (callback = (error) ->) ->
  183. grunt.log.write "Checking latexmk is installed... "
  184. exec "latexmk --version", (error, stdout, stderr) ->
  185. if error? and error.message.match("command not found")
  186. grunt.log.error "FAIL."
  187. grunt.log.errorlns """
  188. Either latexmk is not installed or is not in your PATH.
  189. latexmk comes with TexLive 2013, and must be a version from 2013 or later.
  190. This is a not a fatal error, but compiling will not work without latexmk
  191. """
  192. return callback(error)
  193. else if error?
  194. return callback(error)
  195. else
  196. m = stdout.match(/Version (.*)/)
  197. if !m?
  198. grunt.log.error "FAIL."
  199. grunt.log.error "Unknown latexmk version"
  200. error = new Error("Unknown latexmk version")
  201. else
  202. version = m[1]
  203. if semver.gte(version + ".0", "4.39.0")
  204. grunt.log.writeln "OK."
  205. grunt.log.writeln "Running latexmk version #{version}"
  206. else
  207. grunt.log.error "FAIL."
  208. grunt.log.errorlns """
  209. latexmk version is too old (#{version}). Must be 4.39 or greater.
  210. This is a not a fatal error, but compiling will not work without latexmk
  211. """
  212. error = new Error("latexmk is too old")
  213. callback(error)
  214. checkS3: (callback = (error) ->) ->
  215. Settings = require "settings-sharelatex"
  216. if Settings.filestore.backend==""
  217. grunt.log.writeln "No backend specified. Assuming Amazon S3"
  218. Settings.filestore.backend = "s3"
  219. if Settings.filestore.backend=="s3"
  220. grunt.log.write "Checking S3 credentials... "
  221. try
  222. client = knox.createClient({
  223. key: Settings.filestore.s3.key
  224. secret: Settings.filestore.s3.secret
  225. bucket: Settings.filestore.stores.user_files
  226. })
  227. catch e
  228. grunt.log.error "FAIL."
  229. grunt.log.errorlns """
  230. Please configure your Amazon S3 credentials in config/settings.development.coffee
  231. Amazon S3 (Simple Storage Service) is a cloud storage service provided by
  232. Amazon. ShareLaTeX uses S3 for storing binary files like images. You can
  233. sign up for an account and find out more at:
  234. http://aws.amazon.com/s3/
  235. """
  236. return callback()
  237. client.getFile "does-not-exist", (error, response) ->
  238. unless response? and response.statusCode == 404
  239. grunt.log.error "FAIL."
  240. grunt.log.errorlns """
  241. Could not connect to Amazon S3. Please check your credentials.
  242. """
  243. else
  244. grunt.log.write "OK."
  245. callback()
  246. else
  247. grunt.log.writeln "Filestore other than S3 configured. Not checking S3."
  248. callback()
  249. checkFS: (callback = (error) ->) ->
  250. Settings = require "settings-sharelatex"
  251. if Settings.filestore.backend=="fs"
  252. grunt.log.write "Checking FS configuration..."
  253. fs = require("fs")
  254. fs.exists Settings.filestore.stores.user_files, (exists) ->
  255. if exists
  256. grunt.log.write "OK."
  257. else
  258. grunt.log.error "FAIL."
  259. grunt.log.errorlns """
  260. Could not find directory "#{Settings.filestore.stores.user_files}".
  261. Please check your configuration.
  262. """
  263. else
  264. grunt.log.writeln "Filestore other than FS configured. Not checking FS."
  265. callback()
  266. checkMake: (callback = (error) ->) ->
  267. grunt.log.write "Checking make is installed... "
  268. exec "make --version", (error, stdout, stderr) ->
  269. if error? and error.message.match("command not found")
  270. grunt.log.error "FAIL."
  271. grunt.log.errorlns """
  272. Either make is not installed or is not in your path.
  273. On Ubuntu you can install make with:
  274. sudo apt-get install build-essential
  275. """
  276. return callback(error)
  277. else if error?
  278. return callback(error)
  279. else
  280. grunt.log.write "OK."
  281. return callback()
  282. buildDeb: (callback = (error) ->) ->
  283. # TODO: filestore uses local 'uploads' directory, not configurable in settings
  284. command = ["-s", "dir", "-t", "deb", "-n", "sharelatex", "-v", "0.0.1", "--verbose"]
  285. command.push(
  286. "--maintainer", "ShareLaTeX <team@sharelatex.com>"
  287. "--config-files", "/etc/sharelatex/settings.coffee",
  288. "--directories", "/var/data/sharelatex"
  289. "--directories", "/var/log/sharelatex"
  290. )
  291. command.push(
  292. "--depends", "redis-server > 2.6.12"
  293. "--depends", "mongodb-10gen > 2.4.0"
  294. "--depends", "nodejs > 0.10.0"
  295. )
  296. template = fs.readFileSync("package/upstart/sharelatex-template").toString()
  297. for service in SERVICES
  298. fs.writeFileSync "package/upstart/sharelatex-#{service.name}", template.replace(/SERVICE/g, service.name)
  299. command.push(
  300. "--deb-upstart", "package/upstart/sharelatex-#{service.name}"
  301. )
  302. after_install_script = """
  303. #!/bin/sh
  304. sudo adduser --system --group --home /var/www/sharelatex --no-create-home sharelatex
  305. mkdir -p /var/log/sharelatex
  306. chown sharelatex:sharelatex /var/log/sharelatex
  307. """
  308. for dir in ["user_files", "uploads", "compiles", "cache", "dump"]
  309. after_install_script += """
  310. mkdir -p /var/data/sharelatex/#{dir}
  311. chown sharelatex:sharelatex /var/data/sharelatex/#{dir}
  312. """
  313. for service in SERVICES
  314. after_install_script += "service sharelatex-#{service.name} restart\n"
  315. fs.writeFileSync "package/scripts/after_install.sh", after_install_script
  316. command.push("--after-install", "package/scripts/after_install.sh")
  317. command.push("--exclude", "'**/.git'")
  318. for path in ["filestore/user_files", "filestore/uploads", "clsi/cache", "clsi/compiles"]
  319. command.push "--exclude", path
  320. for service in SERVICES
  321. command.push "#{service.name}=/var/www/sharelatex/"
  322. command.push(
  323. "package/config/settings.coffee=/etc/sharelatex/settings.coffee"
  324. )
  325. console.log "fpm " + command.join(" ")
  326. proc = spawn "fpm", command, stdio: "inherit"
  327. proc.on "close", (code) ->
  328. if code != 0
  329. callback(new Error("exit code: #{code}"))
  330. else
  331. callback()