Skip to content

Commit 592ff70

Browse files
Merge pull request #92 from pappleby64/ComputeAdmin
Changes to add-vm image, refcatoring of gallery item creation, better…
2 parents 67bdf14 + 223a39d commit 592ff70

1 file changed

Lines changed: 102 additions & 101 deletions

File tree

ComputeAdmin/AzureStack.ComputeAdmin.psm1

Lines changed: 102 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,6 @@ Function Add-VMImage{
9090
[bool] $CreateGalleryItem = $true
9191
)
9292

93-
$Domain = ""
94-
try {
95-
$uriARMEndpoint = [System.Uri] $ArmEndpoint
96-
$Domain = $ArmEndpoint.Split(".")[-3] + '.' + $ArmEndpoint.Split(".")[-2] + '.' + $ArmEndpoint.Split(".")[-1]
97-
}
98-
catch {
99-
Write-Error "The specified ARM endpoint was invalid"
100-
}
101-
10293
if($CreateGalleryItem -eq $false -and $PSBoundParameters.ContainsKey('title'))
10394
{
10495
Write-Error -Message "The title parameter only applies to creating a gallery item." -ErrorAction Stop
@@ -127,20 +118,24 @@ Function Add-VMImage{
127118
}
128119

129120
#same for storage
130-
if (-not (Get-AzureRmStorageAccount -Name $storageAccountName -ResourceGroupName $resourceGroupName -ErrorAction SilentlyContinue)) {
121+
$storageAccount = Get-AzureRmStorageAccount -Name $storageAccountName -ResourceGroupName $resourceGroupName -ErrorAction SilentlyContinue
122+
if (-not ($storageAccount)) {
131123
$storageAccount = New-AzureRmStorageAccount -Name $storageAccountName -Location $location -ResourceGroupName $resourceGroupName -Type Standard_LRS
132124
}
133125
Set-AzureRmCurrentStorageAccount -StorageAccountName $storageAccountName -ResourceGroupName $resourceGroupName
134126
#same for container
135-
if (-not (Get-AzureStorageContainer -Name $containerName -ErrorAction SilentlyContinue)) {
136-
New-AzureStorageContainer -Name $containerName -Permission Blob
127+
$container = Get-AzureStorageContainer -Name $containerName -ErrorAction SilentlyContinue
128+
if (-not ($container)) {
129+
$container = New-AzureStorageContainer -Name $containerName -Permission Blob
137130
}
138131

139132
if($pscmdlet.ParameterSetName -eq "VMImageFromLocal")
140133
{
134+
$storageAccount.PrimaryEndpoints.Blob
141135
$script:osDiskName = Split-Path $osDiskLocalPath -Leaf
142-
$script:osDiskBlobURIFromLocal = "https://$storageAccountName.blob.$Domain/$containerName/$osDiskName"
143-
Add-AzureRmVhd -Destination $osDiskBlobURIFromLocal -ResourceGroupName $resourceGroupName -LocalFilePath $osDiskLocalPath -OverWrite
136+
$script:osDiskBlobURIFromLocal = '{0}{1}/{2}' -f $storageAccount.PrimaryEndpoints.Blob.AbsoluteUri, $containerName,$osDiskName
137+
#Add-AzureRmVhd -Destination $osDiskBlobURIFromLocal -ResourceGroupName $resourceGroupName -LocalFilePath $osDiskLocalPath -OverWrite
138+
Set-AzureStorageBlobContent -File $osDiskLocalPath -Container $containerName -Blob $osDiskName
144139

145140
$script:dataDiskBlobURIsFromLocal = New-Object System.Collections.ArrayList
146141
if ($PSBoundParameters.ContainsKey('dataDisksLocalPaths'))
@@ -265,97 +260,14 @@ Function Add-VMImage{
265260

266261
if($CreateGalleryItem -eq $true -And $platformImage.Properties.ProvisioningState -eq 'Succeeded')
267262
{
268-
Add-Type -AssemblyName System.IO.Compression.FileSystem
269-
$basePath = Split-Path -Parent $MyInvocation.MyCommand.Module.Path
270-
$compressedGalleryItemPath = Join-Path $basePath 'CustomizedVMGalleryItem.azpkg'
271-
$extractedGalleryItemPath = Join-Path $basePath 'galleryItem'
272-
273-
New-Item -ItemType directory -Path $extractedGalleryItemPath
274-
275-
[System.IO.Compression.ZipFile]::ExtractToDirectory($compressedGalleryItemPath, $extractedGalleryItemPath)
276-
277-
$createUIDefinitionPath = Join-Path $extractedGalleryItemPath 'DeploymentTemplates\CreateUIDefinition.json'
278-
$JSON = Get-Content $createUIDefinitionPath | Out-String | ConvertFrom-Json
279-
$JSON.parameters.osPlatform = $osType
280-
$JSON.parameters.imageReference.publisher = $publisher
281-
$JSON.parameters.imageReference.offer = $offer
282-
$JSON.parameters.imageReference.sku = $sku
283-
$JSON | ConvertTo-Json -Compress| set-content $createUIDefinitionPath
284-
285-
$manifestPath = Join-Path $extractedGalleryItemPath 'manifest.json'
286-
$JSON = Get-Content $manifestPath | Out-String | ConvertFrom-Json
287-
288-
$displayName = ''
289-
if ($PSBoundParameters.ContainsKey('title'))
290-
{
291-
$displayName = $title
292-
}
293-
else
294-
{
295-
$displayName = "{0}-{1}-{2}" -f $publisher, $offer, $sku
296-
}
297-
298-
$name = "$offer$sku"
299-
#Remove periods so that the offer and sku can be part of the MarketplaceItem name
300-
$name =$name -replace "\.","-"
301-
302-
$JSON.name = $name
303-
$JSON.publisher = $publisher
304-
$JSON.version = $version
305-
$JSON.displayName = $displayName
306-
$JSON.publisherDisplayName = $publisher
307-
$JSON.publisherLegalName = $publisher
308-
$JSON | ConvertTo-Json -Compress| set-content $manifestPath
309-
310-
$stringsPath = Join-Path $extractedGalleryItemPath 'strings\resources.resjson'
311-
$JSON = Get-Content $stringsPath | Out-String | ConvertFrom-Json
312-
313-
$descriptionToSet = ''
314-
if ($PSBoundParameters.ContainsKey('description'))
315-
{
316-
$descriptionToSet = $description
317-
}
318-
else
319-
{
320-
$descriptionToSet = "Create a virtual machine from a VM image. Publisher: {0}, Offer: {1}, Sku:{2}, Version: {3}" -f $publisher, $offer, $sku, $version
321-
}
322-
323-
$extractedName = 'MarketplaceItem.zip'
324-
$compressedGalleryPackagerPath = Join-Path $basePath $extractedName
325-
$extractedGalleryPackagerPath = Join-Path $basePath 'MarketplaceItem'
326-
327-
$JSON.longSummary = $descriptionToSet
328-
$JSON.description = $descriptionToSet
329-
$JSON.summary = $descriptionToSet
330-
$JSON | ConvertTo-Json -Compress | set-content $stringsPath
331-
332-
Invoke-WebRequest -Uri http://www.aka.ms/azurestackmarketplaceitem -OutFile $compressedGalleryPackagerPath
333-
334-
[System.IO.Compression.ZipFile]::ExtractToDirectory($compressedGalleryPackagerPath, $extractedGalleryPackagerPath)
335-
336-
$extractedGalleryPackagerExePath = Join-Path $extractedGalleryPackagerPath "Azure Stack Marketplace Item Generator and Sample\AzureGalleryPackageGenerator"
337-
338-
$galleryItemName = $publisher + "." + $name + "." + $version + ".azpkg"
339-
$newPKGPath = Join-Path $extractedGalleryPackagerExePath $galleryItemName
340-
341-
$currentPath = $pwd
342-
343-
cd $extractedGalleryPackagerExePath
344-
345-
.\AzureGalleryPackager.exe package -m $manifestPath -o .
346-
347-
cd $currentPath
348-
349-
$galleryItemURI = "https://$storageAccountName.blob.$Domain/$containerName/$galleryItemName"
350-
351-
$blob = Set-AzureStorageBlobContent –Container $containerName –File $newPKGPath –Blob $galleryItemName
263+
$GalleryItem = CreateGalleyItem -publisher $publisher -offer $offer -sku $sku -version $version -osType $osType -title $title -description $description
264+
$blob = $container| Set-AzureStorageBlobContent –File $GalleryItem.FullName –Blob $galleryItem.Name
265+
$galleryItemURI = '{0}{1}/{2}' -f $storageAccount.PrimaryEndpoints.Blob.AbsoluteUri, $containerName,$galleryItem.Name
352266

353267
Add-AzureRMGalleryItem -SubscriptionId $subscription -GalleryItemUri $galleryItemURI -ApiVersion 2015-04-01
354268

355269
#cleanup
356-
Remove-Item $extractedGalleryItemPath -Recurse -Force
357-
Remove-Item $extractedGalleryPackagerPath -Recurse -Force
358-
Remove-Item $compressedGalleryPackagerPath
270+
Remove-Item $GalleryItem
359271
}
360272

361273
Remove-AzureStorageContainer –Name $containerName -Force
@@ -642,3 +554,92 @@ function New-Server2016VMImage {
642554
}
643555
}
644556
}
557+
558+
Function CreateGalleyItem{
559+
Param(
560+
[ValidatePattern([a-zA-Z0-9-]{3,})]
561+
[String] $publisher,
562+
[ValidatePattern([a-zA-Z0-9-]{3,})]
563+
[String] $offer,
564+
[ValidatePattern([a-zA-Z0-9-]{3,})]
565+
[String] $sku,
566+
[ValidatePattern(\d+\.\d+\.\d+)]
567+
[String] $version,
568+
[ValidateSet('Windows' ,'Linux')]
569+
[String] $osType,
570+
[string] $title,
571+
[string] $description
572+
)
573+
$workdir = '{0}\{1}' -f $env:TEMP, [System.Guid]::NewGuid().ToString()
574+
New-Item $workdir -ItemType Directory | Out-Null
575+
$basePath = (Get-Module AzureStack.ComputeAdmin).ModuleBase
576+
$compressedGalleryItemPath = Join-Path $basePath 'CustomizedVMGalleryItem.azpkg'
577+
Copy-Item -Path $compressedGalleryItemPath -Destination "$workdir\CustomizedVMGalleryItem.zip"
578+
$extractedGalleryItemPath = Join-Path $workdir 'galleryItem'
579+
New-Item -ItemType directory -Path $extractedGalleryItemPath | Out-Null
580+
expand-archive -Path "$workdir\CustomizedVMGalleryItem.zip" -DestinationPath $extractedGalleryItemPath -Force
581+
582+
$extractedName = 'MarketplaceItem.zip'
583+
Invoke-WebRequest -Uri http://www.aka.ms/azurestackmarketplaceitem -OutFile "$workdir\MarketplaceItem.zip"
584+
Expand-Archive -Path "$workdir\MarketplaceItem.zip" -DestinationPath $workdir -Force
585+
586+
#region UIDef
587+
$createUIDefinitionPath = Join-Path $extractedGalleryItemPath 'DeploymentTemplates\CreateUIDefinition.json'
588+
$JSON = Get-Content $createUIDefinitionPath | Out-String | ConvertFrom-Json
589+
$JSON.parameters.osPlatform = $osType
590+
$JSON.parameters.imageReference.publisher = $publisher
591+
$JSON.parameters.imageReference.offer = $offer
592+
$JSON.parameters.imageReference.sku = $sku
593+
$JSON | ConvertTo-Json -Compress| set-content $createUIDefinitionPath
594+
#endregion
595+
596+
#region Manifest
597+
$manifestPath = Join-Path $extractedGalleryItemPath 'manifest.json'
598+
$JSON = Get-Content $manifestPath | Out-String | ConvertFrom-Json
599+
600+
if (!$title)
601+
{
602+
$title = "{0}-{1}-{2}" -f $publisher, $offer, $sku
603+
}
604+
$name = "$offer$sku"
605+
#Remove periods so that the offer and sku can be part of the MarketplaceItem name
606+
$name =$name -replace "\.","-"
607+
$JSON.name = $name
608+
$JSON.publisher = $publisher
609+
$JSON.version = $version
610+
$JSON.displayName = $title
611+
$JSON.publisherDisplayName = $publisher
612+
$JSON.publisherLegalName = $publisher
613+
$JSON | ConvertTo-Json -Compress| set-content $manifestPath
614+
615+
#endregion
616+
617+
#region Strings
618+
if (!$description)
619+
{
620+
$description = "Create a virtual machine from a VM image. Publisher: {0}, Offer: {1}, Sku:{2}, Version: {3}" -f $publisher, $offer, $sku, $version
621+
}
622+
$stringsPath = Join-Path $extractedGalleryItemPath 'strings\resources.resjson'
623+
$JSON = Get-Content $stringsPath | Out-String | ConvertFrom-Json
624+
$JSON.longSummary = $description
625+
$JSON.description = $description
626+
$JSON.summary = $description
627+
$JSON | ConvertTo-Json -Compress | set-content $stringsPath
628+
#endregion
629+
630+
$extractedGalleryPackagerExePath = Join-Path $workdir "Azure Stack Marketplace Item Generator and Sample\AzureGalleryPackageGenerator"
631+
$galleryItemName = $publisher + "." + $name + "." + $version + ".azpkg"
632+
$currentPath = $pwd
633+
cd $extractedGalleryPackagerExePath
634+
.\AzureGalleryPackager.exe package -m $manifestPath -o $workdir
635+
cd $currentPath
636+
637+
#cleanup
638+
Remove-Item $extractedGalleryItemPath -Recurse -Force
639+
Remove-Item "$workdir\Azure Stack Marketplace Item Generator and Sample" -Recurse -Force
640+
Remove-Item "$workdir\CustomizedVMGalleryItem.zip"
641+
Remove-Item "$workdir\MarketplaceItem.zip"
642+
$azpkg = '{0}\{1}' -f $workdir, $galleryItemName
643+
return Get-Item -LiteralPath $azpkg
644+
}
645+

0 commit comments

Comments
 (0)