1+ param (
2+ [Parameter ()]
3+ [string ]$manifestTemplateFolder = " ./manifests" ,
4+ [Parameter ()]
5+ [string ]$IMAGE = " devopsshield/devsecops-pygoat" ,
6+ [Parameter ()]
7+ [string ]$TAG = " latest" ,
8+ [Parameter ()]
9+ [string ]$dnsResourceGroupName = " rg-dns-prod" ,
10+ [Parameter ()]
11+ [string ]$dnsZoneName = " cad4devops.com" ,
12+ [Parameter ()]
13+ [ValidateSet (" " , " -dev" , " -test" )]
14+ [string ]$environmentSuffix = " -test" , # "-dev", "-test", ""
15+ [Parameter ()]
16+ [string ]$dnsRecordSetName = " pygoat${environmentSuffix} " ,
17+ [Parameter ()]
18+ [string ]$HOSTURL = " ${dnsRecordSetName} .${dnsZoneName} " ,
19+ [Parameter ()]
20+ [string ]$serviceName = " pygoat-svc" ,
21+ [Parameter ()]
22+ [string ]$namespace = " pygoat${environmentSuffix} " ,
23+ [Parameter ()]
24+ [string ]$subscriptionId = " Microsoft Azure Sponsorship"
25+ )
26+ # docker pull devopsshield/devsecops-pygoat:latest
27+
28+ # echo parameters
29+ Write-Host " manifestTemplateFolder: $manifestTemplateFolder "
30+ Write-Host " IMAGE: $IMAGE "
31+ Write-Host " TAG: $TAG "
32+ Write-Host " HOSTURL: $HOSTURL "
33+ Write-Host " serviceName: $serviceName "
34+ Write-Host " namespace: $namespace "
35+ Write-Host " dnsResourceGroupName: $dnsResourceGroupName "
36+ Write-Host " dnsZoneName: $dnsZoneName "
37+ Write-Host " dnsRecordSetName: $dnsRecordSetName "
38+ Write-Host " subscriptionId: $subscriptionId "
39+ Write-Host " environmentSuffix: $environmentSuffix "
40+
41+
42+ # create a namespace if it does not exist
43+ Write-Output " Creating namespace $namespace if it does not exist"
44+ kubectl create namespace $namespace -- dry- run= client - o yaml | kubectl apply -f -
45+
46+ # deploy k8s manifests
47+ Write-Output " Deploying k8s manifests in folder $manifestTemplateFolder "
48+
49+ # loop through each manifest file in the folder with extension template.yaml
50+ $manifestFiles = Get-ChildItem - Path $manifestTemplateFolder - Filter " *.template.yaml"
51+ foreach ($manifestFile in $manifestFiles ) {
52+ Write-Output " Processing manifest file $manifestFile "
53+ $manifestFileContent = Get-Content $manifestFile.FullName
54+ # replace #{image}# with the value of the environment variable IMAGE
55+ $manifestFileContent = $manifestFileContent -replace " #\{image\}#" , $IMAGE
56+ # replace #{tag}# with the value of the environment variable TAG
57+ $manifestFileContent = $manifestFileContent -replace " #\{tag\}#" , $TAG
58+ # replace #{host}# with the value of the environment variable HOST
59+ $manifestFileContent = $manifestFileContent -replace " #\{host\}#" , $HOSTURL
60+ # create a new file with the same name but without the .template extension
61+ $newEnvironmentSuffix = $environmentSuffix -replace " -" , " ."
62+ $newManifestFile = $manifestFile.FullName -replace " .template" , $newEnvironmentSuffix
63+ Write-Output " Writing processed manifest file $newManifestFile "
64+ Set-Content - Path $newManifestFile - Value $manifestFileContent
65+ # apply the manifest file
66+ Write-Output " Applying manifest file $newManifestFile "
67+ kubectl apply -f $manifestFile.FullName -- namespace $namespace
68+ }
69+
70+ Write-Output " Finished deploying k8s manifests"
71+
72+ # get the external IP address of the service
73+ $service = kubectl get service $serviceName -- namespace $namespace - o json | ConvertFrom-Json
74+ $externalIp = $service.status.loadBalancer.ingress [0 ].ip
75+ Write-Output " External IP address of the service $serviceName is $externalIp "
76+
77+ # get all pods in the namespace
78+ $pods = kubectl get pods -- namespace $namespace
79+ Write-Output " Pods in namespace ${namespace} :"
80+ Write-Output $pods
81+
82+ # now get all
83+ Write-Output " Getting all resources in namespace $namespace "
84+ kubectl get all -- namespace $namespace
85+
86+ # give instructions to access the service
87+ Write-Output " To access the service, open a web browser and go to http://$externalIp "
88+
89+ # open a web browser
90+ Write-Output " Opening a web browser to http://$externalIp "
91+ Start-Process " http://$externalIp "
92+
93+ # create a DNS record for the service in Azure DNS
94+ Write-Output " Creating a DNS record for the service in Azure DNS"
95+
96+ # login to Azure
97+ Write-Output " Logging in to Azure"
98+ az login
99+
100+ # set subscription
101+ Write-Output " Setting subscription to $subscriptionId "
102+ az account set -- subscription " $subscriptionId "
103+
104+ # show the current subscription
105+ Write-Output " Current subscription:"
106+ az account show
107+
108+ Write-Output " Creating DNS record set $dnsRecordSetName in zone $dnsZoneName in resource group $dnsResourceGroupName "
109+ # delete the existing DNS record set if it exists
110+ Write-Output " Deleting existing DNS record set $dnsRecordSetName in zone $dnsZoneName in resource group $dnsResourceGroupName "
111+ az network dns record- set a delete `
112+ -- resource- group $dnsResourceGroupName `
113+ -- zone- name $dnsZoneName `
114+ -- name $dnsRecordSetName `
115+ -- yes
116+ Write-Output " DNS record set $dnsRecordSetName deleted in zone $dnsZoneName in resource group $dnsResourceGroupName "
117+ az network dns record- set a create `
118+ -- resource- group $dnsResourceGroupName `
119+ -- name $dnsRecordSetName `
120+ -- zone- name $dnsZoneName
121+ Write-Output " DNS record set $dnsRecordSetName created in zone $dnsZoneName in resource group $dnsResourceGroupName "
122+ az network dns record- set a add-record `
123+ -- resource- group $dnsResourceGroupName `
124+ -- zone- name $dnsZoneName `
125+ -- record- set-name $dnsRecordSetName `
126+ -- ipv4- address $externalIp
127+
128+ Write-Output " DNS record set $dnsRecordSetName created in zone $dnsZoneName in resource group $dnsResourceGroupName "
129+
130+ Write-Output " Finished creating DNS record set"
131+
132+ # test the DNS record
133+ Write-Output " Testing the DNS record"
134+
135+ # open a web browser
136+ Write-Output " Opening a web browser to http://$HOSTURL "
137+ Start-Process " http://$HOSTURL "
0 commit comments