Public/Copy-FMTImages.ps1
function Copy-FMTImages { param( [parameter()] [string] $targetResourceGroup, [parameter()] [string] $storageAccount, [parameter()] [string] $storageContainer = 'silk', [parameter()] [string] $cnodeVersion, [parameter()] [string] $dnodeVersion, [parameter(Mandatory)] [string] $sasToken, [parameter()] [string] $souceStorageAccount = "silkimages", [parameter()] [string] $Location, [parameter()] [switch] $grantStorageAccess ) # Check AZ context $session = New-FMTSession if (!$session) { $message = '-- stopping --' return $message } # version parse if (!$cnodeVersion -and !$dnodeVersion) { Write-Host -ForegroundColor yellow "No image versions specified, please set -cnodeVersion or -dnodeVersion (or both)" pause } if (!$targetResourceGroup) { $rglist = Get-AzResourceGroup $targetResourceGroup = Build-MenuFromArray -array $rglist -property resourcegroupname -message "Select the appropriate Resource Group" } # new or existing storage account and container - fix this $rg = Get-AzResourceGroup -Name $targetResourceGroup -ErrorAction SilentlyContinue if (!$rg) { Write-Host -ForegroundColor yellow "Resource group -- $targetResourceGroup -- Not found. Please check and try again." Pause } if (!$Location) { $Location = $rg.Location } if (!$storageAccount) { $storage_accountname = 'images' + ( -join ((48..57) + (65..90) + (97..122) | Get-Random -Count 10 | ForEach-Object {[char]$_})).ToLower() } else { $storage_accountname = $storageAccount } # if ($cnodeVersion) { $cImageName = "k2c-cnode-" + $cnodeVersion.Replace('.','-') $cImageFileName = $cImageName + ".vhd" $currentCImage = Get-AzImage -Name $cimageName -ResourceGroupName $targetResourceGroup -ErrorAction SilentlyContinue if ($currentCImage) { Write-Host -ForegroundColor yellow "Image -- $cImageFileName -- Already exists in resource group -- $targetResourceGroup." } } if ($dnodeVersion) { $dImageName = "azure-dnode-" + $dnodeVersion.Replace('.','-') $dImageFileName = $dImageName + ".vhd" $currentDImage = Get-AzImage -Name $dimageName -ResourceGroupName $targetResourceGroup -ErrorAction SilentlyContinue if ($currentDImage) { Write-Host -ForegroundColor yellow "Image -- $dImageFileName -- Already exists in resource group -- $targetResourceGroup." } } # Create target storage account if (!$storageAccount) { Write-Host -ForegroundColor yellow "Creating Storage account $storage_accountname" $sa = New-AzStorageAccount -Name $storage_accountname -ResourceGroupName $rg.ResourceGroupName -Location $Location -SkuName Standard_LRS -AllowBlobPublicAccess $false } else { $sa = Get-AzStorageAccount -ResourceGroupName $rg.ResourceGroupName -Name $storage_accountname -ErrorAction SilentlyContinue if (!$sa) { $sa = New-AzStorageAccount -Name $storage_accountname -ResourceGroupName $rg.ResourceGroupName -Location $Location -SkuName Standard_LRS -AllowBlobPublicAccess $false } } # Grant access to the storage account ACL (to be written if required) <# [IPAddress]$myIpAddress = Get-CloudShellIpAddress $addressPrefixString = $myIpAddress.IPAddressToString + '/32' #> # Create container if absent $sc = $sa | Get-AzStorageContainer -Name $StorageContainer -ErrorAction SilentlyContinue if (!$sc) { try { $sc = $sa | New-AzStorageContainer -Name $StorageContainer } catch { Write-Error 'Can not create or read container in this storage account, please check your permissions' } } # Generate storage contexts Write-Host -ForegroundColor yellow "Copying $cimageFileName and $dimageFileName to $storage_accountname" $sakeys = $sa | Get-AzStorageAccountKey $srcContext = New-AzStorageContext -StorageAccountName $souceStorageAccount -SasToken $sasToken $dstContext = New-AzStorageContext -StorageAccountName $sa.StorageAccountName -StorageAccountKey $sakeys[0].Value if ($cnodeVersion) { Start-AzStorageBlobCopy -DestContainer $StorageContainer -SrcBlob $cimageFileName -SrcContainer 'images' -DestContext $dstContext -Context $srcContext -DestBlob $cimageFileName } if ($dnodeVersion) { Start-AzStorageBlobCopy -DestContainer $StorageContainer -SrcBlob $dimageFileName -SrcContainer 'images' -DestContext $dstContext -Context $srcContext -DestBlob $dimageFileName } # copy between contexts if ($cnodeVersion) { Get-AzStorageBlobCopyState -Blob $cimageFileName -Container $StorageContainer -Context $dstContext -WaitForComplete } if ($dnodeVersion) { Get-AzStorageBlobCopyState -Blob $dimageFileName -Container $StorageContainer -Context $dstContext -WaitForComplete } if ($cnodeVersion) { # Create cnode Image $cimageURI = $sc.CloudBlobContainer.Uri.AbsoluteUri + '/' + $cimageFileName $cimageConfig = New-AzImageConfig -Location $Location $cimageConfig = Set-AzImageOsDisk -Image $cimageConfig -OsType Linux -OsState Generalized -BlobUri $cimageURI New-AzImage -ImageName $cimageName -ResourceGroupName $rg.ResourceGroupName -Image $cimageConfig } if ($dnodeVersion) { # Create dnode Image $dimageURI = $sc.CloudBlobContainer.Uri.AbsoluteUri + '/' + $dimageFileName $dimageConfig = New-AzImageConfig -Location $Location $dimageConfig = Set-AzImageOsDisk -Image $dimageConfig -OsType Linux -OsState Generalized -BlobUri $dimageURI New-AzImage -ImageName $dimageName -ResourceGroupName $rg.ResourceGroupName -Image $dimageConfig } } |