diff --git a/packages/server/src/api/controllers/deploy/Deployment.js b/packages/server/src/api/controllers/deploy/Deployment.js index 609ce0e8fb..d65d2714dc 100644 --- a/packages/server/src/api/controllers/deploy/Deployment.js +++ b/packages/server/src/api/controllers/deploy/Deployment.js @@ -1,13 +1,14 @@ const { getAppQuota } = require("./quota") const env = require("../../../environment") +const newid = require("../../../db/newid") /** * This is used to pass around information about the deployment that is occurring */ class Deployment { - constructor(id, appId) { - this._id = id + constructor(appId, id = null) { this.appId = appId + this._id = id || newid() } // purely so that we can do quota stuff outside the main deployment context @@ -30,6 +31,9 @@ class Deployment { } setVerification(verification) { + if (this.verification.quota) { + this.quota = this.verification.quota + } this.verification = verification } @@ -44,6 +48,18 @@ class Deployment { } } + fromJSON(json) { + if (json.verification) { + this.setVerification(json.verification) + } + if (json.quota) { + this.setQuota(json.quota) + } + if (json.status) { + this.setStatus(json.status, json.err) + } + } + getJSON() { const obj = { _id: this._id, @@ -56,8 +72,8 @@ class Deployment { if (this.verification && this.verification.cfDistribution) { obj.cfDistribution = this.verification.cfDistribution } - if (this.verification && this.verification.quota) { - obj.quota = this.verification.quota + if (this.quota) { + obj.quota = this.quota } return obj } diff --git a/packages/server/src/api/controllers/deploy/index.js b/packages/server/src/api/controllers/deploy/index.js index 00cd7254bc..a37fad62a0 100644 --- a/packages/server/src/api/controllers/deploy/index.js +++ b/packages/server/src/api/controllers/deploy/index.js @@ -1,6 +1,6 @@ const CouchDB = require("pouchdb") const PouchDB = require("../../../db") -const newid = require("../../../db/newid") + const env = require("../../../environment") const deployment = env.SELF_HOSTED ? require("./selfDeploy") @@ -69,7 +69,7 @@ async function storeLocalDeploymentHistory(deployment) { deploymentDoc = { _id: "_local/deployments", history: {} } } - const deploymentId = deploymentJSON._id || newid() + const deploymentId = deploymentJSON._id // first time deployment if (!deploymentDoc.history[deploymentId]) @@ -82,14 +82,12 @@ async function storeLocalDeploymentHistory(deployment) { } await db.put(deploymentDoc) - return { - _id: deploymentId, - ...deploymentDoc.history[deploymentId], - } + deployment.fromJSON(deploymentDoc.history[deploymentId]) + return deployment } -async function deployApp({ appId, deploymentId }) { - const deployment = new Deployment(deploymentId, appId) +async function deployApp(deployment) { + const appId = deployment.getAppId() try { await deployment.init() deployment.setVerification(await preDeployment(deployment)) @@ -144,15 +142,11 @@ exports.deploymentProgress = async function(ctx) { } exports.deployApp = async function(ctx) { - const deployment = await storeLocalDeploymentHistory({ - appId: ctx.user.appId, - status: DeploymentStatus.PENDING, - }) + let deployment = new Deployment(ctx.user.appId) + deployment.setStatus(DeploymentStatus.PENDING) + deployment = await storeLocalDeploymentHistory(deployment) - await deployApp({ - ...ctx.user, - deploymentId: deployment._id, - }) + await deployApp(deployment) ctx.body = deployment }