2018-04-21 02:59:30 -03:00
|
|
|
/*****************************************************************************\
|
|
|
|
| ( )_ _ |
|
|
|
|
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
|
2020-07-01 15:00:40 -03:00
|
|
|
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' v `\ /'_`\ |
|
2019-03-09 16:59:31 -03:00
|
|
|
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| (˅) |( (_) ) |
|
2018-04-21 02:59:30 -03:00
|
|
|
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
|
|
|
|
| | | ( )_) | |
|
|
|
|
| (_) \___/' |
|
|
|
|
| |
|
|
|
|
| General Bots Copyright (c) Pragmatismo.io. All rights reserved. |
|
|
|
|
| Licensed under the AGPL-3.0. |
|
2018-11-11 19:09:18 -02:00
|
|
|
| |
|
2018-04-21 02:59:30 -03:00
|
|
|
| According to our dual licensing model, this program can be used either |
|
|
|
|
| under the terms of the GNU Affero General Public License, version 3, |
|
|
|
|
| or under a proprietary license. |
|
|
|
|
| |
|
|
|
|
| The texts of the GNU Affero General Public License with an additional |
|
|
|
|
| permission and of our proprietary license can be found at and |
|
|
|
|
| in the LICENSE file you have received along with this program. |
|
|
|
|
| |
|
|
|
|
| This program is distributed in the hope that it will be useful, |
|
2018-09-11 19:40:53 -03:00
|
|
|
| but WITHOUT ANY WARRANTY, without even the implied warranty of |
|
2018-04-21 02:59:30 -03:00
|
|
|
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
|
|
| GNU Affero General Public License for more details. |
|
|
|
|
| |
|
|
|
|
| "General Bots" is a registered trademark of Pragmatismo.io. |
|
|
|
|
| The licensing of the program under the AGPLv3 does not imply a |
|
|
|
|
| trademark license. Therefore any rights, title and interest in |
|
|
|
|
| our trademarks remain entirely with us. |
|
|
|
|
| |
|
|
|
|
\*****************************************************************************/
|
|
|
|
|
2018-11-11 19:09:18 -02:00
|
|
|
/**
|
|
|
|
* @fileoverview General Bots server core.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
2018-09-24 11:04:36 -03:00
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
const Path = require('path');
|
2019-03-09 16:59:31 -03:00
|
|
|
import urlJoin = require('url-join');
|
2018-11-12 12:20:44 -02:00
|
|
|
const Fs = require('fs');
|
|
|
|
const express = require('express');
|
2018-11-30 11:55:44 -02:00
|
|
|
const child_process = require('child_process');
|
2019-01-31 11:32:33 -02:00
|
|
|
const graph = require('@microsoft/microsoft-graph-client');
|
2019-08-22 17:28:11 -03:00
|
|
|
const rimraf = require('rimraf');
|
2018-11-12 12:20:44 -02:00
|
|
|
|
2020-04-01 15:42:57 -03:00
|
|
|
import { GBError, GBLog, GBMinInstance, IGBCoreService, IGBInstance, IGBPackage, IGBDeployer } from 'botlib';
|
2018-11-12 12:20:44 -02:00
|
|
|
import { AzureSearch } from 'pragmatismo-io-framework';
|
2019-05-14 23:02:21 -03:00
|
|
|
import { GBServer } from '../../../src/app';
|
2020-05-12 19:20:59 -03:00
|
|
|
import { GuaribasPackage } from '../models/GBModel';
|
2019-01-31 11:32:33 -02:00
|
|
|
import { GBAdminService } from './../../admin.gbapp/services/GBAdminService';
|
2019-05-14 23:02:21 -03:00
|
|
|
import { AzureDeployerService } from './../../azuredeployer.gbapp/services/AzureDeployerService';
|
2018-11-12 12:20:44 -02:00
|
|
|
import { KBService } from './../../kb.gbapp/services/KBService';
|
|
|
|
import { GBConfigService } from './GBConfigService';
|
2018-11-26 14:09:09 -02:00
|
|
|
import { GBImporter } from './GBImporterService';
|
2018-11-26 15:54:34 -02:00
|
|
|
import { GBVMService } from './GBVMService';
|
2020-01-26 17:43:50 -03:00
|
|
|
import { CollectionUtil } from 'pragmatismo-io-framework';
|
2020-08-15 11:39:43 -03:00
|
|
|
const MicrosoftGraph = require('@microsoft/microsoft-graph-client');
|
2020-04-01 15:42:57 -03:00
|
|
|
|
2019-01-31 11:32:33 -02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Deployer service for bots, themes, ai and more.
|
|
|
|
*/
|
|
|
|
|
2020-04-02 19:03:57 -03:00
|
|
|
export class GBDeployer implements IGBDeployer {
|
2018-11-12 12:20:44 -02:00
|
|
|
public static deployFolder = 'packages';
|
2019-08-22 01:54:30 +00:00
|
|
|
public static workFolder = 'work';
|
2018-11-12 12:20:44 -02:00
|
|
|
public core: IGBCoreService;
|
|
|
|
public importer: GBImporter;
|
2018-09-10 12:09:48 -03:00
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
constructor(core: IGBCoreService, importer: GBImporter) {
|
2018-09-24 11:04:36 -03:00
|
|
|
this.core = core;
|
|
|
|
this.importer = importer;
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
public static getConnectionStringFromInstance(instance: IGBInstance) {
|
2020-08-15 11:39:43 -03:00
|
|
|
return `Server=tcp:${instance.storageServer}.database.windows.net,1433;Database=${instance.storageName};User ID=${instance.storageUsername};Password=${instance.storagePassword};Trusted_Connection=False;Encrypt=True;Connection Timeout=30;`;
|
2018-11-12 12:20:44 -02:00
|
|
|
}
|
|
|
|
|
2018-09-11 19:33:58 -03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Performs package deployment in all .gbai or default.
|
|
|
|
*
|
2018-12-18 13:50:35 -02:00
|
|
|
*/
|
|
|
|
public async deployPackages(core: IGBCoreService, server: any, appPackages: IGBPackage[]) {
|
2018-11-12 12:20:44 -02:00
|
|
|
const _this = this;
|
2018-12-18 13:50:35 -02:00
|
|
|
|
2020-05-15 14:07:30 -03:00
|
|
|
let paths = [urlJoin(process.env.PWD, GBDeployer.deployFolder), urlJoin(process.env.PWD, GBDeployer.workFolder)];
|
|
|
|
const additionalPath = GBConfigService.get('ADDITIONAL_DEPLOY_PATH');
|
|
|
|
if (additionalPath !== undefined && additionalPath !== '') {
|
|
|
|
paths = paths.concat(additionalPath.toLowerCase().split(';'));
|
|
|
|
}
|
|
|
|
const botPackages: string[] = [];
|
|
|
|
const gbappPackages: string[] = [];
|
|
|
|
let generalPackages: string[] = [];
|
|
|
|
|
|
|
|
async function scanPackageDirectory(path) {
|
|
|
|
const isDirectory = source => Fs.lstatSync(source).isDirectory();
|
|
|
|
const getDirectories = source =>
|
|
|
|
Fs.readdirSync(source)
|
|
|
|
.map(name => Path.join(source, name))
|
|
|
|
.filter(isDirectory);
|
|
|
|
|
|
|
|
const dirs = getDirectories(path);
|
|
|
|
await CollectionUtil.asyncForEach(dirs, async element => {
|
2020-07-18 16:38:58 -03:00
|
|
|
element = element.toLowerCase();
|
2020-08-15 11:39:43 -03:00
|
|
|
|
2020-05-15 14:07:30 -03:00
|
|
|
if (element === '.') {
|
|
|
|
GBLog.info(`Ignoring ${element}...`);
|
|
|
|
} else {
|
2020-08-15 11:39:43 -03:00
|
|
|
const name = Path.basename(element).toLowerCase();
|
|
|
|
|
2020-10-18 17:46:16 -03:00
|
|
|
if (
|
|
|
|
process.env.GBAPP_SKIP &&
|
|
|
|
(process.env.GBAPP_SKIP.toLowerCase().indexOf(name) !== -1 || process.env.GBAPP_SKIP === 'true')
|
|
|
|
) {
|
2020-08-15 11:39:43 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-15 14:07:30 -03:00
|
|
|
if (element.endsWith('.gbot')) {
|
|
|
|
botPackages.push(element);
|
2020-07-18 16:38:58 -03:00
|
|
|
} else if (element.endsWith('.gbapp') || element.endsWith('.gblib')) {
|
2020-05-15 14:07:30 -03:00
|
|
|
gbappPackages.push(element);
|
|
|
|
} else {
|
|
|
|
generalPackages.push(element);
|
|
|
|
}
|
2018-11-26 14:09:09 -02:00
|
|
|
}
|
2020-05-15 14:07:30 -03:00
|
|
|
});
|
|
|
|
}
|
2018-11-26 14:09:09 -02:00
|
|
|
|
2020-05-15 14:07:30 -03:00
|
|
|
GBLog.info(`Starting looking for packages (.gbot, .gbtheme, .gbkb, .gbapp)...`);
|
|
|
|
await CollectionUtil.asyncForEach(paths, async e => {
|
|
|
|
GBLog.info(`Looking in: ${e}...`);
|
|
|
|
await scanPackageDirectory(e);
|
|
|
|
});
|
2018-11-26 14:09:09 -02:00
|
|
|
|
2020-07-18 16:38:58 -03:00
|
|
|
// Deploys all .gblib files first.
|
2018-12-18 13:50:35 -02:00
|
|
|
|
2020-07-18 16:38:58 -03:00
|
|
|
let list = [];
|
|
|
|
for (let index = 0; index < gbappPackages.length; index++) {
|
|
|
|
const element = gbappPackages[index];
|
|
|
|
if (element.endsWith('.gblib')) {
|
|
|
|
list.push(element);
|
|
|
|
gbappPackages.splice(index, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let index = 0; index < gbappPackages.length; index++) {
|
|
|
|
const element = gbappPackages[index];
|
|
|
|
list.push(element);
|
|
|
|
}
|
|
|
|
await this.deployAppPackages(list, core, appPackages);
|
2018-09-10 12:09:48 -03:00
|
|
|
|
2020-05-15 14:07:30 -03:00
|
|
|
GBLog.info(`App Package deployment done.`);
|
2020-05-12 19:20:59 -03:00
|
|
|
|
2020-08-15 11:39:43 -03:00
|
|
|
({ generalPackages } = await this.deployDataPackages(core, botPackages, _this, generalPackages));
|
2018-09-10 12:09:48 -03:00
|
|
|
}
|
|
|
|
|
2020-02-26 15:20:47 -03:00
|
|
|
public async deployBlankBot(botId: string) {
|
2020-02-25 10:13:38 -03:00
|
|
|
let instance = await this.importer.createBotInstance(botId);
|
2020-02-26 15:20:47 -03:00
|
|
|
|
2020-06-14 21:40:41 -03:00
|
|
|
const bootInstance = GBServer.globals.bootInstance;
|
2020-08-15 11:39:43 -03:00
|
|
|
const accessToken = await GBServer.globals.minBoot.adminService.acquireElevatedToken(bootInstance.instanceId);
|
2020-02-25 12:37:10 -03:00
|
|
|
|
|
|
|
const service = new AzureDeployerService(this);
|
2020-02-27 20:49:05 -03:00
|
|
|
let application = await service.createApplication(accessToken, botId);
|
2020-02-25 12:37:10 -03:00
|
|
|
|
|
|
|
instance.marketplaceId = (application as any).appId;
|
2020-08-15 11:39:43 -03:00
|
|
|
instance.marketplacePassword = await service.createApplicationSecret(accessToken, (application as any).id);
|
2020-02-25 12:37:10 -03:00
|
|
|
instance.adminPass = GBAdminService.getRndPassword();
|
2020-03-08 09:24:28 -03:00
|
|
|
instance.title = botId;
|
2020-05-19 12:36:17 -03:00
|
|
|
instance.activationCode = instance.botId;
|
2020-05-27 23:01:44 -03:00
|
|
|
instance.state = 'active';
|
2020-08-15 11:39:43 -03:00
|
|
|
instance.nlpScore = 0.8; // TODO: Migrate to Excel Config.xlsx.
|
2020-05-30 19:30:11 -03:00
|
|
|
instance.searchScore = 0.45;
|
2020-06-14 21:40:41 -03:00
|
|
|
instance.whatsappServiceKey = bootInstance.whatsappServiceKey;
|
|
|
|
instance.whatsappServiceNumber = bootInstance.whatsappServiceNumber;
|
|
|
|
instance.whatsappServiceUrl = bootInstance.whatsappServiceUrl;
|
2020-03-08 09:24:28 -03:00
|
|
|
|
2020-02-25 12:37:10 -03:00
|
|
|
await this.core.saveInstance(instance);
|
2020-02-25 10:13:38 -03:00
|
|
|
|
2020-03-08 09:24:28 -03:00
|
|
|
return await this.deployBotFull(instance, GBServer.globals.publicAddress);
|
2020-02-25 10:13:38 -03:00
|
|
|
}
|
|
|
|
|
2020-06-11 09:47:59 -03:00
|
|
|
public async botExists(botId: string): Promise<boolean> {
|
2020-04-15 20:56:28 -03:00
|
|
|
const service = new AzureDeployerService(this);
|
2020-06-11 09:47:59 -03:00
|
|
|
return await service.botExists(botId);
|
2020-04-15 20:56:28 -03:00
|
|
|
}
|
2018-09-11 19:33:58 -03:00
|
|
|
/**
|
|
|
|
* Deploys a bot to the storage.
|
2018-09-09 14:39:37 -03:00
|
|
|
*/
|
|
|
|
|
2020-03-08 09:24:28 -03:00
|
|
|
public async deployBotFull(instance: IGBInstance, publicAddress: string): Promise<IGBInstance> {
|
2019-05-14 23:02:21 -03:00
|
|
|
const service = new AzureDeployerService(this);
|
|
|
|
const username = GBConfigService.get('CLOUD_USERNAME');
|
|
|
|
const password = GBConfigService.get('CLOUD_PASSWORD');
|
2020-02-25 12:37:10 -03:00
|
|
|
const accessToken = await GBAdminService.getADALTokenFromUsername(username, password);
|
2019-05-14 23:02:21 -03:00
|
|
|
const group = GBConfigService.get('CLOUD_GROUP');
|
|
|
|
const subscriptionId = GBConfigService.get('CLOUD_SUBSCRIPTIONID');
|
|
|
|
|
2020-06-11 09:47:59 -03:00
|
|
|
if (await service.botExists(instance.botId)) {
|
2019-08-22 01:54:30 +00:00
|
|
|
await service.updateBot(
|
|
|
|
instance.botId,
|
|
|
|
group,
|
2019-05-14 23:02:21 -03:00
|
|
|
instance.title,
|
|
|
|
instance.description,
|
2019-08-22 01:54:30 +00:00
|
|
|
`${publicAddress}/api/messages/${instance.botId}`
|
2019-05-14 23:02:21 -03:00
|
|
|
);
|
2019-05-15 12:41:04 -03:00
|
|
|
} else {
|
2019-08-23 02:23:00 -03:00
|
|
|
let botId = GBConfigService.get('BOT_ID');
|
2020-05-11 10:46:01 -03:00
|
|
|
let bootInstance = await this.core.loadInstanceByBotId(botId);
|
2019-08-23 02:23:00 -03:00
|
|
|
|
|
|
|
instance.searchHost = bootInstance.searchHost;
|
|
|
|
instance.searchIndex = bootInstance.searchIndex;
|
|
|
|
instance.searchIndexer = bootInstance.searchIndexer;
|
|
|
|
instance.searchKey = bootInstance.searchKey;
|
|
|
|
instance.whatsappServiceKey = bootInstance.whatsappServiceKey;
|
|
|
|
instance.whatsappServiceNumber = bootInstance.whatsappServiceNumber;
|
|
|
|
instance.whatsappServiceUrl = bootInstance.whatsappServiceUrl;
|
2019-08-24 18:46:04 -03:00
|
|
|
instance.storageServer = bootInstance.storageServer;
|
|
|
|
instance.storageName = bootInstance.storageName;
|
|
|
|
instance.storageUsername = bootInstance.storageUsername;
|
|
|
|
instance.storagePassword = bootInstance.storagePassword;
|
2020-04-15 05:08:50 +00:00
|
|
|
instance.cloudLocation = bootInstance.cloudLocation;
|
|
|
|
instance.speechEndpoint = bootInstance.speechEndpoint;
|
|
|
|
instance.speechKey = bootInstance.speechKey;
|
2019-08-23 14:36:47 -03:00
|
|
|
|
2019-05-14 23:02:21 -03:00
|
|
|
instance = await service.internalDeployBot(
|
|
|
|
instance,
|
|
|
|
accessToken,
|
|
|
|
instance.botId,
|
|
|
|
instance.title,
|
|
|
|
group,
|
|
|
|
instance.description,
|
2019-08-22 01:54:30 +00:00
|
|
|
`${publicAddress}/api/messages/${instance.botId}`,
|
2019-05-14 23:02:21 -03:00
|
|
|
'global',
|
|
|
|
instance.nlpAppId,
|
|
|
|
instance.nlpKey,
|
|
|
|
instance.marketplaceId,
|
|
|
|
instance.marketplacePassword,
|
|
|
|
subscriptionId
|
|
|
|
);
|
2019-08-22 01:54:30 +00:00
|
|
|
|
|
|
|
await GBServer.globals.minService.mountBot(instance);
|
2019-05-14 23:02:21 -03:00
|
|
|
}
|
2020-03-08 09:24:28 -03:00
|
|
|
return await this.core.saveInstance(instance);
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
|
2020-10-18 21:28:19 -03:00
|
|
|
public async publishNLP(instance: IGBInstance): Promise<void> {
|
|
|
|
const service = new AzureDeployerService(this);
|
|
|
|
const res = await service.publishNLP(
|
|
|
|
instance.cloudLocation,
|
|
|
|
instance.nlpAppId,
|
|
|
|
instance.nlpAuthoringKey,
|
|
|
|
);
|
|
|
|
if(res.status !== 200) throw res.bodyAsText;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public async trainNLP(instance: IGBInstance): Promise<void> {
|
|
|
|
const service = new AzureDeployerService(this);
|
|
|
|
const res = await service.trainNLP(
|
|
|
|
instance.cloudLocation,
|
|
|
|
instance.nlpAppId,
|
|
|
|
instance.nlpAuthoringKey,
|
|
|
|
);
|
|
|
|
if(res.status !== 200) throw res.bodyAsText;
|
|
|
|
let sleep = ms => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
setTimeout(resolve, ms);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
sleep(5000);
|
|
|
|
}
|
|
|
|
|
2020-10-18 17:46:16 -03:00
|
|
|
public async refreshNLPEntity(instance: IGBInstance, listName, listData): Promise<void> {
|
|
|
|
const service = new AzureDeployerService(this);
|
|
|
|
const res = await service.refreshEntityList(
|
|
|
|
instance.cloudLocation,
|
|
|
|
instance.nlpAppId,
|
|
|
|
listName,
|
|
|
|
instance.nlpAuthoringKey,
|
|
|
|
listData
|
|
|
|
);
|
|
|
|
if(res.status !== 200) throw res.bodyAsText;
|
2020-10-18 21:28:19 -03:00
|
|
|
|
2020-10-18 17:46:16 -03:00
|
|
|
}
|
|
|
|
|
2020-02-25 10:13:38 -03:00
|
|
|
/**
|
|
|
|
* Deploys a bot to the storage from a .gbot folder.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public async deployBotFromLocalPath(localPath: string, publicAddress: string): Promise<void> {
|
|
|
|
const packageName = Path.basename(localPath);
|
|
|
|
let instance = await this.importer.importIfNotExistsBotPackage(undefined, packageName, localPath);
|
2020-09-19 21:57:00 -03:00
|
|
|
await this.deployBotFull(instance, publicAddress);
|
2020-02-25 10:13:38 -03:00
|
|
|
}
|
2019-08-22 01:54:30 +00:00
|
|
|
|
2020-05-23 17:59:01 -03:00
|
|
|
public async loadParamsFromExcel(min: GBMinInstance): Promise<any> {
|
2020-08-15 11:39:43 -03:00
|
|
|
let token = await min.adminService.acquireElevatedToken(min.instance.instanceId);
|
2020-05-23 17:59:01 -03:00
|
|
|
|
|
|
|
let siteId = process.env.STORAGE_SITE_ID;
|
|
|
|
let libraryId = process.env.STORAGE_LIBRARY;
|
|
|
|
|
|
|
|
let client = MicrosoftGraph.Client.init({
|
|
|
|
authProvider: done => {
|
|
|
|
done(null, token);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const botId = min.instance.botId;
|
|
|
|
const path = `/${botId}.gbai/${botId}.gbot`;
|
|
|
|
|
2020-08-15 11:39:43 -03:00
|
|
|
let res = await client
|
|
|
|
.api(`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/root:${path}:/children`)
|
2020-05-23 17:59:01 -03:00
|
|
|
.get();
|
|
|
|
|
|
|
|
// Performs validation.
|
|
|
|
|
|
|
|
let document = res.value.filter(m => {
|
2020-08-15 11:39:43 -03:00
|
|
|
return m.name === 'Config.xlsx';
|
2020-05-23 17:59:01 -03:00
|
|
|
});
|
2020-06-14 21:40:41 -03:00
|
|
|
if (document === undefined || document.length === 0) {
|
|
|
|
GBLog.info(`Config.xlsx not found on .bot folder, check the package.`);
|
|
|
|
return null;
|
2020-05-23 17:59:01 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates workbook session that will be discarded.
|
|
|
|
|
2020-08-15 11:39:43 -03:00
|
|
|
let results = await client
|
|
|
|
.api(
|
|
|
|
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${libraryId}/drive/items/${document[0].id}/workbook/worksheets('General')/range(address='A7:B100')`
|
|
|
|
)
|
2020-05-23 17:59:01 -03:00
|
|
|
.get();
|
|
|
|
|
|
|
|
let index = 0;
|
|
|
|
let obj = {};
|
|
|
|
for (; index < results.text.length; index++) {
|
2020-08-15 11:39:43 -03:00
|
|
|
if (results.text[index][0] === '') {
|
2020-05-23 17:59:01 -03:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
obj[results.text[index][0]] = results.text[index][1];
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2019-08-22 01:54:30 +00:00
|
|
|
/**
|
2019-08-22 17:28:11 -03:00
|
|
|
* UndDeploys a bot to the storage.
|
2019-08-22 01:54:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
public async undeployBot(botId: string, packageName: string): Promise<void> {
|
|
|
|
const service = new AzureDeployerService(this);
|
|
|
|
|
|
|
|
const group = GBConfigService.get('CLOUD_GROUP');
|
|
|
|
|
2020-06-11 09:47:59 -03:00
|
|
|
if (await service.botExists(botId)) {
|
2020-08-15 11:39:43 -03:00
|
|
|
await service.deleteBot(botId, group);
|
2019-08-22 01:54:30 +00:00
|
|
|
}
|
|
|
|
GBServer.globals.minService.unmountBot(botId);
|
|
|
|
await this.core.deleteInstance(botId);
|
2020-07-20 16:38:29 -03:00
|
|
|
const packageFolder = Path.join(process.env.PWD, 'work', `${botId}.gbai`, packageName);
|
2019-08-22 17:28:11 -03:00
|
|
|
}
|
2018-11-30 11:55:44 -02:00
|
|
|
public async deployPackageToStorage(instanceId: number, packageName: string): Promise<GuaribasPackage> {
|
2018-09-09 14:39:37 -03:00
|
|
|
return GuaribasPackage.create({
|
2018-04-21 02:59:30 -03:00
|
|
|
packageName: packageName,
|
2018-11-27 22:56:11 -02:00
|
|
|
instanceId: instanceId
|
2018-09-24 11:04:36 -03:00
|
|
|
});
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
|
2019-03-09 16:59:31 -03:00
|
|
|
public async deployFromSharePoint(instanceId: number) {
|
2019-01-31 11:32:33 -02:00
|
|
|
const adminService = new GBAdminService(this.core);
|
|
|
|
const accessToken = adminService.acquireElevatedToken(instanceId);
|
|
|
|
|
|
|
|
// Initialize Graph client.
|
|
|
|
|
|
|
|
const client = graph.Client.init({
|
|
|
|
authProvider: done => {
|
2019-03-09 16:59:31 -03:00
|
|
|
done(undefined, accessToken);
|
2019-01-31 11:32:33 -02:00
|
|
|
}
|
|
|
|
});
|
2020-04-01 15:42:57 -03:00
|
|
|
|
2020-08-15 11:39:43 -03:00
|
|
|
// TODO: Today a download only approach is used.
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
|
2019-03-09 16:59:31 -03:00
|
|
|
public async deployPackage(min: GBMinInstance, localPath: string) {
|
2018-11-12 12:20:44 -02:00
|
|
|
const packageType = Path.extname(localPath);
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2020-07-18 16:38:58 -03:00
|
|
|
const _this = this;
|
|
|
|
let handled = false;
|
|
|
|
let pck = null;
|
|
|
|
|
|
|
|
// .gbapp package or platform package checking.
|
|
|
|
|
|
|
|
await CollectionUtil.asyncForEach(min.appPackages, async (e: IGBPackage) => {
|
2020-10-18 17:46:16 -03:00
|
|
|
try {
|
|
|
|
if (
|
|
|
|
(pck = await e.onExchangeData(min, 'handlePackage', {
|
|
|
|
name: localPath,
|
|
|
|
createPackage: async packageName => {
|
|
|
|
return await _this.deployPackageToStorage(min.instance.instanceId, packageName);
|
|
|
|
},
|
|
|
|
updatePackage: async (p: GuaribasPackage) => {
|
|
|
|
p.save();
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
) {
|
|
|
|
handled = true;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
GBLog.error(error);
|
2020-07-18 16:38:58 -03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (handled) {
|
|
|
|
return pck;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deploy platform packages here.
|
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
switch (packageType) {
|
2018-11-12 12:20:44 -02:00
|
|
|
case '.gbot':
|
2020-05-23 17:59:01 -03:00
|
|
|
if (Fs.existsSync(localPath)) {
|
|
|
|
await this.deployBotFromLocalPath(localPath, GBServer.globals.publicAddress);
|
|
|
|
}
|
2020-10-18 17:46:16 -03:00
|
|
|
if (process.env.ENABLE_PARAMS_ONLINE === 'true') {
|
2020-08-22 18:41:54 -03:00
|
|
|
min.instance.params = await this.loadParamsFromExcel(min);
|
|
|
|
}
|
2020-05-23 17:59:01 -03:00
|
|
|
await this.core.saveInstance(min.instance);
|
|
|
|
|
2019-08-22 01:54:30 +00:00
|
|
|
break;
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
case '.gbkb':
|
|
|
|
const service = new KBService(this.core.sequelize);
|
2020-05-12 19:20:59 -03:00
|
|
|
await service.deployKb(this.core, this, localPath, min);
|
2019-08-22 17:28:11 -03:00
|
|
|
break;
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2018-11-26 14:09:09 -02:00
|
|
|
case '.gbdialog':
|
2018-11-26 15:54:34 -02:00
|
|
|
const vm = new GBVMService();
|
2019-05-15 22:30:14 -03:00
|
|
|
await vm.loadDialogPackage(localPath, min, this.core, this);
|
2019-08-22 17:28:11 -03:00
|
|
|
break;
|
2019-08-23 14:36:47 -03:00
|
|
|
|
2020-05-14 12:46:57 -03:00
|
|
|
case '.gbtheme':
|
|
|
|
const packageName = Path.basename(localPath);
|
2020-05-15 14:07:30 -03:00
|
|
|
GBServer.globals.server.use(`/themes/${packageName}`, express.static(localPath));
|
2020-05-14 12:46:57 -03:00
|
|
|
GBLog.info(`Theme (.gbtheme) assets accessible at: /themes/${packageName}.`);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '.gbapp':
|
|
|
|
await this.callGBAppCompiler(localPath, this.core);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '.gblib':
|
|
|
|
await this.callGBAppCompiler(localPath, this.core);
|
|
|
|
break;
|
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
default:
|
2019-03-09 16:59:31 -03:00
|
|
|
const err = GBError.create(`Unhandled package type: ${packageType}.`);
|
2018-09-24 11:04:36 -03:00
|
|
|
Promise.reject(err);
|
|
|
|
break;
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 11:55:44 -02:00
|
|
|
public async undeployPackageFromLocalPath(instance: IGBInstance, localPath: string) {
|
2018-11-12 12:20:44 -02:00
|
|
|
const packageType = Path.extname(localPath);
|
|
|
|
const packageName = Path.basename(localPath);
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2019-08-23 14:36:47 -03:00
|
|
|
const p = await this.getStoragePackageByName(instance.instanceId, packageName);
|
2020-05-12 19:20:59 -03:00
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
switch (packageType) {
|
2019-08-22 01:54:30 +00:00
|
|
|
case '.gbot':
|
2020-04-19 13:04:38 -03:00
|
|
|
const packageObject = JSON.parse(Fs.readFileSync(urlJoin(localPath, 'package.json'), 'utf8'));
|
2019-08-22 01:54:30 +00:00
|
|
|
await this.undeployBot(packageObject.botId, packageName);
|
|
|
|
break;
|
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
case '.gbkb':
|
|
|
|
const service = new KBService(this.core.sequelize);
|
2020-06-03 21:31:00 -03:00
|
|
|
rimraf.sync(localPath);
|
2020-04-03 02:50:33 -03:00
|
|
|
return await service.undeployKbFromStorage(instance, this, p.packageId);
|
2018-09-09 14:39:37 -03:00
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
case '.gbui':
|
2019-08-22 17:28:11 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '.gbtheme':
|
2020-06-03 21:31:00 -03:00
|
|
|
rimraf.sync(localPath);
|
2018-09-24 11:04:36 -03:00
|
|
|
break;
|
2018-09-09 14:39:37 -03:00
|
|
|
|
2018-11-27 22:56:11 -02:00
|
|
|
case '.gbdialog':
|
2020-06-03 21:31:00 -03:00
|
|
|
rimraf.sync(localPath);
|
2018-11-27 22:56:11 -02:00
|
|
|
break;
|
|
|
|
|
2020-05-14 12:46:57 -03:00
|
|
|
case '.gblib':
|
|
|
|
break;
|
|
|
|
case '.gbapp':
|
|
|
|
break;
|
2018-09-09 14:39:37 -03:00
|
|
|
default:
|
2019-03-09 16:59:31 -03:00
|
|
|
const err = GBError.create(`Unhandled package type: ${packageType}.`);
|
2018-09-24 11:04:36 -03:00
|
|
|
Promise.reject(err);
|
|
|
|
break;
|
2018-09-09 14:39:37 -03:00
|
|
|
}
|
2019-08-23 14:36:47 -03:00
|
|
|
rimraf.sync(localPath);
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
public async rebuildIndex(instance: IGBInstance, searchSchema: any) {
|
2018-11-12 12:20:44 -02:00
|
|
|
const search = new AzureSearch(
|
2018-09-24 11:04:36 -03:00
|
|
|
instance.searchKey,
|
|
|
|
instance.searchHost,
|
|
|
|
instance.searchIndex,
|
2018-11-27 22:56:11 -02:00
|
|
|
instance.searchIndexer
|
2018-09-24 11:04:36 -03:00
|
|
|
);
|
2018-10-22 15:33:23 -03:00
|
|
|
|
2018-11-30 11:55:44 -02:00
|
|
|
const connectionString = GBDeployer.getConnectionStringFromInstance(instance);
|
2018-10-22 15:33:23 -03:00
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
const dsName = 'gb';
|
2018-11-04 09:19:03 -02:00
|
|
|
try {
|
|
|
|
await search.deleteDataSource(dsName);
|
|
|
|
} catch (err) {
|
2019-03-09 16:59:31 -03:00
|
|
|
if (err.code !== 404) {
|
2018-11-04 09:19:03 -02:00
|
|
|
// First time, nothing to delete.
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 10:39:54 -03:00
|
|
|
// TODO: Use temporary names for index for exchanging them after the new one is created.
|
2020-07-18 16:38:58 -03:00
|
|
|
|
2018-11-04 09:19:03 -02:00
|
|
|
try {
|
|
|
|
await search.deleteIndex();
|
|
|
|
} catch (err) {
|
2019-03-09 16:59:31 -03:00
|
|
|
if (err.code !== 404) {
|
2018-11-04 09:19:03 -02:00
|
|
|
// First time, nothing to delete.
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
2020-05-23 17:59:01 -03:00
|
|
|
|
2020-05-23 11:10:06 -03:00
|
|
|
try {
|
|
|
|
await search.createDataSource(dsName, dsName, 'GuaribasQuestion', 'azuresql', connectionString);
|
|
|
|
} catch (err) {
|
|
|
|
GBLog.error(err);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
await search.createIndex(searchSchema, dsName);
|
2018-09-24 11:04:36 -03:00
|
|
|
}
|
|
|
|
|
2019-08-23 14:36:47 -03:00
|
|
|
public async getStoragePackageByName(instanceId: number, packageName: string): Promise<GuaribasPackage> {
|
2018-11-12 12:20:44 -02:00
|
|
|
const where = { packageName: packageName, instanceId: instanceId };
|
2019-03-08 06:49:22 -03:00
|
|
|
|
2019-08-23 14:36:47 -03:00
|
|
|
return await GuaribasPackage.findOne({
|
2018-11-27 22:56:11 -02:00
|
|
|
where: where
|
2018-09-24 11:04:36 -03:00
|
|
|
});
|
2018-09-09 14:39:37 -03:00
|
|
|
}
|
2018-11-30 11:55:44 -02:00
|
|
|
|
2019-10-23 22:05:46 -03:00
|
|
|
public setupDefaultGBUI() {
|
2018-11-30 11:55:44 -02:00
|
|
|
const root = 'packages/default.gbui';
|
2019-05-27 09:26:40 -03:00
|
|
|
const npm = urlJoin(process.env.PWD, 'node_modules', '.bin', 'npm');
|
2018-11-30 11:55:44 -02:00
|
|
|
if (!Fs.existsSync(`${root}/build`)) {
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`Preparing default.gbui (it may take some additional time for the first time)...`);
|
2018-11-30 11:55:44 -02:00
|
|
|
Fs.writeFileSync(`${root}/.env`, 'SKIP_PREFLIGHT_CHECK=true');
|
2019-05-27 09:26:40 -03:00
|
|
|
child_process.execSync(`${npm} install`, { cwd: root });
|
|
|
|
child_process.execSync(`${npm} run build`, { cwd: root });
|
2018-11-30 11:55:44 -02:00
|
|
|
}
|
|
|
|
}
|
2018-12-18 13:50:35 -02:00
|
|
|
|
|
|
|
private async deployDataPackages(
|
2019-03-08 06:37:13 -03:00
|
|
|
core: IGBCoreService,
|
2018-12-18 13:50:35 -02:00
|
|
|
botPackages: string[],
|
|
|
|
_this: this,
|
2020-07-20 16:38:29 -03:00
|
|
|
generalPackages: string[]
|
2018-12-18 13:50:35 -02:00
|
|
|
) {
|
|
|
|
try {
|
|
|
|
await core.syncDatabaseStructure();
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deploys all .gbot files first.
|
|
|
|
|
2020-01-26 17:43:50 -03:00
|
|
|
await CollectionUtil.asyncForEach(botPackages, async e => {
|
2018-12-18 13:50:35 -02:00
|
|
|
if (e !== 'packages\\boot.gbot') {
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`Deploying bot: ${e}...`);
|
2020-02-25 10:13:38 -03:00
|
|
|
await _this.deployBotFromLocalPath(e, GBServer.globals.publicAddress);
|
2020-07-12 10:13:54 -03:00
|
|
|
GBLog.info(`√ Bot: ${e} deployed...`);
|
2018-12-18 13:50:35 -02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then all remaining generalPackages are loaded.
|
|
|
|
|
2020-07-20 16:38:29 -03:00
|
|
|
const instances = core.loadInstances();
|
|
|
|
await CollectionUtil.asyncForEach(instances, async instance => {
|
|
|
|
this.mountGBKBAssets(`{instance.botId}.gbkb`, instance.botId, `{instance.botId}.gbkb`);
|
2018-12-18 13:50:35 -02:00
|
|
|
});
|
|
|
|
|
2020-05-15 14:07:30 -03:00
|
|
|
GBLog.info(`Package deployment done.`);
|
2020-08-15 11:39:43 -03:00
|
|
|
return { generalPackages };
|
2018-12-18 13:50:35 -02:00
|
|
|
}
|
|
|
|
|
2020-07-20 16:38:29 -03:00
|
|
|
public mountGBKBAssets(packageName: any, botId: string, filename: string) {
|
2020-08-15 11:39:43 -03:00
|
|
|
GBServer.globals.server.use(
|
|
|
|
`/kb/${botId}.gbai/${packageName}/subjects`,
|
|
|
|
express.static(urlJoin(filename, 'subjects'))
|
|
|
|
);
|
2020-07-20 16:38:29 -03:00
|
|
|
GBServer.globals.server.use(`/kb/${botId}.gbai/${packageName}/assets`, express.static(urlJoin(filename, 'assets')));
|
|
|
|
GBServer.globals.server.use(`/kb/${botId}.gbai/${packageName}/images`, express.static(urlJoin(filename, 'images')));
|
|
|
|
GBServer.globals.server.use(`/kb/${botId}.gbai/${packageName}/audios`, express.static(urlJoin(filename, 'audios')));
|
|
|
|
GBServer.globals.server.use(`/kb/${botId}.gbai/${packageName}/videos`, express.static(urlJoin(filename, 'videos')));
|
|
|
|
GBLog.info(`KB (.gbkb) assets accessible at: /kb/${botId}.gbai/${packageName}.`);
|
2019-08-24 18:46:04 -03:00
|
|
|
}
|
|
|
|
|
2019-05-27 08:16:43 -03:00
|
|
|
private isSystemPackage(name: string): Boolean {
|
2020-08-15 11:39:43 -03:00
|
|
|
const names = [
|
|
|
|
'analytics.gblib',
|
|
|
|
'console.gblib',
|
|
|
|
'security.gbapp',
|
|
|
|
'whatsapp.gblib',
|
|
|
|
'sharepoint.gblib',
|
|
|
|
'core.gbapp',
|
|
|
|
'admin.gbapp',
|
|
|
|
'azuredeployer.gbapp',
|
|
|
|
'customer-satisfaction.gbapp',
|
|
|
|
'kb.gbapp'
|
|
|
|
];
|
2019-05-27 08:16:43 -03:00
|
|
|
|
|
|
|
return names.indexOf(name) > -1;
|
|
|
|
}
|
|
|
|
|
2020-01-26 17:43:50 -03:00
|
|
|
private async deployAppPackages(gbappPackages: string[], core: any, appPackages: any[]) {
|
2018-12-18 13:50:35 -02:00
|
|
|
let appPackagesProcessed = 0;
|
2020-01-26 17:43:50 -03:00
|
|
|
await CollectionUtil.asyncForEach(gbappPackages, async e => {
|
2019-05-27 08:16:43 -03:00
|
|
|
const filenameOnly = Path.basename(e);
|
|
|
|
|
2018-12-18 13:50:35 -02:00
|
|
|
// Skips .gbapp inside deploy folder.
|
2019-05-27 08:16:43 -03:00
|
|
|
if (this.isSystemPackage(filenameOnly) === false) {
|
2020-05-14 12:46:57 -03:00
|
|
|
appPackagesProcessed = await this.callGBAppCompiler(e, core, appPackages, appPackagesProcessed);
|
2018-12-18 13:50:35 -02:00
|
|
|
}
|
|
|
|
});
|
2019-03-08 06:49:22 -03:00
|
|
|
|
2018-12-18 13:50:35 -02:00
|
|
|
return appPackagesProcessed;
|
|
|
|
}
|
2020-05-14 12:46:57 -03:00
|
|
|
|
2020-08-15 11:39:43 -03:00
|
|
|
public async callGBAppCompiler(
|
|
|
|
gbappPath: string,
|
|
|
|
core: IGBCoreService,
|
|
|
|
appPackages: any[] = undefined,
|
|
|
|
appPackagesProcessed: number = 0
|
|
|
|
) {
|
2020-05-14 12:48:36 -03:00
|
|
|
GBLog.info(`Deploying General Bots Application (.gbapp) or Library (.gblib): ${Path.basename(gbappPath)}...`);
|
2020-05-14 12:46:57 -03:00
|
|
|
let folder = Path.join(gbappPath, 'node_modules');
|
2020-08-15 11:39:43 -03:00
|
|
|
if (process.env.GBAPP_DISABLE_COMPILE !== 'true') {
|
2020-05-14 12:46:57 -03:00
|
|
|
if (!Fs.existsSync(folder)) {
|
|
|
|
GBLog.info(`Installing modules for ${gbappPath}...`);
|
|
|
|
child_process.execSync('npm install', { cwd: gbappPath });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
folder = Path.join(gbappPath, 'dist');
|
|
|
|
try {
|
2020-08-15 11:39:43 -03:00
|
|
|
if (process.env.GBAPP_DISABLE_COMPILE !== 'true') {
|
2020-05-14 12:46:57 -03:00
|
|
|
GBLog.info(`Compiling: ${gbappPath}.`);
|
|
|
|
child_process.execSync(Path.join(process.env.PWD, 'node_modules/.bin/tsc'), { cwd: gbappPath });
|
|
|
|
}
|
2020-07-03 14:35:42 -03:00
|
|
|
|
2020-07-18 16:38:58 -03:00
|
|
|
if (gbappPath.endsWith('.gbapp')) {
|
2020-07-03 14:35:42 -03:00
|
|
|
const m = await import(gbappPath);
|
|
|
|
const p = new m.Package();
|
|
|
|
await p.loadPackage(core, core.sequelize);
|
|
|
|
if (appPackages !== undefined) {
|
|
|
|
appPackages.push(p);
|
|
|
|
}
|
2020-05-14 12:46:57 -03:00
|
|
|
}
|
2020-07-18 16:38:58 -03:00
|
|
|
|
2020-05-14 12:46:57 -03:00
|
|
|
GBLog.info(`.gbapp or .gblib deployed: ${gbappPath}.`);
|
|
|
|
appPackagesProcessed++;
|
2020-08-15 11:39:43 -03:00
|
|
|
} catch (error) {
|
2020-07-18 16:38:58 -03:00
|
|
|
GBLog.error(`Error compiling package, message: ${error.message}\n${error.stack}`);
|
|
|
|
if (error.stdout) {
|
|
|
|
GBLog.error(`Error compiling package, stdout: ${gbappPath}:\n${error.stdout.toString()}`);
|
|
|
|
}
|
2020-05-14 12:46:57 -03:00
|
|
|
appPackagesProcessed++;
|
|
|
|
}
|
|
|
|
return appPackagesProcessed;
|
|
|
|
}
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|