From 77f4f088ec83f4ef4d48150059664f0b1fd909c8 Mon Sep 17 00:00:00 2001 From: Yuwei Zhou Date: Thu, 30 Dec 2021 15:58:29 +0800 Subject: [PATCH] [Spring-Cloud] Remove unused method --- src/spring-cloud/azext_spring_cloud/custom.py | 636 +----------------- 1 file changed, 3 insertions(+), 633 deletions(-) diff --git a/src/spring-cloud/azext_spring_cloud/custom.py b/src/spring-cloud/azext_spring_cloud/custom.py index 0c40a825a57..981d0387c52 100644 --- a/src/spring-cloud/azext_spring_cloud/custom.py +++ b/src/spring-cloud/azext_spring_cloud/custom.py @@ -7,7 +7,6 @@ import requests import re import os -import shlex from azure.core.exceptions import HttpResponseError from azure.mgmt.cosmosdb import CosmosDBManagementClient @@ -17,9 +16,7 @@ from time import sleep from ._stream_utils import stream_logs from azure.mgmt.core.tools import (parse_resource_id, is_valid_resource_id) -from ._utils import (_get_upload_local_file, _get_persistent_disk_size, - get_portal_uri, get_azure_files_info, - wait_till_end) +from ._utils import (get_portal_uri, wait_till_end) from knack.util import CLIError from .vendored_sdks.appplatform.v2020_07_01 import models from .vendored_sdks.appplatform.v2020_11_01_preview import models as models_20201101preview @@ -29,10 +26,9 @@ AppPlatformManagementClient as AppPlatformManagementClient_20201101preview ) from knack.log import get_logger -from .azure_storage_file import FileService -from azure.cli.core.azclierror import ClientRequestError, FileOperationError, InvalidArgumentValueError, RequiredArgumentMissingError +from azure.cli.core.azclierror import ClientRequestError, FileOperationError, InvalidArgumentValueError from azure.cli.core.commands.client_factory import get_mgmt_service_client -from azure.cli.core.util import get_file_json, sdk_no_wait +from azure.cli.core.util import sdk_no_wait from azure.cli.core.profiles import ResourceType, get_sdk from azure.mgmt.applicationinsights import ApplicationInsightsManagementClient from azure.cli.core.commands import cached_put @@ -41,7 +37,6 @@ from ._resource_quantity import validate_cpu, validate_memory from six.moves.urllib import parse from threading import Thread -from threading import Timer import sys import json import base64 @@ -319,291 +314,6 @@ def app_append_persistent_storage(cmd, client, resource_group, service, name, return app_updated -# This function is deprecated, see app.py#app_create -def app_create(cmd, client, resource_group, service, name, - assign_endpoint=None, - cpu=None, - memory=None, - instance_count=None, - disable_probe=None, - runtime_version=None, - jvm_options=None, - env=None, - enable_persistent_storage=None, - assign_identity=None, - persistent_storage=None, - loaded_public_certificate_file=None): - cpu = validate_cpu(cpu) - memory = validate_memory(memory) - apps = _get_all_apps(client, resource_group, service) - if name in apps: - raise CLIError("App '{}' already exists.".format(name)) - logger.warning("[1/4] Creating app with name '{}'".format(name)) - properties = models_20220101preview.AppResourceProperties() - properties.temporary_disk = models_20220101preview.TemporaryDisk( - size_in_gb=5, mount_path="/tmp") - - resource = client.services.get(resource_group, service) - - _validate_instance_count(resource.sku.tier, instance_count) - - if enable_persistent_storage: - properties.persistent_disk = models_20220101preview.PersistentDisk( - size_in_gb=_get_persistent_disk_size(resource.sku.tier), mount_path="/persistent") - else: - properties.persistent_disk = models_20220101preview.PersistentDisk( - size_in_gb=0, mount_path="/persistent") - - if persistent_storage: - data = get_file_json(persistent_storage, throw_on_empty=False) - custom_persistent_disks = [] - - if data: - if not data.get('customPersistentDisks'): - raise InvalidArgumentValueError("CustomPersistentDisks must be provided in the json file") - for item in data['customPersistentDisks']: - invalidProperties = not item.get('storageName') or \ - not item.get('customPersistentDiskProperties').get('type') or \ - not item.get('customPersistentDiskProperties').get('shareName') or \ - not item.get('customPersistentDiskProperties').get('mountPath') - if invalidProperties: - raise InvalidArgumentValueError("StorageName, Type, ShareName, MountPath mast be provided in the json file") - storage_resource = client.storages.get(resource_group, service, item['storageName']) - custom_persistent_disk_properties = models_20220101preview.AzureFileVolume( - type=item['customPersistentDiskProperties']['type'], - share_name=item['customPersistentDiskProperties']['shareName'], - mount_path=item['customPersistentDiskProperties']['mountPath'], - mount_options=item['customPersistentDiskProperties']['mountOptions'] if 'mountOptions' in item['customPersistentDiskProperties'] else None, - read_only=item['customPersistentDiskProperties']['readOnly'] if 'readOnly' in item['customPersistentDiskProperties'] else None) - - custom_persistent_disks.append( - models_20220101preview.CustomPersistentDiskResource( - storage_id=storage_resource.id, - custom_persistent_disk_properties=custom_persistent_disk_properties)) - properties.custom_persistent_disks = custom_persistent_disks - - if loaded_public_certificate_file is not None: - data = get_file_json(loaded_public_certificate_file) - if data: - if not data.get('loadedCertificates'): - raise FileOperationError("loadedCertificates must be provided in the json file") - loaded_certificates = [] - for item in data['loadedCertificates']: - invalidProperties = not item.get('certificateName') or not item.get('loadTrustStore') - if invalidProperties: - raise FileOperationError("certificateName, loadTrustStore must be provided in the json file") - certificate_resource = client.certificates.get(resource_group, service, item['certificateName']) - loaded_certificates.append(models_20220101preview. - LoadedCertificate(resource_id=certificate_resource.id, - load_trust_store=item['loadTrustStore'])) - properties.loaded_certificates = loaded_certificates - - app_resource = models_20220101preview.AppResource() - app_resource.properties = properties - app_resource.location = resource.location - if assign_identity is True: - app_resource.identity = models_20220101preview.ManagedIdentityProperties(type="systemassigned") - - poller = client.apps.begin_create_or_update( - resource_group, service, name, app_resource) - while poller.done() is False: - sleep(APP_CREATE_OR_UPDATE_SLEEP_INTERVAL) - - # create default deployment - logger.warning( - "[2/4] Creating default deployment with name '{}'".format(DEFAULT_DEPLOYMENT_NAME)) - default_deployment_resource = _default_deployment_resource_builder(cpu, memory, env, jvm_options, runtime_version, - instance_count, disable_probe, active=True) - poller = client.deployments.begin_create_or_update(resource_group, - service, - name, - DEFAULT_DEPLOYMENT_NAME, - default_deployment_resource) - - logger.warning("[3/4] Setting default deployment to production") - properties.public = assign_endpoint - - app_resource.location = resource.location - - app_poller = client.apps.begin_update(resource_group, service, name, app_resource) - logger.warning( - "[4/4] Updating app '{}' (this operation can take a while to complete)".format(name)) - while not poller.done() or not app_poller.done(): - sleep(DEPLOYMENT_CREATE_OR_UPDATE_SLEEP_INTERVAL) - - active_deployment = client.deployments.get( - resource_group, service, name, DEFAULT_DEPLOYMENT_NAME) - app = client.apps.get(resource_group, service, name) - app.properties.active_deployment = active_deployment - logger.warning("App create succeeded") - return app - - -def _default_deployment_resource_builder(cpu, memory, env, jvm_options, runtime_version, instance_count, disable_probe=None, active=False): - resource_requests = models_20220101preview.ResourceRequests(cpu=cpu, memory=memory) - container_probe_settings = None - if disable_probe is not None: - container_probe_settings = models_20220101preview.ContainerProbeSettings(disable_probe=disable_probe) - - deployment_settings = models_20220101preview.DeploymentSettings( - resource_requests=resource_requests, - environment_variables=env, - container_probe_settings=container_probe_settings) - - file_type = "NetCoreZip" if runtime_version == AppPlatformEnums.RuntimeVersion.NET_CORE31 else "Jar" - user_source_info = _format_user_source(file_type, '', runtime_version=runtime_version, jvm_options=jvm_options) - properties = models_20220101preview.DeploymentResourceProperties( - deployment_settings=deployment_settings, - source=user_source_info, - active=active) - - sku = models_20220101preview.Sku(name="S0", tier="STANDARD", capacity=instance_count) - deployment_resource = models_20220101preview.DeploymentResource(properties=properties, sku=sku) - return deployment_resource - - -def _format_user_source(file_type, - path, - artifact_selector=None, - jvm_options=None, - runtime_version=None, - main_entry=None, - version=None): - if file_type == 'NetCoreZip': - return models_20220101preview.NetCoreZipUploadedUserSourceInfo( - relative_path=path, - main_entry=main_entry, - runtime_version=runtime_version, - version=version - ) - elif file_type == 'Jar': - return models_20220101preview.JarUploadedUserSourceInfo( - relative_path=path, - runtime_version=runtime_version, - jvm_options=jvm_options, - version=version - ) - elif file_type == 'Source': - return models_20220101preview.SourceUploadedUserSourceInfo( - relative_path=path, - artifact_selector=artifact_selector, - version=version - ) - else: - return None - - -# This function is deprecated, see app.py#app_update -def app_update(cmd, client, resource_group, service, name, - assign_endpoint=None, - deployment=None, - runtime_version=None, - jvm_options=None, - main_entry=None, - env=None, - disable_probe=None, - enable_persistent_storage=None, - https_only=None, - enable_end_to_end_tls=None, - persistent_storage=None, - loaded_public_certificate_file=None): - resource = client.services.get(resource_group, service) - location = resource.location - - properties = models_20220101preview.AppResourceProperties(public=assign_endpoint, https_only=https_only, - enable_end_to_end_tls=enable_end_to_end_tls) - if enable_persistent_storage is True: - properties.persistent_disk = models_20220101preview.PersistentDisk( - size_in_gb=_get_persistent_disk_size(resource.sku.tier), mount_path="/persistent") - if enable_persistent_storage is False: - properties.persistent_disk = models_20220101preview.PersistentDisk(size_in_gb=0) - - if persistent_storage: - data = get_file_json(persistent_storage, throw_on_empty=False) - custom_persistent_disks = [] - - if data: - if not data.get('customPersistentDisks'): - raise InvalidArgumentValueError("CustomPersistentDisks must be provided in the json file") - for item in data['customPersistentDisks']: - invalidProperties = not item.get('storageName') or \ - not item.get('customPersistentDiskProperties').get('type') or \ - not item.get('customPersistentDiskProperties').get('shareName') or \ - not item.get('customPersistentDiskProperties').get('mountPath') - if invalidProperties: - raise InvalidArgumentValueError("StorageName, Type, ShareName, MountPath mast be provided in the json file") - storage_resource = client.storages.get(resource_group, service, item['storageName']) - custom_persistent_disk_properties = models_20220101preview.AzureFileVolume( - type=item['customPersistentDiskProperties']['type'], - share_name=item['customPersistentDiskProperties']['shareName'], - mount_path=item['customPersistentDiskProperties']['mountPath'], - mount_options=item['customPersistentDiskProperties']['mountOptions'] if 'mountOptions' in item['customPersistentDiskProperties'] else None, - read_only=item['customPersistentDiskProperties']['readOnly'] if 'readOnly' in item['customPersistentDiskProperties'] else None) - - custom_persistent_disks.append( - models_20220101preview.CustomPersistentDiskResource( - storage_id=storage_resource.id, - custom_persistent_disk_properties=custom_persistent_disk_properties)) - properties.custom_persistent_disks = custom_persistent_disks - if loaded_public_certificate_file is not None: - data = get_file_json(loaded_public_certificate_file) - if data: - if not data.get('loadedCertificates'): - raise CLIError("loadedCertificates must be provided in the json file") - loaded_certificates = [] - for item in data['loadedCertificates']: - invalidProperties = not item.get('certificateName') or not item.get('loadTrustStore') - if invalidProperties: - raise CLIError("certificateName, loadTrustStore must be provided in the json file") - certificate_resource = client.certificates.get(resource_group, service, item['certificateName']) - loaded_certificates.append(models_20220101preview. - LoadedCertificate(resource_id=certificate_resource.id, - load_trust_store=item['loadTrustStore'])) - properties.loaded_certificates = loaded_certificates - - app_resource = models_20220101preview.AppResource() - app_resource.properties = properties - app_resource.location = location - - logger.warning("[1/2] updating app '{}'".format(name)) - poller = client.apps.begin_update( - resource_group, service, name, app_resource) - while poller.done() is False: - sleep(APP_CREATE_OR_UPDATE_SLEEP_INTERVAL) - - app_updated = client.apps.get(resource_group, service, name) - - logger.warning("[2/2] Updating deployment '{}'".format(deployment.name)) - container_probe_settings = None - if disable_probe is not None: - container_probe_settings = models_20220101preview.ContainerProbeSettings(disable_probe=disable_probe) - - source_type = deployment.properties.source.type - if source_type in ['Jar', 'NetCoreZip'] and (jvm_options or main_entry or runtime_version): - source = _format_user_source(deployment.properties.source.type, - deployment.properties.source.relative_path, - jvm_options=jvm_options, - runtime_version=runtime_version, - main_entry=main_entry) - - deployment_settings = models_20220101preview.DeploymentSettings( - environment_variables=env, - container_probe_settings=container_probe_settings) - properties = models_20220101preview.DeploymentResourceProperties( - source=source, - deployment_settings=deployment_settings) - deployment_resource = models_20220101preview.DeploymentResource(properties=properties) - poller = client.deployments.begin_update( - resource_group, service, name, deployment.name, deployment_resource) - while poller.done() is False: - sleep(DEPLOYMENT_CREATE_OR_UPDATE_SLEEP_INTERVAL) - - deployment = client.deployments.get( - resource_group, service, name, deployment.name) - app_updated.properties.active_deployment = deployment - return app_updated - - def app_delete(cmd, client, resource_group, service, @@ -670,84 +380,6 @@ def app_get(cmd, client, return app -# This function is deprecated, see app.py#app_deploy -def app_deploy(cmd, client, resource_group, service, name, - version=None, - deployment=None, - disable_validation=None, - artifact_path=None, - source_path=None, - target_module=None, - runtime_version=None, - jvm_options=None, - main_entry=None, - env=None, - disable_probe=None, - container_image=None, - container_registry=None, - registry_username=None, - registry_password=None, - container_command=None, - container_args=None, - no_wait=False): - logger.warning(LOG_RUNNING_PROMPT) - - old_deployment = client.deployments.get(resource_group, service, name, deployment.name) - - file_type, file_path = _get_upload_local_file(runtime_version, artifact_path, source_path, container_image) - if file_type == 'Container': - if old_deployment.properties.source and old_deployment.properties.source.type == 'Container': - return _app_deploy_container(client, resource_group, service, name, deployment.name, - None, - None, - None, - env, - disable_probe, - container_image, - container_registry, - registry_username, - registry_password, - container_command, - container_args, - no_wait, - True) - else: - return _app_deploy_container(client, resource_group, service, name, deployment.name, - old_deployment.properties.deployment_settings.resource_requests.cpu, - old_deployment.properties.deployment_settings.resource_requests.memory, - old_deployment.sku.capacity, - env, - disable_probe, - container_image, - container_registry, - registry_username, - registry_password, - container_command, - container_args, - no_wait, - False) - else: - return _app_deploy(client, - resource_group, - service, - name, - deployment.name, - version, - file_path, - runtime_version, - jvm_options, - None, - None, - None, - env, - disable_probe, - main_entry, - target_module, - no_wait, - file_type, - True) - - def app_scale(cmd, client, resource_group, service, name, deployment=None, cpu=None, @@ -926,87 +558,6 @@ def app_append_loaded_public_certificate(cmd, client, resource_group, service, n return app_updated -# This function is deprecated, see app.py#deployment_create -def deployment_create(cmd, client, resource_group, service, app, name, - skip_clone_settings=False, - version=None, - artifact_path=None, - source_path=None, - disable_validation=None, - target_module=None, - runtime_version=None, - jvm_options=None, - main_entry=None, - cpu=None, - memory=None, - instance_count=None, - env=None, - disable_probe=None, - container_image=None, - container_registry=None, - registry_username=None, - registry_password=None, - container_command=None, - container_args=None, - no_wait=False): - cpu = validate_cpu(cpu) - memory = validate_memory(memory) - logger.warning(LOG_RUNNING_PROMPT) - deployments = _get_all_deployments(client, resource_group, service, app) - if any(iter(x for x in deployments if x.name == name)): - raise CLIError("Deployment " + name + " already exists") - - resource = client.services.get(resource_group, service) - _validate_instance_count(resource.sku.tier, instance_count) - - if not skip_clone_settings: - active_deployment = next(iter(x for x in deployments if x.properties.active), None) - if not active_deployment: - logger.warning("No production deployment found, use --skip-clone-settings to skip copying settings from " - "production deployment.") - else: - cpu = cpu or active_deployment.properties.deployment_settings.resource_requests.cpu - memory = memory or active_deployment.properties.deployment_settings.resource_requests.memory - instance_count = instance_count or active_deployment.sku.capacity - jvm_options = jvm_options or (active_deployment.properties.source.jvm_options if hasattr(active_deployment.properties.source, 'jvm_options') else None) - env = env or active_deployment.properties.deployment_settings.environment_variables - if active_deployment.properties.deployment_settings.container_probe_settings is not None: - disable_probe = disable_probe or active_deployment.properties.deployment_settings.container_probe_settings.disable_probe - else: - cpu = cpu or "1" - memory = memory or "1Gi" - instance_count = instance_count or 1 - - file_type, file_path = _get_upload_local_file(runtime_version, artifact_path, source_path, container_image) - if file_type == 'Container': - return _app_deploy_container(client, resource_group, service, app, name, - cpu, - memory, - instance_count, - env, - disable_probe, - container_image, - container_registry, - registry_username, - registry_password, - container_command, - container_args, - no_wait) - else: - return _app_deploy(client, resource_group, service, app, name, version, file_path, - runtime_version, - jvm_options, - cpu, - memory, - instance_count, - env, - disable_probe, - main_entry, - target_module, - no_wait, - file_type) - - def _validate_instance_count(sku, instance_count=None): if instance_count is not None: sku = sku.upper() @@ -1501,187 +1052,6 @@ def _get_redis_primary_key(cli_ctx, resource_id): return keys.primary_key -def _get_all_deployments(client, resource_group, service, app): - deployments_resource = client.deployments.list( - resource_group, service, app) - return [x for x in deployments_resource] - - -def _get_all_apps(client, resource_group, service): - apps = [] - apps_resource = client.apps.list(resource_group, service) - apps = list(apps_resource) - apps = (app.name for app in apps) - return apps - - -# pylint: disable=too-many-locals, no-member -def _app_deploy(client, resource_group, service, app, name, version, path, runtime_version, jvm_options, cpu, memory, - instance_count, - env, - disable_probe=None, - main_entry=None, - target_module=None, - no_wait=False, - file_type=None, - update=False): - if file_type is None: - # update means command is az spring-cloud app deploy xxx - if update: - raise RequiredArgumentMissingError( - "One of the following arguments are required: '--artifact-path' or '--source-path'") - # if command is az spring-cloud app deployment create xxx, create default deployment - else: - logger.warning( - "Creating default deployment without artifact/source folder. Please specify the --artifact-path/--source-path argument explicitly if needed.") - default_deployment_resource = _default_deployment_resource_builder(cpu, memory, env, jvm_options, - runtime_version, instance_count, disable_probe) - return sdk_no_wait(no_wait, client.deployments.begin_create_or_update, - resource_group, service, app, name, default_deployment_resource) - upload_url = None - relative_path = None - logger.warning("file_type is {}".format(file_type)) - logger.warning("[1/3] Requesting for upload URL") - try: - response = client.apps.get_resource_upload_url(resource_group, service, app) - upload_url = response.upload_url - relative_path = response.relative_path - except (AttributeError, HttpResponseError) as e: - raise CLIError( - "Failed to get a SAS URL to upload context. Error: {}".format(e.message)) - - resource_requests = None - if cpu is not None or memory is not None: - resource_requests = models_20220101preview.ResourceRequests(cpu=cpu, memory=memory) - - container_probe_settings = None - if disable_probe is not None: - container_probe_settings = models_20220101preview.ContainerProbeSettings(disable_probe=disable_probe) - - deployment_settings = models_20220101preview.DeploymentSettings( - resource_requests=resource_requests, - environment_variables=env, - container_probe_settings=container_probe_settings) - sku = models_20220101preview.Sku(name="S0", tier="STANDARD", capacity=instance_count) - user_source_info = _format_user_source(file_type, - relative_path, - artifact_selector=target_module, - jvm_options=jvm_options, - runtime_version=runtime_version, - main_entry=main_entry, - version=version) - properties = models_20220101preview.DeploymentResourceProperties( - deployment_settings=deployment_settings, - source=user_source_info) - # upload file - if not upload_url: - raise CLIError("Failed to get a SAS URL to upload context.") - account_name, endpoint_suffix, share_name, relative_name, sas_token = get_azure_files_info(upload_url) - logger.warning("[2/3] Uploading package to blob") - file_service = FileService(account_name, sas_token=sas_token, endpoint_suffix=endpoint_suffix) - file_service.create_file_from_path(share_name, None, relative_name, path) - - if file_type == "Source" and not no_wait: - def get_log_url(): - try: - log_file_url_response = client.deployments.get_log_file_url( - resource_group_name=resource_group, - service_name=service, - app_name=app, - deployment_name=name) - if not log_file_url_response: - return None - return log_file_url_response.url - except HttpResponseError: - return None - - def get_logs_loop(): - log_url = None - while not log_url or log_url == old_log_url: - log_url = get_log_url() - sleep(10) - - logger.warning("Trying to fetch build logs") - stream_logs(client.deployments, resource_group, service, - app, name, logger_level_func=print) - - old_log_url = get_log_url() - - timer = Timer(3, get_logs_loop) - timer.daemon = True - timer.start() - - # create deployment - logger.warning( - "[3/3] Updating deployment in app '{}' (this operation can take a while to complete)".format(app)) - deployment_resource = models.DeploymentResource(properties=properties, sku=sku) - if update: - return sdk_no_wait(no_wait, client.deployments.begin_update, - resource_group, service, app, name, deployment_resource) - - return sdk_no_wait(no_wait, client.deployments.begin_create_or_update, - resource_group, service, app, name, deployment_resource) - - -# pylint: disable=too-many-locals, no-member -def _app_deploy_container(client, resource_group, service, app, name, cpu, memory, - instance_count, - env, - disable_probe, - container_image, - container_registry, - registry_username, - registry_password, - container_command, - container_args, - no_wait=False, - update=False): - resource_requests = None - if cpu is not None or memory is not None: - resource_requests = models_20220101preview.ResourceRequests(cpu=cpu, memory=memory) - if container_command is not None: - container_command = shlex.split(container_command) - if container_args is not None: - container_args = shlex.split(container_args) - - container_probe_settings = None - if disable_probe is not None: - container_probe_settings = models_20220101preview.ContainerProbeSettings(disable_probe=disable_probe) - - deployment_settings = models_20220101preview.DeploymentSettings( - resource_requests=resource_requests, - environment_variables=env, - container_probe_settings=container_probe_settings) - sku = models_20220101preview.Sku(name="S0", tier="STANDARD", capacity=instance_count) - image_registry_credential = models_20220101preview.ImageRegistryCredential( - username=registry_username, - password=registry_password # [SuppressMessage("Microsoft.Security", "CS001:SecretInline", Justification="false positive")] - ) if registry_username is not None and registry_password is not None else None - custom_container = models_20220101preview.CustomContainer( - server=container_registry, - container_image=container_image, - command=container_command, - args=container_args, - image_registry_credential=image_registry_credential, - ) - user_source_info = models_20220101preview.CustomContainerUserSourceInfo( - custom_container=custom_container) - properties = models_20220101preview.DeploymentResourceProperties( - deployment_settings=deployment_settings, - source=user_source_info) - - # create deployment - logger.warning( - "Updating deployment in app '{}' (this operation can take a while to complete)".format(app)) - deployment_resource = models_20220101preview.DeploymentResource(properties=properties, sku=sku) - if update: - return sdk_no_wait(no_wait, client.deployments.begin_update, - resource_group, service, app, name, deployment_resource) - - return sdk_no_wait(no_wait, client.deployments.begin_create_or_update, - resource_group, service, app, name, deployment_resource) - - # pylint: disable=bare-except, too-many-statements def _get_app_log(url, user_name, password, format_json, exceptions): logger_seg_regex = re.compile(r'([^\.])[^\.]+\.')