Gruntfile.coffee 15 KB

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