-
Notifications
You must be signed in to change notification settings - Fork 175
/
setup.ps1
120 lines (91 loc) · 4.72 KB
/
setup.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
$setupFolder = "./utils/base-templates"
$deployFolder = "./deploy"
$pythonName = "python-function-publisher"
$dotnetName = "csharp-function-subscriber"
$javascriptName = "javascript-function-subscriber"
$sourceFolder = "./src"
$tag = "edge"
$pythonFolder = "$sourceFolder/$pythonName"
$dotnetFolder = "$sourceFolder/$dotnetName"
$javascriptFolder = "$sourceFolder/$javascriptName"
# Prompts
$resourceBase = Read-Host -Prompt "Enter resource name base"
$location = Read-Host -Prompt "Enter location"
$groupName= "$resourceBase"
$clusterName= "$resourceBase" + "-cluster"
$registryName="${resourceBase}reg"
$storageName = "${resourceBase}sa"
# Resource Group
Write-Host
Write-Host "Creating resource group $groupName..."
az group create -n $groupName -l $location
# AKS
Write-Host
Write-Host "Creating AKS cluster $clusterName..."
az aks create -g $groupName -n $clusterName --generate-ssh-keys
az aks get-credentials -n $clusterName -g $groupName
# ACR
Write-Host
Write-Host "Creating ACR registry $registryName..."
az acr create --resource-group $groupName --name $registryName --sku Basic
$CLIENT_ID= az aks show --resource-group $groupName --name $clusterName --query "servicePrincipalProfile.clientId" --output tsv
$ACR_ID= az acr show --name $registryName --resource-group $groupName --query "id" --output tsv
Write-Host
Write-Host "Connecting ACR and AKS..."
az role assignment create --assignee $CLIENT_ID --role acrpull --scope $ACR_ID
az acr login -n $registryName
# Install Dapr
Write-Host
Write-Host "Installing Dapr on $clusterName..."
dapr init --kubernetes
Write-Host
Write-Host "Installing Redis as the Dapr state store on $clusterName..."
helm install redis stable/redis --set rbac.create=true
Start-Sleep -Seconds 60
$redisHost= $(kubectl get service redis-master -o=custom-columns=IP:.spec.clusterIP --no-headers=true) + ":6379"
$encoded = kubectl get secret --namespace default redis -o jsonpath="{.data.redis-password}"
$redisSecret = [System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($encoded))
(Get-Content $setupFolder/redis-base.yaml) | Foreach-Object {$_ -replace "REPLACE_HOST", $redisHost} | Foreach-Object {$_ -replace "REPLACE_SECRET", $redisSecret} | Set-Content $deployFolder/redis.yaml
# Install KEDA
Write-Host
Write-Host "Installing KEDA on $clusterName..."
#Using Helm 3.0 to install Keda
helm repo add kedacore https://kedacore.github.io/charts
helm repo update
kubectl create namespace keda
helm install keda kedacore/keda --namespace keda
#### Application section
# Provision Azure Storage
Write-Host
Write-Host "Creating storage account $storageName..."
az group create -l $location -n $groupName
az storage account create --sku Standard_LRS -l $location -g $groupName -n $storageName --kind StorageV2
$CONNECTION_STRING= az storage account show-connection-string -g $groupName -n $storageName --query connectionString
az storage queue create -n items --connection-string $CONNECTION_STRING
# Build images and deployment templates
Write-Host
Write-Host "Building and publishing images..."
$trimmedConnectionString = $CONNECTION_STRING -replace "`"", ""
$encodedConnectionString = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($trimmedConnectionString))
(Get-Content $setupFolder/python-function-publisher-base.yaml) `
| Foreach-Object {$_ -replace "IMAGE_NAME", "$registryName.azurecr.io/${pythonName}:$tag"} `
| Foreach-Object {$_ -replace "CONNECTION_STRING_B64", $encodedConnectionString} `
| Set-Content $deployFolder/python-function-publisher.yaml
docker build -t "$registryName.azurecr.io/${pythonName}:$tag" $pythonFolder
docker push "$registryName.azurecr.io/${pythonName}:$tag"
(Get-Content $setupFolder/csharp-function-subscriber-base.yaml) `
| Foreach-Object {$_ -replace "IMAGE_NAME", "$registryName.azurecr.io/${dotnetName}:$tag"} `
| Foreach-Object {$_ -replace "CONNECTION_STRING_B64", $encodedConnectionString} `
| Set-Content $deployFolder/csharp-function-subscriber.yaml
docker build -t "$registryName.azurecr.io/${dotnetName}:$tag" $dotnetFolder
docker push "$registryName.azurecr.io/${dotnetName}:$tag"
(Get-Content $setupFolder/javascript-function-subscriber-base.yaml) `
| Foreach-Object {$_ -replace "IMAGE_NAME", "$registryName.azurecr.io/${javascriptName}:$tag"} `
| Foreach-Object {$_ -replace "CONNECTION_STRING_B64", $encodedConnectionString} `
| Set-Content $deployFolder/javascript-function-subscriber.yaml
docker build -t "$registryName.azurecr.io/${javascriptName}:$tag" $javascriptFolder
docker push "$registryName.azurecr.io/${javascriptName}:$tag"
# Deploy
Write-Host
Write-Host "Deploying application..."
kubectl apply -f $deployFolder