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