Gruntfile.coffee 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. Helpers =
  121. installService: (repo_src, dir, callback = (error) ->) ->
  122. Helpers.cloneGitRepo repo_src, dir, (error) ->
  123. return callback(error) if error?
  124. Helpers.installNpmModules dir, (error) ->
  125. return callback(error) if error?
  126. Helpers.runGruntInstall dir, (error) ->
  127. return callback(error) if error?
  128. callback()
  129. updateService: (dir, callback = (error) ->) ->
  130. Helpers.updateGitRepo dir, (error) ->
  131. return callback(error) if error?
  132. Helpers.installNpmModules dir, (error) ->
  133. return callback(error) if error?
  134. Helpers.runGruntInstall dir, (error) ->
  135. return callback(error) if error?
  136. callback()
  137. cloneGitRepo: (repo_src, dir, callback = (error) ->) ->
  138. if !fs.existsSync(dir)
  139. proc = spawn "git", ["clone", repo_src, dir], stdio: "inherit"
  140. proc.on "close", () ->
  141. callback()
  142. else
  143. console.log "#{dir} already installed, skipping."
  144. callback()
  145. updateGitRepo: (dir, callback = (error) ->) ->
  146. proc = spawn "git", ["checkout", "master"], cwd: dir, stdio: "inherit"
  147. proc.on "close", () ->
  148. proc = spawn "git", ["pull"], cwd: dir, stdio: "inherit"
  149. proc.on "close", () ->
  150. callback()
  151. installNpmModules: (dir, callback = (error) ->) ->
  152. proc = spawn "npm", ["install"], stdio: "inherit", cwd: dir
  153. proc.on "close", () ->
  154. callback()
  155. createDataDirs: (callback = (error) ->) ->
  156. DIRS = [
  157. "tmp/dumpFolder"
  158. "tmp/uploads"
  159. "data/user_files"
  160. "data/compiles"
  161. "data/cache"
  162. ]
  163. jobs = []
  164. for dir in DIRS
  165. do (dir) ->
  166. jobs.push (callback) ->
  167. path = Path.join(__dirname, dir)
  168. grunt.log.writeln "Ensuring '#{path}' exists"
  169. exec "mkdir -p #{path}", callback
  170. async.series jobs, callback
  171. installConfig: (callback = (error) ->) ->
  172. src = "config/settings.development.coffee.example"
  173. dest = "config/settings.development.coffee"
  174. if !fs.existsSync(dest)
  175. grunt.log.writeln "Creating config at #{dest}"
  176. config = fs.readFileSync(src).toString()
  177. config = config.replace /CRYPTO_RANDOM/g, () ->
  178. crypto.randomBytes(64).toString("hex")
  179. fs.writeFileSync dest, config
  180. callback()
  181. else
  182. grunt.log.writeln "Config file already exists. Skipping."
  183. callback()
  184. runGruntInstall: (dir, callback = (error) ->) ->
  185. proc = spawn "grunt", ["install"], stdio: "inherit", cwd: dir
  186. proc.on "close", () ->
  187. callback()
  188. checkRedis: (callback = (error) ->) ->
  189. grunt.log.write "Checking Redis is running... "
  190. exec "redis-cli info", (error, stdout, stderr) ->
  191. if error? and error.message.match("Could not connect")
  192. grunt.log.error "FAIL. Redis is not running"
  193. return callback(error)
  194. else if error?
  195. return callback(error)
  196. else
  197. m = stdout.match(/redis_version:(.*)/)
  198. if !m?
  199. grunt.log.error "FAIL."
  200. grunt.log.error "Unknown redis version"
  201. error = new Error("Unknown redis version")
  202. else
  203. version = m[1]
  204. if semver.gte(version, "2.6.12")
  205. grunt.log.writeln "OK."
  206. grunt.log.writeln "Running Redis version #{version}"
  207. else
  208. grunt.log.error "FAIL."
  209. grunt.log.error "Redis version is too old (#{version}). Must be 2.6.12 or greater."
  210. error = new Error("Redis version is too old (#{version}). Must be 2.6.12 or greater.")
  211. callback(error)
  212. checkLatexmk: (callback = (error) ->) ->
  213. grunt.log.write "Checking latexmk is installed... "
  214. exec "latexmk --version", (error, stdout, stderr) ->
  215. if error? and error.message.match("not found")
  216. grunt.log.error "FAIL."
  217. grunt.log.errorlns """
  218. Either latexmk is not installed or is not in your PATH.
  219. latexmk comes with TexLive 2013, and must be a version from 2013 or later.
  220. If you have already have TeXLive installed, then make sure it is
  221. included in your PATH (example for 64-bit linux):
  222. export PATH=$PATH:/usr/local/texlive/2014/bin/x86_64-linux/
  223. This is a not a fatal error, but compiling will not work without latexmk.
  224. """
  225. return callback(error)
  226. else if error?
  227. return callback(error)
  228. else
  229. m = stdout.match(/Version (.*)/)
  230. if !m?
  231. grunt.log.error "FAIL."
  232. grunt.log.error "Unknown latexmk version"
  233. error = new Error("Unknown latexmk version")
  234. else
  235. version = m[1]
  236. if semver.gte(version + ".0", "4.39.0")
  237. grunt.log.writeln "OK."
  238. grunt.log.writeln "Running latexmk version #{version}"
  239. else
  240. grunt.log.error "FAIL."
  241. grunt.log.errorlns """
  242. latexmk version is too old (#{version}). Must be 4.39 or greater.
  243. This is a not a fatal error, but compiling will not work without latexmk
  244. """
  245. error = new Error("latexmk is too old")
  246. callback(error)
  247. checkAspell: (callback = (error) ->) ->
  248. grunt.log.write "Checking aspell is installed... "
  249. exec "aspell dump dicts", (error, stdout, stderr) ->
  250. if error? and error.message.match("not found")
  251. grunt.log.error "FAIL."
  252. grunt.log.errorlns """
  253. Either aspell is not installed or is not in your PATH.
  254. On Ubuntu you can install aspell with:
  255. sudo apt-get install aspell
  256. Or on a mac:
  257. brew install aspell
  258. This is not a fatal error, but the spell-checker will not work without aspell
  259. """
  260. return callback(error)
  261. else if error?
  262. return callback(error)
  263. else
  264. grunt.log.writeln "OK."
  265. grunt.log.writeln "The following spell check dictionaries are available:"
  266. grunt.log.write stdout
  267. callback()
  268. callback(error)
  269. checkS3: (callback = (error) ->) ->
  270. Settings = require "settings-sharelatex"
  271. if Settings.filestore.backend==""
  272. grunt.log.writeln "No backend specified. Assuming Amazon S3"
  273. Settings.filestore.backend = "s3"
  274. if Settings.filestore.backend=="s3"
  275. grunt.log.write "Checking S3 credentials... "
  276. try
  277. client = knox.createClient({
  278. key: Settings.filestore.s3.key
  279. secret: Settings.filestore.s3.secret
  280. bucket: Settings.filestore.stores.user_files
  281. })
  282. catch e
  283. grunt.log.error "FAIL."
  284. grunt.log.errorlns """
  285. Please configure your Amazon S3 credentials in config/settings.development.coffee
  286. Amazon S3 (Simple Storage Service) is a cloud storage service provided by
  287. Amazon. ShareLaTeX uses S3 for storing binary files like images. You can
  288. sign up for an account and find out more at:
  289. http://aws.amazon.com/s3/
  290. """
  291. return callback()
  292. client.getFile "does-not-exist", (error, response) ->
  293. unless response? and response.statusCode == 404
  294. grunt.log.error "FAIL."
  295. grunt.log.errorlns """
  296. Could not connect to Amazon S3. Please check your credentials.
  297. """
  298. else
  299. grunt.log.writeln "OK."
  300. callback()
  301. else
  302. grunt.log.writeln "Filestore other than S3 configured. Not checking S3."
  303. callback()
  304. checkFS: (callback = (error) ->) ->
  305. Settings = require "settings-sharelatex"
  306. if Settings.filestore.backend=="fs"
  307. grunt.log.write "Checking FS configuration... "
  308. fs = require("fs")
  309. fs.exists Settings.filestore.stores.user_files, (exists) ->
  310. if exists
  311. grunt.log.writeln "OK."
  312. else
  313. grunt.log.error "FAIL."
  314. grunt.log.errorlns """
  315. Could not find directory "#{Settings.filestore.stores.user_files}".
  316. Please check your configuration.
  317. """
  318. callback()
  319. else
  320. grunt.log.writeln "Filestore other than FS configured. Not checking FS."
  321. callback()
  322. checkMake: (callback = (error) ->) ->
  323. grunt.log.write "Checking make is installed... "
  324. exec "make --version", (error, stdout, stderr) ->
  325. if error? and error.message.match("not found")
  326. grunt.log.error "FAIL."
  327. grunt.log.errorlns """
  328. Either make is not installed or is not in your path.
  329. On Ubuntu you can install make with:
  330. sudo apt-get install build-essential
  331. """
  332. return callback(error)
  333. else if error?
  334. return callback(error)
  335. else
  336. grunt.log.write "OK."
  337. return callback()
  338. buildDeb: (callback = (error) ->) ->
  339. # TODO: filestore uses local 'uploads' directory, not configurable in settings
  340. command = ["-s", "dir", "-t", "deb", "-n", "sharelatex", "-v", "0.0.1", "--verbose"]
  341. command.push(
  342. "--maintainer", "ShareLaTeX <team@sharelatex.com>"
  343. "--config-files", "/etc/sharelatex/settings.coffee",
  344. "--directories", "/var/data/sharelatex"
  345. "--directories", "/var/log/sharelatex"
  346. )
  347. command.push(
  348. "--depends", "redis-server > 2.6.12"
  349. "--depends", "mongodb-10gen > 2.4.0"
  350. "--depends", "nodejs > 0.10.0"
  351. )
  352. template = fs.readFileSync("package/upstart/sharelatex-template").toString()
  353. for service in SERVICES
  354. fs.writeFileSync "package/upstart/sharelatex-#{service.name}", template.replace(/SERVICE/g, service.name)
  355. command.push(
  356. "--deb-upstart", "package/upstart/sharelatex-#{service.name}"
  357. )
  358. after_install_script = """
  359. #!/bin/sh
  360. sudo adduser --system --group --home /var/www/sharelatex --no-create-home sharelatex
  361. mkdir -p /var/log/sharelatex
  362. chown sharelatex:sharelatex /var/log/sharelatex
  363. """
  364. for dir in ["user_files", "uploads", "compiles", "cache", "dump"]
  365. after_install_script += """
  366. mkdir -p /var/data/sharelatex/#{dir}
  367. chown sharelatex:sharelatex /var/data/sharelatex/#{dir}
  368. """
  369. for service in SERVICES
  370. after_install_script += "service sharelatex-#{service.name} restart\n"
  371. fs.writeFileSync "package/scripts/after_install.sh", after_install_script
  372. command.push("--after-install", "package/scripts/after_install.sh")
  373. command.push("--exclude", "'**/.git'")
  374. for path in ["filestore/user_files", "filestore/uploads", "clsi/cache", "clsi/compiles"]
  375. command.push "--exclude", path
  376. for service in SERVICES
  377. command.push "#{service.name}=/var/www/sharelatex/"
  378. command.push(
  379. "package/config/settings.coffee=/etc/sharelatex/settings.coffee"
  380. )
  381. console.log "fpm " + command.join(" ")
  382. proc = spawn "fpm", command, stdio: "inherit"
  383. proc.on "close", (code) ->
  384. if code != 0
  385. callback(new Error("exit code: #{code}"))
  386. else
  387. callback()