diff --git a/services/web/app/coffee/Features/Compile/ClsiCookieManager.coffee b/services/web/app/coffee/Features/Compile/ClsiCookieManager.coffee index 66b407f6f9..2bee2ba5e6 100644 --- a/services/web/app/coffee/Features/Compile/ClsiCookieManager.coffee +++ b/services/web/app/coffee/Features/Compile/ClsiCookieManager.coffee @@ -7,31 +7,35 @@ if Settings.redis.clsi_cookie_secondary? Cookie = require('cookie') logger = require "logger-sharelatex" -buildKey = (project_id)-> - return "clsiserver:#{project_id}" clsiCookiesEnabled = Settings.clsiCookie?.key? and Settings.clsiCookie.key.length != 0 -module.exports = ClsiCookieManager = +module.exports = (backendGroup)-> + + buildKey : (project_id)-> + if backendGroup? + return "clsiserver:#{backendGroup}:#{project_id}" + else + return "clsiserver:#{project_id}" _getServerId : (project_id, callback = (err, serverId)->)-> - rclient.get buildKey(project_id), (err, serverId)-> + rclient.get @buildKey(project_id), (err, serverId)=> if err? return callback(err) if !serverId? or serverId == "" - return ClsiCookieManager._populateServerIdViaRequest project_id, callback + return @_populateServerIdViaRequest project_id, callback else return callback(null, serverId) _populateServerIdViaRequest :(project_id, callback = (err, serverId)->)-> url = "#{Settings.apis.clsi.url}/project/#{project_id}/status" - request.get url, (err, res, body)-> + request.get url, (err, res, body)=> if err? logger.err err:err, project_id:project_id, "error getting initial server id for project" return callback(err) - ClsiCookieManager.setServerId project_id, res, (err, serverId)-> + @setServerId project_id, res, (err, serverId)-> if err? logger.err err:err, project_id:project_id, "error setting server id via populate request" callback(err, serverId) @@ -43,7 +47,7 @@ module.exports = ClsiCookieManager = setServerId: (project_id, response, callback = (err, serverId)->)-> if !clsiCookiesEnabled return callback() - serverId = ClsiCookieManager._parseServerIdFromResponse(response) + serverId = @_parseServerIdFromResponse(response) if !serverId? # We don't get a cookie back if it hasn't changed return callback() if rclient_secondary? @@ -53,19 +57,19 @@ module.exports = ClsiCookieManager = _setServerIdInRedis: (rclient, project_id, serverId, callback = (err) ->) -> multi = rclient.multi() - multi.set buildKey(project_id), serverId - multi.expire buildKey(project_id), Settings.clsiCookie.ttl + multi.set @buildKey(project_id), serverId + multi.expire @buildKey(project_id), Settings.clsiCookie.ttl multi.exec callback clearServerId: (project_id, callback = (err)->)-> if !clsiCookiesEnabled return callback() - rclient.del buildKey(project_id), callback + rclient.del @buildKey(project_id), callback getCookieJar: (project_id, callback = (err, jar)->)-> if !clsiCookiesEnabled return callback(null, request.jar()) - ClsiCookieManager._getServerId project_id, (err, serverId)=> + @_getServerId project_id, (err, serverId)=> if err? logger.err err:err, project_id:project_id, "error getting server id" return callback(err) diff --git a/services/web/app/coffee/Features/Compile/ClsiManager.coffee b/services/web/app/coffee/Features/Compile/ClsiManager.coffee index c27bc2fe90..8e7e6f4c5b 100755 --- a/services/web/app/coffee/Features/Compile/ClsiManager.coffee +++ b/services/web/app/coffee/Features/Compile/ClsiManager.coffee @@ -7,7 +7,8 @@ ProjectGetter = require("../Project/ProjectGetter") ProjectEntityHandler = require("../Project/ProjectEntityHandler") logger = require "logger-sharelatex" Url = require("url") -ClsiCookieManager = require("./ClsiCookieManager") +ClsiCookieManager = require("./ClsiCookieManager")() +GoogleCloudClsiCookieManager = require("./ClsiCookieManager")("googlecloud") ClsiStateManager = require("./ClsiStateManager") _ = require("underscore") async = require("async") @@ -75,21 +76,45 @@ module.exports = ClsiManager = callback() _makeRequest: (project_id, opts, callback)-> + ClsiManager._makeGoogleCloudRequest project_id, opts, -> ClsiCookieManager.getCookieJar project_id, (err, jar)-> if err? logger.err err:err, "error getting cookie jar for clsi request" return callback(err) opts.jar = jar + timer = new Metrics.Timer("compile.linode") request opts, (err, response, body)-> + timer.done() if err? logger.err err:err, project_id:project_id, url:opts?.url, "error making request to clsi" return callback(err) ClsiCookieManager.setServerId project_id, response, (err)-> if err? logger.warn err:err, project_id:project_id, "error setting server id" - return callback err, response, body + _makeGoogleCloudRequest: (project_id, baseOpts, callback)-> + if !Settings.apis.clsigc?.url? + return callback() + opts = _.clone(baseOpts) + opts.url = opts.url.replace(Settings.apis.clsi.url, Settings.apis.clsigc?.url) + GoogleCloudClsiCookieManager.getCookieJar project_id, (err, jar)-> + if err? + logger.err err:err, "error getting cookie jar for clsi request" + return callback(err) + opts.jar = jar + timer = new Metrics.Timer("compile.google") + request opts, (err, response, body)-> + timer.done() + if err? + logger.err err:err, project_id:project_id, url:opts?.url, "error making request to clsi" + return callback(err) + GoogleCloudClsiCookieManager.setServerId project_id, response, (err)-> + if err? + logger.warn err:err, project_id:project_id, "error setting server id" + return callback err, response, body + + _getCompilerUrl: (compileGroup, project_id, user_id, action) -> host = Settings.apis.clsi.url path = "/project/#{project_id}" diff --git a/services/web/test/unit/coffee/Compile/ClsiCookieManagerTests.coffee b/services/web/test/unit/coffee/Compile/ClsiCookieManagerTests.coffee index f490af42e1..42a8aac3bc 100644 --- a/services/web/test/unit/coffee/Compile/ClsiCookieManagerTests.coffee +++ b/services/web/test/unit/coffee/Compile/ClsiCookieManagerTests.coffee @@ -40,7 +40,7 @@ describe "ClsiCookieManager", -> "request": @request "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), warn: sinon.stub() } - @ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires + @ClsiCookieManager = SandboxedModule.require(modulePath, {requires:@requires})() @@ -107,7 +107,7 @@ describe "ClsiCookieManager", -> it "should not set the server id if clsiCookies are not enabled", (done)-> delete @settings.clsiCookie.key - @ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires + @ClsiCookieManager = SandboxedModule.require(modulePath, requires:@requires)() @ClsiCookieManager.setServerId @project_id, @response, (err, serverId)=> @redisMulti.exec.called.should.equal false done() @@ -129,7 +129,7 @@ describe "ClsiCookieManager", -> @RedisWrapper.client = sinon.stub() @RedisWrapper.client.withArgs("clsi_cookie").returns(@redis) @RedisWrapper.client.withArgs("clsi_cookie_secondary").returns(@redis_secondary) - @ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires + @ClsiCookieManager = SandboxedModule.require(modulePath, requires:@requires)() @ClsiCookieManager._parseServerIdFromResponse = sinon.stub().returns("clsi-8") @ClsiCookieManager.setServerId @project_id, @response, (err, serverId)=> @redisSecondaryMulti.set.calledWith("clsiserver:#{@project_id}", "clsi-8").should.equal true @@ -150,7 +150,7 @@ describe "ClsiCookieManager", -> it "should return empty cookie jar if clsiCookies are not enabled", (done)-> delete @settings.clsiCookie.key - @ClsiCookieManager = SandboxedModule.require modulePath, requires:@requires + @ClsiCookieManager = SandboxedModule.require(modulePath, requires:@requires)() @ClsiCookieManager.getCookieJar @project_id, (err, jar)-> assert.deepEqual jar, realRequst.jar() done() diff --git a/services/web/test/unit/coffee/Compile/ClsiManagerTests.coffee b/services/web/test/unit/coffee/Compile/ClsiManagerTests.coffee index bd02af2f28..850c03465e 100644 --- a/services/web/test/unit/coffee/Compile/ClsiManagerTests.coffee +++ b/services/web/test/unit/coffee/Compile/ClsiManagerTests.coffee @@ -31,7 +31,7 @@ describe "ClsiManager", -> "../Project/ProjectGetter": @ProjectGetter = {} "../DocumentUpdater/DocumentUpdaterHandler": @DocumentUpdaterHandler = getProjectDocsIfMatch: sinon.stub().callsArgWith(2,null,null) - "./ClsiCookieManager": @ClsiCookieManager + "./ClsiCookieManager": => @ClsiCookieManager "./ClsiStateManager": @ClsiStateManager "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), err: sinon.stub(), warn: sinon.stub() } "request": @request = sinon.stub() @@ -486,6 +486,22 @@ describe "ClsiManager", -> done() + describe "_makeGoogleCloudRequest", -> + + beforeEach -> + @settings.apis.clsigc = + url : "https://compiles.googlecloud.test" + @response = {there:"something"} + @request.callsArgWith(1, null, @response) + + it "should change the domain on the url", (done)-> + @opts = + url: @ClsiManager._getCompilerUrl(null, @project_id) + + @ClsiManager._makeGoogleCloudRequest @project_id, @opts, => + args = @request.args[0] + args[0].url.should.equal "https://compiles.googlecloud.test/project/#{@project_id}" + done()