Skip to content

Commit e3e1249

Browse files
samkreterderekbekoe
authored andcommitted
Add Windows Client Support For Container Exec Command (#5893)
1 parent a9aae1f commit e3e1249

1 file changed

Lines changed: 35 additions & 5 deletions

File tree

  • src/command_modules/azure-cli-container/azure/cli/command_modules/container

src/command_modules/azure-cli-container/azure/cli/command_modules/container/custom.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
# pylint: disable=too-few-public-methods,too-many-arguments,no-self-use,too-many-locals,line-too-long,unused-argument
77

88
import errno
9+
try:
10+
import msvcrt
11+
except ImportError:
12+
# Not supported for Linux machines.
13+
pass
914
import platform
1015
import select
1116
import shlex
@@ -248,10 +253,6 @@ def container_logs(cmd, resource_group_name, name, container_name=None, follow=F
248253
def container_exec(cmd, resource_group_name, name, container_name=None, exec_command=None, terminal_row_size=20, terminal_col_size=80):
249254
"""Start exec for a container. """
250255

251-
if platform.system() is WINDOWS_NAME:
252-
logger.warning("The container exec operation is currently not supported for Windows.")
253-
return
254-
255256
start_container_client = cf_start_container(cmd.cli_ctx)
256257
container_group_client = cf_container_groups(cmd.cli_ctx)
257258
container_group = container_group_client.get(resource_group_name, name)
@@ -265,11 +266,40 @@ def container_exec(cmd, resource_group_name, name, container_name=None, exec_com
265266

266267
execContainerResponse = start_container_client.launch_exec(resource_group_name, name, container_name, exec_command, terminal_size)
267268

268-
_start_exec_pipe(execContainerResponse.web_socket_uri, execContainerResponse.password)
269+
if platform.system() is WINDOWS_NAME:
270+
_start_exec_pipe_win(execContainerResponse.web_socket_uri, execContainerResponse.password)
271+
else:
272+
_start_exec_pipe(execContainerResponse.web_socket_uri, execContainerResponse.password)
273+
269274
else:
270275
raise CLIError('--container-name required when container group has more than one container.')
271276

272277

278+
def _start_exec_pipe_win(web_socket_uri, password):
279+
280+
def _on_ws_open(ws):
281+
ws.send(password)
282+
t = threading.Thread(target=_capture_stdin, args=[ws])
283+
t.daemon = True
284+
t.start()
285+
286+
ws = websocket.WebSocketApp(web_socket_uri, on_open=_on_ws_open, on_message=_on_ws_msg)
287+
288+
ws.run_forever()
289+
290+
291+
def _on_ws_msg(ws, msg):
292+
sys.stdout.write(msg)
293+
sys.stdout.flush()
294+
295+
296+
def _capture_stdin(ws):
297+
while True:
298+
if msvcrt.kbhit:
299+
x = msvcrt.getch()
300+
ws.send(x)
301+
302+
273303
def _start_exec_pipe(web_socket_uri, password):
274304
ws = websocket.create_connection(web_socket_uri)
275305

0 commit comments

Comments
 (0)