1+ param (
2+ [Parameter ()]
3+ [string ]$nameSuffix = " ek005" ,
4+ [Parameter ()]
5+ [string ]$deploymentName = " deploy-rg-defectdojo-$nameSuffix " ,
6+ [Parameter ()]
7+ [string ]$location = " canadacentral" ,
8+ [Parameter ()]
9+ [string ]$templateFile = " main.bicep" ,
10+ [Parameter ()]
11+ [string ]$resourceGroupName = " rg-defectdojo-$nameSuffix " ,
12+ [Parameter ()]
13+ [string ]$subscriptionId = " IT Test" ,
14+ [Parameter ()]
15+ [string ]$sshKeyPath = " $HOME \.ssh\vm-defectdojo-${nameSuffix} -id_rsa"
16+ )
17+
18+ # function to generate random password
19+ function New-Password {
20+ param (
21+ [int ]$length = 32
22+ )
23+
24+ $chars = [char []](' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+-=[]{}|;:,.<>?' )
25+ $password = -join ($chars | Get-Random - Count $length )
26+ return $password
27+ }
28+
29+ # install az cli if not already installed
30+ if (-not (Get-Command az - ErrorAction SilentlyContinue)) {
31+ Write-Output " Installing az cli"
32+ Invoke-Expression (New-Object System.Net.WebClient).DownloadString(' https://aka.ms/installazurecliwindows' )
33+ }
34+ else {
35+ Write-Output " az cli already installed"
36+ }
37+
38+ # login
39+ Write-Output " Logging in to Azure"
40+ az login
41+
42+ # set subscription
43+ Write-Output " Setting subscription to $subscriptionId "
44+ az account set -- subscription " $subscriptionId "
45+
46+ # echo parameters
47+ Write-Output " nameSuffix: $nameSuffix "
48+ Write-Output " deploymentName: $deploymentName "
49+ Write-Output " location: $location "
50+ Write-Output " templateFile: $templateFile "
51+ Write-Output " resourceGroupName: $resourceGroupName "
52+
53+ # deploy
54+ # create resource group
55+ Write-Output " Creating resource group $resourceGroupName in location $location "
56+ az group create -- name $resourceGroupName `
57+ -- location $location
58+
59+ # generate ssh key pair
60+ Write-Output " Generating ssh key pair at $sshKeyPath "
61+ if (-not (Test-Path $sshKeyPath )) {
62+ ssh- keygen - t rsa - b 2048 -f $sshKeyPath - q - N " "
63+ }
64+ else {
65+ Write-Output " ssh key pair already exists"
66+ }
67+
68+ # echo ssh public key
69+ Write-Output " Public key:"
70+ $sshPublicKey = Get-Content " $sshKeyPath .pub"
71+ Write-Output $sshPublicKey
72+
73+ # generate random password for postgresql
74+ $password = New-Password - length 32
75+ Write-Output " Generated password for PostgreSQL: $password "
76+
77+ # deploy bicep
78+ Write-Output " Deploying bicep template $templateFile to resource group $resourceGroupName "
79+ az deployment group create `
80+ -- name $deploymentName `
81+ -- resource- group $resourceGroupName `
82+ -- template- file main.bicep `
83+ -- parameters sshPublicKey= " `" $sshPublicKey `" " `
84+ -- parameters administratorLoginPassword= " `" $password `" " `
85+ -- parameters nameSuffix= " `" $nameSuffix `" "
86+
87+ # output vm public ip address from deployment output
88+ $fqdn = (az deployment group show `
89+ -- name $deploymentName `
90+ -- resource- group $resourceGroupName `
91+ -- query " properties.outputs.fqdn.value" `
92+ -- output tsv)
93+
94+ Write-Output " DefectDojo is deployed at $fqdn "
95+
96+ # output postgresql fqdn from deployment output
97+ $fullyQualifiedDomainName = (az deployment group show `
98+ -- name $deploymentName `
99+ -- resource- group $resourceGroupName `
100+ -- query " properties.outputs.fullyQualifiedDomainName.value" `
101+ -- output tsv)
102+
103+ Write-Output " PostgreSQL is deployed at $fullyQualifiedDomainName "
104+
105+ # output admin username from deployment output
106+ $adminUsername = (az deployment group show `
107+ -- name $deploymentName `
108+ -- resource- group $resourceGroupName `
109+ -- query " properties.outputs.adminUsername.value" `
110+ -- output tsv)
111+
112+ Write-Output " Admin username is $adminUsername "
113+
114+ # get psql password from deployment output
115+ $administratorLogin = (az deployment group show `
116+ -- name $deploymentName `
117+ -- resource- group $resourceGroupName `
118+ -- query " properties.outputs.administratorLogin.value" `
119+ -- output tsv)
120+
121+ # give ssh instructions
122+ Write-Output " To ssh into the VM, run the following command:"
123+ Write-Output " ssh -i $sshKeyPath $adminUsername @$fqdn "
124+
125+ # give psql instructions
126+ Write-Output " To connect to PostgreSQL, run the following command:"
127+ Write-Output " psql -h $fullyQualifiedDomainName -U $administratorLogin -P $password "
0 commit comments