Cleaning up deployments into the app DB, moving API keys to be in global DB (which will be tenancy managed) and adding concept of platform info DB.

This commit is contained in:
mike12345567 2021-07-14 16:29:32 +01:00
parent 41bd4958cf
commit f2c2ead3fc
5 changed files with 48 additions and 60 deletions

View File

@ -11,9 +11,13 @@ exports.ViewNames = {
exports.StaticDatabases = { exports.StaticDatabases = {
GLOBAL: { GLOBAL: {
name: "global-db", name: "global-db",
docs: {
apiKeys: "apikeys",
}, },
DEPLOYMENTS: { },
name: "deployments", // contains information about tenancy and so on
PLATFORM_INFO: {
name: "global-info",
}, },
} }

View File

@ -1,8 +1,32 @@
const builderDB = require("../../db/builder") const CouchDB = require("../../db")
const { StaticDatabases } = require("@budibase/auth/db")
const GLOBAL_DB = StaticDatabases.GLOBAL.name
const KEYS_DOC = StaticDatabases.GLOBAL.docs.apiKeys
async function getBuilderMainDoc() {
const db = new CouchDB(GLOBAL_DB)
try {
return await db.get(KEYS_DOC)
} catch (err) {
// doesn't exist yet, nothing to get
return {
_id: KEYS_DOC,
}
}
}
async function setBuilderMainDoc(doc) {
// make sure to override the ID
doc._id = KEYS_DOC
const db = new CouchDB(GLOBAL_DB)
return db.put(doc)
}
exports.fetch = async function (ctx) { exports.fetch = async function (ctx) {
try { try {
const mainDoc = await builderDB.getBuilderMainDoc() const mainDoc = await getBuilderMainDoc()
ctx.body = mainDoc.apiKeys ? mainDoc.apiKeys : {} ctx.body = mainDoc.apiKeys ? mainDoc.apiKeys : {}
} catch (err) { } catch (err) {
/* istanbul ignore next */ /* istanbul ignore next */
@ -15,12 +39,12 @@ exports.update = async function (ctx) {
const value = ctx.request.body.value const value = ctx.request.body.value
try { try {
const mainDoc = await builderDB.getBuilderMainDoc() const mainDoc = await getBuilderMainDoc()
if (mainDoc.apiKeys == null) { if (mainDoc.apiKeys == null) {
mainDoc.apiKeys = {} mainDoc.apiKeys = {}
} }
mainDoc.apiKeys[key] = value mainDoc.apiKeys[key] = value
const resp = await builderDB.setBuilderMainDoc(mainDoc) const resp = await setBuilderMainDoc(mainDoc)
ctx.body = { ctx.body = {
_id: resp.id, _id: resp.id,
_rev: resp.rev, _rev: resp.rev,

View File

@ -1,6 +1,6 @@
const PouchDB = require("../../../db") const CouchDB = require("../../../db")
const Deployment = require("./Deployment") const Deployment = require("./Deployment")
const { Replication, StaticDatabases } = require("@budibase/auth/db") const { Replication } = require("@budibase/auth/db")
const { DocumentTypes } = require("../../../db/utils") const { DocumentTypes } = require("../../../db/utils")
// the max time we can wait for an invalidation to complete before considering it failed // the max time we can wait for an invalidation to complete before considering it failed
@ -31,11 +31,12 @@ async function checkAllDeployments(deployments) {
async function storeDeploymentHistory(deployment) { async function storeDeploymentHistory(deployment) {
const appId = deployment.getAppId() const appId = deployment.getAppId()
const deploymentJSON = deployment.getJSON() const deploymentJSON = deployment.getJSON()
const db = new PouchDB(StaticDatabases.DEPLOYMENTS.name) const db = new CouchDB(appId)
let deploymentDoc let deploymentDoc
try { try {
deploymentDoc = await db.get(appId) // theres only one deployment doc per app database
deploymentDoc = await db.get(DocumentTypes.DEPLOYMENTS)
} catch (err) { } catch (err) {
deploymentDoc = { _id: appId, history: {} } deploymentDoc = { _id: appId, history: {} }
} }
@ -67,7 +68,7 @@ async function deployApp(deployment) {
}) })
await replication.replicate() await replication.replicate()
const db = new PouchDB(productionAppId) const db = new CouchDB(productionAppId)
const appDoc = await db.get(DocumentTypes.APP_METADATA) const appDoc = await db.get(DocumentTypes.APP_METADATA)
appDoc.appId = productionAppId appDoc.appId = productionAppId
appDoc.instance._id = productionAppId appDoc.instance._id = productionAppId
@ -98,8 +99,9 @@ async function deployApp(deployment) {
exports.fetchDeployments = async function (ctx) { exports.fetchDeployments = async function (ctx) {
try { try {
const db = new PouchDB(StaticDatabases.DEPLOYMENTS.name) const appId = ctx.appId
const deploymentDoc = await db.get(ctx.appId) const db = new CouchDB(appId)
const deploymentDoc = await db.get(DocumentTypes.DEPLOYMENTS)
const { updated, deployments } = await checkAllDeployments( const { updated, deployments } = await checkAllDeployments(
deploymentDoc, deploymentDoc,
ctx.user ctx.user
@ -115,8 +117,9 @@ exports.fetchDeployments = async function (ctx) {
exports.deploymentProgress = async function (ctx) { exports.deploymentProgress = async function (ctx) {
try { try {
const db = new PouchDB(StaticDatabases.DEPLOYMENTS.name) const appId = ctx.appId
const deploymentDoc = await db.get(ctx.appId) const db = new CouchDB(appId)
const deploymentDoc = await db.get(DocumentTypes.DEPLOYMENTS)
ctx.body = deploymentDoc[ctx.params.deploymentId] ctx.body = deploymentDoc[ctx.params.deploymentId]
} catch (err) { } catch (err) {
ctx.throw( ctx.throw(

View File

@ -1,38 +0,0 @@
const CouchDB = require("./index")
const { StaticDatabases } = require("./utils")
const env = require("../environment")
const SELF_HOST_ERR = "Unable to access builder DB/doc - not self hosted."
const BUILDER_DB = StaticDatabases.BUILDER
/**
* This is the builder database, right now this is a single, static database
* that is present across the whole system and determines some core functionality
* for the builder (e.g. storage of API keys). This has been limited to self hosting
* as it doesn't make as much sense against the currently design Cloud system.
*/
exports.getBuilderMainDoc = async () => {
if (!env.SELF_HOSTED) {
throw SELF_HOST_ERR
}
const db = new CouchDB(BUILDER_DB.name)
try {
return await db.get(BUILDER_DB.baseDoc)
} catch (err) {
// doesn't exist yet, nothing to get
return {
_id: BUILDER_DB.baseDoc,
}
}
}
exports.setBuilderMainDoc = async doc => {
if (!env.SELF_HOSTED) {
throw SELF_HOST_ERR
}
// make sure to override the ID
doc._id = BUILDER_DB.baseDoc
const db = new CouchDB(BUILDER_DB.name)
return db.put(doc)
}

View File

@ -34,6 +34,7 @@ const DocumentTypes = {
DATASOURCE: "datasource", DATASOURCE: "datasource",
DATASOURCE_PLUS: "datasource_plus", DATASOURCE_PLUS: "datasource_plus",
QUERY: "query", QUERY: "query",
DEPLOYMENTS: "deployments",
} }
const ViewNames = { const ViewNames = {
@ -49,13 +50,7 @@ const SearchIndexes = {
ROWS: "rows", ROWS: "rows",
} }
exports.StaticDatabases = { exports.StaticDatabases = StaticDatabases
BUILDER: {
name: "builder-db",
baseDoc: "builder-doc",
},
...StaticDatabases,
}
const BudibaseInternalDB = { const BudibaseInternalDB = {
_id: "bb_internal", _id: "bb_internal",