1919import stat
2020import subprocess
2121import sys
22+ from typing import Optional
2223import tempfile
2324import threading
2425import time
9495)
9596from azure .cli .core .commands import LongRunningOperation
9697from azure .cli .core .commands .client_factory import get_subscription_id
98+ from azure .cli .core .commands .progress import PollerProgressBar
99+ from azure .core .polling .base_polling import LocationPolling , _is_empty , BadResponse , _as_json
97100from azure .cli .core .profiles import ResourceType
101+ from azure .mgmt .core .polling .arm_polling import ARMPolling
98102from azure .cli .core .util import in_cloud_console , sdk_no_wait
99103from azure .core .exceptions import ResourceNotFoundError as ResourceNotFoundErrorAzCore
100104from azure .mgmt .containerservice .models import KubernetesSupportPlan
@@ -2027,13 +2031,46 @@ def aks_get_versions(cmd, client, location):
20272031 return client .list_kubernetes_versions (location )
20282032
20292033
2034+ class RunCommandLocationPolling (LocationPolling ):
2035+ """Extends LocationPolling but uses the body content instead of the status code for the status"""
2036+
2037+ @staticmethod
2038+ def _get_provisioning_state (response : Optional [str ]):
2039+ """Attempt to get provisioning state from resource.
2040+
2041+ :param azure.core.pipeline.transport.HttpResponse response: latest REST call response.
2042+ :returns: Status if found, else 'None'.
2043+ """
2044+ if _is_empty (response ):
2045+ return None
2046+ body = _as_json (response )
2047+ return body .get ("properties" , {}).get ("provisioningState" )
2048+
2049+ def get_status (self , pipeline_response ):
2050+ """Process the latest status update retrieved from the same URL as
2051+ the previous request.
2052+
2053+ :param azure.core.pipeline.PipelineResponse response: latest REST call response.
2054+ :raises: BadResponse if status not 200 or 204.
2055+ """
2056+ response = pipeline_response .http_response
2057+ if _is_empty (response ):
2058+ raise BadResponse (
2059+ "The response from long running operation does not contain a body."
2060+ )
2061+
2062+ status = self ._get_provisioning_state (response )
2063+ return status or "Succeeded"
2064+
2065+
20302066def aks_runcommand (cmd , client , resource_group_name , name , command_string = "" , command_files = None , no_wait = False ):
20312067 colorama .init ()
20322068
20332069 mc = client .get (resource_group_name , name )
20342070
20352071 if not command_string :
20362072 raise ValidationError ('Command cannot be empty.' )
2073+
20372074 RunCommandRequest = cmd .get_models ('RunCommandRequest' , resource_type = ResourceType .MGMT_CONTAINERSERVICE ,
20382075 operation_group = 'managed_clusters' )
20392076 request_payload = RunCommandRequest (command = command_string )
@@ -2046,8 +2083,15 @@ def aks_runcommand(cmd, client, resource_group_name, name, command_string="", co
20462083 request_payload .cluster_token = _get_dataplane_aad_token (
20472084 cmd .cli_ctx , "6dae42f8-4368-4678-94ff-3960e28e3630" )
20482085
2086+ polling_interval = 5
2087+ retry_total = 0
2088+
20492089 command_result_poller = sdk_no_wait (
2050- no_wait , client .begin_run_command , resource_group_name , name , request_payload , polling_interval = 5 , retry_total = 0
2090+ no_wait , client .begin_run_command , resource_group_name , name , request_payload ,
2091+ # NOTE: Note sure if retry_total is used in ARMPolling
2092+ polling = ARMPolling (polling_interval , lro_options = {"final-state-via" : "location" }, lro_algorithms = [RunCommandLocationPolling ()], retry_total = retry_total ),
2093+ polling_interval = polling_interval ,
2094+ retry_total = retry_total
20512095 )
20522096 if no_wait :
20532097 # pylint: disable=protected-access
@@ -2058,7 +2102,8 @@ def aks_runcommand(cmd, client, resource_group_name, name, command_string="", co
20582102 command_id = command_id_regex .findall (command_result_polling_url )[0 ]
20592103 _aks_command_result_in_progess_helper (client , resource_group_name , name , command_id )
20602104 return
2061- return _print_command_result (cmd .cli_ctx , command_result_poller .result (300 ))
2105+
2106+ return LongRunningOperation (cmd .cli_ctx , progress_bar = PollerProgressBar (cmd .cli_ctx , command_result_poller ))(command_result_poller )
20622107
20632108
20642109def aks_command_result (cmd , client , resource_group_name , name , command_id = "" ):
0 commit comments