Cleanup json formatted packer template references (#9165)

This commit is contained in:
Shamil Mubarakshin
2024-01-15 16:40:23 +01:00
committed by GitHub
parent 5ee9ae1e60
commit 4d310f0a59
3 changed files with 11 additions and 38 deletions

View File

@@ -28,7 +28,7 @@ Function CreateAzureVMFromPackerTemplate {
The location where the Azure virtual machine will be provisioned. Example: "eastus" The location where the Azure virtual machine will be provisioned. Example: "eastus"
.EXAMPLE .EXAMPLE
CreateAzureVMFromPackerTemplate -SubscriptionId {YourSubscriptionId} -ResourceGroupName {ResourceGroupName} -TemplateFile "C:\BuildVmImages\temporaryTemplate.json" -VirtualMachineName "testvm1" -AdminUsername "shady1" -AdminPassword "SomeSecurePassword1" -AzureLocation "eastus" CreateAzureVMFromPackerTemplate -SubscriptionId {SubscriptionId} -ResourceGroupName {ResourceGroupName} -VirtualMachineName "testvm1" -ManagedImageName {ManagedImageName} -AdminUsername "shady1" -AdminPassword "SomeSecurePassword1" -AzureLocation "eastus"
#> #>
param ( param (
[Parameter(Mandatory = $True)] [Parameter(Mandatory = $True)]

View File

@@ -176,30 +176,20 @@ Function GenerateResourcesAndImage {
} }
Write-Host "Access to packer generated VM will be restricted to agent IP Address: $AgentIp." Write-Host "Access to packer generated VM will be restricted to agent IP Address: $AgentIp."
if ($TemplatePath.Contains("pkr.hcl")) { if ($PSVersionTable.PSVersion.Major -eq 5) {
if ($PSVersionTable.PSVersion.Major -eq 5) { Write-Verbose "PowerShell 5 detected. Replacing double quotes with escaped double quotes in allowed inbound IP addresses."
Write-Verbose "PowerShell 5 detected. Replacing double quotes with escaped double quotes in allowed inbound IP addresses." $AllowedInboundIpAddresses = '[\"{0}\"]' -f $AgentIp
$AllowedInboundIpAddresses = '[\"{0}\"]' -f $AgentIp }
} elseif ($PSVersionTable.PSVersion.Major -eq 7 -and $PSVersionTable.PSVersion.Minor -le 2) {
elseif ($PSVersionTable.PSVersion.Major -eq 7 -and $PSVersionTable.PSVersion.Minor -le 2) { Write-Verbose "PowerShell 7.0-7.2 detected. Replacing double quotes with escaped double quotes in allowed inbound IP addresses."
Write-Verbose "PowerShell 7.0-7.2 detected. Replacing double quotes with escaped double quotes in allowed inbound IP addresses." $AllowedInboundIpAddresses = '[\"{0}\"]' -f $AgentIp
$AllowedInboundIpAddresses = '[\"{0}\"]' -f $AgentIp
}
else {
$AllowedInboundIpAddresses = '["{0}"]' -f $AgentIp
}
} }
else { else {
$AllowedInboundIpAddresses = $AgentIp $AllowedInboundIpAddresses = '["{0}"]' -f $AgentIp
} }
} }
else { else {
if ($TemplatePath.Contains("pkr.hcl")) { $AllowedInboundIpAddresses = "[]"
$AllowedInboundIpAddresses = "[]"
}
else {
$AllowedInboundIpAddresses = ""
}
} }
Write-Debug "Allowed inbound IP addresses: $AllowedInboundIpAddresses." Write-Debug "Allowed inbound IP addresses: $AllowedInboundIpAddresses."
@@ -216,16 +206,6 @@ Function GenerateResourcesAndImage {
$TagsJson = $TagsJson -replace '"', '\"' $TagsJson = $TagsJson -replace '"', '\"'
} }
Write-Debug "Tags JSON: $TagsJson." Write-Debug "Tags JSON: $TagsJson."
if ($TemplatePath.Contains(".json")) {
Write-Verbose "Injecting tags into packer template."
if ($Tags) {
$BuilderScriptPathInjected = $TemplatePath.Replace(".json", "-temp.json")
$PackerTemplateContent = Get-Content -Path $TemplatePath | ConvertFrom-Json
$PackerTemplateContent.builders | Add-Member -Name "azure_tags" -Value $Tags -MemberType NoteProperty
$PackerTemplateContent | ConvertTo-Json -Depth 3 | Out-File -Encoding Ascii $BuilderScriptPathInjected
$TemplatePath = $BuilderScriptPathInjected
}
}
$InstallPassword = $env:UserName + [System.GUID]::NewGuid().ToString().ToUpper() $InstallPassword = $env:UserName + [System.GUID]::NewGuid().ToString().ToUpper()

View File

@@ -69,14 +69,7 @@ jobs:
$TemplateDirectoryPath = Join-Path "images" $TemplateDirectoryName | Resolve-Path $TemplateDirectoryPath = Join-Path "images" $TemplateDirectoryName | Resolve-Path
$TemplateFileName = "${{ parameters.image_template_name }}" $TemplateFileName = "${{ parameters.image_template_name }}"
if ($TemplateFileName) { $TemplatePath = Join-Path $TemplateDirectoryPath $TemplateFileName
$TemplatePath = Join-Path $TemplateDirectoryPath $TemplateFileName
} else {
$TemplatePath = Join-Path $TemplateDirectoryPath "$ImageType.pkr.hcl"
if ( -not (Test-Path $TemplatePath) ) {
$TemplatePath = Join-Path $TemplateDirectoryPath "$ImageType.json"
}
}
Write-Host "##vso[task.setvariable variable=TemplateDirectoryPath;]$TemplateDirectoryPath" Write-Host "##vso[task.setvariable variable=TemplateDirectoryPath;]$TemplateDirectoryPath"
Write-Host "##vso[task.setvariable variable=TemplatePath;]$TemplatePath" Write-Host "##vso[task.setvariable variable=TemplatePath;]$TemplatePath"