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