2019-06-28 23:59:27 +02:00
|
|
|
const { tmpdir } = require("os");
|
|
|
|
const { join } = require("path");
|
|
|
|
const uuid = require("uuid/v1");
|
2019-07-05 17:56:53 +02:00
|
|
|
const { take, takeRight, last } = require("lodash/fp");
|
2019-09-10 07:14:45 +02:00
|
|
|
const { splitKey, $, joinKey } = require("@budibase/core").common;
|
2019-06-28 23:59:27 +02:00
|
|
|
const { unzipTarGzPackageToRuntime } = require("../../utilities/targzAppPackage");
|
|
|
|
const { getRuntimePackageDirectory } = require("../../utilities/runtimePackages");
|
2019-07-26 18:08:59 +02:00
|
|
|
const { pathExists } = require("fs-extra");
|
2019-06-28 23:59:27 +02:00
|
|
|
const createInstanceDb = require("../../initialise/createInstanceDb");
|
2019-07-01 23:49:13 +02:00
|
|
|
const { createWriteStream } = require("fs");
|
2019-07-03 00:15:15 +02:00
|
|
|
const { applictionVersionPackage } = require("../../utilities/createAppPackage");
|
|
|
|
const { getApisWithFullAccess } = require("../../utilities/budibaseApi");
|
2019-06-28 23:59:27 +02:00
|
|
|
|
2019-07-09 08:29:50 +02:00
|
|
|
module.exports = (context) => {
|
|
|
|
const { config } = context;
|
2019-07-03 00:15:15 +02:00
|
|
|
const datastoreModule = require(`../../../datastores/datastores/${config.datastore}`);
|
|
|
|
return ({
|
2019-06-28 23:59:27 +02:00
|
|
|
initialiseInstance : async ({ instance, apis }) => {
|
|
|
|
const appKey = $(instance.key, [
|
|
|
|
splitKey,
|
2019-07-01 23:49:13 +02:00
|
|
|
take(2),
|
|
|
|
joinKey
|
2019-06-28 23:59:27 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
const application = await apis.recordApi.load(appKey);
|
|
|
|
|
|
|
|
const versionId = $(instance.version.key, [
|
|
|
|
splitKey,
|
2019-07-05 17:56:53 +02:00
|
|
|
last
|
2019-06-28 23:59:27 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
const runtimeDir = getRuntimePackageDirectory(
|
|
|
|
application.name,
|
|
|
|
versionId);
|
2019-07-05 17:56:53 +02:00
|
|
|
|
2019-07-26 18:08:59 +02:00
|
|
|
if(!await pathExists(runtimeDir))
|
2019-07-01 23:49:13 +02:00
|
|
|
await downloadAppPackage(apis, instance, application.name, versionId);
|
2019-07-07 10:03:37 +02:00
|
|
|
|
|
|
|
const dbConfig = await createInstanceDb(
|
2019-07-09 08:29:50 +02:00
|
|
|
context,
|
2019-07-07 10:03:37 +02:00
|
|
|
datastoreModule,
|
|
|
|
application,
|
|
|
|
instance
|
|
|
|
);
|
|
|
|
|
2019-06-28 23:59:27 +02:00
|
|
|
|
|
|
|
instance.datastoreconfig = JSON.stringify(dbConfig);
|
|
|
|
instance.isNew = false;
|
2019-07-05 17:56:53 +02:00
|
|
|
instance.transactionId = "";
|
|
|
|
|
|
|
|
await apis.recordApi.save(instance);
|
2019-07-03 00:15:15 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
createNewUser: async ({user, apis}) => {
|
2019-07-11 08:35:29 +02:00
|
|
|
|
|
|
|
if(!user.createdByMaster) return;
|
|
|
|
|
2019-07-05 17:56:53 +02:00
|
|
|
const instance = await apis.recordApi.load(user.instance.key);
|
2019-07-03 00:15:15 +02:00
|
|
|
|
|
|
|
const appKey = $(instance.key, [
|
|
|
|
splitKey,
|
|
|
|
take(2),
|
|
|
|
joinKey
|
|
|
|
]);
|
|
|
|
|
|
|
|
const application = await apis.recordApi.load(appKey);
|
|
|
|
|
|
|
|
const versionId = $(instance.version.key, [
|
|
|
|
splitKey,
|
2019-07-05 17:56:53 +02:00
|
|
|
last,
|
2019-07-03 00:15:15 +02:00
|
|
|
]);
|
|
|
|
|
2019-07-09 08:29:50 +02:00
|
|
|
const appPackage = await applictionVersionPackage(
|
|
|
|
context,
|
2019-07-03 00:15:15 +02:00
|
|
|
application.name,
|
2019-07-09 08:29:50 +02:00
|
|
|
versionId,
|
|
|
|
instance.key);
|
2019-07-03 00:15:15 +02:00
|
|
|
|
2019-07-05 17:56:53 +02:00
|
|
|
const instanceApis = await getApisWithFullAccess(
|
|
|
|
datastoreModule.getDatastore(
|
|
|
|
JSON.parse(instance.datastoreconfig)),
|
2019-07-03 00:15:15 +02:00
|
|
|
appPackage);
|
|
|
|
|
|
|
|
const authUser = instanceApis.authApi.getNewUser();
|
|
|
|
authUser.name = user.name;
|
|
|
|
authUser.accessLevels = [instance.version.defaultAccessLevel];
|
2019-07-11 08:35:29 +02:00
|
|
|
|
2019-07-03 00:15:15 +02:00
|
|
|
await instanceApis.authApi.createUser(authUser);
|
2019-07-18 09:05:16 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
setDefaultVersion : async ({apis, version}) => {
|
|
|
|
const appKey = $(version.key, [
|
|
|
|
splitKey,
|
|
|
|
take(2),
|
|
|
|
joinKey
|
|
|
|
]);
|
|
|
|
|
|
|
|
const application = await apis.recordApi.load(appKey);
|
|
|
|
|
|
|
|
if(application.defaultVersion.key) return;
|
|
|
|
|
|
|
|
application.defaultVersion = version;
|
|
|
|
|
|
|
|
await apis.recordApi.save(application);
|
2019-06-28 23:59:27 +02:00
|
|
|
}
|
2019-07-03 00:15:15 +02:00
|
|
|
|
|
|
|
});
|
|
|
|
}
|
2019-06-28 23:59:27 +02:00
|
|
|
|
2019-07-01 23:49:13 +02:00
|
|
|
const downloadAppPackage = async (apis, instance, appName, versionId) => {
|
|
|
|
const inputStream = await apis.recordApi.downloadFile(instance.version.key, "package.tar.gz");
|
2019-06-28 23:59:27 +02:00
|
|
|
|
|
|
|
const tempFilePath = join(tmpdir(), `bbpackage_${uuid()}.tar.gz`);
|
2019-07-01 23:49:13 +02:00
|
|
|
const outputStream = createWriteStream(tempFilePath);
|
2019-06-28 23:59:27 +02:00
|
|
|
|
|
|
|
await new Promise((resolve,reject) => {
|
|
|
|
inputStream.pipe(outputStream);
|
|
|
|
outputStream.on('error', reject);
|
|
|
|
outputStream.on('finish', resolve);
|
|
|
|
});
|
|
|
|
|
|
|
|
await unzipTarGzPackageToRuntime(tempFilePath, appName, versionId);
|
|
|
|
}
|