From ed706485b4ab652b49d2e6c8c353a95cdfb9d034 Mon Sep 17 00:00:00 2001 From: Anthony Clack Date: Thu, 26 Apr 2018 21:36:20 +0100 Subject: [PATCH] Updated to allow offline use by pre-downloading MarketplaceItem.zip. --- ComputeAdmin/AzureStack.ComputeAdmin.psm1 | 73 +++++++++++++++++------ 1 file changed, 54 insertions(+), 19 deletions(-) diff --git a/ComputeAdmin/AzureStack.ComputeAdmin.psm1 b/ComputeAdmin/AzureStack.ComputeAdmin.psm1 index a1d8bed6..fb084f24 100644 --- a/ComputeAdmin/AzureStack.ComputeAdmin.psm1 +++ b/ComputeAdmin/AzureStack.ComputeAdmin.psm1 @@ -135,6 +135,9 @@ function Add-AzsVMImage { [Parameter(ParameterSetName = 'VMImageFromAzure')] [bool] $CreateGalleryItem = $true, + [Parameter(ParameterSetName = 'VMImageFromLocal')] + [string] $MarketPlaceZipPath = '', + [switch] $Force ) @@ -296,8 +299,8 @@ function Add-AzsVMImage { Set-AzureRmCurrentStorageAccount -StorageAccountName $storageAccountName -ResourceGroupName $resourceGroupName $container = Get-AzureStorageContainer -Name $containerName -ErrorAction SilentlyContinue - if ($CreateGalleryItem -eq $true -And $platformImage.Properties.ProvisioningState -eq 'Succeeded') { - $GalleryItem = CreateGalleryItem -publisher $publisher -offer $offer -sku $sku -version $version -osType $osType -title $title -description $description + if ($CreateGalleryItem -eq $true -And $platformImage.Properties.ProvisioningState -eq 'Succeeded') { + $GalleryItem = CreateGalleryItem -publisher $publisher -offer $offer -sku $sku -version $version -osType $osType -title $title -description $description -MarketPlaceZipPath "$MarketPlaceZipPath" $null = $container| Set-AzureStorageBlobContent -File $GalleryItem.FullName -Blob $galleryItem.Name $galleryItemURI = '{0}{1}/{2}' -f $storageAccount.PrimaryEndpoints.Blob.AbsoluteUri, $containerName, $galleryItem.Name @@ -470,7 +473,11 @@ function New-AzsServer2016VMImage { [bool] $CreateGalleryItem = $true, [Parameter()] - [bool] $Net35 = $true + [bool] $Net35 = $true, + + [Parameter()] + [string] $MarketPlaceZipPath + ) begin { function CreateWindowsVHD { @@ -657,10 +664,10 @@ function New-AzsServer2016VMImage { if ($CreateGalleryItem) { $description = "This evaluation image should not be used for production workloads." - Add-AzsVMImage -sku $sku -osDiskLocalPath $ImagePath @PublishArguments -title "Windows Server 2016 Datacenter Core Eval" -description $description -CreateGalleryItem $CreateGalleryItem + Add-AzsVMImage -sku $sku -osDiskLocalPath $ImagePath @PublishArguments -title "Windows Server 2016 Datacenter Core Eval" -description $description -CreateGalleryItem $CreateGalleryItem -MarketPlaceZipPath "$MarketPlaceZipPath" } else { - Add-AzsVMImage -sku $sku -osDiskLocalPath $ImagePath @PublishArguments -CreateGalleryItem $CreateGalleryItem + Add-AzsVMImage -sku $sku -osDiskLocalPath $ImagePath @PublishArguments -CreateGalleryItem $CreateGalleryItem -MarketPlaceZipPath "$MarketPlaceZipPath" } } catch { @@ -690,11 +697,11 @@ function New-AzsServer2016VMImage { Write-Verbose -Message "Server Full VHD already found." } if ($CreateGalleryItem) { - $description = "This evaluation image should not be used for production workloads." - Add-AzsVMImage -sku $sku -osDiskLocalPath $ImagePath @PublishArguments -title "Windows Server 2016 Datacenter Eval" -description $description -CreateGalleryItem $CreateGalleryItem + $description = "This evaluation image should not be used for production workloads." + Add-AzsVMImage -sku $sku -osDiskLocalPath $ImagePath @PublishArguments -title "Windows Server 2016 Datacenter Eval" -description $description -CreateGalleryItem $CreateGalleryItem -MarketPlaceZipPath "$MarketPlaceZipPath" } - else { - Add-AzsVMImage -sku $sku -osDiskLocalPath $ImagePath @PublishArguments -CreateGalleryItem $CreateGalleryItem + else { + Add-AzsVMImage -sku $sku -osDiskLocalPath $ImagePath @PublishArguments -CreateGalleryItem $CreateGalleryItem -MarketPlaceZipPath "$MarketPlaceZipPath" } } catch { @@ -729,7 +736,9 @@ Function CreateGalleryItem { [string] $Title, - [string] $Description + [string] $Description, + + [string] $MarketPlaceZipPath = '' ) $workdir = '{0}{1}' -f [System.IO.Path]::GetTempPath(), [System.Guid]::NewGuid().ToString() New-Item $workdir -ItemType Directory | Out-Null @@ -740,17 +749,43 @@ Function CreateGalleryItem { New-Item -ItemType directory -Path $extractedGalleryItemPath | Out-Null expand-archive -Path "$workdir\CustomizedVMGalleryItem.zip" -DestinationPath $extractedGalleryItemPath -Force - $maxAttempts = 5 - for ($retryAttempts = 1; $retryAttempts -le $maxAttempts; $retryAttempts++) { - try { - Write-Verbose -Message "Downloading Azure Stack Marketplace Item Generator Attempt $retryAttempts" -Verbose - Invoke-WebRequest -Uri http://www.aka.ms/azurestackmarketplaceitem -OutFile "$workdir\MarketplaceItem.zip" - break + # Check if we were provided with the MarketPlace Item Generator File + if ( $MarketPlaceZipPath -eq '' ) + { + + $maxAttempts = 5 + for ($retryAttempts = 1; $retryAttempts -le $maxAttempts; $retryAttempts++) { + try { + Write-Verbose -Message "Downloading Azure Stack Marketplace Item Generator Attempt $retryAttempts" -Verbose + Invoke-WebRequest -Uri http://www.aka.ms/azurestackmarketplaceitem -OutFile "$workdir\MarketplaceItem.zip" + break + } + catch { + if ($retryAttempts -ge $maxAttempts) { + Write-Error "Failed to download Azure Stack Marketplace Item Generator" -ErrorAction Stop + } + } } - catch { - if ($retryAttempts -ge $maxAttempts) { - Write-Error "Failed to download Azure Stack Marketplace Item Generator" -ErrorAction Stop + } + else + { + write-verbose -Message "Copying Marketplace Item Generator to working directory" -Verbose + + # Copy the provided file into the working directory + if ( Test-Path -Path "$MarketPlaceZipPath" ) + { + try { + Write-Verbose -Message "Copying Market Place Item Generator zip file into working directory" -Verbose + copy-item -Path "$MarketPlaceZipPath" -Destination "$workdir\MarketplaceItem.zip" } + catch + { + Write-Error "Failed to copy Market Place Item Generator zip into working directory: $_" -ErrorAction Stop + } + } + else + { + Write-Error "Provided Market Place Item Generator zip file does not exist" -ErrorAction Stop } }