-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closes #78 Add support to specify seeds to be created at server start up #80
Changes from 5 commits
b391617
ee9f727
3cb14e6
458279b
1e79ac7
8358905
4b69b8c
e692913
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/*globals*/ | ||
/*globals requireJS*/ | ||
/*eslint-env node*/ | ||
|
||
/** | ||
|
@@ -13,6 +13,7 @@ var fs = require('fs'), | |
Q = require('q'), | ||
path = require('path'), | ||
requireUncached = require('require-uncached'), | ||
storageUtils = requireJS('common/storage/util'), | ||
SVGMapDeffered; | ||
|
||
function walkDir(dir, done) { | ||
|
@@ -416,6 +417,121 @@ function getComponentsJson(logger, callback) { | |
return deferred.promise.nodeify(callback); | ||
} | ||
|
||
function createStartUpProjects(gmeConfig, gmeAuth, storage, logger, url) { | ||
var deferred = Q.defer(), | ||
existingProjectIds = [], | ||
WorkerRequest = require('./server/worker/workerrequests'), | ||
worker = new WorkerRequest(logger.fork('worker'), gmeConfig, url), | ||
projectsToCreate = []; | ||
|
||
|
||
Q.ninvoke(storage, 'getProjects', {}) | ||
.then(function (projectList) { | ||
|
||
projectList.forEach(function (projectInfo) { | ||
existingProjectIds.push(projectInfo._id); | ||
}); | ||
|
||
(gmeConfig.seedProjects.createAtStartup || []).forEach(function (projectInfo) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You already check in the validator that this is an array.. |
||
var creatorId = projectInfo.creatorId || gmeConfig.authentication.admin, | ||
ownerId = projectInfo.ownerId || creatorId, | ||
id = storageUtils.getProjectIdFromOwnerIdAndProjectName(ownerId, projectInfo.projectName); | ||
|
||
if (existingProjectIds.indexOf(id) === -1) { | ||
projectsToCreate.push({ | ||
id: id, | ||
seedId: projectInfo.seedId, | ||
projectName: projectInfo.projectName, | ||
ownerId: ownerId, | ||
creatorId: creatorId, | ||
rights: JSON.parse(JSON.stringify(projectInfo.rights)), | ||
}); | ||
} | ||
}); | ||
|
||
if (projectsToCreate.length === 0) { | ||
return; | ||
} | ||
|
||
|
||
return Q.ninvoke(gmeAuth, 'listUsers', null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again - this returns a promise... |
||
}) | ||
.then(function (users) { | ||
var promises = []; | ||
|
||
if (projectsToCreate.length === 0) { | ||
return; | ||
} | ||
projectsToCreate.forEach(function (projectInfo) { | ||
var existingCreator = false, | ||
i; | ||
|
||
for (i = 0; i < users.length; i += 1) { | ||
if (users[i]._id === projectInfo.creatorId) { | ||
existingCreator = true; | ||
break; | ||
} | ||
} | ||
|
||
if (existingCreator === false) { | ||
logger.error('Cannot create project [' + projectInfo.id + '] as creator [' + | ||
projectInfo.creatorId + '] is missing!'); | ||
projectInfo.failed = true; | ||
promises.push(null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nm, it looks like the tests covers that 👍 |
||
} else { | ||
promises.push(gmeAuth.generateJWTokenForAuthenticatedUser(projectInfo.creatorId)); | ||
} | ||
|
||
}); | ||
return Q.all(promises); | ||
}) | ||
.then(function (webTokens) { | ||
var promises = []; | ||
|
||
if (projectsToCreate.length === 0) { | ||
return; | ||
} | ||
|
||
projectsToCreate.forEach(function (projectInfo, index) { | ||
if (projectInfo.failed) { | ||
return; | ||
} | ||
logger.info('Creating \'' + projectInfo.projectName + '\' for \'' + projectInfo.ownerId + | ||
'\' from seed[' + projectInfo.seedId + '].'); | ||
promises.push(Q.ninvoke(worker, 'seedProject', webTokens[index], projectInfo.projectName, | ||
projectInfo.ownerId, {seedName: projectInfo.seedId, type: 'file'})); | ||
}); | ||
return Q.all(promises); | ||
}) | ||
.then(function () { | ||
var authorizationRequests = [], | ||
projectAuthParams = { | ||
entityType: gmeAuth.authorizer.ENTITY_TYPES.PROJECT | ||
}; | ||
|
||
projectsToCreate.forEach(function (projectInfo) { | ||
var userOrOrg, | ||
id = storageUtils.getProjectIdFromOwnerIdAndProjectName(projectInfo.ownerId, | ||
projectInfo.projectName); | ||
|
||
if (projectInfo.failed) { | ||
return; | ||
} | ||
|
||
for (userOrOrg in projectInfo.rights) { | ||
logger.info('Authorizing \'' + userOrOrg + '\' to use \'' + projectInfo.projectName + | ||
'\' of \'' + projectInfo.ownerId + '.'); | ||
authorizationRequests.push(gmeAuth.authorizer.setAccessRights(userOrOrg, | ||
id, projectInfo.rights[userOrOrg], projectAuthParams)); | ||
} | ||
}); | ||
return Q.all(authorizationRequests); | ||
}) | ||
.then(deferred.resolve) | ||
.catch(deferred.reject); | ||
return deferred.promise; | ||
} | ||
|
||
module.exports = { | ||
isGoodExtraAsset: isGoodExtraAsset, | ||
getComponentNames: getComponentNames, | ||
|
@@ -430,5 +546,6 @@ module.exports = { | |
getRedirectUrlParameter: getRedirectUrlParameter, | ||
getSeedDictionary: getSeedDictionary, | ||
getSeedDictionarySync: getSeedDictionarySync, | ||
getComponentsJson: getComponentsJson | ||
getComponentsJson: getComponentsJson, | ||
createStartUpProjects: createStartUpProjects, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't
storage.getProjects
already return a promise? More importantly the user isn't specified and would only return guest's projects..There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The user should be the creator (as it would have access to the owners projects too).