Gruntfile.coffee 13 KB

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