Gruntfile.coffee 15 KB

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