Skip to content

Commit 543d48d

Browse files
author
Florian Wagner
authored
Merge pull request #46 from h2floh/h2floh/azurefrontdoor
Add Azure Front Door option
2 parents befcc25 + e48eb4a commit 543d48d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+961
-99
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
resources:
2+
containers:
3+
- container: geobotagent
4+
image: h2floh/geobotagent
5+
6+
# No CI trigger
7+
trigger: none
8+
9+
# PR builds
10+
pr:
11+
branches:
12+
include:
13+
- master
14+
paths:
15+
include:
16+
- Deploy
17+
- AzureDevOps/oneclickdeploytest.yaml
18+
19+
# Scheduled Build on change Mo-Sa
20+
schedules:
21+
- cron: "0 18 * * 1-6"
22+
displayName: Daily 3am build on change (Seoul)
23+
branches:
24+
include:
25+
- master
26+
always: false
27+
- cron: "0 18 * * 0"
28+
displayName: Weekly 3am build (Seoul)
29+
branches:
30+
include:
31+
- master
32+
always: true
33+
34+
jobs:
35+
- job: deploy_destroy
36+
displayName: 'Deploy & Destroy'
37+
timeoutInMinutes: 120
38+
pool:
39+
name: Default
40+
demands:
41+
- docker
42+
container: geobotagent
43+
variables:
44+
# This group must include following variables:
45+
# - ServicePrincipalID (Client/App ID)
46+
# - ServicePrincipalSecret (Client Secret)
47+
# - TenantId (AAD Id)
48+
# - SubscriptionId (Azure Subscription)
49+
- group: SubscriptionDetails
50+
steps:
51+
- pwsh: |
52+
# generate random bot name
53+
$BOTNAME = -join ((97..122) | Get-Random -Count 1 | % {[char]$_}) + -join ((48..57) + (97..122) | Get-Random -Count 9 | % {[char]$_})
54+
55+
Write-Host "##vso[task.setvariable variable=BOTNAME]$BOTNAME"
56+
displayName: 'Generate Bot Name'
57+
errorActionPreference: continue
58+
59+
- pwsh: |
60+
# Azure Login
61+
az login --service-principal --username $(ServicePrincipalID) --password $(ServicePrincipalSecret) --tenant $(TenantId)
62+
63+
# Terraform
64+
Write-Host "##vso[task.setvariable variable=ARM_CLIENT_ID]$(ServicePrincipalID)"
65+
Write-Host "##vso[task.setvariable variable=ARM_CLIENT_SECRET]$(ServicePrincipalSecret)"
66+
Write-Host "##vso[task.setvariable variable=ARM_SUBSCRIPTION_ID]$(SubscriptionId)"
67+
Write-Host "##vso[task.setvariable variable=ARM_TENANT_ID]$(TenantId)"
68+
displayName: 'Prepare Azure connection for CLI & Terraform '
69+
70+
- pwsh: |
71+
# One Click Deploy
72+
73+
Deploy/OneClickDeploy.ps1 -BOT_NAME $env:BOTNAME -AZUREFRONTDOOR $True -AUTOAPPROVE $True
74+
75+
# $Lastexitcode $True -> Success, we have to change it to 0
76+
exit -not $LASTEXITCODE
77+
errorActionPreference: continue
78+
displayName: OneClickDeploy
79+
80+
- pwsh: |
81+
# One Click Destroy
82+
83+
Deploy/OneClickDestroy.ps1 -BOT_NAME $env:BOTNAME -AUTOAPPROVE $True
84+
85+
# $Lastexitcode $True -> Success, we have to change it to 0
86+
exit -not $LASTEXITCODE
87+
errorActionPreference: continue
88+
displayName: OneClickDestroy
89+
condition: always()
90+
91+
- pwsh: |
92+
# Az logout
93+
az logout
94+
95+
# Clear ENV Variables
96+
# Terraform
97+
$env:ARM_CLIENT_ID=""
98+
$env:ARM_CLIENT_SECRET=""
99+
$env:ARM_SUBSCRIPTION_ID=""
100+
$env:ARM_TENANT_ID=""
101+
failOnStderr: true
102+
displayName: 'Az Logout'
103+
condition: always()
104+
105+

Deploy/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ botnotselfcontained.zip
55
# Ignore Terraform tfvars
66
*.tfvars.*
77
# Ignore Terraform plan files (we just use this notation since internet search didn't brought any ideas)
8-
*.tfplan
8+
*.tfplan
9+
# Ignore IaC Folder
10+
IaC/

Deploy/DeployInfrastructure.ps1

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ Terraform Deployment of the base infrastructure
77
88
This script will do following steps:
99
10-
1. Deploy Infrastructure with Terraform
10+
1. Determine which variant will be deployed
11+
2. Deploy Infrastructure with Terraform
1112
1213
After the script is successfully executed the Bot can be deployed to WebApps and infrastructure is ready for import
1314
a SSL certificate and activation of TrafficManager
@@ -36,6 +37,10 @@ param(
3637
[Parameter(HelpMessage="Region used for global services")]
3738
[string] $BOT_GLOBAL_REGION = "japaneast",
3839

40+
# Distribution Service: TrafficManager or Azure FrontDoor - Default: $False -> TrafficManager, $True -> AzureFrontDoor
41+
[Parameter(HelpMessage="Distribution Service: TrafficManager or Azure FrontDoor - Default: `$False -> TrafficManager, `$True -> AzureFrontDoor")]
42+
[bool] $AZUREFRONTDOOR = $False,
43+
3944
# Terraform and SSL creation Automation Flag. $False -> Interactive, Approval $True -> Automatic Approval
4045
[Parameter(HelpMessage="Terraform and SSL creation Automation Flag. `$False -> Interactive, Approval `$True -> Automatic Approval")]
4146
[bool] $AUTOAPPROVE = $False
@@ -50,8 +55,20 @@ $azureBotRegions = "$(Get-ScriptPath)/$terraformFolder/azure_bot_regions.tfvars.
5055
# Tell who you are (See HelperFunction.ps1)
5156
Write-WhoIAm
5257

58+
# Choosing Terraform "Template", it is easier to copy everything to IaC folder for the script flow after deployment (retrieval of Outputs)
59+
Write-Host "## 1. Determine which variant will be deployed"
60+
if ($AZUREFRONTDOOR)
61+
{
62+
Write-Host "### Front Door, copying template to IaC folder..."
63+
Copy-TerraformFolder -FROM "IaCAFD"
64+
}
65+
else {
66+
Write-Host "### TrafficManager, copying template to IaC folder..."
67+
Copy-TerraformFolder -FROM "IaCTM"
68+
}
69+
5370
# Execute first Terraform to create the infrastructure
54-
Write-Host "## 1. Deploy Infrastructure with Terraform"
71+
Write-Host "## 2. Deploy Infrastructure with Terraform"
5572

5673
# Create Variable file for Terraform
5774
$result = Set-RegionalVariableFile -FILENAME $azureBotRegions -BOT_REGIONS $BOT_REGIONS

Deploy/HelperFunctions.ps1

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,37 @@ function Check-ServiceAvailability {
6666
[Parameter(HelpMessage="Full Qualified Domain Name to check")]
6767
[string] $FQDN
6868
)
69-
# Not working in PowerShellCore: Resolve-DnsName -Name $FQDN -DnsOnly > $null 2> $1
70-
# Changing to nslookup
71-
$resolved = nslookup $FQDN 2> $null
72-
if ((($resolved | Select-String $FQDN).Length -gt 0) -and (($resolved | Select-String "server can't find").Length -eq 0))
69+
70+
$available = $True
71+
72+
if ($Service -eq "FrontDoor")
73+
{
74+
# FrontDoor DNS always exists, curl on unavailable resource reveals 302 redirect to /pages/404.html (notfound)
75+
# curl -I https://mygeobot2.azurefd.net
76+
# HTTP/1.1 302 Found
77+
# Content-Length: 0
78+
# Location: /pages/404.html
79+
# Server: Microsoft-IIS/10.0
80+
# X-MSEdge-Ref: Ref A: C18D49B3B18F4EBB950B562E01AB4347 Ref B: SLAEDGE0808 Ref C: 2020-01-30T04:40:26Z
81+
# Date: Thu, 30 Jan 2020 04:40:26 GMT
82+
$CurlArgument = '-I', "https://$FQDN"
83+
$httpresult = curl @CurlArgument 2> $null
84+
$result = [string]::Concat($httpresult)
85+
$available = $result.Contains("302 Found") -and $result.Contains("404.html")
86+
}
87+
else {
88+
# Not working in PowerShellCore: Resolve-DnsName -Name $FQDN -DnsOnly > $null 2> $1
89+
# Changing to nslookup
90+
$resolved = nslookup $FQDN 2> $null
91+
$available = -not ((($resolved | Select-String $FQDN).Length -gt 0) -and (($resolved | Select-String "server can't find").Length -eq 0))
92+
}
93+
94+
if (-not $available)
7395
{
7496
Write-Host -ForegroundColor Red "### ERROR, $Service with name '$FQDN' already exists. Please try another Bot Name."
75-
return $False
76-
} else {
77-
return $True
7897
}
98+
99+
return $available
79100
}
80101

81102
function Set-RegionalVariableFile {
@@ -210,4 +231,24 @@ function Invoke-Terraform {
210231
# Forward Execution result of Terraform
211232
$LASTEXITCODE=$TFEXEC
212233
$global:LastExitCode=$TFEXEC
234+
}
235+
236+
function Copy-TerraformFolder {
237+
<#
238+
.SYNOPSIS
239+
Copys contents from one Terraform Folder to another folder
240+
#>
241+
param(
242+
[Parameter(Mandatory=$True, HelpMessage="Source Folder")]
243+
[string] $FROM,
244+
245+
[Parameter(HelpMessage="Destination Folder")]
246+
[string] $TO = "IaC"
247+
)
248+
# Ensure target folder exists
249+
New-Item -ItemType Directory -Force -Path "$(Get-ScriptPath)\$TO" > $null
250+
# Remove any content in target folder
251+
Get-ChildItem "$(Get-ScriptPath)\$TO" -Recurse -Force | Remove-Item -Recurse -Force
252+
# Copy content
253+
Copy-Item "$(Get-ScriptPath)\$FROM\*" -Destination "$(Get-ScriptPath)\$TO" -Recurse -Force
213254
}
File renamed without changes.

0 commit comments

Comments
 (0)