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