Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ImageFactory scripts - dont create custom image in the factory lab #255

Merged
merged 1 commit into from
Aug 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions Scripts/ImageFactory/Scripts/CleanUpFactory.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
param
(
[Parameter(Mandatory=$true, HelpMessage="The name of the DevTest Lab resource group")]
[string] $ResourceGroupName,

[Parameter(Mandatory=$true, HelpMessage="The name of the DevTest Lab to clean up")]
[string] $DevTestLabName
)
Expand All @@ -18,7 +15,7 @@ $deleteVmBlock = {
Import-Module $modulePath
LoadProfile
Write-Output "##[section]Deleting VM: $vmName"
Remove-AzureRmResource -ResourceId $resourceId -ApiVersion 2017-04-26-preview -Force
Remove-AzureRmResource -ResourceId $resourceId -ApiVersion 2016-05-15 -Force
Write-Output "##[section]Completed deleting $vmName"
}

Expand Down Expand Up @@ -57,7 +54,8 @@ foreach ($currentVm in $allVms){
}

# Find any custom images that failed to provision and delete those
$bustedLabCustomImages = Get-AzureRmResource -ResourceName $DevTestLabName -ResourceGroupName $ResourceGroupName -ResourceType 'Microsoft.DevTestLab/labs/customImages' -ApiVersion '2017-04-26-preview' | Where-Object {($_.Properties.ProvisioningState -ne "Succeeded") -and ($_.Properties.ProvisioningState -ne "Creating")}
$ResourceGroupName = (Find-AzureRmResource -ResourceType 'Microsoft.DevTestLab/labs' | Where-Object { $_.Name -eq $DevTestLabName}).ResourceGroupName
$bustedLabCustomImages = Get-AzureRmResource -ResourceName $DevTestLabName -ResourceGroupName $ResourceGroupName -ResourceType 'Microsoft.DevTestLab/labs/customImages' -ApiVersion '2016-05-15' | Where-Object {($_.Properties.ProvisioningState -ne "Succeeded") -and ($_.Properties.ProvisioningState -ne "Creating")}

# Delete the custom images we found in the search above
foreach ($imageToDelete in $bustedLabCustomImages) {
Expand All @@ -66,15 +64,11 @@ foreach ($imageToDelete in $bustedLabCustomImages) {

if($jobs.Count -ne 0)
{
try{
Write-Output "Waiting for VM Delete jobs to complete"
foreach ($job in $jobs){
Receive-Job $job -Wait | Write-Output
}
}
finally{
Remove-Job -Job $jobs
Write-Output "Waiting for VM Delete jobs to complete"
foreach ($job in $jobs){
Receive-Job $job -Wait | Write-Output
}
Remove-Job -Job $jobs
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion Scripts/ImageFactory/Scripts/CreateImageFromVHD.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
},
"resources": [
{
"apiVersion": "2017-04-26-preview",
"apiVersion": "2016-05-15",
"name": "[variables('resourceName')]",
"type": "Microsoft.DevTestLab/labs/customimages",
"tags": {
Expand Down
214 changes: 87 additions & 127 deletions Scripts/ImageFactory/Scripts/DistributeImages.ps1

Large diffs are not rendered by default.

125 changes: 107 additions & 18 deletions Scripts/ImageFactory/Scripts/DistributionHelpers.psm1
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
function SelectSubscription($subId){
# switch to another subscription assuming it's not the one we're already on
if((Get-AzureRmContext).Subscription.SubscriptionId -ne $subId){
if((Get-AzureRmContext).Subscription.Id -ne $subId){
Write-Output "Switching to subscription $subId"
Select-AzureRmSubscription -SubscriptionId $subId | Out-Null
Set-AzureRmContext -SubscriptionId $subId | Out-Null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is affected by this bug: Azure/azure-powershell#3954

A workaround is to remove the subscription check in SelectSubscription.

}
}

Expand All @@ -20,21 +20,14 @@ function getTagValue($resource, $tagName){
$result
}

function ShouldCopyImageToLab ($lab, $image)
function ShouldCopyImageToLab ($lab, $imagePathValue)
{
$retval = $false

$imagePathTag = getTagValue $image 'ImagePath'
if(!$imagePathTag) {
#this image does not have the ImagePath tag. Dont copy it
$retval = $false
}
else{
foreach ($labImagePath in $lab.ImagePaths) {
if ($imagePathTag.StartsWith($labImagePath.Replace("/", "\"))) {
$retVal = $true;
break;
}
foreach ($labImagePath in $lab.ImagePaths) {
if ($imagePathValue.StartsWith($labImagePath.Replace("/", "\"))) {
$retVal = $true;
break;
}
}
$retval
Expand Down Expand Up @@ -78,17 +71,113 @@ function SaveProfile {
Remove-Item $profilePath
}

Save-AzureRmProfile -Path $profilePath
Save-AzureRmContext -Path $profilePath
}

function LoadProfile {
$scriptFolder = Split-Path $Script:MyInvocation.MyCommand.Path
Select-AzureRmProfile -Path (Join-Path $scriptFolder "profile.json") | Out-Null
Import-AzureRmContext -Path (Join-Path $scriptFolder "profile.json") | Out-Null
}

function deleteImage ($resourceGroupName, $resourceName)
{
{
Write-Output "##[section]Deleting Image: $resourceName"
Remove-AzureRmResource -ResourceName $resourceName -ResourceGroupName $resourceGroupName -ResourceType 'Microsoft.DevTestLab/labs/customImages' -ApiVersion '2017-04-26-preview' -Force
Remove-AzureRmResource -ResourceName $resourceName -ResourceGroupName $resourceGroupName -ResourceType 'Microsoft.DevTestLab/labs/customImages' -ApiVersion '2016-05-15' -Force
Write-Output "##[section]Completed deleting $resourceName"
}

function GetImageName ($imagePathValue)
{
$splitImagePath = $imagePathValue.Split('\')
if($splitImagePath.Length -eq 1){
#the image is directly in the GoldenImages folder. Just use the file name as the image name.
$imagename = $splitImagePath[0]
}
else {
#this image is in a folder within GoldenImages. Name the image <FolderName> <fileName> with <FolderName> set to the name of the folder that contains the image
$segmentCount = $splitImagePath.Length
$imagename = $splitImagePath[$segmentCount - 2] + "_" + $splitImagePath[$segmentCount - 1]
}

#clean up some special characters in the image name and stamp it with todays date
$imagename = $imagename.Replace(".json", "").Replace(".", "_").Replace(" ", "-")
$imagename = $imagename + "-" + (Get-Date -Format 'MMM-d-yyyy')
return $imagename
}

function GetLabStorageInfo ($lab)
{
$labRgName= $lab.ResourceGroupName
$sourceLab = Get-AzureRmResource -ResourceName $lab.Name -ResourceGroupName $labRgName -ResourceType 'Microsoft.DevTestLab/labs'
$storageAcctValue = $sourceLab.Properties.artifactsStorageAccount
$storageAcctName = $storageAcctValue.Substring($storageAcctValue.LastIndexOf('/') + 1)

$storageAcct = (Get-AzureRMStorageAccountKey -StorageAccountName $storageAcctName -ResourceGroupName $labRgName)
# Azure Powershell version 1.3.2 or below - https://msdn.microsoft.com/en-us/library/mt607145.aspx
$storageAcctKey = $storageAcct.Key1
if ($storageAcctKey -eq $null) {
# Azure Powershell version 1.4 or greater:
$storageAcctKey = $storageAcct.Value[0]
}
$result = @{
resourceGroupName = $labRgName
storageAcctName = $storageAcctName
storageAcctKey = $storageAcctKey
}
return $result
}

function EnsureRootContainerExists ($labStorageInfo)
{
$storageContext = New-AzureStorageContext -StorageAccountName $labStorageInfo.storageAcctName -StorageAccountKey $labStorageInfo.storageAcctKey
$rootContainerName = 'imagefactoryvhds'
$rootContainer = Get-AzureStorageContainer -Context $storageContext -Name $rootContainerName -ErrorAction Ignore
if($rootContainer -eq $null)
{
Write-Output "Creating the $rootContainerName container in the target storage account"
$rootContainer = New-AzureStorageContainer -Context $storageContext -Name $rootContainerName
}
}

function GetImageInfosForLab ($DevTestLabName)
{
$lab = Find-AzureRmResource -ResourceType 'Microsoft.DevTestLab/labs' | Where-Object { $_.Name -eq $DevTestLabName}
$labRgName= $lab.ResourceGroupName
$sourceLab = Get-AzureRmResource -ResourceName $DevTestLabName -ResourceGroupName $labRgName -ResourceType 'Microsoft.DevTestLab/labs'
$storageAcctValue = $sourceLab.Properties.artifactsStorageAccount
$storageAcctName = $storageAcctValue.Substring($storageAcctValue.LastIndexOf('/') + 1)

$storageAcct = (Get-AzureRMStorageAccountKey -StorageAccountName $storageAcctName -ResourceGroupName $labRgName)
# Azure Powershell version 1.3.2 or below - https://msdn.microsoft.com/en-us/library/mt607145.aspx
$storageAcctKey = $storageAcct.Key1
if ($storageAcctKey -eq $null) {
# Azure Powershell version 1.4 or greater:
$storageAcctKey = $storageAcct.Value[0]
}

$storageContext = New-AzureStorageContext -StorageAccountName $storageAcctName -StorageAccountKey $storageAcctKey

$rootContainerName = 'imagefactoryvhds'

$jsonBlobs = Get-AzureStorageBlob -Context $storageContext -Container $rootContainerName -Blob '*json'

Write-Host "Downloading $($jsonBlobs.Length) json files from storage account"
$downloadFolder = Join-Path $env:TEMP 'ImageFactoryDownloads'
if(Test-Path -Path $downloadFolder)
{
Remove-Item $downloadFolder -Recurse | Out-Null
}
New-Item -Path $downloadFolder -ItemType Directory | Out-Null
$jsonBlobs | Get-AzureStorageBlobContent -Destination $downloadFolder | Out-Null

$sourceImageInfos = @()

$downloadedFileNames = Get-ChildItem -Path $downloadFolder
foreach($file in $downloadedFileNames)
{
$imageObj = (gc $file.FullName -Raw) | ConvertFrom-Json
$sourceImageInfos += $imageObj
}

return $sourceImageInfos
}
47 changes: 25 additions & 22 deletions Scripts/ImageFactory/Scripts/MakeGoldenImageVMs.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
[Parameter(Mandatory=$true, HelpMessage="The location of the factory configuration files")]
[string] $ConfigurationLocation,

[Parameter(Mandatory=$true, HelpMessage="The name of the resource group")]
[string] $ResourceGroupName,

[Parameter(Mandatory=$true, HelpMessage="The name of the lab")]
[string] $DevTestLabName,

Expand All @@ -19,7 +16,10 @@
[int] $StandardTimeoutMinutes,

[Parameter(Mandatory=$true, HelpMessage="The name of the lab")]
[string] $vmSize
[string] $vmSize,

[Parameter(HelpMessage="Specifies whether or not to sysprep the created VMs")]
[boolean] $includeSysprep = $true

)

Expand All @@ -39,10 +39,10 @@ function IsVirtualMachineReady ($vmName, $status)

if ($status.Count -gt 1) {
# We have both parameters (provisioning state + power state) - this is the default case
if (($status[0].Code -eq "ProvisioningState/succeeded") -and ($status[1].Code -eq "PowerState/running")) {
if (($status[0].Code -eq "ProvisioningState/succeeded") -and ($status[1].Code -eq "PowerState/deallocated")) {
$retval = $true
}
elseif (($status[1].Code -eq "ProvisioningState/succeeded") -and ($status[0].Code -eq "PowerState/running")) {
elseif (($status[1].Code -eq "ProvisioningState/succeeded") -and ($status[0].Code -eq "PowerState/deallocated")) {
$retval = $true
}
}
Expand Down Expand Up @@ -73,7 +73,14 @@ foreach ($file in $files)
$imagePath = $file.FullName.Substring($imageListLocation.Length + 1)

#determine a VM name for each file
$vmName = $file.BaseName.Replace("_", "").Replace(" ", "");
$vmName = $file.BaseName.Replace("_", "").Replace(" ", "").Replace(".", "")
$intName = 0
if ([System.Int32]::TryParse($vmName, [ref]$intName))
{
Write-Output "Adding prefix to vm named $vmName because it cannot be fully numeric"
$vmName = ('vm' + $vmName)
}

if($vmName.Length -gt 15){
$shortenedName = $vmName.Substring(0, 13)
Write-Output "VM name $vmName is too long. Shortening to $shortenedName"
Expand All @@ -92,31 +99,27 @@ foreach ($file in $files)
$usedVmNames += $vmName

Write-Output "Starting job to create a VM named $vmName for $imagePath"
$jobs += Start-Job -Name $file.Name -FilePath $makeVmScriptLocation -ArgumentList $modulePath, $file.FullName, $ResourceGroupName, $DevTestLabName, $vmName, $imagePath, $machineUserName, $machinePassword, $vmSize
$jobs += Start-Job -Name $file.Name -FilePath $makeVmScriptLocation -ArgumentList $modulePath, $file.FullName, $DevTestLabName, $vmName, $imagePath, $machineUserName, $machinePassword, $vmSize, $includeSysprep
}

try{
$jobCount = $jobs.Count
Write-Output "Waiting for $jobCount VM creation jobs to complete"
foreach ($job in $jobs){
$jobOutput = Receive-Job $job -Wait
Write-Output $jobOutput
$createdVMName = $jobOutput[$jobOutput.Length - 1]
if($createdVMName){
$createdVms.Add($createdVMName)
}
$jobCount = $jobs.Count
Write-Output "Waiting for $jobCount VM creation jobs to complete"
foreach ($job in $jobs){
$jobOutput = Receive-Job $job -Wait
Write-Output $jobOutput
$createdVMName = $jobOutput[$jobOutput.Length - 1]
if($createdVMName){
$createdVms.Add($createdVMName)
}
}
finally{
Remove-Job -Job $jobs
}
Remove-Job -Job $jobs

#get machines that show up in the VM blade so we can apply the GoldenImage Tag
$allVms = Find-AzureRmResource -ResourceType "Microsoft.Compute/virtualMachines"

for ($index = 0; $index -lt $createdVms.Count; $index++){
$currentVmName = $createdVms[$index]
$currentVmValue = $allVms | Where-Object {$_.Name -eq $currentVmName -and $_.ResourceGroupName.StartsWith($DevTestLabName)}
$currentVmValue = $allVms | Where-Object {$_.Name -eq $currentVmName -and $_.ResourceGroupName.StartsWith($DevTestLabName, "CurrentCultureIgnoreCase")}
if(!$currentVmValue){
Write-Error "##[error]$currentVmName was not created successfully. It does not appear in the VM blade"
continue;
Expand Down
Loading