Skip to content

Commit

Permalink
Merge 6f30c13 into a991c5c
Browse files Browse the repository at this point in the history
  • Loading branch information
gluka-CBS committed Jul 23, 2019
2 parents a991c5c + 6f30c13 commit 657db32
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
4 changes: 3 additions & 1 deletion Config.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ function Get-AvailableConfigOptions {
"Description" = "Select between tar, gz, zip formats or any combination between these."},
@{"Name" = "zip_password";
"Description" = "If this parameter is set, after the image is generated,
a password protected zip archive with the image will be created."},
a password protected zip archive with the image will be created.
Previous parameter, compression_format, must contain zip format
in order for this parameter to be used"},
@{"Name" = "gold_image"; "DefaultValue" = $false; "AsBoolean" = $true;
"Description" = "It will stop the image generation after the updates are installed and cleaned."},
@{"Name" = "gold_image_path";
Expand Down
50 changes: 29 additions & 21 deletions WinImageBuilder.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ function Compress-Image {
}
throw "7za.exe failed while creating tar file for image: $tmpName"
}
Remove-Item -Force $ImagePath
Remove-Item -Force $VirtualDiskPath
}
if ($compresionFormat -eq "gz") {
Write-Log "Compressing $tmpName to gzip"
Expand All @@ -869,7 +869,16 @@ function Compress-Image {
$tmpName = ($tmpName + ".gz")
}
if ($compresionFormat -eq "zip") {
Write-Log "Archiving $VirtualDiskPath to zip $tmpName"
if ($ZipPassword) {
$zipPath = $tmpName + ".zip"
$7zip = Get-7zipPath
Write-Log "Creating protected zip..."
Write-Log "The zip password is: $ZipPassword"
Start-Executable -Command @("$7zip", "a", "-tzip", "$zipPath", `
"$tmpName", "-p$ZipPassword", "-mx1")
Remove-Item -Force $tmpName
} else {
Write-Log "Archiving $VirtualDiskPath to zip $tmpName"
# Avoid storing the full path in the archive
Write-Log "Creating zip archive..."
$zipName = $tmpName + ".zip"
Expand All @@ -880,25 +889,18 @@ function Compress-Image {
}
throw "7za.exe failed while creating tar file for image: $tmpName"
}
Remove-Item -Force $tmpName
Remove-Item -Force $tmpName
}
}
} finally {
Pop-Location }
Write-Log "Archiving finished." }
}
} catch {
Remove-Item -Force $tmpName -ErrorAction SilentlyContinue
Remove-Item -Force $VirtualDiskPath -ErrorAction SilentlyContinue
throw
}
if ($ZipPassword) {
$zipPath = $tmpName + ".zip"
$7zip = Get-7zipPath
Write-Log "Creating protected zip..."
Write-Log "The zip password is: $ZipPassword"
Start-Executable -Command @("$7zip", "a", "-tzip", "$zipPath", `
"$tmpName", "-p$ZipPassword", "-mx1")
Remove-Item -Force $tmpName
}
Pop-Location
if (Test-Path $ImagePath) {
throw "File $ImagePath already exists. The image has been created at $tmpName."
}
Expand Down Expand Up @@ -1247,6 +1249,10 @@ function New-WindowsOnlineImage {
Remove-IniFileValue -Path $offlineConfigFilePath `
-Key 'zip_password' -Section 'DEFAULT'
}
if ($windowsImageConfig.compression_format) {
Remove-IniFileValue -Path $offlineConfigFilePath `
-Key 'compression_format' -Section 'DEFAULT'
}
New-WindowsCloudImage -ConfigFilePath $offlineConfigFilePath

if ($windowsImageConfig.run_sysprep) {
Expand Down Expand Up @@ -1284,6 +1290,7 @@ function New-WindowsOnlineImage {
-CompressQcow2 $windowsImageConfig.compress_qcow2
Remove-Item -Force $virtualDiskPath
}

if ($windowsImageConfig.compression_format) {
Compress-Image -VirtualDiskPath $uncompressedImagePath `
-ImagePath $windowsImageConfig['image_path'] `
Expand Down Expand Up @@ -1358,6 +1365,8 @@ function New-WindowsCloudImage {
'Image' = $image;
'AdministratorPassword' = $windowsImageConfig.administrator_password;
}
$barePath = Get-PathWithoutExtension $windowsImageConfig.image_path
$uncompressedImagePath = $barePath + ".vhdx"
if ($windowsImageConfig.product_key) {
$xmlParams.Add('productKey', $windowsImageConfig.product_key);
}
Expand Down Expand Up @@ -1416,11 +1425,13 @@ function New-WindowsCloudImage {
} elseif ($vhdPath -ne $windowsImageConfig.image_path) {
Move-Item -Force $vhdPath $windowsImageConfig.image_path
}

if ($windowsImageConfig.zip_password) {
New-ProtectedZip -ZipPassword $windowsImageConfig.zip_password `
-virtualDiskPath $windowsImageConfig.image_path
}

if ($windowsImageConfig.compression_format) {
Compress-Image -VirtualDiskPath $uncompressedImagePath `
-ImagePath $windowsImageConfig['image_path'] `
-compressionFormats $windowsImageConfig.compression_format `
-ZipPassword $windowsImageConfig.zip_password
}
Write-Log "Cloud image generation finished."
}

Expand Down Expand Up @@ -1552,9 +1563,6 @@ function New-WindowsFromGoldenImage {
Convert-VirtualDisk -vhdPath $windowsImageConfig.gold_image_path -outPath $uncompressedImagePath `
-format "RAW"
Remove-Item -Force $windowsImageConfig.gold_image_path
if (!($windowsImageConfig.compression_format -match ".tar.gz")) {
$windowsImageConfig.compression_format = ".tar.gz" + $windowsImageConfig.compression_format
}
}
if ($windowsImageConfig.image_type -eq "KVM") {
$uncompressedImagePath = $barePath + ".qcow2"
Expand Down

0 comments on commit 657db32

Please sign in to comment.