Skip to content

Commit

Permalink
[Feature] 新增非受管软件包,安装时自动deploy元数据 #6986
Browse files Browse the repository at this point in the history
  • Loading branch information
sunhaolin committed Oct 11, 2024
1 parent 7f5823b commit 385406c
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 2 deletions.
75 changes: 73 additions & 2 deletions packages/metadata-api/package.service.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
/*
* @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";
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
* 软件包服务启动后也需要抛出事件。
Expand Down Expand Up @@ -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)
}
}

},

/**
Expand All @@ -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;
}

},

/**
Expand Down
38 changes: 38 additions & 0 deletions services/service-package-loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
}
}
}
},
Expand Down Expand Up @@ -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;
}

}

},

/**
Expand Down

0 comments on commit 385406c

Please sign in to comment.