2018-09-10 12:09:48 -03:00
|
|
|
import { IGBPackage } from 'botlib';
|
2018-04-21 02:59:30 -03:00
|
|
|
/*****************************************************************************\
|
|
|
|
| ( )_ _ |
|
|
|
|
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
|
|
|
|
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' _ `\ /'_`\ |
|
|
|
|
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| ( ) |( (_) ) |
|
|
|
|
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
|
|
|
|
| | | ( )_) | |
|
|
|
|
| (_) \___/' |
|
|
|
|
| |
|
|
|
|
| General Bots Copyright (c) Pragmatismo.io. All rights reserved. |
|
|
|
|
| Licensed under the AGPL-3.0. |
|
|
|
|
| |
|
|
|
|
| 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, |
|
|
|
|
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
|
|
| 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. |
|
|
|
|
| |
|
|
|
|
\*****************************************************************************/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const logger = require("../../../src/logger");
|
|
|
|
const Path = require("path");
|
|
|
|
const UrlJoin = require("url-join");
|
2018-09-10 12:09:48 -03:00
|
|
|
const Fs = require("fs");
|
|
|
|
const WaitUntil = require("wait-until");
|
|
|
|
const express = require("express");
|
2018-04-21 02:59:30 -03:00
|
|
|
|
|
|
|
import { KBService } from './../../kb.gbapp/services/KBService';
|
|
|
|
import { GBImporter } from "./GBImporter";
|
2018-09-10 12:09:48 -03:00
|
|
|
import { IGBCoreService, IGBInstance } from "botlib";
|
2018-04-21 02:59:30 -03:00
|
|
|
import { GBConfigService } from "./GBConfigService";
|
2018-09-09 14:39:37 -03:00
|
|
|
import { GBError } from "botlib";
|
2018-04-21 02:59:30 -03:00
|
|
|
import { GuaribasPackage } from '../models/GBModel';
|
|
|
|
|
|
|
|
/** Deployer service for bots, themes, ai and more. */
|
|
|
|
export class GBDeployer {
|
|
|
|
core: IGBCoreService;
|
|
|
|
|
|
|
|
importer: GBImporter;
|
|
|
|
|
|
|
|
workDir: string = "./work";
|
|
|
|
|
2018-09-10 12:09:48 -03:00
|
|
|
static deployFolder = "deploy";
|
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
constructor(core: IGBCoreService, importer: GBImporter) {
|
|
|
|
this.core = core;
|
|
|
|
this.importer = importer;
|
|
|
|
}
|
|
|
|
|
2018-09-10 12:09:48 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Performs package deployment in all .gbai or default.
|
|
|
|
*
|
|
|
|
* */
|
|
|
|
public deployPackages(core: IGBCoreService, server: any, appPackages: Array<IGBPackage>) {
|
|
|
|
let _this = this;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
try {
|
|
|
|
let totalPackages = 0;
|
|
|
|
let additionalPath = GBConfigService.get("ADDITIONAL_DEPLOY_PATH");
|
|
|
|
let paths = [GBDeployer.deployFolder];
|
|
|
|
if (additionalPath) {
|
|
|
|
paths = paths.concat(additionalPath.toLowerCase().split(";"));
|
|
|
|
}
|
|
|
|
let botPackages = new Array<string>();
|
|
|
|
let gbappPackages = new Array<string>();
|
|
|
|
let generalPackages = new Array<string>();
|
|
|
|
|
|
|
|
function doIt(path) {
|
|
|
|
const isDirectory = source => Fs.lstatSync(source).isDirectory()
|
|
|
|
const getDirectories = source =>
|
|
|
|
Fs.readdirSync(source).map(name => Path.join(source, name)).filter(isDirectory)
|
|
|
|
|
|
|
|
let dirs = getDirectories(path);
|
|
|
|
dirs.forEach(element => {
|
|
|
|
if (element.startsWith('.')) {
|
|
|
|
logger.info(`Ignoring ${element}...`);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (element.endsWith('.gbot')) {
|
|
|
|
botPackages.push(element);
|
|
|
|
}
|
|
|
|
else if (element.endsWith('.gbapp')) {
|
|
|
|
gbappPackages.push(element);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
generalPackages.push(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info(`Starting looking for packages (.gbot, .gbtheme, .gbkb, .gbapp)...`);
|
|
|
|
paths.forEach(e => {
|
|
|
|
logger.info(`Looking in: ${e}...`);
|
|
|
|
doIt(e);
|
|
|
|
});
|
|
|
|
|
|
|
|
/** Deploys all .gbapp files first. */
|
|
|
|
|
|
|
|
let appPackagesProcessed = 0;
|
|
|
|
|
|
|
|
gbappPackages.forEach(e => {
|
|
|
|
logger.info(`Deploying app: ${e}...`);
|
|
|
|
|
|
|
|
// Skips .gbapp inside deploy folder.
|
|
|
|
if (!e.startsWith('deploy')) {
|
|
|
|
import(e).then(m => {
|
|
|
|
let p = new m.Package();
|
|
|
|
p.loadPackage(core, core.sequelize);
|
|
|
|
appPackages.push(p);
|
|
|
|
logger.info(`App (.gbapp) deployed: ${e}.`);
|
|
|
|
appPackagesProcessed++;
|
|
|
|
}).catch(err => {
|
|
|
|
logger.info(`Error deploying App (.gbapp): ${e}: ${err}`);
|
|
|
|
appPackagesProcessed++;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
appPackagesProcessed++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
WaitUntil()
|
|
|
|
.interval(1000)
|
|
|
|
.times(10)
|
|
|
|
.condition(function (cb) {
|
|
|
|
logger.info(`Waiting for app package deployment...`);
|
|
|
|
cb(appPackagesProcessed == gbappPackages.length);
|
|
|
|
})
|
|
|
|
.done(function (result) {
|
|
|
|
logger.info(`App Package deployment done.`);
|
|
|
|
|
|
|
|
core.syncDatabaseStructure();
|
|
|
|
|
|
|
|
/** Deploys all .gbot files first. */
|
|
|
|
|
|
|
|
botPackages.forEach(e => {
|
|
|
|
logger.info(`Deploying bot: ${e}...`);
|
|
|
|
_this.deployBot(e);
|
|
|
|
logger.info(`Bot: ${e} deployed...`);
|
|
|
|
});
|
|
|
|
|
|
|
|
/** Then all remaining generalPackages are loaded. */
|
|
|
|
|
|
|
|
generalPackages.forEach(filename => {
|
|
|
|
|
|
|
|
let filenameOnly = Path.basename(filename);
|
|
|
|
logger.info(`Deploying package: ${filename}...`);
|
|
|
|
|
|
|
|
/** 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") {
|
|
|
|
server.use("/themes/" + filenameOnly, express.static(filename));
|
|
|
|
logger.info(`Theme (.gbtheme) assets accessible at: ${"/themes/" + filenameOnly}.`);
|
|
|
|
|
|
|
|
|
|
|
|
/** Knowledge base for bots. */
|
|
|
|
|
|
|
|
} else if (Path.extname(filename) === ".gbkb") {
|
|
|
|
server.use(
|
|
|
|
"/kb/" + filenameOnly + "/subjects",
|
|
|
|
express.static(UrlJoin(filename, "subjects"))
|
|
|
|
);
|
|
|
|
logger.info(`KB (.gbkb) assets accessible at: ${"/kb/" + filenameOnly}.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (Path.extname(filename) === ".gbui" || filename.endsWith(".git")) {
|
|
|
|
// Already Handled
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Unknown package format. */
|
|
|
|
|
|
|
|
else {
|
|
|
|
let err = new Error(`Package type not handled: ${filename}.`);
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
totalPackages++;
|
|
|
|
});
|
|
|
|
|
|
|
|
WaitUntil()
|
|
|
|
.interval(1000)
|
|
|
|
.times(5)
|
|
|
|
.condition(function (cb) {
|
|
|
|
logger.info(`Waiting for package deployment...`);
|
|
|
|
cb(totalPackages == (generalPackages.length));
|
|
|
|
})
|
|
|
|
.done(function (result) {
|
|
|
|
if (botPackages.length === 0) {
|
|
|
|
logger.info(`The bot server is running empty: No bot instances have been found, at least one .gbot file must be deployed.`);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logger.info(`Package deployment done.`);
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
logger.error(err);
|
|
|
|
reject(err)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
/**
|
|
|
|
* Deploys a bot to the storage.
|
|
|
|
*/
|
|
|
|
|
|
|
|
async deployBot(localPath: string): Promise<IGBInstance> {
|
2018-04-21 02:59:30 -03:00
|
|
|
let packageType = Path.extname(localPath);
|
|
|
|
let packageName = Path.basename(localPath);
|
2018-09-09 14:39:37 -03:00
|
|
|
let instance = await this.importer.importIfNotExistsBotPackage(
|
2018-04-21 02:59:30 -03:00
|
|
|
packageName,
|
2018-09-09 14:39:37 -03:00
|
|
|
localPath
|
2018-04-21 02:59:30 -03:00
|
|
|
);
|
2018-09-09 14:39:37 -03:00
|
|
|
return instance;
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
async deployPackageToStorage(
|
2018-04-21 02:59:30 -03:00
|
|
|
instanceId: number,
|
2018-09-09 14:39:37 -03:00
|
|
|
packageName: string): Promise<GuaribasPackage> {
|
|
|
|
return GuaribasPackage.create({
|
2018-04-21 02:59:30 -03:00
|
|
|
packageName: packageName,
|
|
|
|
instanceId: instanceId
|
|
|
|
});
|
2018-09-09 14:39:37 -03:00
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
deployTheme(localPath: string) {
|
2018-04-21 02:59:30 -03:00
|
|
|
// DISABLED: Until completed, "/ui/public".
|
|
|
|
// FsExtra.copy(localPath, this.workDir + packageName)
|
|
|
|
// .then(() => {
|
2018-09-09 14:39:37 -03:00
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
// })
|
|
|
|
// .catch(err => {
|
|
|
|
// var gberr = GBError.create(
|
|
|
|
// `GuaribasBusinessError: Error copying package: ${localPath}.`
|
|
|
|
// );
|
|
|
|
// });
|
|
|
|
}
|
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
async deployPackageFromLocalPath(localPath: string) {
|
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
let packageType = Path.extname(localPath);
|
|
|
|
|
|
|
|
switch (packageType) {
|
|
|
|
case ".gbot":
|
2018-09-09 14:39:37 -03:00
|
|
|
return this.deployBot(localPath);
|
2018-04-21 02:59:30 -03:00
|
|
|
|
|
|
|
case ".gbtheme":
|
2018-09-09 14:39:37 -03:00
|
|
|
return this.deployTheme(localPath);
|
2018-04-21 02:59:30 -03:00
|
|
|
|
|
|
|
// PACKAGE: Put in package logic.
|
|
|
|
case ".gbkb":
|
2018-09-10 19:27:56 -03:00
|
|
|
let service = new KBService(this.core.sequelize);
|
2018-09-09 14:39:37 -03:00
|
|
|
return service.deployKb(this.core, this, localPath);
|
2018-04-21 02:59:30 -03:00
|
|
|
|
|
|
|
case ".gbui":
|
2018-09-09 14:39:37 -03:00
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
var err = GBError.create(
|
|
|
|
`GuaribasBusinessError: Unknow package type: ${packageType}.`
|
|
|
|
);
|
2018-09-09 14:39:37 -03:00
|
|
|
Promise.reject(err);
|
2018-04-21 02:59:30 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
async undeployPackageFromLocalPath(
|
2018-04-21 02:59:30 -03:00
|
|
|
instance: IGBInstance,
|
2018-09-09 14:39:37 -03:00
|
|
|
localPath: string
|
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
) {
|
|
|
|
let packageType = Path.extname(localPath);
|
|
|
|
let packageName = Path.basename(localPath);
|
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
let p = await this.getPackageByName(instance.instanceId, packageName);
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
switch (packageType) {
|
|
|
|
case ".gbot":
|
|
|
|
// TODO: this.undeployBot(packageName, localPath);
|
|
|
|
break;
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
case ".gbtheme":
|
|
|
|
// TODO: this.undeployTheme(packageName, localPath);
|
|
|
|
break;
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
case ".gbkb":
|
2018-09-10 19:27:56 -03:00
|
|
|
let service = new KBService(this.core.sequelize);
|
2018-09-09 14:39:37 -03:00
|
|
|
return service.undeployKbFromStorage(instance, p.packageId);
|
|
|
|
|
|
|
|
case ".gbui":
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
var err = GBError.create(
|
|
|
|
`GuaribasBusinessError: Unknown package type: ${packageType}.`
|
|
|
|
);
|
|
|
|
Promise.reject(err);
|
|
|
|
break;
|
|
|
|
}
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
async getPackageByName(instanceId: number, packageName: string):
|
|
|
|
Promise<GuaribasPackage> {
|
|
|
|
var where = { packageName: packageName, instanceId: instanceId };
|
|
|
|
return GuaribasPackage.findOne({
|
|
|
|
where: where
|
|
|
|
});
|
|
|
|
}
|
2018-04-21 02:59:30 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Hot deploy processing.
|
|
|
|
*
|
|
|
|
*/
|
2018-09-09 14:39:37 -03:00
|
|
|
async scanBootPackage() {
|
2018-04-21 02:59:30 -03:00
|
|
|
|
|
|
|
const deployFolder = "deploy";
|
|
|
|
let bootPackage = GBConfigService.get("BOOT_PACKAGE");
|
|
|
|
|
|
|
|
if (bootPackage === "none") {
|
2018-09-09 14:39:37 -03:00
|
|
|
return Promise.resolve(true);
|
2018-04-21 02:59:30 -03:00
|
|
|
} else {
|
2018-09-09 14:39:37 -03:00
|
|
|
return this.deployPackageFromLocalPath(
|
|
|
|
UrlJoin(deployFolder, bootPackage)
|
2018-04-21 02:59:30 -03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|