azure-cli icon indicating copy to clipboard operation
azure-cli copied to clipboard

Exposing the output of `az --version` in JSON and other parsable formats.

Open nascarsayan opened this issue 2 years ago • 8 comments

Related command

az --version

Is your feature request related to a problem? Please describe.

az --version outputs several valuable information apart from the current versions of installed azure-cli modules and extensions. This includes:

  1. Which code CLI modules are not up-to-date (azure-cli , azure-cli-core, azure-cli-telemetry).
  2. Path to the underlying python used by az. The mode of installation of this python.
  3. Path to the extensions directory.

All this information is not available in a code-parsable format. We currently need to do string parsing which can be flaky.

Describe the solution you'd like

az --version -o json --query '<> can be used to query a specific property of the az installation. We can decide on the schema of the exposed property.

Describe alternatives you've considered

Currently, I need to parse the string output, using regex, etc. to get the python installation location, and also to check if upgrade is available.

Additional context

nascarsayan avatar Sep 25 '23 02:09 nascarsayan

Thank you for opening this issue, we will look into it.

yonzhan avatar Sep 25 '23 02:09 yonzhan

We already have az version for similar purpose (https://github.com/Azure/azure-cli/pull/11680, https://github.com/Azure/azure-cli/pull/13001).

> az version
{
  "azure-cli": "2.51.0",
  "azure-cli-core": "2.51.0",
  "azure-cli-telemetry": "1.1.0",
  "extensions": {
    "next": "0.1.3",
    "ssh": "1.1.6",
    "storage-preview": "0.8.3"
  }
}

To better help us understand your needs, could you let us know:

  • Which properties do you want az version to include?
  • Do you have a proposal or recommendation about how they should look like?

jiasli avatar Sep 25 '23 06:09 jiasli

Yes, I'm aware of az version command. I'm fine with either of the following:

  • adding the support for formatted output (like json) to az --version
  • adding the support of displaying all the extra info through az version itself.

In case it's easier to change the az version command, something like this could be the output:

$ az version --full

{
  "azure-cli": "2.51.0",
  "azure-cli-core": "2.51.0",
  "azure-cli-telemetry": "1.1.0",
  "extensions": {
    "next": "0.1.3",
    "ssh": "1.1.6",
    "storage-preview": "0.8.3"
  },
  "source": {
    "python": {
      "location": "C:\\Program Files\\Microsoft SDKs\\Azure\\CLI2\\python.exe",
      "version": "Python (Windows) 3.10.10 (tags/v3.10.10:aad5f6a, Feb  7 2023, 17:20:36) [MSC v.1929 64 bit (AMD64)]"
    },
    "extensions": {
      "location": "C:\\Users\\contosouser\\.azure\\cliextensions",
    },
    "updates": {
      "azure-cli": "2.52.0",
      "azure-cli-core": "2.52.0",
      "azure-cli-telemetry": "1.1.0"
    }
  }
}

nascarsayan avatar Sep 25 '23 12:09 nascarsayan

Thank you @nascarsayan for the information. Could you also share the use case of these properties? Then we will evaluate with our PMs whether we want to add them.

jiasli avatar Sep 26 '23 02:09 jiasli

Sure!

  • python location : We need to evaluate if the python is 32-bit or 64-bit MSI. az arcappliance vmware command requires 64-bit python, so our onboarding script has to extract the python location of the azure-cli, and run:
    $azVersion = az --version *>&1;
    $azVersionLines = $azVersion -split "`n"
    $pyLoc = $azVersionLines | Where-Object { $_ -match "^Python location" }
    $pythonExe = $pyLoc -replace "^Python location '(.+?)'$", '$1'
    $arch = & $pythonExe -c "import struct; print(struct.calcsize('P') * 8)";
    # assert($arch -ge 64);
    
    If the python is not 64-bit, we need to download the 64-bit MSI, and install it.
  • updates: Since az upgrade does not currently provide a non-interactive way of upgrading the azure CLI installed as MSI, we need to detect if updates for azure-cli core packages are available. If upgrade is available, we download the latest MSI and install it on top of the current az.

For our usecase, we don't require the folder location of extensions, and the python version (since 64-bit is hard to parse from the text output). However, we could add these details for completeness. I am flexible on the new schema, as long as the information can be extracted.

nascarsayan avatar Sep 26 '23 03:09 nascarsayan

@nascarsayan, while it is true you can detect if the python.exe is 32-bit or 64-bit with struct.calcsize:

> & "C:\Program Files\Microsoft SDKs\Azure\CLI2\python.exe" -c "import struct; print(struct.calcsize('P') * 8)"
64

(I guess you find this approach from https://stackoverflow.com/questions/1405913/how-do-i-determine-if-my-python-shell-is-executing-in-32bit-or-64bit), it actually doesn't need to be that complicated.

The 64-bit MSI is installed under C:\Program Files (ProgramFiles64Folder):

https://github.com/Azure/azure-cli/blob/aeb94ed12a46acdd14e7c4e2633908e149f4a1bc/build_scripts/windows/Product.wxs#L16

and 32-bit MSI is installed under C:\Program Files (x86) (ProgramFilesFolder):

https://github.com/Azure/azure-cli/blob/aeb94ed12a46acdd14e7c4e2633908e149f4a1bc/build_scripts/windows/Product.wxs#L26

So you may simply get the installation location of Azure CLI with:

> (Get-Command az).Source
C:\Program Files\Microsoft SDKs\Azure\CLI2\wbin\az.cmd

then detect 64/32-bit based on the installation path.

jiasli avatar Sep 26 '23 09:09 jiasli

az upgrade does not currently provide a non-interactive way of upgrading the azure CLI installed as MSI

az upgrade is still in preview and is known to have some problems. I noticed you have created https://github.com/Azure/azure-cli/issues/27456 for this topic and I commented at https://github.com/Azure/azure-cli/issues/27456#issuecomment-1735221981. The fact is that it may never be possible to do so.

The new-version-detection logic of az --version and az upgrade is also very primitive - it checks the main branch of the Azure CLI repo:

https://github.com/Azure/azure-cli/blob/b6ebd6fac6342c1112c2bb03927509d39263cdee/src/azure-cli-core/azure/cli/core/util.py#L295-L312

We have deprecated some packages for old operating systems, such as Ubuntu 16.04 DEB, CentOS 7 RPM, but because of this single-source check, az --version and az upgrade still show there is a new version available on these operating systems while there is actually not.

The initial design of az version was to make it as slim as possible, without any network request. We can consider adding an argument such as --include-latest-versions for adding latest versions to the output JSON.

jiasli avatar Sep 26 '23 10:09 jiasli

Hi, we're sending this friendly reminder because we haven't heard back from you in a while. We need more information about this issue to help address it. Please be sure to give us your input within the next 7 days. If we don't hear back from you within 14 days of this comment the issue will be automatically closed. Thank you!