Usability issues with long running cmdlets
Cmdlet(s)
New-AzureRmRedisCache
New-AzureRmKeyVault
Add-AzureKeyVaultCertificate
New-AzureRmWebApp
Publish-AzureWebsiteProject
PowerShell Version
5.1.14393.103
Module Version
Latest (2.1.0 - 3.1.0)
OS Version
10.0.14393.103
Description
Long running cmdlets are painful to work with, there's no consistency when it comes to their return types, behavior and so on.
Highlighted cmdlets are impossible to use in automation scripts without multiple hacks (Start-Sleep or polling on random properties different for each cmdlet)
Examples
New-AzureRmRedisCache
Returns immediately, obviously nothing is created at that point, in order to wait until this completes, cmdlet user has to poll on ProvisioningState like this:
while (($redis = Get-AzureRmRedisCache -ResourceGroupName $resourceGroupName -Name $redisName).ProvisioningState -ne "Succeeded")
{
Write-Host "'$($redis.HostName)' status is '$($redis.ProvisioningState)', retrying in 16s"
Start-Sleep -Seconds 16
}
No running as job, no flag to wait, everybody has to write a code like this
New-AzureRmKeyVault
Returns immediately, but the DNS name is not registered with Azure DNS service so in case you actually need to use said KeyVault in your script - it will never work. Again, we have a code like this:
Write-Host "Waiting until '$hostname' becomes available"
# AzDNS SLA for propagation to all authoritative DNS resolvers is 15 seconds at the 99.9th percentile
# We don't attempt to resolve the hostname sooner because failed DNS resolutions are cached for up to a minute
Start-Sleep -Seconds 15
while ((Resolve-DnsName -Name $hostname -DnsOnly -ErrorAction SilentlyContinue) -eq $null)
{
Write-Host "'$hostname' is not available, retrying in 4s"
Start-Sleep -Seconds 4
}
Add-AzureKeyVaultCertificate
Returns straigh away and has to be polled on:
while ((Get-AzureKeyVaultCertificateOperation -VaultName $keyVaultName -Name $certName).Status -ne "completed")
{
Write-Host "Certificate '$certName' is not ready, retrying in 4s"
Start-Sleep -Seconds 4
}
Now some people may have noticed that this time we're waiting for magic string "completed", unlike "Succeeded" with Redis.
New-AzureRmWebApp
Same as KeyVault, returns without registering DNS name, so requires DNS name polling
Publish-AzureWebsiteProject
Is worse than others actually, not only it requires you to poll on DNS name (if you create web app and immediately try to deploy), but also can fail saying WinRM listener was not ready. Not even possible to poll on, since nothing to poll on, so we have a code like this:
while ((Publish-AzureWebsiteProject -Name $name -Package $webDeployPackage -ErrorAction SilentlyContinue) -eq $null)
{
Write-Host "Azure Web App '$name' is not ready to accept WebDeploy requests, retrying in 4s"
Start-Sleep -Seconds 4
}
Now, this is not even a full list, these are just examples - we, developers and consumers of PS cmdlets have to write hacky and inconsitent polling just in order to use Azure PS Cmdlets
We encountered these issues since we built end-to-end environment deployment script which executes cmdlets fast and without any manual intervention, but I believe this is essential use case which has to be supported
@TarasRoshko I notice that all the cmdlets in the list are cmdlets that return before the action is completed. In your opinion, is it better to have cmdlets that do not return until the job is completed (and can be run as a job), better to return and have a standard polling mechanism, or both?
For example, we could have all such cmdlets implement the AsJob parameter as a consistent solution.
@akurmi @sumedhbarde-msft @SiddharthChatrolaMS assignign you guys to address the issue for your individual cmdlets.
Assigning us to update the standards accordingly
@markcowl exactly
Ideally I would suggest to unify all the return types so they look like a Promise
But unlike C# Task, It would be essential to support extra properties defined by cmdlets themselves (in addition to core promise properties)
As an example: we provision Redis but no other resources depend on it per se (can be deployed in parallel), we only await it at the very end of the script, but right after we kick off async Redis provisioning, we need to get PrimaryKey so it can be used by other to-be-deployed resources (we create Redis conn string and push it to a KeyVault)
in pseudo code:
$redisTask = New-AzureRmRedisCache
$resource1Task = New-SomeResource
$resource1Task.Wait() # only if resource2 depends on it, example assumes it does
$resource2Task = New-SomeOtherResource -Key $redisTask.Properties.PrimaryKey
$resource2Task.Wait()
$redisTask.Wait() # await resources being provisioned in background (just Redis in this example)
So basically I would suggest returning Promises from all long running cmdlets, but allow these promises to contain extra cmdlet-defined properties which could be useful to know without waiting for the resource to complete provisioning
Does this make sense?
Hello! How is this bug going? It is really causing us pain in deployment, it is very messy and we are having the exact same issue of OP @TarasRoshko .
We are adding so much hacks to go past this. Nothing is standard.
For example our logging:
[deploy.ps1:116 I] Creating KeyVault 'foobarkvpersonal'
[deploy.ps1:598 I] Waiting until 'foobarkvpersonal.vault.azure.net' becomes available
[deploy.ps1:300 I] Creating WinRM cert
[deploy.ps1:356 I] Certificate 'winrm-cert' is not ready, retrying in 4s
[deploy.ps1:356 I] Certificate 'winrm-cert' is not ready, retrying in 4s
[deploy.ps1:303 I] Creating Credential Service cert
[deploy.ps1:356 I] Certificate 'credential-service-cert' is not ready, retrying in 4s
[deploy.ps1:356 I] Certificate 'credential-service-cert' is not ready, retrying in 4s
[deploy.ps1:356 I] Certificate 'credential-service-cert' is not ready, retrying in 4s
[deploy.ps1:560 I] Creating Azure Web App 'foobarcontrollerpersonal'
[deploy.ps1:598 I] Waiting until 'foobarcontrollerpersonal.azurewebsites.net' becomes available
[deploy.ps1:560 I] Creating Azure Web App 'foobarservicepersonal'
[deploy.ps1:598 I] Waiting until 'foobarservicepersonal.azurewebsites.net' becomes available
And stuff like turning off a VM
[create-image.ps1:110 I] Waiting for VM 'foovm' to shut down
[create-image.ps1:199 I] VM 'foovm' is still running (current power state is 'running'); sleeping for 4s
[create-image.ps1:199 I] VM 'foovm' is still running (current power state is 'running'); sleeping for 4s
[create-image.ps1:199 I] VM 'foovm' is still running (current power state is 'running'); sleeping for 4s
[create-image.ps1:199 I] VM 'foovm' is still running (current power state is 'running'); sleeping for 4s
[create-image.ps1:199 I] VM 'foovm' is still running (current power state is 'running'); sleeping for 4s
[create-image.ps1:199 I] VM 'foovm' is still running (current power state is 'running'); sleeping for 4s
[create-image.ps1:199 I] VM 'foovm' is still running (current power state is 'running'); sleeping for 4s
[create-image.ps1:199 I] VM 'foovm' is still running (current power state is 'running'); sleeping for 4s
[create-image.ps1:199 I] VM 'foovm' is still running (current power state is 'running'); sleeping for 4s
[create-image.ps1:199 I] VM 'foovm' is still running (current power state is 'running'); sleeping for 4s
I'm porting some PowerShell code that I've used to start and stop AWS EC2 instances to AzureRM. I was surprised to find that the Start-AzureRmVM and Stop-AzureRmVM cmdlets were blocking, which is different behavior than the corresponding EC2 cmdlets, Start-EC2Instance and Stop-EC2Instance, which return an object immediately. To workaround this problem I've had to resort to wrapping the AzureRM cmdlets in scriptblocks that I call with the Start-Job cmdlet. This leads to some fairly complicated PowerShell code (IMO). I recently expanded my script (at this point a full blown PowerShell scripting module) to handle the blocking nature of these cmdlets: Suspend-AzureRmSqlDatabase Resume-AzureRmSqlDatabase Suspend-AzureRmAnalysisServicesServer Resume-AzureRmAnalysisServicesServer Update-AzureRmVM Set-AzureRmSqlDatabase Set-AzureRmAnalysisServicesServer Set-AzureRmResource
Coming from AWS PowerShell, I had the same cognitive dissonance as @SiloReed. The lack of consistency is one thing but equally difficult for me to adjust to is the fact that return objects are both irregular (and poorly documented). In AWSPowerShell (and AWSPowerShell.NetCore) detailed error info is returned in $AWSHistory, returned objects are strictly consistent and (IMHO) brilliantly documented.
My poster child for all the complex of issues here is New-AzureRmSqlServer, which appears to run one way when launched in a local console but never works reliably in Azure Automation runbooks.
I know this team has a big task ahead of it. But it still feels with each release as if the larger requirements expressed in this issue are secondary to just getting it out the door as opposed to taking the time to build cmdlets that, over time, are consistent and behave in precisely the same way.
@markcowl any update? It has been over a year since we reported this bug, how is the status to enforce consistency in the API
@mohamedmansour @TarasRoshko Today we will be releasing Compute cmdlets that use 'AsJob' as the paradigm for tracking the progress for long-running operations. This will flow out into additional cmdlets in the coming weeks.