diff --git a/sky/adaptors/azure.py b/sky/adaptors/azure.py index 44618a8f64f..8d1d66bf736 100644 --- a/sky/adaptors/azure.py +++ b/sky/adaptors/azure.py @@ -2,9 +2,13 @@ # pylint: disable=import-outside-toplevel import functools +import json +import subprocess import threading from sky.adaptors import common +from sky.utils import subprocess_utils +from sky.utils import ux_utils azure = common.LazyImport( 'azure', @@ -14,19 +18,40 @@ _session_creation_lock = threading.RLock() +# There is no programmatic way to get current account details anymore. +# https://github.com/Azure/azure-sdk-for-python/issues/21561 + + +def _get_account(): + result = subprocess_utils.run('az account show -o json', + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + + if result.returncode != 0: + error_message = result.stderr.decode() + with ux_utils.print_exception_no_traceback(): + raise RuntimeError( + 'Failed to execute az account show -o json command. ' + f'Error: {error_message}') + + try: + return json.loads(result.stdout.decode()) + except json.JSONDecodeError as e: + error_message = result.stderr.decode() + with ux_utils.print_exception_no_traceback(): + raise RuntimeError( + 'Failed to parse the output of az account show -o json ' + f'command. Error: {error_message}') from e + -@common.load_lazy_modules(modules=_LAZY_MODULES) def get_subscription_id() -> str: """Get the default subscription id.""" - from azure.common import credentials - return credentials.get_cli_profile().get_subscription_id() + return _get_account()['id'] -@common.load_lazy_modules(modules=_LAZY_MODULES) def get_current_account_user() -> str: """Get the default account user.""" - from azure.common import credentials - return credentials.get_cli_profile().get_current_account_user() + return _get_account()['user']['name'] @common.load_lazy_modules(modules=_LAZY_MODULES) diff --git a/sky/skylet/providers/azure/config.py b/sky/skylet/providers/azure/config.py index 0c1827a1141..a4f3fb37536 100644 --- a/sky/skylet/providers/azure/config.py +++ b/sky/skylet/providers/azure/config.py @@ -6,13 +6,13 @@ import time from typing import Any, Callable -from azure.common.credentials import get_cli_profile from azure.identity import AzureCliCredential from azure.mgmt.network import NetworkManagementClient from azure.mgmt.resource import ResourceManagementClient from azure.mgmt.resource.resources.models import DeploymentMode from sky.utils import common_utils +from sky.adaptors.azure import get_subscription_id UNIQUE_ID_LEN = 4 _WAIT_NSG_CREATION_NUM_TIMEOUT_SECONDS = 600 @@ -51,7 +51,7 @@ def _configure_resource_group(config): # https://docs.microsoft.com/en-us/azure/virtual-machines/windows/tutorial-availability-sets subscription_id = config["provider"].get("subscription_id") if subscription_id is None: - subscription_id = get_cli_profile().get_subscription_id() + subscription_id = get_subscription_id() # Increase the timeout to fix the Azure get-access-token (used by ray azure # node_provider) timeout issue. # Tracked in https://github.com/Azure/azure-cli/issues/20404#issuecomment-1249575110