diff --git a/src/models/Group-model.js b/src/models/Group-model.js index 2ab3f77..a7187a1 100644 --- a/src/models/Group-model.js +++ b/src/models/Group-model.js @@ -6,6 +6,23 @@ var Group = sequelize.define('group', { title: { type: Sequelize.STRING, allowNull: true } }) -Group.prototype.getGroupDir = () => '/data/groups/' + this.groupname +const groupPrefix = '/data/groups/' + +Group.afterCreate(async group => { + let groupDir = groupPrefix + group.id + if (!(await fs.pathExists(groupDir))) { + await fs.mkdirp(groupDir) + } +}) + +Group.afterDestroy(async group => { + let groupDir = groupPrefix + group.id + if (await fs.pathExists(groupDir)) { + await fs.remove(groupDir) + } +}) + +Group.getGroupDir = (groupId) => groupPrefix + groupId +Group.prototype.getGroupDir = () => Group.getGroupDir(this.id) module.exports = Group diff --git a/src/models/Job-model.js b/src/models/Job-model.js index e8bf9f2..bd2bd71 100644 --- a/src/models/Job-model.js +++ b/src/models/Job-model.js @@ -39,4 +39,6 @@ User.prototype.canAccessJob = async (resource) => { return true } +Job.prototype.getJobDir = () => Pit.getPitDir(this.id) + module.exports = Job diff --git a/src/models/Pit-model.js b/src/models/Pit-model.js index f412138..0d7437f 100644 --- a/src/models/Pit-model.js +++ b/src/models/Pit-model.js @@ -3,12 +3,12 @@ const sequelize = require('./db.js') const fs = require('fs-extra') -const pitPrefix = '/data/pits/' - var Pit = sequelize.define('pit', { id: { type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true } }) +const pitPrefix = '/data/pits/' + Pit.afterCreate(async pit => { let pitDir = pitPrefix + pit.id if (!(await fs.pathExists(pitDir))) { @@ -23,4 +23,7 @@ Pit.afterDestroy(async pit => { } }) +Pit.getPitDir = (pitId) => pitPrefix + pitId +Pit.prototype.getPitDir = () => Pit.getPitDir(this.id) + module.exports = Pit diff --git a/src/models/User-model.js b/src/models/User-model.js index bebcab8..299d22b 100644 --- a/src/models/User-model.js +++ b/src/models/User-model.js @@ -4,8 +4,6 @@ const Group = require('./Group-model.js') const fs = require('fs-extra') -const homePrefix = '/data/home/' - var User = sequelize.define('user', { id: { type: Sequelize.STRING, primaryKey: true }, password: { type: Sequelize.STRING, allowNull: false }, @@ -17,25 +15,28 @@ var User = sequelize.define('user', { User.belongsToMany(Group, { through: 'UserGroup' }) Group.belongsToMany(User, { through: 'UserGroup' }) -User.beforeCreate(async user => { - let homeDir = homePrefix + user.id - if (!(await fs.pathExists(homeDir))) { - await fs.mkdirp(homeDir) - } -}) - -User.afterDestroy(async user => { - let homeDir = homePrefix + user.id - if (await fs.pathExists(homeDir)) { - await fs.remove(homeDir) - } -}) +const userPrefix = '/data/home/' User.prototype.isMemberOf = async group => { group = Group.findById(group) return group && this.hasGroup(group) } -User.prototype.getHomeDir = () => homePrefix + this.id +User.afterCreate(async user => { + let userDir = userPrefix + user.id + if (!(await fs.pathExists(userDir))) { + await fs.mkdirp(userDir) + } +}) + +User.afterDestroy(async user => { + let userDir = userPrefix + user.id + if (await fs.pathExists(userDir)) { + await fs.remove(userDir) + } +}) + +User.getUserDir = (userId) => userPrefix + userId +User.prototype.getUserDir = () => User.getUserDir(this.id) module.exports = User diff --git a/src/routes/groups.js b/src/routes/groups.js index acc3061..258feef 100644 --- a/src/routes/groups.js +++ b/src/routes/groups.js @@ -37,19 +37,4 @@ exports.initApp = function(app) { res.status(403).send() } }) - - app.put('/nodes/:node/resources/:resource/groups/:group', function(req, res) { - _addGroup(_getResource(req), req, res, entity => { - _emitEntityChange('resource', entity) - }) - }) - - app.delete('/nodes/:node/resources/:resource/groups/:group', function(req, res) { - _removeGroup(_getResource(req), req, res, entity => { - _emitEntityChange('resource', entity) - _emitRestricted() - }) - }) - - } diff --git a/src/routes/nodes.js b/src/routes/nodes.js index 499071b..7ed056d 100644 --- a/src/routes/nodes.js +++ b/src/routes/nodes.js @@ -95,7 +95,7 @@ router.put('/:id/groups/:group', (req, res) => { } }) -router.delete('/:node/groups/:group', (req, res) => { +router.delete('/:id/groups/:group', (req, res) => { if (req.user.admin) { let node = db.nodes[req.params.node] if (node) { @@ -116,3 +116,16 @@ router.delete('/:node/groups/:group', (req, res) => { } _emitRestricted() }) + +router.put('/:id/resources/:resource/groups/:group', async (req, res) => { + _addGroup(_getResource(req), req, res, entity => { + _emitEntityChange('resource', entity) + }) +}) + +router.delete('/:id/resources/:resource/groups/:group', async (req, res) => { + _removeGroup(_getResource(req), req, res, entity => { + _emitEntityChange('resource', entity) + _emitRestricted() + }) +}) diff --git a/src/scheduler.js b/src/scheduler.js index 64b4943..890ccea 100644 --- a/src/scheduler.js +++ b/src/scheduler.js @@ -70,14 +70,14 @@ function getBasicEnv(job) { return { JOB_NUMBER: job.id, DATA_ROOT: '/data', - JOB_DIR: nodesModule.getPitDir(job.id) + JOB_DIR: job.getJobDir() } } function getPreparationEnv(job) { let env = getBasicEnv(job) - if (job.continueJob) { - env.CONTINUE_JOB_NUMBER = job.continueJob + if (job.continues) { + env.CONTINUE_JOB_NUMBER = job.continues } return env } @@ -89,21 +89,6 @@ function setJobState(job, state) { saveJob(job) } -function loadJob (jobId) { - let job = db.jobs[jobId] - if (job) { - return job - } - let jobPath = path.join(nodesModule.getPitDir(jobId), 'meta.json') - if (fs.existsSync(jobPath)) { - return JSON.parse(fs.readFileSync(jobPath, 'utf8')) - } -} - -function saveJob (job) { - fs.writeFileSync(path.join(nodesModule.getPitDir(job.id), 'meta.json'), JSON.stringify(job)) -} - function prepareJob(job) { let env = getPreparationEnv(job) if (job.origin) {