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

Added try catch block added for azlogin #10556

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

RaviAkshintala
Copy link
Contributor

This PR adds a try-catch block for the azlogin function. * If an account is already logged in, it prints a warning message "Already logged in...". * If the login fails, it prints an error message "Login failed...". This change will help to handle the login process more gracefully and provide better user feedback.

Related issue:

#10236

Check list

  • Related issue / work item is attached
  • Tests are written (if applicable)
  • Documentation is updated (if applicable)
  • Changes are tested and related VM images are successfully generated

@@ -240,13 +240,19 @@ Function GenerateResourcesAndImage {

try {
# Login to Azure subscription
try {
$azAccount = az account show -o none
Copy link
Contributor

Choose a reason for hiding this comment

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

Where azAccount variable is used? Why do we need it?
az account show -o none always returns nothing because -o none means --output none. What do we expect to set to azAccount variable?

@@ -240,13 +240,19 @@ Function GenerateResourcesAndImage {

try {
# Login to Azure subscription
try {
$azAccount = az account show -o none
Write-Warning "Already logged in..."
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is it warning? It looks like expected behaviour for me. Should it be Write-Verbose?

@@ -240,13 +240,19 @@ Function GenerateResourcesAndImage {

try {
# Login to Azure subscription
try {
Copy link
Contributor

Choose a reason for hiding this comment

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

This script will never enter catch block because az account show -o none command will never throw exception.
Please check other places in this script to see how to handle command output correctly.

Copy link

@feliasson feliasson Sep 12, 2024

Choose a reason for hiding this comment

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

@maxim-lobanov this is not correct, the az account show -o none gives error if not logged in.

Easy to reproduce locally:

az logout
az account show -o none

No subscription found. Run 'az account set' to select a subscription.

See my comment here why it would never get into the catch block

image

Edit: Maybe I misunderstood, if you meant the command is not causing exception to enter the catch block but you are aware it still produces error in the outputs if not logged in.

try {
$azAccount = az account show -o none
Write-Warning "Already logged in..."
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please follow PowerShell styleguide as it is recommended in this repo readme? See https://github.com/actions/runner-images/blob/main/CONTRIBUTING.md for more details on correct style.

  1. } and catch should be on the same line
  2. content of catch block should have different indentation from catch block itself.

@feliasson
Copy link

feliasson commented Sep 12, 2024

I just wanted to add here that the current approach here for this try/catch block will not work, I know this because I have tested it.

When not logged in you will get the following:

ERROR: Please run 'az login' to setup account.
ERROR: The subscription of '<subscription>' doesn't exist in cloud 'AzureCloud'.

It happens because the Powershell try can not catch the error from an external program, so it does not get into the catch block and thereafter tries to set the subscription after the catch block (hence the 2nd error).

In #10602 I have solved this using the following way:

try {
    az account show -o none 2>$null || Write-Error $_
    Write-Verbose "Already logged in..."
}

The 2>$null writes the error to $null if not logged in to silence it, the pipeline chain operator || runs the next command because LASTEXITCODE is not 0, so the Write-Error $_ is there just to cause a Powershell error to get into the catch block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants