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";
|
|
|
|
|
2018-05-13 16:35:57 -03:00
|
|
|
const UrlJoin = require("url-join");
|
2018-06-04 08:03:23 -03:00
|
|
|
|
2018-04-23 13:52:03 -03:00
|
|
|
import { AzureSearch } from "pragmatismo-io-framework";
|
2018-06-04 05:33:37 -03:00
|
|
|
const { DialogSet, TextPrompt, NumberPrompt } = require('botbuilder-dialogs');
|
|
|
|
const { createTextPrompt, createNumberPrompt } = require('botbuilder-prompts');
|
2018-04-21 02:59:30 -03:00
|
|
|
import { GBMinInstance } from "botlib";
|
|
|
|
import { IGBDialog } from "botlib";
|
|
|
|
import { GBDeployer } from '../../core.gbapp/services/GBDeployer';
|
|
|
|
import { GBImporter } from '../../core.gbapp/services/GBImporter';
|
|
|
|
import { GBConfigService } from '../../core.gbapp/services/GBConfigService';
|
2018-05-07 20:45:11 -03:00
|
|
|
import { KBService } from './../../kb.gbapp/services/KBService';
|
2018-06-04 05:33:37 -03:00
|
|
|
import { BotAdapter } from "botbuilder";
|
2018-04-21 02:59:30 -03:00
|
|
|
|
|
|
|
export class AdminDialog extends IGBDialog {
|
|
|
|
|
2018-06-04 05:33:37 -03:00
|
|
|
static setup(bot: BotAdapter, min: GBMinInstance) {
|
2018-04-25 00:58:34 -03:00
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
let importer = new GBImporter(min.core);
|
|
|
|
let deployer = new GBDeployer(min.core, importer);
|
|
|
|
|
2018-06-04 05:33:37 -03:00
|
|
|
min.dialogs.add("/admin", [
|
2018-09-03 13:43:09 -03:00
|
|
|
async (dc, args) => {
|
|
|
|
const prompt = "Please, authenticate:";
|
|
|
|
await dc.prompt('textPrompt', prompt);
|
|
|
|
},
|
|
|
|
async (dc, value) => {
|
|
|
|
let text = value;
|
|
|
|
const user = min.userState.get(dc.context);
|
2018-06-04 05:33:37 -03:00
|
|
|
|
2018-09-03 13:43:09 -03:00
|
|
|
if (
|
|
|
|
!user.authenticated ||
|
|
|
|
text === GBConfigService.get("ADMIN_PASS")
|
|
|
|
) {
|
|
|
|
user.authenticated = true;
|
|
|
|
dc.context.sendActivity(
|
|
|
|
"Welcome to Pragmatismo.io GeneralBots Administration."
|
|
|
|
);
|
|
|
|
await dc.prompt('textPrompt', "Which task do you wanna run now?");
|
|
|
|
} else {
|
|
|
|
dc.endAll();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async (dc, value) => {
|
|
|
|
var text = value;
|
|
|
|
const user = min.userState.get(dc.context);
|
2018-06-04 05:33:37 -03:00
|
|
|
|
2018-09-03 13:43:09 -03:00
|
|
|
if (text === "quit") {
|
|
|
|
user.authenticated = false;
|
|
|
|
dc.replace("/");
|
|
|
|
} else if (text === "sync") {
|
|
|
|
min.core.syncDatabaseStructure(() => { });
|
|
|
|
dc.context.sendActivity("Sync started...");
|
|
|
|
dc.replace("/admin", {
|
|
|
|
firstRun: false
|
|
|
|
});
|
|
|
|
} else if (text.split(" ")[0] === "rebuildIndex") {
|
|
|
|
AdminDialog.rebuildIndexCommand(min, dc, () =>
|
2018-06-04 05:33:37 -03:00
|
|
|
dc.replace("/admin", {
|
2018-04-21 02:59:30 -03:00
|
|
|
firstRun: false
|
2018-09-03 13:43:09 -03:00
|
|
|
})
|
|
|
|
);
|
|
|
|
} else if (text.split(" ")[0] === "deployPackage") {
|
|
|
|
AdminDialog.deployPackageCommand(text, dc, deployer, min, () =>
|
|
|
|
dc.replace("/admin", {
|
|
|
|
firstRun: false
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else if (text.split(" ")[0] === "redeployPackage") {
|
|
|
|
AdminDialog.undeployPackageCommand(text, min, dc, () => {
|
|
|
|
AdminDialog.deployPackageCommand(text, dc, deployer, min, () => {
|
|
|
|
dc.context.sendActivity("Redeploy done.");
|
2018-06-04 05:33:37 -03:00
|
|
|
dc.replace("/admin", {
|
2018-04-21 02:59:30 -03:00
|
|
|
firstRun: false
|
|
|
|
});
|
|
|
|
});
|
2018-09-03 13:43:09 -03:00
|
|
|
});
|
|
|
|
} else if (text.split(" ")[0] === "undeployPackage") {
|
|
|
|
AdminDialog.undeployPackageCommand(text, min, dc, () =>
|
2018-06-04 05:33:37 -03:00
|
|
|
dc.replace("/admin", {
|
2018-04-21 02:59:30 -03:00
|
|
|
firstRun: false
|
2018-09-03 13:43:09 -03:00
|
|
|
})
|
|
|
|
);
|
|
|
|
} else if (text.split(" ")[0] === "applyPackage") {
|
|
|
|
dc.context.sendActivity("Applying in progress...");
|
|
|
|
min.core.loadInstance(text.split(" ")[1], (item, err) => {
|
|
|
|
dc.context.sendActivity("Applying done...");
|
|
|
|
dc.replace("/");
|
|
|
|
});
|
|
|
|
dc.replace("/admin", {
|
|
|
|
firstRun: false
|
|
|
|
});
|
|
|
|
} else if (text.split(" ")[0] === "rat") {
|
|
|
|
min.conversationalService.sendEvent(dc, "play", { playerType: "login", data: null });
|
|
|
|
dc.context.sendActivity("Realize login clicando no botão de login, por favor...");
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
2018-09-03 13:43:09 -03:00
|
|
|
}
|
|
|
|
])
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
2018-09-03 13:43:09 -03:00
|
|
|
|
2018-06-04 05:33:37 -03:00
|
|
|
static undeployPackageCommand(text: any, min: GBMinInstance, dc, cb) {
|
2018-04-21 02:59:30 -03:00
|
|
|
let packageName = text.split(" ")[1];
|
|
|
|
let importer = new GBImporter(min.core);
|
|
|
|
let deployer = new GBDeployer(min.core, importer);
|
2018-06-04 05:33:37 -03:00
|
|
|
dc.context.sendActivity(`Undeploying package ${packageName}...`);
|
2018-04-21 02:59:30 -03:00
|
|
|
deployer.undeployPackageFromLocalPath(
|
|
|
|
min.instance,
|
|
|
|
UrlJoin("deploy", packageName),
|
|
|
|
(data, err) => {
|
2018-06-04 05:33:37 -03:00
|
|
|
dc.context.sendActivity(`Package ${packageName} undeployed...`);
|
2018-04-21 02:59:30 -03:00
|
|
|
cb();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static deployPackageCommand(
|
|
|
|
text: string,
|
2018-06-04 05:33:37 -03:00
|
|
|
dc,
|
2018-04-21 02:59:30 -03:00
|
|
|
deployer: GBDeployer,
|
|
|
|
min: GBMinInstance,
|
|
|
|
cb
|
|
|
|
) {
|
|
|
|
let packageName = text.split(" ")[1];
|
2018-06-04 05:33:37 -03:00
|
|
|
dc.context.sendActivity(`Deploying package ${packageName}... (It may take a few seconds)`);
|
2018-04-24 05:36:48 -03:00
|
|
|
|
2018-06-04 05:33:37 -03:00
|
|
|
// TODO: Find packages in all possible locations.
|
2018-04-24 05:36:48 -03:00
|
|
|
let additionalPath = GBConfigService.get("ADDITIONAL_DEPLOY_PATH");
|
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
deployer.deployPackageFromLocalPath(
|
2018-04-24 05:36:48 -03:00
|
|
|
UrlJoin(additionalPath, packageName),
|
2018-04-21 02:59:30 -03:00
|
|
|
(data, err) => {
|
2018-06-04 05:33:37 -03:00
|
|
|
dc.context.sendActivity(`Package ${packageName} deployed... Please run rebuildIndex command.`);
|
2018-04-25 00:58:34 -03:00
|
|
|
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-04 05:33:37 -03:00
|
|
|
static rebuildIndexCommand(min: GBMinInstance, dc, cb) {
|
2018-04-21 02:59:30 -03:00
|
|
|
let search = new AzureSearch(
|
|
|
|
min.instance.searchKey,
|
|
|
|
min.instance.searchHost,
|
|
|
|
min.instance.searchIndex,
|
|
|
|
min.instance.searchIndexer
|
|
|
|
);
|
2018-06-04 05:33:37 -03:00
|
|
|
dc.context.sendActivity("Rebuilding index...");
|
2018-04-25 00:58:34 -03:00
|
|
|
search.deleteIndex((data, err) => {
|
|
|
|
let kbService = new KBService();
|
|
|
|
search.createIndex(kbService.getSearchSchema(min.instance.searchIndex), "gb", (data, err) => {
|
2018-06-04 05:33:37 -03:00
|
|
|
dc.context.sendActivity("Index rebuilt.");
|
2018-04-25 00:58:34 -03:00
|
|
|
});
|
2018-04-21 02:59:30 -03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|