2018-05-11 22:18:38 -03:00
|
|
|
#! /usr/bin / env node
|
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, |
|
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-09-24 11:04:36 -03:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const logger = require("./logger");
|
|
|
|
const express = require("express");
|
|
|
|
const bodyParser = require("body-parser");
|
|
|
|
|
2018-10-30 19:52:40 -03:00
|
|
|
import { GBConfigService } from "../packages/core.gbapp/services/GBConfigService";
|
|
|
|
import { GBConversationalService } from "../packages/core.gbapp/services/GBConversationalService";
|
|
|
|
import { GBMinService } from "../packages/core.gbapp/services/GBMinService";
|
|
|
|
import { GBDeployer } from "../packages/core.gbapp/services/GBDeployer";
|
|
|
|
import { GBWhatsappPackage } from "./../packages/whatsapp.gblib/index";
|
|
|
|
import { GBCoreService } from "../packages/core.gbapp/services/GBCoreService";
|
|
|
|
import { GBImporter } from "../packages/core.gbapp/services/GBImporter";
|
|
|
|
import { GBAnalyticsPackage } from "../packages/analytics.gblib";
|
|
|
|
import { GBCorePackage } from "../packages/core.gbapp";
|
|
|
|
import { GBKBPackage } from "../packages/kb.gbapp";
|
|
|
|
import { GBSecurityPackage } from "../packages/security.gblib";
|
|
|
|
import { GBAdminPackage } from "../packages/admin.gbapp/index";
|
|
|
|
import { GBCustomerSatisfactionPackage } from "../packages/customer-satisfaction.gbapp";
|
|
|
|
import { GBAdminService } from "../packages/admin.gbapp/services/GBAdminService";
|
|
|
|
import { GuaribasInstance } from "../packages/core.gbapp/models/GBModel";
|
|
|
|
import { AzureDeployerService } from "../packages/azuredeployer.gbapp/services/AzureDeployerService";
|
2018-10-25 18:13:51 -03:00
|
|
|
import { IGBPackage, IGBInstance } from "botlib";
|
2018-09-24 11:04:36 -03:00
|
|
|
|
|
|
|
let appPackages = new Array<IGBPackage>();
|
2018-05-11 23:27:00 -03:00
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
/**
|
|
|
|
* General Bots open-core entry point.
|
|
|
|
*/
|
|
|
|
export class GBServer {
|
2018-11-04 09:19:03 -02:00
|
|
|
|
2018-09-24 15:27:26 -03:00
|
|
|
/**
|
|
|
|
* Program entry-point.
|
|
|
|
*/
|
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
static run() {
|
2018-11-04 09:19:03 -02:00
|
|
|
|
|
|
|
logger.info(`The Bot Server is in STARTING mode...`);
|
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
// Creates a basic HTTP server that will serve several URL, one for each
|
|
|
|
// bot instance. This allows the same server to attend multiple Bot on
|
|
|
|
// the Marketplace until GB get serverless.
|
|
|
|
|
2018-09-24 11:04:36 -03:00
|
|
|
let port = process.env.port || process.env.PORT || 4242;
|
|
|
|
let server = express();
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2018-09-24 11:04:36 -03:00
|
|
|
server.use(bodyParser.json()); // to support JSON-encoded bodies
|
|
|
|
server.use(
|
|
|
|
bodyParser.urlencoded({
|
|
|
|
// to support URL-encoded bodies
|
|
|
|
extended: true
|
|
|
|
})
|
|
|
|
);
|
2018-05-11 23:27:00 -03:00
|
|
|
|
2018-10-25 18:13:51 -03:00
|
|
|
let bootInstance: IGBInstance;
|
2018-09-09 16:40:04 -03:00
|
|
|
server.listen(port, () => {
|
|
|
|
(async () => {
|
|
|
|
try {
|
2018-11-04 09:19:03 -02:00
|
|
|
logger.info(`Now accepting connections on ${port}...`);
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2018-09-09 16:40:04 -03:00
|
|
|
// Reads basic configuration, initialize minimal services.
|
2018-09-09 14:39:37 -03:00
|
|
|
|
2018-09-24 11:04:36 -03:00
|
|
|
GBConfigService.init();
|
|
|
|
let core = new GBCoreService();
|
2018-10-25 18:13:51 -03:00
|
|
|
|
2018-10-15 19:05:43 -03:00
|
|
|
// Ensures cloud / on-premises infrastructure is setup.
|
|
|
|
|
2018-11-04 09:19:03 -02:00
|
|
|
logger.info(`Establishing a development local proxy (ngrok)...`);
|
2018-10-28 21:56:51 -03:00
|
|
|
let proxyAddress = await core.ensureProxy(port);
|
|
|
|
|
|
|
|
let azureDeployer = new AzureDeployerService();
|
2018-10-14 19:58:54 -03:00
|
|
|
|
2018-10-25 18:13:51 -03:00
|
|
|
try {
|
|
|
|
await core.initDatabase();
|
|
|
|
} catch (error) {
|
2018-11-04 09:19:03 -02:00
|
|
|
logger.info(`Deploying cognitive infrastructure (on the cloud / on premises)...`);
|
2018-10-25 21:57:28 -03:00
|
|
|
try {
|
|
|
|
bootInstance = await azureDeployer.deployFarm(proxyAddress);
|
|
|
|
} catch (error) {
|
2018-10-28 21:56:51 -03:00
|
|
|
logger.warn(
|
2018-11-04 09:19:03 -02:00
|
|
|
"In case of error, please cleanup any infrastructure objects created during this procedure before running again."
|
2018-10-28 21:56:51 -03:00
|
|
|
);
|
2018-10-25 21:57:28 -03:00
|
|
|
throw error;
|
|
|
|
}
|
2018-10-25 18:13:51 -03:00
|
|
|
core.writeEnv(bootInstance);
|
2018-11-04 09:19:03 -02:00
|
|
|
logger.info(`File .env written, starting General Bots...`);
|
2018-10-25 18:13:51 -03:00
|
|
|
GBConfigService.init();
|
|
|
|
|
|
|
|
await core.initDatabase();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Get .gb* templates from GitHub and download do additional deploy folder.
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2018-10-22 15:33:23 -03:00
|
|
|
// Check admin password.
|
2018-10-25 18:13:51 -03:00
|
|
|
|
2018-09-24 11:04:36 -03:00
|
|
|
let conversationalService = new GBConversationalService(core);
|
|
|
|
let adminService = new GBAdminService(core);
|
|
|
|
let password = GBConfigService.get("ADMIN_PASS");
|
2018-09-12 05:18:37 -03:00
|
|
|
|
2018-09-24 11:04:36 -03:00
|
|
|
if (!GBAdminService.StrongRegex.test(password)) {
|
|
|
|
throw new Error(
|
|
|
|
"STOP: Please, define a really strong password in ADMIN_PASS environment variable before running the server."
|
|
|
|
);
|
|
|
|
}
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2018-09-24 11:04:36 -03:00
|
|
|
// NOTE: the semicolon is necessary before this line.
|
|
|
|
// Loads all system packages.
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2018-09-24 11:04:36 -03:00
|
|
|
[
|
|
|
|
GBAdminPackage,
|
|
|
|
GBAnalyticsPackage,
|
|
|
|
GBCorePackage,
|
|
|
|
GBSecurityPackage,
|
|
|
|
GBKBPackage,
|
|
|
|
GBCustomerSatisfactionPackage,
|
|
|
|
GBWhatsappPackage
|
|
|
|
].forEach(e => {
|
|
|
|
logger.info(`Loading sys package: ${e.name}...`);
|
|
|
|
let p = Object.create(e.prototype) as IGBPackage;
|
|
|
|
p.loadPackage(core, core.sequelize);
|
|
|
|
});
|
|
|
|
|
2018-09-24 15:27:26 -03:00
|
|
|
// Loads all bot instances from object storage, if it's formatted.
|
|
|
|
|
2018-10-25 18:13:51 -03:00
|
|
|
logger.info(`Loading instances from storage...`);
|
2018-09-24 15:27:26 -03:00
|
|
|
let instances: GuaribasInstance[];
|
|
|
|
try {
|
|
|
|
instances = await core.loadInstances();
|
2018-10-28 21:56:51 -03:00
|
|
|
let instance = instances[0];
|
2018-11-04 09:19:03 -02:00
|
|
|
|
2018-10-28 21:56:51 -03:00
|
|
|
if (process.env.NODE_ENV === "development") {
|
2018-11-01 21:06:11 -03:00
|
|
|
logger.info(`Updating bot endpoint to local reverse proxy (ngrok)...`);
|
2018-10-28 21:56:51 -03:00
|
|
|
|
|
|
|
await azureDeployer.updateBotProxy(
|
|
|
|
instance.botId,
|
|
|
|
instance.botId,
|
|
|
|
`${proxyAddress}/api/messages/${instance.botId}`
|
|
|
|
);
|
|
|
|
}
|
2018-09-24 15:27:26 -03:00
|
|
|
} catch (error) {
|
2018-10-25 18:13:51 -03:00
|
|
|
if (error.parent.code === "ELOGIN") {
|
2018-10-28 21:56:51 -03:00
|
|
|
let group = GBConfigService.get("CLOUD_GROUP");
|
|
|
|
let serverName = GBConfigService.get("STORAGE_SERVER").split(
|
|
|
|
".database.windows.net"
|
|
|
|
)[0];
|
|
|
|
await azureDeployer.openStorageFirewall(group, serverName);
|
2018-10-25 18:13:51 -03:00
|
|
|
} else {
|
|
|
|
// Check if storage is empty and needs formatting.
|
|
|
|
|
|
|
|
let isInvalidObject =
|
|
|
|
error.parent.number == 208 || error.parent.errno == 1; // MSSQL or SQLITE.
|
|
|
|
|
|
|
|
if (isInvalidObject) {
|
|
|
|
if (GBConfigService.get("STORAGE_SYNC") != "true") {
|
|
|
|
throw `Operating storage is out of sync or there is a storage connection error. Try setting STORAGE_SYNC to true in .env file. Error: ${
|
|
|
|
error.message
|
|
|
|
}.`;
|
|
|
|
} else {
|
|
|
|
logger.info(
|
|
|
|
`Storage is empty. After collecting storage structure from all .gbapps it will get synced.`
|
|
|
|
);
|
|
|
|
}
|
2018-10-15 19:05:43 -03:00
|
|
|
} else {
|
2018-10-25 18:13:51 -03:00
|
|
|
throw `Cannot connect to operating storage: ${error.message}.`;
|
2018-10-11 10:53:22 -03:00
|
|
|
}
|
2018-09-24 15:27:26 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deploy packages and format object store according to .gbapp storage models.
|
|
|
|
|
2018-10-22 15:33:23 -03:00
|
|
|
logger.info(`Deploying packages...`);
|
|
|
|
let deployer = new GBDeployer(core, new GBImporter(core));
|
2018-11-04 09:19:03 -02:00
|
|
|
await deployer.rebuildIndex(instances[0]);
|
2018-09-24 11:04:36 -03:00
|
|
|
await deployer.deployPackages(core, server, appPackages);
|
2018-09-24 15:27:26 -03:00
|
|
|
|
|
|
|
// If instances is undefined here it's because storage has been formatted.
|
|
|
|
// Load all instances from .gbot found on deploy package directory.
|
|
|
|
if (!instances) {
|
2018-10-25 18:13:51 -03:00
|
|
|
let saveInstance = new GuaribasInstance(bootInstance);
|
2018-11-01 15:11:23 -03:00
|
|
|
await saveInstance.save();
|
2018-09-24 15:27:26 -03:00
|
|
|
instances = await core.loadInstances();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup server dynamic (per bot instance) resources and listeners.
|
|
|
|
|
2018-10-22 15:33:23 -03:00
|
|
|
logger.info(`Building instances.`);
|
|
|
|
let minService = new GBMinService(
|
|
|
|
core,
|
|
|
|
conversationalService,
|
|
|
|
adminService,
|
|
|
|
deployer
|
|
|
|
);
|
2018-09-24 15:27:26 -03:00
|
|
|
await minService.buildMin(server, appPackages, instances);
|
2018-09-24 11:04:36 -03:00
|
|
|
logger.info(`The Bot Server is in RUNNING mode...`);
|
2018-09-24 15:27:26 -03:00
|
|
|
|
2018-09-24 11:04:36 -03:00
|
|
|
return core;
|
2018-09-09 16:40:04 -03:00
|
|
|
} catch (err) {
|
2018-09-24 19:56:11 -03:00
|
|
|
logger.error(`STOP: ${err} ${err.stack ? err.stack : ""}`);
|
2018-09-24 15:27:26 -03:00
|
|
|
process.exit(1);
|
2018-09-09 16:40:04 -03:00
|
|
|
}
|
2018-09-24 11:04:36 -03:00
|
|
|
})();
|
|
|
|
});
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// First line to run.
|
|
|
|
|
2018-09-24 11:04:36 -03:00
|
|
|
GBServer.run();
|