Skip to content

Commit

Permalink
Merge pull request #72 from tksh164/improve-logging
Browse files Browse the repository at this point in the history
Improve logging
  • Loading branch information
tksh164 authored Mar 30, 2024
2 parents 118367b + 09d1120 commit 3cdfcca
Show file tree
Hide file tree
Showing 10 changed files with 861 additions and 488 deletions.
269 changes: 158 additions & 111 deletions template/customscripts/common.psm1

Large diffs are not rendered by default.

122 changes: 79 additions & 43 deletions template/customscripts/configure-lab-host.ps1

Large diffs are not rendered by default.

20 changes: 15 additions & 5 deletions template/customscripts/create-base-vhd-job.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ Import-Module -Name $PSModuleNameToImport -Force

$labConfig = Get-LabDeploymentConfig
Start-ScriptLogging -OutputDirectory $labConfig.labHost.folderPath.log -FileName $LogFileName
Set-ScriptLogDefaultContext -LogContext ('{0}_{1}_{2}' -f $OperatingSystem, $ImageIndex, $Culture)

'Lab deployment config:' | Write-ScriptLog
$labConfig | ConvertTo-Json -Depth 16 | Write-Host

$params = @{
Expand All @@ -49,12 +52,13 @@ if ($PSBoundParameters.Keys.Contains('IsoFileNameSuffix')) {
Suffix = $IsoFileNameSuffix
}
$copiedIsoFilePath = [IO.Path]::Combine($labConfig.labHost.folderPath.temp, (Format-IsoFileName @params))
'Copying an ISO file for concurrency from "{0}" to "{1}"...' -f $isoFilePath, $copiedIsoFilePath | Write-ScriptLog -Context $env:ComputerName
Copy-Item -LiteralPath $isoFilePath -Destination $copiedIsoFilePath -Force -PassThru | Format-List -Property '*' | Out-String | Write-ScriptLog -Context $env:ComputerName
'Copy an ISO file to "{0}" from "{1}" for concurrency.' -f $copiedIsoFilePath, $isoFilePath | Write-ScriptLog
Copy-Item -LiteralPath $isoFilePath -Destination $copiedIsoFilePath -Force -PassThru | Format-List -Property '*' | Out-String | Write-ScriptLog
$isoFilePath = $copiedIsoFilePath
'Copy an ISO file for concurrency completed.' | Write-ScriptLog
}

'Converting the ISO file to a VHD file...' | Write-ScriptLog -Context $env:ComputerName
'Convert the ISO file to a VHD file.' | Write-ScriptLog

$params = @{
OperatingSystem = $OperatingSystem
Expand Down Expand Up @@ -85,13 +89,19 @@ if ($updatePackage.Count -ne 0) {
}
Convert-WindowsImage @params

'Convert the ISO file to a VHD file completed.' | Write-ScriptLog

if (-not (Test-Path -PathType Leaf -LiteralPath $vhdFilePath)) {
throw 'The created VHD "{0}" does not exist.' -f $vhdFilePath
$logMessage = 'The converted VHD file "{0}" does not exist.' -f $vhdFilePath
$logMessage | Write-ScriptLog -Level Error
throw $logMessage
}

if ($PSBoundParameters.Keys.Contains('IsoFileNameSuffix')) {
# Remove the copied ISO file.
'Remove the copied ISO file "{0}".' -f $isoFilePath | Write-ScriptLog
Remove-Item -LiteralPath $isoFilePath -Force
'Remove the copied ISO file completed.' | Write-ScriptLog
}

'The base VHD creation job has been completed.' | Write-ScriptLog
Stop-ScriptLogging
25 changes: 16 additions & 9 deletions template/customscripts/create-base-vhd.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ try {

$labConfig = Get-LabDeploymentConfig
Start-ScriptLogging -OutputDirectory $labConfig.labHost.folderPath.log

'Lab deployment config:' | Write-ScriptLog
$labConfig | ConvertTo-Json -Depth 16 | Write-Host

# Base VHD specs.
Expand All @@ -89,28 +91,32 @@ try {
Culture = $labConfig.guestOS.culture
}

'Creating the temp folder if it does not exist...' | Write-ScriptLog -Context $env:ComputerName
'Create the temp folder if it does not exist.' | Write-ScriptLog
New-Item -ItemType Directory -Path $labConfig.labHost.folderPath.temp -Force
'Create the temp folder completed.' | Write-ScriptLog

'Creating the VHD folder if it does not exist...' | Write-ScriptLog -Context $env:ComputerName
'Create the VHD folder if it does not exist.' | Write-ScriptLog
New-Item -ItemType Directory -Path $labConfig.labHost.folderPath.vhd -Force
'Create the VHD folder completed.' | Write-ScriptLog

'Downloading the Convert-WindowsImage.ps1...' | Write-ScriptLog -Context $env:ComputerName
'Download the Convert-WindowsImage.ps1.' | Write-ScriptLog
$params = @{
SourceUri = 'https://raw.githubusercontent.com/microsoft/MSLab/master/Tools/Convert-WindowsImage.ps1'
DownloadFolder = $labConfig.labHost.folderPath.temp
FileNameToSave = 'Convert-WindowsImage.ps1'
}
$convertWimScriptFile = Invoke-FileDownload @params
$convertWimScriptFile
'Download the Convert-WindowsImage.ps1 completed.' | Write-ScriptLog

'Clarifying the base VHD''s specification...' | Write-ScriptLog -Context $env:ComputerName
'Clarify the base VHD''s specification.' | Write-ScriptLog
$dedupedVhdSpecs = Get-DeduplicatedBaseVhdSpec -BaseVhdSpec $addsDcVhdSpec, $wacVhdSpec, $hciNodeVhdSpec
$dedupedVhdSpecs | Format-Table -Property 'OperatingSystem', 'ImageIndex', 'Culture' | Out-String | Write-ScriptLog -Context $env:ComputerName
$dedupedVhdSpecs | Format-Table -Property 'OperatingSystem', 'ImageIndex', 'Culture' | Out-String | Write-ScriptLog
$vhdSpecs = Get-PracticalBaseVhdSpec -BaseVhdSpec $dedupedVhdSpecs
$vhdSpecs | Format-Table -Property 'OperatingSystem', 'ImageIndex', 'Culture', 'IsoFileNameSuffix' | Out-String | Write-ScriptLog -Context $env:ComputerName
$vhdSpecs | Format-Table -Property 'OperatingSystem', 'ImageIndex', 'Culture', 'IsoFileNameSuffix' | Out-String | Write-ScriptLog
'Clarify the base VHD''s specification completed.' | Write-ScriptLog

'Creating the base VHD creation jobs...' | Write-ScriptLog -Context $env:ComputerName
'Create the base VHD creation jobs.' | Write-ScriptLog
$jobScriptFilePath = [IO.Path]::Combine($PSScriptRoot, 'create-base-vhd-job.ps1')
$jobs = @()
foreach ($spec in $vhdSpecs) {
Expand All @@ -125,15 +131,16 @@ try {
if ($spec.IsoFileNameSuffix -ne $null) {
$jobParams.IsoFileNameSuffix = $spec.IsoFileNameSuffix
}
'Starting a base VHD creation job "{0}"...' -f $jobName | Write-ScriptLog -Context $env:ComputerName
'Start a base VHD creation job "{0}".' -f $jobName | Write-ScriptLog
$jobs += Start-Job -Name $jobName -LiteralPath $jobScriptFilePath -InputObject ([PSCustomObject] $jobParams)
}

$jobs | Format-Table -Property Id, Name, State, HasMoreData, PSBeginTime, PSEndTime
$jobs | Receive-Job -Wait
$jobs | Format-Table -Property Id, Name, State, HasMoreData, PSBeginTime, PSEndTime
'Create the base VHD creation jobs completed.' | Write-ScriptLog

'The base VHDs creation has been completed.' | Write-ScriptLog -Context $env:ComputerName
'The base VHDs creation has been completed.' | Write-ScriptLog
}
catch {
$jobs | Format-Table -Property Id, Name, State, HasMoreData, PSBeginTime, PSEndTime
Expand Down
Loading

0 comments on commit 3cdfcca

Please sign in to comment.