diff --git a/src/containerapp-preview/HISTORY.rst b/src/containerapp-preview/HISTORY.rst index feb2781d99b..00c5aa4cc3d 100644 --- a/src/containerapp-preview/HISTORY.rst +++ b/src/containerapp-preview/HISTORY.rst @@ -5,7 +5,7 @@ Release History upcoming ++++++ * containerapp-preview requires the version of containerapp extension >= 0.3.36 - +* containerapp-preview auto install containerapp extension if not exist 1.0.0b1 ++++++ diff --git a/src/containerapp-preview/azext_containerapp_preview/__init__.py b/src/containerapp-preview/azext_containerapp_preview/__init__.py index c3da75d3d51..88d2075256e 100644 --- a/src/containerapp-preview/azext_containerapp_preview/__init__.py +++ b/src/containerapp-preview/azext_containerapp_preview/__init__.py @@ -6,7 +6,7 @@ from azure.cli.core import AzCommandsLoader from azext_containerapp_preview._help import helps # pylint: disable=unused-import -from azext_containerapp_preview._utils import (_get_azext_containerapp_module) +from azext_containerapp_preview._utils import (_get_azext_containerapp_module, auto_install_containerapp_extension_if_not_exist) class ContainerappPreviewCommandsLoader(AzCommandsLoader): @@ -18,6 +18,7 @@ def __init__(self, cli_ctx=None): client_factory=None) super(ContainerappPreviewCommandsLoader, self).__init__(cli_ctx=cli_ctx, custom_command_type=containerapp_preview_custom) + auto_install_containerapp_extension_if_not_exist(self) def load_command_table(self, args): from azext_containerapp_preview.commands import load_command_table diff --git a/src/containerapp-preview/azext_containerapp_preview/_utils.py b/src/containerapp-preview/azext_containerapp_preview/_utils.py index f99953bc1ce..32f00133d8d 100644 --- a/src/containerapp-preview/azext_containerapp_preview/_utils.py +++ b/src/containerapp-preview/azext_containerapp_preview/_utils.py @@ -4,7 +4,7 @@ # -------------------------------------------------------------------------------------------- # pylint: disable=line-too-long -from azure.cli.core.azclierror import NoTTYError, ValidationError +from azure.cli.core.azclierror import ValidationError from knack.util import CLIError from knack.log import get_logger @@ -38,6 +38,28 @@ def _get_azext_containerapp_module(module_name): # Import the extension module from importlib import import_module azext_custom = import_module(module_name) + + # need to reload preview's _help, because the containerapp's _help will overwrite the preview's _help after importing the containerapp module. + from azext_containerapp_preview import _help + from importlib import reload + reload(_help) + return azext_custom except ImportError as ie: raise CLIError(ie) from ie + + +def auto_install_containerapp_extension_if_not_exist(cmd): + from azure.cli.core.extension import extension_exists + + if not extension_exists(GA_CONTAINERAPP_EXTENSION_NAME): + _install_containerapp_extension(cmd, GA_CONTAINERAPP_EXTENSION_NAME) + + +def _install_containerapp_extension(cmd, extension_name, upgrade=False): + try: + from azure.cli.core.extension import operations + operations.add_extension(cmd=cmd, extension_name=extension_name, upgrade=upgrade) + except Exception: # nopa pylint: disable=broad-except + return False + return True