budibase/packages/server/appPackages/master/main.js

62 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-06-28 23:59:27 +02:00
const { tmpdir } = require("os");
const { join } = require("path");
const uuid = require("uuid/v1");
const { take, takeRight } = 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-06-28 23:59:27 +02:00
module.exports = (config) => ({
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);
2019-07-01 23:49:13 +02:00
const datastoreModule = require(`../../../datastores/datastores/${config.datastore}`);
2019-06-28 23:59:27 +02:00
const dbConfig = await createInstanceDb(
2019-07-01 23:49:13 +02:00
datastoreModule,
2019-06-28 23:59:27 +02:00
config.datastoreConfig,
application.id,
instance.id
);
const versionId = $(instance.version.key, [
splitKey,
2019-07-01 23:49:13 +02:00
takeRight(1),
joinKey
2019-06-28 23:59:27 +02:00
]);
const runtimeDir = getRuntimePackageDirectory(
application.name,
versionId);
if(!await exists(runtimeDir))
2019-07-01 23:49:13 +02:00
await downloadAppPackage(apis, instance, application.name, versionId);
2019-06-28 23:59:27 +02:00
instance.datastoreconfig = JSON.stringify(dbConfig);
instance.isNew = false;
await apis.recordApi.save(instance);
}
});
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);
}