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-07-01 23:49:13 +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");
|
|
|
|
const { exists } = require("../../utilities/fsawait");
|
|
|
|
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-03 00:15:15 +02:00
|
|
|
module.exports = (config) => {
|
|
|
|
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-06-28 23:59:27 +02:00
|
|
|
if(!await exists(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(
|
|
|
|
config,
|
|
|
|
datastoreModule,
|
|
|
|
config.datastoreConfig,
|
|
|
|
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-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
|
|
|
]);
|
|
|
|
|
|
|
|
const appPackage = applictionVersionPackage(
|
2019-07-05 17:56:53 +02:00
|
|
|
config,
|
2019-07-03 00:15:15 +02:00
|
|
|
application.name,
|
|
|
|
versionId);
|
|
|
|
|
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];
|
|
|
|
await instanceApis.authApi.createUser(authUser);
|
|
|
|
|
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);
|
|
|
|
}
|