From c733f7fcca9d055b075b81db1098b73160b6e0b3 Mon Sep 17 00:00:00 2001 From: Tejesh Mehta Date: Thu, 22 Jan 2015 16:38:36 -0800 Subject: [PATCH 01/11] correct deep copy cv from hello-runnable to another user --- lib/models/apis/runnable.js | 42 +++++++++++++++++++++++---- lib/routes/contexts/versions/index.js | 33 ++++++++++++++++++++- 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/lib/models/apis/runnable.js b/lib/models/apis/runnable.js index f039232b2..cbe665fec 100644 --- a/lib/models/apis/runnable.js +++ b/lib/models/apis/runnable.js @@ -126,13 +126,27 @@ Runnable.prototype.destroyInstances = function (instances, cb) { }, cb); }; -Runnable.prototype.copyInstance = function (sessionUser, build, parentInstance, body, cb) { +Runnable.prototype.copyInstance = function (build, parentInstance, newProps, cb) { debug('copyInstance', formatArgs(arguments)); - body.parent = parentInstance.shortHash; - body.build = build.toJSON()._id; - body.env = body.env || parentInstance.env; - body.owner = body.owner || parentInstance.owner; - + if (newProps === 'body') { + // we are using all the stuff from parentInstance and letting it gen a new name + newProps = {}; // just for sanity + } + var body = { + parent: parentInstance.shortHash, + env: parentInstance.env, + owner: parentInstance.owner, + build: build.toJSON()._id + }; + // little bit of sanity checking for props + var newName = newProps.name; + if (newName && newName !== '') { + body.name = newName; + } + var env = newProps.env; + if (env && Array.isArray(env)) { + body.env = env; + } // Calling out to the API to fetch the project and env, then create a new Build this.createInstance(body, cb); }; @@ -181,6 +195,22 @@ Runnable.prototype.buildBuild = function (build, opts, cb) { buildModel.build(opts, cb); }; + +Runnable.prototype.createContextVersion = function (contextId, cb) { + debug('createContextVersion', formatArgs(arguments)); + this + .newContext(contextId.toString()) + .createVersion(cb); +}; + +Runnable.prototype.copyVersionIcvFile = function (contextId, cvId, icvId, cb) { + debug('copyVersionIcvFile', formatArgs(arguments)); + this + .newContext(contextId) + .newVersion(cvId) + .copyFilesFromSource(icvId, cb); +}; + Runnable.prototype.addAppCodeVersionsToContextVersion = function (appCodeVersions, contextVersion, cb) { debug('addAppCodeVersionsToContextVersion', formatArgs(arguments)); diff --git a/lib/routes/contexts/versions/index.js b/lib/routes/contexts/versions/index.js index 40cc92d7f..4bba0025d 100644 --- a/lib/routes/contexts/versions/index.js +++ b/lib/routes/contexts/versions/index.js @@ -27,6 +27,7 @@ var isPopulatedArray = validations.isPopulatedArray; var transformations = require('middlewares/transformations'); var mavis = require('middlewares/apis').mavis; var docker = require('middlewares/apis').docker; +var runnable = require('middlewares/apis').runnable; var error = require('error'); var ownerIsHelloRunnable = require('middlewares/owner-is-hello-runnable'); @@ -208,10 +209,40 @@ app.post('/contexts/:contextId/versions/:id/actions/copy', checkFound('contextVersion'), // only supports deep copy for now mw.query('deep').require().validate(validations.equals('true')), - contextVersions.createDeepCopy('sessionUser', 'contextVersion'), + mw.require('contextVersion.owner.github') + .validate(validations.equals(process.env.HELLO_RUNNABLE_GITHUB_ID)) + .validate(validations.notEqualsKeypath('sessionUser.accounts.github.id')) + .then( + // if the build owner is hello-runnable and user is not hello-runnable + deepCopyCvFromHelloRunnable('contextVersion') + ).else( + contextVersions.createDeepCopy('sessionUser', 'contextVersion')), mw.res.status(201), mw.res.json('contextVersion')); +function deepCopyCvFromHelloRunnable (cvKey) { + return flow.series( + // maybe error if the contextVersion has acvs? + // make a new context + mw.req('contextVersion', cvKey), + mw.body().pick('owner'), + runnable.create({}, 'sessionUser'), + runnable.createContext('body'), + mw.req().set('context', 'runnableResult'), + // make a new context-version + runnable.create({}, 'sessionUser'), + runnable.createContextVersion('context._id'), + mw.req().set('newContextVersion', 'runnableResult'), + // use infracodeversion copy route to copy the files + runnable.create({}, 'sessionUser'), + runnable.copyVersionIcvFiles( + 'context._id', + 'newContextVersion._id', + 'contextVersion.infraCodeVersion'), + contextVersions.findById('newContextVersion._id') + ); +} + /** Builds a context version * used internally (github hook, build build), session user may not be real * @param id versionId of the source version From bb0ab267c729c79698d8d34ed6a5b751da23a9d3 Mon Sep 17 00:00:00 2001 From: Tejesh Mehta Date: Thu, 22 Jan 2015 16:42:31 -0800 Subject: [PATCH 02/11] fix typo --- lib/routes/contexts/versions/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/routes/contexts/versions/index.js b/lib/routes/contexts/versions/index.js index 4bba0025d..ee14a01aa 100644 --- a/lib/routes/contexts/versions/index.js +++ b/lib/routes/contexts/versions/index.js @@ -209,7 +209,7 @@ app.post('/contexts/:contextId/versions/:id/actions/copy', checkFound('contextVersion'), // only supports deep copy for now mw.query('deep').require().validate(validations.equals('true')), - mw.require('contextVersion.owner.github') + mw.req('contextVersion.owner.github').require() .validate(validations.equals(process.env.HELLO_RUNNABLE_GITHUB_ID)) .validate(validations.notEqualsKeypath('sessionUser.accounts.github.id')) .then( From a963f05d820906e39f88da98b7c9554cc168da2c Mon Sep 17 00:00:00 2001 From: Tejesh Mehta Date: Thu, 22 Jan 2015 16:56:46 -0800 Subject: [PATCH 03/11] more typos --- lib/routes/contexts/versions/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/routes/contexts/versions/index.js b/lib/routes/contexts/versions/index.js index ee14a01aa..729a1f6d8 100644 --- a/lib/routes/contexts/versions/index.js +++ b/lib/routes/contexts/versions/index.js @@ -227,15 +227,15 @@ function deepCopyCvFromHelloRunnable (cvKey) { mw.req('contextVersion', cvKey), mw.body().pick('owner'), runnable.create({}, 'sessionUser'), - runnable.createContext('body'), + runnable.model.createContext('body'), mw.req().set('context', 'runnableResult'), // make a new context-version runnable.create({}, 'sessionUser'), - runnable.createContextVersion('context._id'), + runnable.model.createContextVersion('context._id'), mw.req().set('newContextVersion', 'runnableResult'), // use infracodeversion copy route to copy the files runnable.create({}, 'sessionUser'), - runnable.copyVersionIcvFiles( + runnable.model.copyVersionIcvFiles( 'context._id', 'newContextVersion._id', 'contextVersion.infraCodeVersion'), From 6d2948ab7403b558b8f623ad1aca983cc1314e1b Mon Sep 17 00:00:00 2001 From: Tejesh Mehta Date: Thu, 22 Jan 2015 16:59:44 -0800 Subject: [PATCH 04/11] typo3 --- lib/models/apis/runnable.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/models/apis/runnable.js b/lib/models/apis/runnable.js index cbe665fec..1bdeac97b 100644 --- a/lib/models/apis/runnable.js +++ b/lib/models/apis/runnable.js @@ -203,7 +203,7 @@ Runnable.prototype.createContextVersion = function (contextId, cb) { .createVersion(cb); }; -Runnable.prototype.copyVersionIcvFile = function (contextId, cvId, icvId, cb) { +Runnable.prototype.copyVersionIcvFiles = function (contextId, cvId, icvId, cb) { debug('copyVersionIcvFile', formatArgs(arguments)); this .newContext(contextId) From f996d888ad2f140eeddffc04f9fcaeda810ed3f4 Mon Sep 17 00:00:00 2001 From: Tejesh Mehta Date: Thu, 22 Jan 2015 17:28:39 -0800 Subject: [PATCH 05/11] revert copyInstance to latest --- lib/models/apis/runnable.js | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/lib/models/apis/runnable.js b/lib/models/apis/runnable.js index 1bdeac97b..f68e52449 100644 --- a/lib/models/apis/runnable.js +++ b/lib/models/apis/runnable.js @@ -126,27 +126,13 @@ Runnable.prototype.destroyInstances = function (instances, cb) { }, cb); }; -Runnable.prototype.copyInstance = function (build, parentInstance, newProps, cb) { +Runnable.prototype.copyInstance = function (sessionUser, build, parentInstance, body, cb) { debug('copyInstance', formatArgs(arguments)); - if (newProps === 'body') { - // we are using all the stuff from parentInstance and letting it gen a new name - newProps = {}; // just for sanity - } - var body = { - parent: parentInstance.shortHash, - env: parentInstance.env, - owner: parentInstance.owner, - build: build.toJSON()._id - }; - // little bit of sanity checking for props - var newName = newProps.name; - if (newName && newName !== '') { - body.name = newName; - } - var env = newProps.env; - if (env && Array.isArray(env)) { - body.env = env; - } + body.parent = parentInstance.shortHash; + body.build = build.toJSON()._id; + body.env = body.env || parentInstance.env; + body.owner = body.owner || parentInstance.owner; + // Calling out to the API to fetch the project and env, then create a new Build this.createInstance(body, cb); }; From dcc85179b8e4a5c81447e2dd80790147589bc7c0 Mon Sep 17 00:00:00 2001 From: Tejesh Mehta Date: Thu, 22 Jan 2015 18:15:19 -0800 Subject: [PATCH 06/11] debug --- lib/routes/builds.js | 3 ++- lib/routes/contexts/index.js | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/routes/builds.js b/lib/routes/builds.js index 83bf47f06..2c60197f1 100644 --- a/lib/routes/builds.js +++ b/lib/routes/builds.js @@ -61,9 +61,10 @@ app.post('/builds/', eachReq.context = context; next(); }, + mw.log('POST BUILD!!!', 'body.owner.github', 'context.owner.github'), mw.req('body.owner.github') .validate(validations.equalsKeypath('context.owner.github')) - .else(mw.next(Boom.badRequest('Context versions\' owners must match build owner')))), + .else(mw.next(Boom.badRequest('Context version\'s owner must match build owner')))), function (req, res, next) { req.contextIds = req.contextVersions.map(pluck('context')); req.contextVersionIds = req.contextVersions.map(pluck('_id')); diff --git a/lib/routes/contexts/index.js b/lib/routes/contexts/index.js index dcf536a77..399476231 100644 --- a/lib/routes/contexts/index.js +++ b/lib/routes/contexts/index.js @@ -98,6 +98,7 @@ app.post('/contexts/', me.isModerator)), contexts.createBy('body'), contexts.model.save(), + mw.log('POST CONTEXT!!!', 'context'), mw.res.send(201, 'context')); /* @returns {error} 405 - not allowed From c40f9597b654a0f7ebce683e029c5ea143c9c4b1 Mon Sep 17 00:00:00 2001 From: Tejesh Mehta Date: Thu, 22 Jan 2015 18:30:00 -0800 Subject: [PATCH 07/11] rename var and more debug --- lib/routes/contexts/versions/index.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/routes/contexts/versions/index.js b/lib/routes/contexts/versions/index.js index 729a1f6d8..f6cc9786b 100644 --- a/lib/routes/contexts/versions/index.js +++ b/lib/routes/contexts/versions/index.js @@ -228,15 +228,18 @@ function deepCopyCvFromHelloRunnable (cvKey) { mw.body().pick('owner'), runnable.create({}, 'sessionUser'), runnable.model.createContext('body'), - mw.req().set('context', 'runnableResult'), + mw.req().set('newContext', 'runnableResult'), // make a new context-version runnable.create({}, 'sessionUser'), - runnable.model.createContextVersion('context._id'), + runnable.model.createContextVersion('newContext._id'), mw.req().set('newContextVersion', 'runnableResult'), // use infracodeversion copy route to copy the files runnable.create({}, 'sessionUser'), + mw.log('DEEP COPY', + 'context._id', 'contextVersion._id', + 'newContext._id', 'newContextVersion._id'), runnable.model.copyVersionIcvFiles( - 'context._id', + 'newContext._id', 'newContextVersion._id', 'contextVersion.infraCodeVersion'), contextVersions.findById('newContextVersion._id') From 1594ca05910a550445f416126190cb87098e7986 Mon Sep 17 00:00:00 2001 From: Tejesh Mehta Date: Thu, 22 Jan 2015 19:06:47 -0800 Subject: [PATCH 08/11] checking wrong owner in version copy route --- lib/routes/contexts/versions/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/routes/contexts/versions/index.js b/lib/routes/contexts/versions/index.js index f6cc9786b..b65897510 100644 --- a/lib/routes/contexts/versions/index.js +++ b/lib/routes/contexts/versions/index.js @@ -209,7 +209,7 @@ app.post('/contexts/:contextId/versions/:id/actions/copy', checkFound('contextVersion'), // only supports deep copy for now mw.query('deep').require().validate(validations.equals('true')), - mw.req('contextVersion.owner.github').require() + mw.req('context.owner.github').require() .validate(validations.equals(process.env.HELLO_RUNNABLE_GITHUB_ID)) .validate(validations.notEqualsKeypath('sessionUser.accounts.github.id')) .then( From 5b27ed1c5b3c649a73ac1230a24205e3ab90562a Mon Sep 17 00:00:00 2001 From: Tejesh Mehta Date: Thu, 22 Jan 2015 19:17:20 -0800 Subject: [PATCH 09/11] add name to createContext --- lib/routes/contexts/index.js | 1 - lib/routes/contexts/versions/index.js | 3 +++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/routes/contexts/index.js b/lib/routes/contexts/index.js index 399476231..dcf536a77 100644 --- a/lib/routes/contexts/index.js +++ b/lib/routes/contexts/index.js @@ -98,7 +98,6 @@ app.post('/contexts/', me.isModerator)), contexts.createBy('body'), contexts.model.save(), - mw.log('POST CONTEXT!!!', 'context'), mw.res.send(201, 'context')); /* @returns {error} 405 - not allowed diff --git a/lib/routes/contexts/versions/index.js b/lib/routes/contexts/versions/index.js index b65897510..00101fd57 100644 --- a/lib/routes/contexts/versions/index.js +++ b/lib/routes/contexts/versions/index.js @@ -7,6 +7,7 @@ var express = require('express'); var app = module.exports = express(); +var uuid = require('uuid'); var mw = require('dat-middleware'); var flow = require('middleware-flow'); @@ -227,6 +228,8 @@ function deepCopyCvFromHelloRunnable (cvKey) { mw.req('contextVersion', cvKey), mw.body().pick('owner'), runnable.create({}, 'sessionUser'), + // create context requires name + mw.body().set('name', uuid()), runnable.model.createContext('body'), mw.req().set('newContext', 'runnableResult'), // make a new context-version From a866f0535597e4308652269e94d84e87ff721d9d Mon Sep 17 00:00:00 2001 From: Tejesh Mehta Date: Thu, 22 Jan 2015 19:32:59 -0800 Subject: [PATCH 10/11] context name must be unique for each owner --- lib/routes/contexts/versions/index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/routes/contexts/versions/index.js b/lib/routes/contexts/versions/index.js index 00101fd57..61705abdc 100644 --- a/lib/routes/contexts/versions/index.js +++ b/lib/routes/contexts/versions/index.js @@ -229,7 +229,10 @@ function deepCopyCvFromHelloRunnable (cvKey) { mw.body().pick('owner'), runnable.create({}, 'sessionUser'), // create context requires name - mw.body().set('name', uuid()), + function (req, res, next) { + req.body.name = uuid(); + next(); + }, runnable.model.createContext('body'), mw.req().set('newContext', 'runnableResult'), // make a new context-version From 7b6e8786f643ccdf38e229104b7f264aadfffb03 Mon Sep 17 00:00:00 2001 From: Tejesh Mehta Date: Thu, 22 Jan 2015 19:56:27 -0800 Subject: [PATCH 11/11] removed logs --- lib/routes/builds.js | 1 - lib/routes/contexts/versions/index.js | 3 --- 2 files changed, 4 deletions(-) diff --git a/lib/routes/builds.js b/lib/routes/builds.js index 2c60197f1..e7692c36d 100644 --- a/lib/routes/builds.js +++ b/lib/routes/builds.js @@ -61,7 +61,6 @@ app.post('/builds/', eachReq.context = context; next(); }, - mw.log('POST BUILD!!!', 'body.owner.github', 'context.owner.github'), mw.req('body.owner.github') .validate(validations.equalsKeypath('context.owner.github')) .else(mw.next(Boom.badRequest('Context version\'s owner must match build owner')))), diff --git a/lib/routes/contexts/versions/index.js b/lib/routes/contexts/versions/index.js index 61705abdc..499ca247a 100644 --- a/lib/routes/contexts/versions/index.js +++ b/lib/routes/contexts/versions/index.js @@ -241,9 +241,6 @@ function deepCopyCvFromHelloRunnable (cvKey) { mw.req().set('newContextVersion', 'runnableResult'), // use infracodeversion copy route to copy the files runnable.create({}, 'sessionUser'), - mw.log('DEEP COPY', - 'context._id', 'contextVersion._id', - 'newContext._id', 'newContextVersion._id'), runnable.model.copyVersionIcvFiles( 'newContext._id', 'newContextVersion._id',