First version of azure-arm automation.

This commit is contained in:
Rodrigo Rodriguez (pragmatismo.io) 2018-10-14 11:38:40 -03:00
parent 633cab410d
commit 7991dced80
9 changed files with 3525 additions and 1482 deletions

View file

@ -0,0 +1,65 @@
/*****************************************************************************\
| ( )_ _ |
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' _ `\ /'_`\ |
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| ( ) |( (_) ) |
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
| | | ( )_) | |
| (_) \___/' |
| |
| 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 UrlJoin = require("url-join")
import { GBMinInstance, IGBPackage, IGBCoreService } from "botlib"
import { Sequelize } from "sequelize-typescript"
import { AzureDeployerService } from "./services/AzureDeployerService"
export class GBWhatsappPackage implements IGBPackage {
sysPackages: IGBPackage[] = null
loadPackage(core: IGBCoreService, sequelize: Sequelize): void {
}
unloadPackage(core: IGBCoreService): void {
}
loadBot(min: GBMinInstance): void {
}
unloadBot(min: GBMinInstance): void {
}
onNewSession(min: GBMinInstance, dc: any): void {
}
}

View file

@ -0,0 +1,304 @@
/*****************************************************************************\
| ( )_ _ |
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' _ `\ /'_`\ |
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| ( ) |( (_) ) |
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
| | | ( )_) | |
| (_) \___/' |
| |
| 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';
import { GBService, IGBInstance } from "botlib"
const msRestAzure = require('ms-rest-azure');
import { ResourceManagementClient } from 'azure-arm-resource'
import { WebSiteManagementClient } from 'azure-arm-website';
import { SqlManagementClient } from "azure-arm-sql";
import { CognitiveServicesManagementClient } from "azure-arm-cognitiveservices";
import { CognitiveServicesAccount } from "azure-arm-cognitiveservices/lib/models";
import { SearchManagementClient } from "azure-arm-search";
import { BotConfiguration, BotService, EndpointService, IBotService, IConnectedService, ServiceTypes } from 'botframework-config';
import { WebResource, ServiceClient } from "ms-rest-js";
import * as simplegit from 'simple-git/promise';
import { AppServicePlan } from "azure-arm-website/lib/models";
const git = simplegit();
const logger = require("../../../src/logger");
const UrlJoin = require("url-join")
export class AzureDeployerService extends GBService {
instance: IGBInstance
resourceClient: ResourceManagementClient.ResourceManagementClient;
webSiteClient: WebSiteManagementClient;
storageClient: SqlManagementClient;
cognitiveClient: CognitiveServicesManagementClient;
searchClient: SearchManagementClient;
provider = 'Microsoft.BotService';
public async process(username: any, password: any, instance: IGBInstance,
subscriptionId: string, location: string) {
let _this = this;
msRestAzure.loginWithUsernamePassword(username, password, async (err, credentials) => {
_this.resourceClient = new ResourceManagementClient.default(credentials, subscriptionId);
_this.webSiteClient = new WebSiteManagementClient(credentials, subscriptionId);
_this.storageClient = new SqlManagementClient(credentials, subscriptionId);
_this.cognitiveClient = new CognitiveServicesManagementClient(credentials, subscriptionId);
_this.searchClient = new SearchManagementClient(credentials, subscriptionId);
let name = "generalbots";
let administratorLogin = ""
let administratorPassword = ""
let serverName = name + "";
let tenantId = '';
logger.info(`Creating Deploy...`);
let deploymentName = await this.createDeploy(name, location);
logger.info(`Creating Server...`);
let serverFarm = await this.createHostingPlan(name, `${name}-server-plan`, location);
await this.createServer(serverFarm.id, name, `${name}-server`, location);
logger.info(`Creating Storage...`);
//await this.createStorageServer(name, `${name}-storage-server`, administratorLogin, administratorPassword, serverName, location);
//await this.createStorage(name, name, `${name}-storage`, location);
logger.info(`Creating NLP...`);
//await this.createNLP(name, `${name}-nlp`, location);
logger.info(`Creating Speech...`);
//await this.createSpeech(name, `${name}-speech`, location);
logger.info(`Creating SpellChecker...`);
//await this.createSpellChecker(name, `${name}-spellchecker`, location);
logger.info(`Creating Text Analytics...`);
//await this.createTextAnalytics(name, `${name}-textanalytics`, location);
logger.info(`Creating Search...`);
//await this.createSearch(name, `${name}-search`, location);
logger.info(`Creating Bot...`);
//await this.createBot(credentials.tokenCache._entries[0].accessToken,
// name, name, name, 'global', subscriptionId, tenantId);
logger.info(`Cleaning Deploy it can take a while...`);
// DISABLED: await this.dangerouslyDeleteDeploy(name);
});
}
private async dangerouslyDeleteDeploy(name) {
return this.resourceClient.resourceGroups.deleteMethod(name);
}
private async createStorageServer(group, name, administratorLogin,
administratorPassword, serverName, location) {
var params = {
location: location,
administratorLogin: administratorLogin,
administratorLoginPassword: administratorPassword,
fullyQualifiedDomainName: `${serverName}.database.windows.net`
};
return this.storageClient.servers.createOrUpdate(group, name, params);
}
private async registerProviders(subscriptionId, baseUrl, accessToken, ){
let query = `subscriptions/${subscriptionId}/providers/${this.provider}/register?api-version=2018-02-01`
let requestUrl = UrlJoin(baseUrl, query);
let req = new WebResource();
req.method = 'POST';
req.url = requestUrl;
req.headers = {};
req.headers['Content-Type'] = 'application/json; charset=utf-8';
req.headers["accept-language"] = '*'
req.headers['x-ms-client-request-id'] = msRestAzure.generateUuid();
req.headers['Authorization'] = 'Bearer ' + accessToken;
let httpClient = new ServiceClient();
let res = await httpClient.sendRequest(req);
}
private async createBot(accessToken, botId, group, name, location, subscriptionId, tenantId) {
let baseUrl = `https://management.azure.com/`;
let appId = '2cac4573-0aea-442a-a222-dcc340000000';
let description = 'description';
let endpoint = 'http://localhost:4242/';
let nlpKey = 'c5869c6c13854434a3f228aad2d6dfb6';
let nlpAppId = "3e431b4f-96a4-4bdb-b2d5-3ea462ddb773";
let parameters = { parameters:{
"location": location,
"sku": {
"name": "F0"
},
"name": name,
//"type": "sampletype",
"id": botId,
"kind": "sdk",
"properties": {
"description": description,
"displayName": name,
"endpoint": endpoint,
"iconUrl": "http://myicon",
"luisAppIds": [
nlpAppId,
],
"luisKey": nlpKey,
"msaAppId": appId
}
}}
let query = `subscriptions/${subscriptionId}/resourceGroups/${group}/providers/${this.provider}/botServices/${botId}?api-version=2017-12-01`;
let requestUrl = UrlJoin(baseUrl, query);
let req = new WebResource();
req.method = 'PUT';
req.url = requestUrl;
req.headers = {};
req.headers['Content-Type'] = 'application/json';
req.headers["accept-language"] = '*'
//req.headers['x-ms-client-request-id'] = msRestAzure.generateUuid();
req.headers['Authorization'] = 'Bearer ' + accessToken;
let requestContent = JSON.stringify(parameters);
req.body = requestContent;
let httpClient = new ServiceClient();
let res = await httpClient.sendRequest(req);
}
private async createSearch(group, name, location) {
var params = {
sku: { name: 'free' },
location: location
};
return this.searchClient.services.createOrUpdate(group, name, params);
}
private async createStorage(group, serverName, name, location) {
var params = {
sku: { name: 'Free' },
createMode: 'Default',
location: location
};
return this.storageClient.databases.createOrUpdate(group,
serverName, name, params);
}
private async createCognitiveServices(group, name, location, kind): Promise<CognitiveServicesAccount> {
// * 'Bing.Autosuggest.v7', 'Bing.CustomSearch',
// * 'Bing.Search.v7', 'Bing.Speech', 'Bing.SpellCheck.v7', 'ComputerVision',
// * 'ContentModerator', 'CustomSpeech', 'CustomVision.Prediction',
// * 'CustomVision.Training', 'Emotion', 'Face', 'LUIS', 'QnAMaker',
// * 'SpeakerRecognition', 'SpeechTranslation', 'TextAnalytics',
// * 'TextTranslation', 'WebLM'
let params = {
sku: { name: 'F0' },
createMode: 'Default',
location: location,
kind: kind,
properties: {}
};
return await this.cognitiveClient.accounts.create(group, name, params);
}
private async createSpeech(group, name, location): Promise<CognitiveServicesAccount> {
return await this.createCognitiveServices(group, name, location, 'SpeechServices');
}
private async createNLP(group, name, location): Promise<CognitiveServicesAccount> {
return await this.createCognitiveServices(group, name, location, 'LUIS');
}
private async createSpellChecker(group, name, location): Promise<CognitiveServicesAccount> {
return await this.createCognitiveServices(group, name, 'global', 'Bing.SpellCheck.v7');
}
private async createTextAnalytics(group, name, location): Promise<CognitiveServicesAccount> {
return await this.createCognitiveServices(group, name, location, 'TextAnalytics');
}
private async createDeploy(name, location) {
var params = { location: location };
return this.resourceClient.resourceGroups.createOrUpdate(name, params);
}
private async createHostingPlan(group, name, location):Promise<AppServicePlan> {
let params = {
serverFarmWithRichSkuName: name,
location: location,
sku: {
name: 'F1',
capacity: 1,
tier: 'Free'
}
};
return this.webSiteClient.appServicePlans.createOrUpdate(group, name, params);
}
private async createServer(farmId, group, name, location) {
var parameters = {
location: location,
serverFarmId: farmId
};
return this.webSiteClient.webApps.createOrUpdate(group, name, parameters);
}
private async updateWebisteConfig(group, serverFarmId, name, location) {
var siteConfig = {
location: location,
serverFarmId: serverFarmId,
numberOfWorkers: 1,
phpVersion: '5.5'
};
return this.webSiteClient.webApps.createOrUpdateConfiguration(group, name, siteConfig);
}
private deleteDeploy(name) {
return this.resourceClient.resourceGroups.deleteMethod(name);
}
async deployGeneralBotsToAzure(){
let status = await git.status();
}
}

View file

@ -1,4 +1,3 @@
import { IGBInstance } from "botlib";
/*****************************************************************************\
| ( )_ _ |
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
@ -100,9 +99,9 @@ export class GBConversationalService implements IGBConversationalService {
// Invokes LUIS.
const model = new LuisRecognizer({
appId: min.instance.nlpAppId,
subscriptionKey: min.instance.nlpSubscriptionKey,
serviceEndpoint: min.instance.nlpServerUrl
applicationId: min.instance.nlpAppId,
endpointKey: min.instance.nlpSubscriptionKey,
endpoint: min.instance.nlpServerUrl
});
let nlp: any;

View file

@ -251,6 +251,7 @@ export class GBDeployer {
});
}
deployTheme(localPath: string) {
// DISABLED: Until completed, "/ui/public".
// FsExtra.copy(localPath, this.workDir + packageName)

View file

@ -37,6 +37,7 @@ const UrlJoin = require("url-join");
const express = require("express");
const logger = require("../../../src/logger");
const request = require("request-promise-native");
const ngrok = require('ngrok');
var crypto = require("crypto");
var AuthenticationContext = require("adal-node").AuthenticationContext;
@ -45,7 +46,8 @@ import {
BotStateSet,
ConversationState,
MemoryStorage,
UserState
UserState,
BotState
} from "botbuilder";
import { GBMinInstance, IGBPackage } from "botlib";
@ -296,6 +298,12 @@ export class GBMinService {
);
}
private async ngrokRefresh(){
const url = await ngrok.connect(9090); // https://757c1652.ngrok.io -> http://localhost:9090
// TODO: Persist to storage and refresh each 8h.
// TODO: Update all bots definition in azure.
}
private async buildBotAdapter(instance: any) {
let adapter = new BotFrameworkAdapter({
appId: instance.marketplaceId,
@ -305,7 +313,8 @@ export class GBMinService {
const storage = new MemoryStorage();
const conversationState = new ConversationState(storage);
const userState = new UserState(storage);
adapter.use(new BotStateSet(conversationState, userState));
//const botState = new BotState(storage);
// TODO: adapter.use();
// The minimal bot is built here.

View file

@ -53,12 +53,6 @@ export class AskDialog extends IGBDialog {
static setup(bot: BotAdapter, min: GBMinInstance) {
const service = new KBService(min.core.sequelize);
const model = new LuisRecognizer({
appId: min.instance.nlpAppId,
subscriptionKey: min.instance.nlpSubscriptionKey,
serviceEndpoint: min.instance.nlpServerUrl
});
min.dialogs.add("/answerEvent", [
async (dc, args) => {

4553
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -31,50 +31,58 @@
},
"dependencies": {
"@microsoft/microsoft-graph-client": "1.3.0",
"@types/chai": "4.1.6",
"@types/mocha": "5.2.5",
"@types/sequelize": "4.27.28",
"@types/url-join": "0.8.2",
"@types/winston": "2.4.4",
"adal-node": "0.1.28",
"async": "2.6.1",
"async-promises": "0.2.1",
"azure-arm-cognitiveservices": "2.2.0",
"azure-arm-resource": "7.0.0",
"azure-arm-search": "^1.2.0-preview",
"azure-arm-sql": "5.1.0",
"azure-arm-website": "5.3.0",
"body-parser": "1.18.3",
"botbuilder": "4.0.0-preview1.2",
"botbuilder-ai": "4.0.0-preview1.2",
"botbuilder-azure": "4.0.0-preview1.2",
"botbuilder": "4.0.6",
"botbuilder-ai": "4.0.6",
"botbuilder-azure": "4.0.6",
"botbuilder-choices": "4.0.0-preview1.2",
"botbuilder-dialogs": "4.0.0-preview1.2",
"botbuilder-dialogs": "4.0.6",
"botbuilder-prompts": "4.0.0-preview1.2",
"botlib": "0.1.3",
"chai": "4.2.0",
"chokidar": "2.0.4",
"csv-parse": "3.0.0",
"csv-parse": "3.1.3",
"dotenv-extended": "2.3.0",
"express": "4.16.3",
"express": "4.16.4",
"express-promise-router": "3.0.3",
"fs-extra": "7.0.0",
"fs-walk": "0.0.2",
"localize": "0.4.7",
"marked": "0.5.0",
"marked": "0.5.1",
"mocha": "5.2.0",
"mocha-typescript": "1.1.17",
"ms": "2.1.1",
"nexmo": "2.3.2",
"ms-rest-azure": "2.5.9",
"nexmo": "2.4.0",
"ngrok": "^3.1.0",
"pragmatismo-io-framework": "1.0.17",
"reflect-metadata": "0.1.12",
"request-promise-native": "1.0.5",
"sequelize": "4.38.1",
"sequelize": "4.39.0",
"sequelize-typescript": "0.6.6",
"sqlite3": "3.1.13",
"simple-git": "^1.105.0",
"sqlite3": "4.0.2",
"swagger-client": "3.8.21",
"tedious": "2.6.4",
"ts-node": "7.0.1",
"typedoc": "0.13.0",
"typescript": "3.1.3",
"url-join": "4.0.0",
"wait-until": "0.0.2",
"walk-promise": "0.2.0",
"winston": "3.1.0",
"@types/chai": "4.1.4",
"@types/mocha": "5.2.5",
"@types/sequelize": "4.27.25",
"@types/url-join": "0.8.2",
"@types/winston": "2.4.4",
"chai": "4.1.2",
"mocha": "5.2.0",
"mocha-typescript": "1.1.17",
"ts-node": "7.0.1",
"typedoc": "0.12.0",
"typescript": "3.0.3"
"winston": "3.1.0"
}
}

View file

@ -55,7 +55,8 @@ import { GBAdminPackage } from "../deploy/admin.gbapp/index";
import { GBCustomerSatisfactionPackage } from "../deploy/customer-satisfaction.gbapp";
import { IGBPackage } from "botlib";
import { GBAdminService } from "../deploy/admin.gbapp/services/GBAdminService";
import { GuaribasInstance } from "deploy/core.gbapp/models/GBModel";
import { GuaribasInstance } from "../deploy/core.gbapp/models/GBModel";
import { AzureDeployerService } from "../deploy/azuredeployer.gblib/services/AzureDeployerService";
let appPackages = new Array<IGBPackage>();
@ -157,7 +158,7 @@ export class GBServer {
error.message
}.`;
}
else{
else {
logger.info(`Storage is empty. After collecting storage structure from all .gbapps it will get synced.`);
}
} else {
@ -195,3 +196,4 @@ export class GBServer {
// First line to run.
GBServer.run();