2018-04-21 02:59:30 -03:00
|
|
|
/*****************************************************************************\
|
|
|
|
| ( )_ _ |
|
|
|
|
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
|
|
|
|
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' _ `\ /'_`\ |
|
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 WaitUntil = require('wait-until');
|
|
|
|
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
|
|
|
|
2019-03-09 16:59:31 -03:00
|
|
|
import { GBError, GBLog, GBMinInstance, IGBCoreService, IGBInstance, IGBPackage } 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';
|
2019-06-05 18:23:31 -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';
|
2019-08-22 01:54:30 +00:00
|
|
|
import { min } from 'moment';
|
|
|
|
import { GBMinService } from './GBMinService';
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2019-01-31 11:32:33 -02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Deployer service for bots, themes, ai and more.
|
|
|
|
*/
|
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
export class GBDeployer {
|
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) {
|
2019-05-14 23:02:21 -03:00
|
|
|
return `Server=tcp:${instance.storageServer}.database.windows.net,1433;Database=${
|
|
|
|
instance.storageName
|
|
|
|
};User ID=${
|
2018-11-12 12:20:44 -02:00
|
|
|
instance.storageUsername
|
2019-05-14 23:02:21 -03:00
|
|
|
};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
|
|
|
|
2018-11-26 14:09:09 -02:00
|
|
|
return new Promise(
|
|
|
|
(resolve: any, reject: any): any => {
|
2019-05-16 12:36:11 -03:00
|
|
|
GBLog.info(`PWD ${process.env.PWD}...`);
|
2018-11-26 14:09:09 -02:00
|
|
|
let totalPackages = 0;
|
2019-08-22 01:54:30 +00:00
|
|
|
let paths = [urlJoin(process.env.PWD, GBDeployer.deployFolder), urlJoin(process.env.PWD, GBDeployer.workFolder)];
|
2018-11-26 14:09:09 -02:00
|
|
|
const additionalPath = GBConfigService.get('ADDITIONAL_DEPLOY_PATH');
|
2019-04-01 09:01:07 -03:00
|
|
|
if (additionalPath !== undefined && additionalPath !== '') {
|
2018-11-26 14:09:09 -02:00
|
|
|
paths = paths.concat(additionalPath.toLowerCase().split(';'));
|
|
|
|
}
|
2019-03-11 19:32:47 -03:00
|
|
|
const botPackages: string[] = [];
|
|
|
|
const gbappPackages: string[] = [];
|
|
|
|
let generalPackages: string[] = [];
|
2018-11-26 14:09:09 -02:00
|
|
|
|
|
|
|
function doIt(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);
|
|
|
|
dirs.forEach(element => {
|
|
|
|
if (element.startsWith('.')) {
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`Ignoring ${element}...`);
|
2018-09-11 19:33:58 -03:00
|
|
|
} else {
|
2018-11-26 14:09:09 -02:00
|
|
|
if (element.endsWith('.gbot')) {
|
|
|
|
botPackages.push(element);
|
|
|
|
} else if (element.endsWith('.gbapp')) {
|
|
|
|
gbappPackages.push(element);
|
|
|
|
} else {
|
|
|
|
generalPackages.push(element);
|
|
|
|
}
|
2018-09-10 12:09:48 -03:00
|
|
|
}
|
2018-11-26 14:09:09 -02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`Starting looking for packages (.gbot, .gbtheme, .gbkb, .gbapp)...`);
|
2018-11-26 14:09:09 -02:00
|
|
|
paths.forEach(e => {
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`Looking in: ${e}...`);
|
2018-11-26 14:09:09 -02:00
|
|
|
doIt(e);
|
|
|
|
});
|
|
|
|
|
2018-12-18 13:50:35 -02:00
|
|
|
// Deploys all .gbapp files first.
|
|
|
|
|
|
|
|
const appPackagesProcessed = this.deployAppPackages(gbappPackages, core, appPackages);
|
2018-09-10 12:09:48 -03:00
|
|
|
|
2018-11-26 14:09:09 -02:00
|
|
|
WaitUntil()
|
|
|
|
.interval(1000)
|
2019-06-26 13:18:15 -03:00
|
|
|
.times(5)
|
2018-12-18 13:50:35 -02:00
|
|
|
.condition(cb => {
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`Waiting for app package deployment...`);
|
2018-12-18 13:50:35 -02:00
|
|
|
cb(appPackagesProcessed === gbappPackages.length);
|
2018-11-26 14:09:09 -02:00
|
|
|
})
|
2019-03-09 16:59:31 -03:00
|
|
|
.done(async () => {
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`App Package deployment done.`);
|
2018-11-26 14:09:09 -02:00
|
|
|
|
2018-12-18 13:50:35 -02:00
|
|
|
({ generalPackages, totalPackages } = await this.deployDataPackages(
|
2019-05-14 23:02:21 -03:00
|
|
|
|
2018-12-18 13:50:35 -02:00
|
|
|
core,
|
|
|
|
botPackages,
|
|
|
|
_this,
|
|
|
|
generalPackages,
|
|
|
|
server,
|
|
|
|
reject,
|
|
|
|
totalPackages,
|
|
|
|
resolve
|
|
|
|
));
|
2018-11-26 14:09:09 -02:00
|
|
|
});
|
2018-11-27 22:56:11 -02:00
|
|
|
}
|
2018-11-26 14:09:09 -02:00
|
|
|
);
|
2018-09-10 12:09:48 -03:00
|
|
|
}
|
|
|
|
|
2018-09-11 19:33:58 -03:00
|
|
|
/**
|
|
|
|
* Deploys a bot to the storage.
|
2018-09-09 14:39:37 -03:00
|
|
|
*/
|
|
|
|
|
2019-08-22 01:54:30 +00:00
|
|
|
public async deployBot(localPath: string, publicAddress: string): Promise<void> {
|
2018-11-12 12:20:44 -02:00
|
|
|
const packageName = Path.basename(localPath);
|
2018-11-28 17:08:06 -02:00
|
|
|
|
2019-05-14 23:02:21 -03:00
|
|
|
const service = new AzureDeployerService(this);
|
|
|
|
let instance = await this.importer.importIfNotExistsBotPackage(undefined, packageName, localPath);
|
|
|
|
|
|
|
|
const username = GBConfigService.get('CLOUD_USERNAME');
|
|
|
|
const password = GBConfigService.get('CLOUD_PASSWORD');
|
|
|
|
const group = GBConfigService.get('CLOUD_GROUP');
|
|
|
|
const subscriptionId = GBConfigService.get('CLOUD_SUBSCRIPTIONID');
|
|
|
|
const accessToken = await GBAdminService.getADALTokenFromUsername(username, password);
|
|
|
|
|
2019-08-22 01:54:30 +00:00
|
|
|
if (await service.botExists(instance.botId, group)) {
|
|
|
|
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-22 17:28:11 -03:00
|
|
|
|
2019-08-23 02:23:00 -03:00
|
|
|
let botId = GBConfigService.get('BOT_ID');
|
|
|
|
let bootInstance = await this.core.loadInstance(botId);
|
|
|
|
|
|
|
|
instance.searchHost = bootInstance.searchHost;
|
|
|
|
instance.searchIndex = bootInstance.searchIndex;
|
|
|
|
instance.searchIndexer = bootInstance.searchIndexer;
|
|
|
|
instance.searchKey = bootInstance.searchKey;
|
|
|
|
instance.whatsappServiceKey = bootInstance.whatsappServiceKey;
|
|
|
|
instance.whatsappBotKey = bootInstance.whatsappBotKey;
|
|
|
|
instance.whatsappServiceNumber = bootInstance.whatsappServiceNumber;
|
|
|
|
instance.whatsappServiceUrl = bootInstance.whatsappServiceUrl;
|
|
|
|
instance.whatsappServiceWebhookUrl = bootInstance.whatsappServiceWebhookUrl;
|
|
|
|
|
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
|
|
|
}
|
2019-05-15 12:41:04 -03:00
|
|
|
await this.core.saveInstance(instance);
|
2019-08-23 02:23:00 -03:00
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
if (await service.botExists(botId, group)) {
|
|
|
|
|
|
|
|
await service.deleteBot(
|
|
|
|
botId, group
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
GBServer.globals.minService.unmountBot(botId);
|
|
|
|
await this.core.deleteInstance(botId);
|
2019-08-22 17:28:11 -03:00
|
|
|
const packageFolder =Path.join(process.env.PWD, 'work', packageName);
|
|
|
|
rimraf.sync(packageFolder);
|
2019-08-22 01:54:30 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 17:28:11 -03:00
|
|
|
public async undeployTheme(packageName: string): Promise<void> {
|
|
|
|
const packageFolder = Path.join(process.env.PWD, 'work', packageName);
|
|
|
|
rimraf.sync(packageFolder);
|
|
|
|
}
|
2019-08-22 01:54:30 +00: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
|
|
|
}
|
|
|
|
});
|
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
|
|
|
|
|
|
|
switch (packageType) {
|
2018-11-12 12:20:44 -02:00
|
|
|
case '.gbot':
|
2019-06-05 18:23:31 -03:00
|
|
|
await this.deployBot(localPath, GBServer.globals.publicAddress);
|
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);
|
2019-05-15 12:41:04 -03:00
|
|
|
await service.deployKb(this.core, this, localPath);
|
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;
|
|
|
|
|
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
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
const p = await this.getPackageByName(instance.instanceId, packageName);
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2019-08-22 01:54:30 +00:00
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
switch (packageType) {
|
2019-08-22 01:54:30 +00:00
|
|
|
case '.gbot':
|
|
|
|
const packageObject = JSON.parse(Fs.readFileSync(urlJoin(localPath, 'package.json'), 'utf8'));
|
|
|
|
await this.undeployBot(packageObject.botId, packageName);
|
|
|
|
break;
|
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
case '.gbkb':
|
|
|
|
const service = new KBService(this.core.sequelize);
|
2018-09-24 11:04:36 -03:00
|
|
|
return 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':
|
|
|
|
this.undeployTheme(packageName);
|
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':
|
2019-08-22 17:28:11 -03:00
|
|
|
|
2018-11-27 22:56:11 -02:00
|
|
|
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
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 11:55:44 -02:00
|
|
|
await search.createDataSource(dsName, dsName, 'GuaribasQuestion', 'azuresql', connectionString);
|
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;
|
|
|
|
}
|
|
|
|
}
|
2019-03-08 06:37:13 -03:00
|
|
|
await search.createIndex(searchSchema, dsName);
|
2018-09-24 11:04:36 -03:00
|
|
|
}
|
|
|
|
|
2018-11-30 11:55:44 -02:00
|
|
|
public async getPackageByName(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
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
return 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-03-08 06:37:13 -03:00
|
|
|
public runOnce() {
|
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-05-14 23:02:21 -03:00
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
core: IGBCoreService,
|
2018-12-18 13:50:35 -02:00
|
|
|
botPackages: string[],
|
|
|
|
_this: this,
|
|
|
|
generalPackages: string[],
|
|
|
|
server: any,
|
|
|
|
reject: any,
|
|
|
|
totalPackages: number,
|
|
|
|
resolve: any
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
await core.syncDatabaseStructure();
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deploys all .gbot files first.
|
|
|
|
|
2019-05-15 12:41:04 -03:00
|
|
|
botPackages.forEach(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}...`);
|
2019-05-27 09:14:49 -03:00
|
|
|
await _this.deployBot(e, GBServer.globals.publicAddress);
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`Bot: ${e} deployed...`);
|
2018-12-18 13:50:35 -02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Then all remaining generalPackages are loaded.
|
|
|
|
|
|
|
|
generalPackages = generalPackages.filter(p => !p.endsWith('.git'));
|
|
|
|
generalPackages.forEach(filename => {
|
|
|
|
const filenameOnly = Path.basename(filename);
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`Deploying package: ${filename}...`);
|
2018-12-18 13:50:35 -02:00
|
|
|
|
|
|
|
// Handles apps for general bots - .gbapp must stay out of deploy folder.
|
|
|
|
|
|
|
|
if (Path.extname(filename) === '.gbapp' || Path.extname(filename) === '.gblib') {
|
|
|
|
// Themes for bots.
|
|
|
|
} else if (Path.extname(filename) === '.gbtheme') {
|
2019-03-09 16:59:31 -03:00
|
|
|
server.use(`/themes/${filenameOnly}`, express.static(filename));
|
|
|
|
GBLog.info(`Theme (.gbtheme) assets accessible at: /themes/${filenameOnly}.`);
|
2018-12-18 13:50:35 -02:00
|
|
|
} else if (Path.extname(filename) === '.gbkb') {
|
2019-03-09 16:59:31 -03:00
|
|
|
server.use(`/kb/${filenameOnly}/subjects`, express.static(urlJoin(filename, 'subjects')));
|
2019-06-05 18:23:31 -03:00
|
|
|
server.use(`/kb/${filenameOnly}/images`, express.static(urlJoin(filename, 'images')));
|
2019-03-09 16:59:31 -03:00
|
|
|
GBLog.info(`KB (.gbkb) assets accessible at: /kb/${filenameOnly}.`);
|
2018-12-18 13:50:35 -02:00
|
|
|
} else if (Path.extname(filename) === '.gbui') {
|
|
|
|
// Already Handled
|
|
|
|
} else if (Path.extname(filename) === '.gbdialog') {
|
|
|
|
// Already Handled
|
2019-05-15 22:30:14 -03:00
|
|
|
} else if (Path.extname(filename) === '.gbignore') {
|
|
|
|
// Ignored
|
2018-12-18 13:50:35 -02:00
|
|
|
} else {
|
|
|
|
// Unknown package format.
|
|
|
|
const err = new Error(`Package type not handled: ${filename}.`);
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
totalPackages++;
|
|
|
|
});
|
|
|
|
|
|
|
|
WaitUntil()
|
|
|
|
.interval(100)
|
|
|
|
.times(5)
|
|
|
|
.condition(cb => {
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`Waiting for package deployment...`);
|
2018-12-18 13:50:35 -02:00
|
|
|
cb(totalPackages === generalPackages.length);
|
|
|
|
})
|
2019-03-09 16:59:31 -03:00
|
|
|
.done(() => {
|
2018-12-18 13:50:35 -02:00
|
|
|
if (botPackages.length === 0) {
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info('Use ADDITIONAL_DEPLOY_PATH to point to a .gbai package folder (no external packages).');
|
2018-12-18 13:50:35 -02:00
|
|
|
} else {
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`Package deployment done.`);
|
2018-12-18 13:50:35 -02:00
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
return { generalPackages, totalPackages };
|
|
|
|
}
|
|
|
|
|
2019-05-27 08:16:43 -03:00
|
|
|
private isSystemPackage(name: string): Boolean {
|
|
|
|
const names = ['core.gbapp', 'admin.gbapp', 'azuredeployer.gbapp', 'customer-satisfaction.gbapp', 'kb.gbapp'];
|
|
|
|
|
|
|
|
return names.indexOf(name) > -1;
|
|
|
|
}
|
|
|
|
|
2018-12-18 13:50:35 -02:00
|
|
|
private deployAppPackages(gbappPackages: string[], core: any, appPackages: any[]) {
|
|
|
|
let appPackagesProcessed = 0;
|
|
|
|
gbappPackages.forEach(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) {
|
2018-12-26 18:57:25 -02:00
|
|
|
|
2019-05-27 08:16:43 -03:00
|
|
|
GBLog.info(`Deploying app: ${e}...`);
|
2018-12-26 18:57:25 -02:00
|
|
|
let folder = Path.join(e, 'node_modules');
|
|
|
|
if (!Fs.existsSync(folder)) {
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`Installing modules for ${e}...`);
|
2018-12-26 18:57:25 -02:00
|
|
|
child_process.execSync('npm install', { cwd: e });
|
|
|
|
}
|
|
|
|
|
|
|
|
folder = Path.join(e, 'dist');
|
|
|
|
if (!Fs.existsSync()) {
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`Compiling ${e}...`);
|
2018-12-26 18:57:25 -02:00
|
|
|
|
|
|
|
try {
|
2019-06-26 13:18:15 -03:00
|
|
|
child_process.execSync(Path.join(process.env.PWD, 'node_modules/.bin/tsc'), { cwd: e });
|
2018-12-26 18:57:25 -02:00
|
|
|
import(e)
|
2019-01-31 11:32:33 -02:00
|
|
|
.then(m => {
|
|
|
|
const p = new m.Package();
|
|
|
|
p.loadPackage(core, core.sequelize);
|
|
|
|
appPackages.push(p);
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.info(`App (.gbapp) deployed: ${e}.`);
|
2019-01-31 11:32:33 -02:00
|
|
|
appPackagesProcessed++;
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.error(`Error deploying .gbapp package: ${e}\n${err}`);
|
2019-01-31 11:32:33 -02:00
|
|
|
appPackagesProcessed++;
|
|
|
|
});
|
2018-12-26 18:57:25 -02:00
|
|
|
} catch (error) {
|
2019-03-08 17:05:58 -03:00
|
|
|
GBLog.error(`Error compiling .gbapp package ${e}:\n${error.stdout.toString()}`);
|
2018-12-18 13:50:35 -02:00
|
|
|
appPackagesProcessed++;
|
2018-12-26 18:57:25 -02:00
|
|
|
}
|
|
|
|
}
|
2018-12-18 13:50:35 -02:00
|
|
|
} else {
|
|
|
|
appPackagesProcessed++;
|
|
|
|
}
|
|
|
|
});
|
2019-03-08 06:49:22 -03:00
|
|
|
|
2018-12-18 13:50:35 -02:00
|
|
|
return appPackagesProcessed;
|
|
|
|
}
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|