fix(all): Update of arm packages and token usage.

This commit is contained in:
rodrigorodriguez 2022-12-15 10:56:27 -03:00
parent e83dc02f77
commit 179b20a248
7 changed files with 872 additions and 1040 deletions

1
.gitignore vendored
View file

@ -23,3 +23,4 @@ GB.log
gb.log
GB.log.json
yarn-error.log
package-lock.json

1816
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -200,16 +200,17 @@ export class GBAdminService implements IGBAdminService {
await deployer.deployPackage(min, localFolder);
}
}
public static async rebuildIndexPackageCommand (min: GBMinInstance, deployer: IGBDeployer) {
public static async rebuildIndexPackageCommand (min: GBMinInstance, deployer: GBDeployer) {
const service = await AzureDeployerService.createInstance(deployer);
await deployer.rebuildIndex(
min.instance,
new AzureDeployerService(deployer).getKBSearchSchema(min.instance.searchIndex)
service.getKBSearchSchema(min.instance.searchIndex)
);
}
public static async syncBotServerCommand (min: GBMinInstance, deployer: GBDeployer) {
const serverName = `${min.instance.botId}-server`;
const service = await AzureDeployerService.createInstance(deployer);
const service = await AzureDeployerService.createInstance(deployer );
service.syncBotServerRepository(min.instance.botId, serverName);
}

View file

@ -37,7 +37,7 @@
'use strict';
import urlJoin from 'url-join';
import { HttpMethods, ServiceClient, WebResource } from '@azure/ms-rest-js';
import { HttpMethods, ServiceClient, TokenCredentials, WebResource } from '@azure/ms-rest-js';
import { CognitiveServicesManagementClient } from '@azure/arm-cognitiveservices';
import { ResourceManagementClient } from '@azure/arm-resources';
import { SubscriptionClient } from '@azure/arm-subscriptions';
@ -54,6 +54,9 @@ import { Account } from '@azure/arm-cognitiveservices';
import MicrosoftGraph from '@microsoft/microsoft-graph-client';
import Spinner from 'cli-spinner';
import * as publicIp from 'public-ip';
import { AccessToken, TokenCredential } from '@azure/core-auth';
const WebSiteResponseTimeout = 900;
const iconUrl = 'https://github.com/pragmatismo-io/BotServer/blob/master/docs/images/generalbots-logo-squared.png';
@ -79,24 +82,26 @@ export class AzureDeployerService implements IGBInstallationDeployer {
public core: IGBCoreService;
private freeTier: boolean;
constructor (deployer: IGBDeployer, freeTier: boolean = true) {
this.deployer = deployer;
this.freeTier = freeTier;
}
public async runSearch (instance: IGBInstance) {
await this.deployer.rebuildIndex(instance, this.getKBSearchSchema(instance.searchIndex));
}
public static async createInstance (deployer: GBDeployer): Promise<AzureDeployerService> {
public static async createInstance (deployer: GBDeployer, freeTier: boolean = false): Promise<AzureDeployerService> {
const username = GBConfigService.get('CLOUD_USERNAME');
const password = GBConfigService.get('CLOUD_PASSWORD');
const credentials = await GBAdminService.getADALCredentialsFromUsername(username, password);
const subscriptionId = GBConfigService.get('CLOUD_SUBSCRIPTIONID');
const service = new AzureDeployerService();
const service = new AzureDeployerService(deployer);
service.core = deployer.core;
service.initServices(credentials, subscriptionId);
service.deployer = deployer;
service.freeTier = freeTier;
const credentials = await GBAdminService.getADALCredentialsFromUsername(username, password);
const token = credentials['tokenCache']._entries[0];
await service.initServices(token.accessToken, token.expiresOn, subscriptionId);
return service;
}
@ -334,19 +339,15 @@ export class AzureDeployerService implements IGBInstallationDeployer {
}
public async openStorageFirewall (groupName, serverName) {
const username = GBConfigService.get('CLOUD_USERNAME');
const password = GBConfigService.get('CLOUD_PASSWORD');
const subscriptionId = GBConfigService.get('CLOUD_SUBSCRIPTIONID');
const credentials = await GBAdminService.getADALCredentialsFromUsername(username, password);
const storageClient = new SqlManagementClient(credentials as any, subscriptionId);
const ip = await publicIp.publicIpv4();
let params = {
startIpAddress: ip,
endIpAddress: ip
};
await storageClient.firewallRules.createOrUpdate(groupName, serverName, 'gb', params);
await this.storageClient.firewallRules.createOrUpdate(groupName, serverName, 'gb', params);
}
public async deployFarm (
@ -357,7 +358,10 @@ export class AzureDeployerService implements IGBInstallationDeployer {
): Promise<IGBInstance> {
const culture = 'en-us';
this.initServices(credentials, subscriptionId);
const token = credentials['tokenCache']._entries[0];
await this.initServices(token.accessToken, token.expiresOn, subscriptionId);
const spinner = new Spinner('%s');
spinner.start();
spinner.setSpinnerString('|/-\\');
@ -580,13 +584,33 @@ export class AzureDeployerService implements IGBInstallationDeployer {
await this.webSiteClient.webApps.syncRepository(group, name);
}
public initServices (credentials: any, subscriptionId: string) {
this.cloud = new ResourceManagementClient(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);
this.accessToken = credentials.tokenCache._entries[0].accessToken;
public async initServices (accessToken: string, expiresOnTimestamp, subscriptionId: string) {
class AccessToken2 implements AccessToken
{
public expiresOnTimestamp: number;
public token: string;
}
class StaticAccessToken implements TokenCredential {
public getToken(): Promise<AccessToken> {
return new Promise<AccessToken>(async (resolve, reject) => {
const t = new AccessToken2();
t.token = accessToken;
t.expiresOnTimestamp = expiresOnTimestamp;
resolve(t);
});
}
}
const token = await new StaticAccessToken();
this.cloud = new ResourceManagementClient(token, subscriptionId);
this.webSiteClient = new WebSiteManagementClient(token, subscriptionId);
this.storageClient = new SqlManagementClient(token, subscriptionId);
this.cognitiveClient = new CognitiveServicesManagementClient(token, subscriptionId);
this.searchClient = new SearchManagementClient(token, subscriptionId);
}
private async createStorageServer (group, name, administratorLogin, administratorPassword, serverName, location) {

View file

@ -213,7 +213,7 @@ export class GBDeployer implements IGBDeployer {
// Creates the MSFT application that will be associated to the bot.
const service = new AzureDeployerService(this);
const service = await AzureDeployerService.createInstance(this);
const application = await service.createApplication(accessToken, botId);
// Fills new instance base information and get App secret.
@ -244,7 +244,7 @@ export class GBDeployer implements IGBDeployer {
* Verifies if bot exists on bot catalog.
*/
public async botExists (botId: string): Promise<boolean> {
const service = new AzureDeployerService(this);
const service = await AzureDeployerService.createInstance(this);
return await service.botExists(botId);
}
@ -255,7 +255,7 @@ export class GBDeployer implements IGBDeployer {
public async deployBotFull (instance: IGBInstance, publicAddress: string): Promise<IGBInstance> {
// Reads base configuration from environent file.
const service = new AzureDeployerService(this);
const service = await AzureDeployerService.createInstance(this);
const username = GBConfigService.get('CLOUD_USERNAME');
const password = GBConfigService.get('CLOUD_PASSWORD');
const accessToken = await GBAdminService.getADALTokenFromUsername(username, password);
@ -323,7 +323,7 @@ export class GBDeployer implements IGBDeployer {
* Performs the NLP publishing process on remote service.
*/
public async publishNLP (instance: IGBInstance): Promise<void> {
const service = new AzureDeployerService(this);
const service = await AzureDeployerService.createInstance(this);
const res = await service.publishNLP(instance.cloudLocation, instance.nlpAppId, instance.nlpAuthoringKey);
if (res.status !== 200 && res.status !== 201) {
throw res.bodyAsText;
@ -334,7 +334,7 @@ export class GBDeployer implements IGBDeployer {
* Trains NLP on the remote service.
*/
public async trainNLP (instance: IGBInstance): Promise<void> {
const service = new AzureDeployerService(this);
const service = await AzureDeployerService.createInstance(this);
const res = await service.trainNLP(instance.cloudLocation, instance.nlpAppId, instance.nlpAuthoringKey);
if (res.status !== 200 && res.status !== 202) {
throw res.bodyAsText;
@ -368,7 +368,7 @@ export class GBDeployer implements IGBDeployer {
* Refreshes NLP entities on the remote service.
*/
public async refreshNLPEntity (instance: IGBInstance, listName, listData): Promise<void> {
const service = new AzureDeployerService(this);
const service = await AzureDeployerService.createInstance(this);
const res = await service.refreshEntityList(
instance.cloudLocation,
instance.nlpAppId,
@ -538,7 +538,7 @@ export class GBDeployer implements IGBDeployer {
public async undeployBot (botId: string, packageName: string): Promise<void> {
// Deletes Bot registration on cloud.
const service = new AzureDeployerService(this);
const service = await AzureDeployerService.createInstance(this);
const group = GBConfigService.get('CLOUD_GROUP');
if (await service.botExists(botId)) {
await service.deleteBot(botId, group);

View file

@ -786,8 +786,8 @@ export class KBService implements IGBKBService {
const p = await deployer.deployPackageToStorage(instance.instanceId, packageName);
await this.importKbPackage(min, localPath, p, instance);
GBDeployer.mountGBKBAssets(packageName, min.botId, localPath);
await deployer.rebuildIndex(instance, new AzureDeployerService(deployer).getKBSearchSchema(instance.searchIndex));
const service = await AzureDeployerService.createInstance(deployer);
await deployer.rebuildIndex(instance, service.getKBSearchSchema(instance.searchIndex));
min['groupCache'] = await KBService.getGroupReplies(instance.instanceId);
await KBService.RefreshNER(min);

View file

@ -119,7 +119,7 @@ export class GBServer {
const core: IGBCoreService = new GBCoreService();
const importer: GBImporter = new GBImporter(core);
const deployer: GBDeployer = new GBDeployer(core, importer);
const azureDeployer: AzureDeployerService = new AzureDeployerService(deployer);
const azureDeployer: AzureDeployerService = await AzureDeployerService.createInstance(deployer);
const adminService: GBAdminService = new GBAdminService(core);
if (process.env.NODE_ENV === 'development' && !process.env.BOT_URL) {