From 385406ca38336d373e7154cc92edf5e0f3b18e6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E6=B5=A9=E6=9E=97?= Date: Fri, 11 Oct 2024 17:54:54 +0800 Subject: [PATCH] =?UTF-8?q?[Feature]=20=E6=96=B0=E5=A2=9E=E9=9D=9E?= =?UTF-8?q?=E5=8F=97=E7=AE=A1=E8=BD=AF=E4=BB=B6=E5=8C=85=EF=BC=8C=E5=AE=89?= =?UTF-8?q?=E8=A3=85=E6=97=B6=E8=87=AA=E5=8A=A8deploy=E5=85=83=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=20#6986?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/metadata-api/package.service.js | 75 +++++++++++++++++++++++- services/service-package-loader/index.js | 38 ++++++++++++ 2 files changed, 111 insertions(+), 2 deletions(-) diff --git a/packages/metadata-api/package.service.js b/packages/metadata-api/package.service.js index 31a2d08dd3..e645966b7b 100644 --- a/packages/metadata-api/package.service.js +++ b/packages/metadata-api/package.service.js @@ -1,8 +1,8 @@ /* * @Author: baozhoutao@steedos.com * @Date: 2022-03-28 09:35:34 - * @LastEditors: baozhoutao@steedos.com - * @LastEditTime: 2022-11-17 10:21:16 + * @LastEditors: 孙浩林 sunhaolin@steedos.com + * @LastEditTime: 2024-10-11 16:39:42 * @Description: */ "use strict"; @@ -10,6 +10,12 @@ const project = require('./package.json'); const packageName = project.name; const packageLoader = require('@steedos/service-meteor-package-loader'); const path = require('path'); +const fs = require('fs'); +const os = require('os'); +const { DbManager } = require('./lib/util/dbManager') +const { jsonToDb } = require('./lib/metadata/deploy/jsonToDb') +const { deleteFolderRecursive } = require('@steedos/metadata-core') +const { loadFileToJson } = require('./lib/metadata/deploy/fileToJson') /** * @typedef {import('moleculer').Context} Context Moleculer's Context * 软件包服务启动后也需要抛出事件。 @@ -37,6 +43,14 @@ module.exports = { */ actions: { + deploy: { + async handler(ctx) { + const { fileBase64 } = ctx.params + const userSession = ctx.meta.user + return await this.deploy(fileBase64, userSession) + } + } + }, /** @@ -51,6 +65,63 @@ module.exports = { */ methods: { + deploy: async function (fileBase64, userSession) { + const dataBuffer = Buffer.from(fileBase64, 'base64'); + + var tempDFolder = path.join(os.tmpdir(), "steedos-dx"); + + if (!fs.existsSync(tempDFolder)) { + fs.mkdirSync(tempDFolder); + } + + var tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "steedos-dx", 'upload-')); + + var zipDir = path.join(tempDir, 'deploy.zip'); + + fs.writeFileSync(zipDir, dataBuffer); + var resMsg = { + status: 0, + msg: '', + }; + + var dbManager = new DbManager(userSession); + try { + let SteedosPackage = await loadFileToJson(tempDir); + + var isEmptyPackage = true; + for (const metadataName in SteedosPackage) { + const metadata = SteedosPackage[metadataName]; + for (const key in metadata) { + if (metadata[key]) { + isEmptyPackage = false; + break; + } + } + } + if (isEmptyPackage) { + throw new Error('data not found in package'); + } + + await dbManager.connect(); + var session = await dbManager.startSession(); + await jsonToDb(SteedosPackage, dbManager, session); + + resMsg.status = 0; + resMsg.msg = "deploy success!"; + + } catch (err) { + resMsg.status = -1; + resMsg.msg = err.message; + } finally { + await dbManager.endSession(); + await dbManager.close(); + } + + deleteFolderRecursive(tempDir); + + return resMsg; + } + }, /** diff --git a/services/service-package-loader/index.js b/services/service-package-loader/index.js index 3ff34cecd8..42131cf57a 100644 --- a/services/service-package-loader/index.js +++ b/services/service-package-loader/index.js @@ -14,6 +14,9 @@ const metaDataCore = require('@steedos/metadata-core'); const { registerMetadataConfigs, loadStandardMetadata, loadRouters } = require('@steedos/metadata-registrar'); const moment = require('moment'); const objectMixin = require('@steedos/service-object-mixin') +const { + compressFiles, deleteFolderRecursive, mkTempFolder +} = require('@steedos/metadata-core'); const loadFlowFile = new metaDataCore.LoadFlowFile(); @@ -83,6 +86,16 @@ module.exports = { // 扫描main/default/data文件夹 await this.importData(path.join(this.settings.packageInfo.path, 'main', 'default', 'data'), false, spaceId); await this.loadDataOnServiceStarted(spaceId); + + let packageInfo = this.settings.packageInfo; + if (!packageInfo) { + return; + } + const { path: packagePath, isUnmanaged } = packageInfo; + if (isUnmanaged && packagePath) { + const userSession = { spaceId: spaceId, userId: spaceDoc.owner }; + await this.deployPackage(packagePath, userSession); + } } } }, @@ -335,7 +348,32 @@ module.exports = { if(datasource){ await datasource.init(); } + }, + async deployPackage(sourcePath, userSession){ + try { + var tempDir = mkTempFolder('deploy-'); + var option = { includeJs: false, tableTitle: 'Steedos Deploy', showLog: true, inDeploy: true }; + + compressFiles(sourcePath, sourcePath, tempDir, option, async (base64, zipDir) => { + const result = await this.broker.call('~packages-@steedos/metadata-api.deploy', { + fileBase64: base64, + }, { + timeout: 0, + meta: { + user: userSession + } + }) + console.log(sourcePath, JSON.stringify(result)) + deleteFolderRecursive(tempDir); + }); + + } catch (err) { + console.log(err); + throw err; + } + } + }, /**