-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Spring] Add subcommand "az spring app connect". (#5358)
- Loading branch information
Showing
9 changed files
with
187 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
# -------------------------------------------------------------------------------------------- | ||
# pylint: disable=logging-fstring-interpolation | ||
|
||
import os | ||
import sys | ||
import websocket | ||
|
||
from knack.log import get_logger | ||
from azure.cli.core.azclierror import CLIInternalError | ||
|
||
logger = get_logger(__name__) | ||
EXEC_PROTOCOL_CONTROL_BYTE_STDOUT = 1 | ||
EXEC_PROTOCOL_CONTROL_BYTE_STDERR = 2 | ||
EXEC_PROTOCOL_CONTROL_BYTE_CLUSTER = 3 | ||
EXEC_PROTOCOL_CTRL_C_MSG = b"\x00\x03" | ||
|
||
|
||
class WebSocketConnection: | ||
def __init__(self, url, token): | ||
self._token = token | ||
self._url = url | ||
self._socket = websocket.WebSocket(enable_multithread=True) | ||
logger.info("Attempting to connect to %s", self._url) | ||
self._socket.connect(self._url, header=["Authorization: Bearer %s" % self._token]) | ||
self.is_connected = True | ||
|
||
def disconnect(self): | ||
logger.warning("Disconnecting...") | ||
self.is_connected = False | ||
self._socket.close() | ||
|
||
def send(self, *args, **kwargs): | ||
return self._socket.send(*args, **kwargs) | ||
|
||
def recv(self, *args, **kwargs): | ||
return self._socket.recv(*args, **kwargs) | ||
|
||
|
||
def recv_remote(connection: WebSocketConnection): | ||
# response_encodings is the ordered list of Unicode encodings to try to decode with before raising an exception | ||
while connection.is_connected: | ||
response = connection.recv() | ||
if not response: | ||
connection.disconnect() | ||
else: | ||
logger.info("Received raw response %s", response.hex()) | ||
control_byte = int(response[0]) | ||
if control_byte in (EXEC_PROTOCOL_CONTROL_BYTE_STDOUT, EXEC_PROTOCOL_CONTROL_BYTE_STDERR): | ||
os.write(sys.stdout.fileno(), response[1:]) | ||
elif control_byte == EXEC_PROTOCOL_CONTROL_BYTE_CLUSTER: | ||
pass # Do nothing for this control byte | ||
else: | ||
connection.disconnect() | ||
raise CLIInternalError("Unexpected message received: %d" % control_byte) | ||
|
||
|
||
def send_stdin(connection: WebSocketConnection): | ||
while connection.is_connected: | ||
ch = sys.stdin.read(1) | ||
if connection.is_connected: | ||
connection.send(ch) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/spring/azext_spring/tests/latest/recordings/test_app_connect.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
interactions: | ||
- request: | ||
body: null | ||
headers: | ||
Accept: | ||
- application/json | ||
Accept-Encoding: | ||
- gzip, deflate | ||
CommandName: | ||
- spring app connect | ||
Connection: | ||
- keep-alive | ||
ParameterSetName: | ||
- -s -g -n --shell-cmd | ||
User-Agent: | ||
- AZURECLI/2.40.0 azsdk-python-mgmt-appplatform/6.1.0 Python/3.10.4 (Linux-5.15.57.1-microsoft-standard-WSL2-x86_64-with-glibc2.35) | ||
method: GET | ||
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/cli/providers/Microsoft.AppPlatform/Spring/cli-unittest/apps/test-app/deployments?api-version=2022-05-01-preview | ||
response: | ||
body: | ||
string: '{"error":{"code":"ResourceGroupNotFound","message":"Resource group | ||
''cli'' could not be found."}}' | ||
headers: | ||
cache-control: | ||
- no-cache | ||
content-length: | ||
- '95' | ||
content-type: | ||
- application/json; charset=utf-8 | ||
date: | ||
- Thu, 22 Sep 2022 04:55:34 GMT | ||
expires: | ||
- '-1' | ||
pragma: | ||
- no-cache | ||
strict-transport-security: | ||
- max-age=31536000; includeSubDomains | ||
x-content-type-options: | ||
- nosniff | ||
x-ms-failure-cause: | ||
- gateway | ||
status: | ||
code: 404 | ||
message: Not Found | ||
version: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters