Fix macOS CI - clean images updates (#9032)

This commit is contained in:
Erik Bershel
2023-12-15 12:16:58 +01:00
committed by GitHub
parent 9e49d08492
commit 5e4039478f
2 changed files with 35 additions and 62 deletions

View File

@@ -89,13 +89,6 @@ function Invoke-SoftwareUpdate {
return return
} }
Write-Host "`t[*] Fetching Software Updates ready to install on '$TemplateName' VM:"
Show-StringWithFormat $newUpdates
$listOfNewUpdates = $($($newUpdates.Split("*")).Split("Title") | Where-Object {$_ -match "Label:"}).Replace("Label: ", '')
Write-Host "`t[*] Installing Software Updates on '$TemplateName' VM:"
Install-SoftwareUpdate -HostName $ipAddress -listOfUpdates $listOfNewUpdates -Password $Password | Show-StringWithFormat
# Check if Action: restart
# Define the next macOS version # Define the next macOS version
$command = "sw_vers" $command = "sw_vers"
$guestMacosVersion = Invoke-SSHPassCommand -HostName $ipAddress -Command $command $guestMacosVersion = Invoke-SSHPassCommand -HostName $ipAddress -Command $command
@@ -103,27 +96,26 @@ function Invoke-SoftwareUpdate {
'12.\d' { $nextOSVersion = 'macOS Ventura|macOS Sonoma' } '12.\d' { $nextOSVersion = 'macOS Ventura|macOS Sonoma' }
'13.\d' { $nextOSVersion = 'macOS Sonoma' } '13.\d' { $nextOSVersion = 'macOS Sonoma' }
} }
# Make an array of updates
$listOfNewUpdates = $newUpdates.split('*').Trim('') Write-Host "`t[*] Fetching Software Updates ready to install on '$TemplateName' VM:"
Show-StringWithFormat $newUpdates
$listOfNewUpdates = $($($newUpdates.Split("*")).Split("Title").where({$_ -match "Label:"}).Replace("Label: ", '').where({$_ -notmatch $nextOSVersion}))
Write-Host "`t[*] Installing Software Updates on '$TemplateName' VM:"
Install-SoftwareUpdate -HostName $ipAddress -listOfUpdates $listOfNewUpdates -Password $Password | Show-StringWithFormat
Write-Host "`t[*] Sleep 60 seconds before the software updates have been installed"
Start-Sleep -Seconds 60
Write-Host "`t[*] Waiting for loginwindow process"
Wait-LoginWindow -HostName $ipAddress | Show-StringWithFormat
# Re-enable AutoLogon after installing a new security software update
Invoke-EnableAutoLogon
foreach ($newupdate in $listOfNewUpdates) { foreach ($newupdate in $listOfNewUpdates) {
# Will be True if the value is not Venture, not empty, and contains "Action: restart" words # Check software updates have been installed
if ($newupdate.Contains("Action: restart") -and !($newupdate -match $nextOSVersion) -and (-not [String]::IsNullOrEmpty($newupdate))) { $updates = Get-SoftwareUpdate -HostName $ipAddress
Write-Host "`t[*] Sleep 120 seconds before the software updates have been installed" if ($updates.Contains("Action: restart") -and !($updates -match $nextOSVersion)) {
Start-Sleep -Seconds 120 Write-Host "`t[x] Software updates failed to install: "
Show-StringWithFormat $updates
Write-Host "`t[*] Waiting for loginwindow process" exit 1
Wait-LoginWindow -HostName $ipAddress | Show-StringWithFormat
# Re-enable AutoLogon after installing a new security software update
Invoke-EnableAutoLogon
# Check software updates have been installed
$updates = Get-SoftwareUpdate -HostName $ipAddress
if ($updates.Contains("Action: restart") -and !($newupdate -match $nextOSVersion)) {
Write-Host "`t[x] Software updates failed to install: "
Show-StringWithFormat $updates
exit 1
}
} }
} }
@@ -136,6 +128,7 @@ function Invoke-SoftwareUpdate {
Invoke-SSHPassCommand -HostName $ipAddress -Command $command | Show-StringWithFormat Invoke-SSHPassCommand -HostName $ipAddress -Command $command | Show-StringWithFormat
} }
function Invoke-UpdateSettings { function Invoke-UpdateSettings {
param ( param (
[Parameter(Mandatory)] [Parameter(Mandatory)]

View File

@@ -266,36 +266,13 @@ function Install-SoftwareUpdate {
[array] $listOfUpdates, [array] $listOfUpdates,
[string] $Password [string] $Password
) )
# If an update is happening on macOS 12 or 13 we will use the prepared list of updates, otherwise, we will install all updates. # If an update is happening on macOS arm64 we will use the additional tool to install updates.
$command = "sw_vers" $osArch = $(arch)
$guestMacosVersion = Invoke-SSHPassCommand -HostName $HostName -Command $command if ($osArch -eq "arm64") {
if ($guestMacosVersion[1] -match "12") { Invoke-SoftwareUpdateArm64 -HostName $HostName -Password $Password -ListOfUpdates $listOfUpdates
foreach ($update in $listOfUpdates) {
# Filtering updates that contain "Ventura" word
if ($update -notmatch "Ventura") {
$command = "sudo /usr/sbin/softwareupdate --restart --verbose --install '$($update.trim())'"
Invoke-SSHPassCommand -HostName $HostName -Command $command
}
}
} elseif ($guestMacosVersion[1] -match "13") {
$osArch = $(arch)
if ($osArch -eq "arm64") {
Invoke-SoftwareUpdateArm64 -HostName $HostName -Password $Password -ListOfUpdates $listOfUpdates
} else {
foreach ($update in $listOfUpdates) {
# Filtering updates that contain "Sonoma" word
if ($update -notmatch "Sonoma") {
$command = "sudo /usr/sbin/softwareupdate --restart --verbose --install '$($update.trim())'"
Invoke-SSHPassCommand -HostName $HostName -Command $command
}
}
}
} else { } else {
$osArch = $(arch) foreach ($update in $listOfUpdates) {
if ($osArch -eq "arm64") { $command = "sudo /usr/sbin/softwareupdate --restart --verbose --install '$($update.trim())'"
Invoke-SoftwareUpdateArm64 -HostName $HostName -Password $Password -ListOfUpdates $listOfUpdates
} else {
$command = "sudo /usr/sbin/softwareupdate --all --install --restart --verbose"
Invoke-SSHPassCommand -HostName $HostName -Command $command Invoke-SSHPassCommand -HostName $HostName -Command $command
} }
} }
@@ -348,14 +325,17 @@ function Invoke-WithRetry {
[int] $RetryCount = 20, [int] $RetryCount = 20,
[int] $Seconds = 60 [int] $Seconds = 60
) )
while ($RetryCount -gt 0) { while ($RetryCount -gt 0) {
if ($Command) { try {
$result = & $Command if ($Command) {
} $result = & $Command
}
if (& $BreakCondition) { if (& $BreakCondition) {
return $result return $result
}
} catch {
Write-Host "`t [!] Error during command execution: $_"
} }
$RetryCount-- $RetryCount--