diff --git a/src/containerapp/azext_containerapp/.flake8 b/src/containerapp/azext_containerapp/.flake8 new file mode 100644 index 00000000000..777d0ca9ecd --- /dev/null +++ b/src/containerapp/azext_containerapp/.flake8 @@ -0,0 +1,4 @@ +[flake8] +ignore = + W503 # line break before binary operator, not compliant with PEP 8 + E203 # whitespace before ':', not compliant with PEP 8 \ No newline at end of file diff --git a/src/containerapp/azext_containerapp/_up_utils.py b/src/containerapp/azext_containerapp/_up_utils.py index 084399ed77a..44f6665158c 100644 --- a/src/containerapp/azext_containerapp/_up_utils.py +++ b/src/containerapp/azext_containerapp/_up_utils.py @@ -15,6 +15,9 @@ MutuallyExclusiveArgumentError) from azure.cli.core.commands.client_factory import get_subscription_id from azure.cli.command_modules.appservice._create_util import check_resource_group_exists +from azure.cli.command_modules.acr.custom import acr_show +from azure.cli.core.commands.client_factory import get_mgmt_service_client +from azure.mgmt.containerregistry import ContainerRegistryManagementClient from knack.log import get_logger from msrestazure.tools import parse_resource_id, is_valid_resource_id, resource_id @@ -382,16 +385,19 @@ def _get_env_and_group_from_log_analytics(cmd, resource_group_name, env:'Contain def _get_acr_from_image(cmd, app): if app.image is not None and "azurecr.io" in app.image: + app.registry_server = app.image.split('/')[0] # TODO what if this conflicts with registry_server param? + parsed = urlparse(app.image) + registry_name = (parsed.netloc if parsed.scheme else parsed.path).split('.')[0] if app.registry_user is None or app.registry_pass is None: logger.info('No credential was provided to access Azure Container Registry. Trying to look up...') - app.registry_server = app.image.split('/')[0] # TODO what if this conflicts with registry_server param? - parsed = urlparse(app.image) - registry_name = (parsed.netloc if parsed.scheme else parsed.path).split('.')[0] - try: - app.registry_user, app.registry_pass, registry_rg = _get_acr_cred(cmd.cli_ctx, registry_name) - app.acr = AzureContainerRegistry(registry_name, ResourceGroup(cmd, registry_rg, None, None)) - except Exception as ex: - raise RequiredArgumentMissingError('Failed to retrieve credentials for container registry. Please provide the registry username and password') from ex + try: + app.registry_user, app.registry_pass, registry_rg = _get_acr_cred(cmd.cli_ctx, registry_name) + app.acr = AzureContainerRegistry(registry_name, ResourceGroup(cmd, registry_rg, None, None)) + except Exception as ex: + raise RequiredArgumentMissingError('Failed to retrieve credentials for container registry. Please provide the registry username and password') from ex + else: + acr_rg = _get_acr_rg(app) + app.acr = AzureContainerRegistry(name=registry_name, resource_group=ResourceGroup(app.cmd, acr_rg, None, None)) def _get_registry_from_app(app): @@ -401,20 +407,27 @@ def _get_registry_from_app(app): app.registry_server = containerapp_def["properties"]["configuration"]["registries"][0]["server"] +def _get_acr_rg(app): + registry_name = app.registry_server[:app.registry_server.rindex(".azurecr.io")] + client = get_mgmt_service_client(app.cmd.cli_ctx, ContainerRegistryManagementClient).registries + return parse_resource_id(acr_show(app.cmd, client, registry_name).id)["resource_group"] + def _get_registry_details(cmd, app: 'ContainerApp'): registry_rg = None registry_name = None if app.registry_server: if "azurecr.io" not in app.registry_server: raise ValidationError("Cannot supply non-Azure registry when using --source.") + parsed = urlparse(app.registry_server) + registry_name = (parsed.netloc if parsed.scheme else parsed.path).split('.')[0] if app.registry_user is None or app.registry_pass is None: logger.info('No credential was provided to access Azure Container Registry. Trying to look up...') - parsed = urlparse(app.registry_server) - registry_name = (parsed.netloc if parsed.scheme else parsed.path).split('.')[0] try: app.registry_user, app.registry_pass, registry_rg = _get_acr_cred(cmd.cli_ctx, registry_name) except Exception as ex: raise RequiredArgumentMissingError('Failed to retrieve credentials for container registry. Please provide the registry username and password') from ex + else: + registry_rg = _get_acr_rg(app) else: registry_rg = app.resource_group.name user = get_profile_username() @@ -423,6 +436,7 @@ def _get_registry_details(cmd, app: 'ContainerApp'): registry_name = f"ca{registry_name}acr" # ACR names must start + end in a letter app.registry_server = registry_name + ".azurecr.io" app.should_create_acr = True + app.acr = AzureContainerRegistry(registry_name, ResourceGroup(cmd, registry_rg, None, None)) @@ -438,6 +452,7 @@ def _set_up_defaults(cmd, name, resource_group_name, logs_customer_id, location, # get ACR details from --image, if possible _get_acr_from_image(cmd, app) + def _create_github_action(app:'ContainerApp', env:'ContainerAppEnvironment', service_principal_client_id, service_principal_client_secret, service_principal_tenant_id, diff --git a/src/containerapp/azext_containerapp/custom.py b/src/containerapp/azext_containerapp/custom.py index 082e1b2a0c8..a3515ca44df 100644 --- a/src/containerapp/azext_containerapp/custom.py +++ b/src/containerapp/azext_containerapp/custom.py @@ -2007,7 +2007,7 @@ def containerapp_up(cmd, service_principal_tenant_id=None): from ._up_utils import (_validate_up_args, _reformat_image, _get_dockerfile_content, _get_ingress_and_target_port, ResourceGroup, ContainerAppEnvironment, ContainerApp, _get_registry_from_app, - _get_registry_details, _create_github_action, _set_up_defaults, up_output) + _get_registry_details, _create_github_action, _set_up_defaults, up_output, AzureContainerRegistry) dockerfile="Dockerfile" # for now the dockerfile name must be "Dockerfile" (until GH actions API is updated) @@ -2033,6 +2033,8 @@ def containerapp_up(cmd, env.create_if_needed(name) app.create_acr_if_needed() + + if source: app.run_acr_build(dockerfile, source)