From d3e82b580610b236cba0fc9db5b737fa694e11da Mon Sep 17 00:00:00 2001 From: "Rodrigo Rodriguez (pragmatismo.io)" Date: Thu, 25 Oct 2018 21:57:28 -0300 Subject: [PATCH] More testing and adjustments on automation. --- .../services/AzureDeployerService.ts | 135 ++++++++---------- deploy/core.gbapp/services/GBConfigService.ts | 3 + deploy/core.gbapp/services/GBCoreService.ts | 1 + src/app.ts | 9 +- 4 files changed, 74 insertions(+), 74 deletions(-) diff --git a/deploy/azuredeployer.gbapp/services/AzureDeployerService.ts b/deploy/azuredeployer.gbapp/services/AzureDeployerService.ts index 811bddaf..ae3fd530 100644 --- a/deploy/azuredeployer.gbapp/services/AzureDeployerService.ts +++ b/deploy/azuredeployer.gbapp/services/AzureDeployerService.ts @@ -249,7 +249,7 @@ export class AzureDeployerService extends GBService { // No .env so asks for cloud credentials to start a new farm. if (!username || !password || !subscriptionId || !location || !botId) { process.stdout.write( - "A empty enviroment is detected. Please, enter details:" + "A empty enviroment is detected. To start automatic deploy, please enter some information:\n" ); } let retriveUsername = () => { @@ -366,22 +366,19 @@ export class AzureDeployerService extends GBService { public async deployBootBot( instance, - name, + botId, endpoint, nlpAppId, nlpKey, subscriptionId, appId ) { - let botId = name + AzureDeployerService.getRndBotId(); - [ - instance.marketplacePassword, - instance.webchatKey - ] = await this.internalDeployBot( + await this.internalDeployBot( + instance, this.accessToken, botId, - name, - name, + botId, + botId, "General BootBot", endpoint, "global", @@ -396,10 +393,6 @@ export class AzureDeployerService extends GBService { return instance; } - private async dangerouslyDeleteDeploy(name) { - return await this.resourceClient.resourceGroups.deleteMethod(name); - } - private async createStorageServer( group, name, @@ -440,7 +433,7 @@ export class AzureDeployerService extends GBService { /** * @see https://github.com/Azure/azure-rest-api-specs/blob/master/specification/botservice/resource-manager/Microsoft.BotService/preview/2017-12-01/botservice.json */ - private async internalDeployBot( + private async internalDeployBot(instance, accessToken, botId, name, @@ -453,56 +446,67 @@ export class AzureDeployerService extends GBService { subscriptionId, appId ) { - let baseUrl = `https://management.azure.com/`; - await this.registerProviders(subscriptionId, baseUrl, accessToken); + return new Promise(async (resolve, reject) => { + let baseUrl = `https://management.azure.com/`; + await this.registerProviders(subscriptionId, baseUrl, accessToken); - let appPassword = AzureDeployerService.getRndPassword(); + let appPassword = AzureDeployerService.getRndPassword(); - let parameters = { - location: location, - sku: { - name: "F0" - }, - name: botId, - kind: "sdk", - properties: { - description: description, - displayName: name, - endpoint: endpoint, - iconUrl: iconUrl, - luisAppIds: [nlpAppId], - luisKey: nlpKey, - msaAppId: appId, - msaAppPassword: appPassword - } - }; + let parameters = { + location: location, + sku: { + name: "F0" + }, + name: botId, + kind: "sdk", + properties: { + description: description, + displayName: name, + endpoint: endpoint, + iconUrl: iconUrl, + luisAppIds: [nlpAppId], + luisKey: nlpKey, + msaAppId: appId, + msaAppPassword: appPassword + } + }; - let httpClient = new ServiceClient(); + let httpClient = new ServiceClient(); - let query = `subscriptions/${subscriptionId}/resourceGroups/${group}/providers/${ - this.provider - }/botServices/${botId}?api-version=${AzureDeployerService.apiVersion}`; - let url = UrlJoin(baseUrl, query); - let req = this.createRequestObject( - url, - accessToken, - JSON.stringify(parameters) - ); - let res = await httpClient.sendRequest(req); + let query = `subscriptions/${subscriptionId}/resourceGroups/${group}/providers/${ + this.provider + }/botServices/${botId}?api-version=${AzureDeployerService.apiVersion}`; + let url = UrlJoin(baseUrl, query); + let req = this.createRequestObject( + url, + accessToken, + JSON.stringify(parameters) + ); + let res = await httpClient.sendRequest(req); - query = `subscriptions/${subscriptionId}/resourceGroups/${group}/providers/Microsoft.BotService/botServices/${botId}/channels/WebChatChannel/listChannelWithKeys?api-version=${ - AzureDeployerService.apiVersion - }`; - url = UrlJoin(baseUrl, query); - req = this.createRequestObject( - url, - accessToken, - JSON.stringify(parameters) - ); - let resChannel = await httpClient.sendRequest(req); + setTimeout(async () => { + query = `subscriptions/${subscriptionId}/resourceGroups/${group}/providers/Microsoft.BotService/botServices/${botId}/channels/WebChatChannel/listChannelWithKeys?api-version=${ + AzureDeployerService.apiVersion + }`; + url = UrlJoin(baseUrl, query); + req = this.createRequestObject( + url, + accessToken, + JSON.stringify(parameters) + ); + let resChannel = await httpClient.sendRequest(req); - let key = (resChannel.bodyAsJson as any).properties.properties.sites[0].key; - return [appPassword, key]; + console.log(resChannel.bodyAsText); + let key = (resChannel.bodyAsJson as any).properties.properties.sites[0] + .key; + + instance.marketplacePassword = appPassword + instance.webchatKey = key + + resolve(instance) + + }, 10000); + }); } private createRequestObject(url: string, accessToken: string, body) { @@ -735,20 +739,6 @@ export class AzureDeployerService extends GBService { return `sa${generated}`; } - private static getRndBotId() { - const passwordGenerator = new PasswordGenerator(); - const options = { - upperCaseAlpha: false, - lowerCaseAlpha: true, - number: true, - specialCharacter: false, - minimumLength: 8, - maximumLength: 8 - }; - let generated = passwordGenerator.generatePassword(options); - return `${generated}`; - } - private static getRndPassword() { const passwordGenerator = new PasswordGenerator(); const options = { @@ -760,6 +750,7 @@ export class AzureDeployerService extends GBService { maximumLength: 14 }; let password = passwordGenerator.generatePassword(options); + password = password.replace("=", ""); return password; } diff --git a/deploy/core.gbapp/services/GBConfigService.ts b/deploy/core.gbapp/services/GBConfigService.ts index 146fa0d0..b6791152 100644 --- a/deploy/core.gbapp/services/GBConfigService.ts +++ b/deploy/core.gbapp/services/GBConfigService.ts @@ -58,6 +58,9 @@ export class GBConfigService { case "CLOUD_USERNAME": value = undefined; break; + case "BOT_ID": + value = undefined; + break; case "CLOUD_PASSWORD": value = undefined; break; diff --git a/deploy/core.gbapp/services/GBCoreService.ts b/deploy/core.gbapp/services/GBCoreService.ts index 4c5fa3a9..b4247c84 100644 --- a/deploy/core.gbapp/services/GBCoreService.ts +++ b/deploy/core.gbapp/services/GBCoreService.ts @@ -307,6 +307,7 @@ export class GBCoreService implements IGBCoreService { `STORAGE_NAME=${instance.storageName}\n` + `STORAGE_USERNAME=${instance.storageUsername}\n` + `STORAGE_PASSWORD=${instance.storagePassword}\n`+ + `STORAGE_SYNC=true\n`+ `CLOUD_USERNAME=${instance.cloudUsername}\n` + `CLOUD_PASSWORD=${instance.cloudPassword}\n` + `CLOUD_SUBSCRIPTIONID=${instance.cloudSubscriptionId}\n` + diff --git a/src/app.ts b/src/app.ts index e90292d9..20693ae7 100644 --- a/src/app.ts +++ b/src/app.ts @@ -102,8 +102,13 @@ export class GBServer { await core.initDatabase(); } catch (error) { logger.info(`Deploying cognitive infrastructure...`); - let azureDeployer = new AzureDeployerService(); - bootInstance = await azureDeployer.deployFarm(proxyAddress); + try { + let azureDeployer = new AzureDeployerService(); + bootInstance = await azureDeployer.deployFarm(proxyAddress); + } catch (error) { + logger.warn("Error while deploying to the cloud, please, cleanup any objects created before running again.") + throw error; + } core.writeEnv(bootInstance); logger.info(`File .env written, starting...`); GBConfigService.init();