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