Gruntfile.coffee 14 KB

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