o
    ;i	                     @   sB  d dl mZmZ d dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d d	lmZmZmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dl m!Z! ej"ddddZ#e#$dej%edfdee& de'fddZ(e#$dej%ej)ddej*d ddfd e&d!e+fd"dZ,dS )#    )OptionalUnionN)Column)Text)_get_environment_name)get_pty_info)synchronizer)timestamp_to_localized_str)
ENV_OPTIONdisplay_tableis_tty)_Client)config)_ContainerProcess)
ensure_env)OutputManager)
StreamType)api_pb2clusterz2Manage and connect to running multi-node clusters.T)namehelpno_args_is_helplistFenvjsonc              	      s   t | } t I dH }t| }|jtj|dI dH }tdddtdddddg}g }|j	j
d	d
 dd |j	D ]}||j|j|jrKt|j|ndtt|jg q:t|||d| d dS )z-List all clusters that are currently running.N)environment_name
Cluster ID   )	min_widthzApp IDz
Start TimeNodesc                 S   s   | j S )N)
started_at)c r"   E/home/ubuntu/.local/lib/python3.10/site-packages/modal/cli/cluster.py<lambda>,   s    zlist_.<locals>.<lambda>T)keyreversePendingz+Active Multi-node Clusters in environment: )r   title)r   r   from_envr   stubClusterListr   ClusterListRequestr   clusterssortappend
cluster_idapp_idr    r	   strlentask_idsr   )r   r   clientr   rescolumn_namesrowsr!   r"   r"   r#   list_   s.   




	r9   shellr   )r   zRank of the node to shell into)defaultr   r0   rankc           	         s  t  I dH }|jtj| dI dH }t|jj|kr(t	
d| d|  |jj| }|dkr4dnd}t d| d	| d
|  d| d	 t }tj|dg|rYtddndtdd}|j|I dH }|rzt|j|| I dH  dS t|j||tjtjd I dH  dS )z*Open a shell to a multi-node cluster node.N)r0   zNo node with rank z in cluster r   z(main) z[green]Opening shell to node  z of cluster z (container z	)[/green]z	/bin/bashT)r:   function_runtime_debug)task_idcommandpty_inforuntime_debug)stdoutstderr)r   r)   r*   
ClusterGetr   ClusterGetRequestr3   r   r4   typerAbortr   getprintr   ContainerExecRequestr   r   ContainerExecr   exec_idattachr   STDOUTwait)	r0   r<   r5   r6   r@   is_mainptyreqexec_resr"   r"   r#   r:   ;   s2   )-typingr   r   rH   
rich.tabler   	rich.textr   modal._objectr   modal._output.ptyr   modal._utils.async_utilsr   modal._utils.time_utilsr	   modal.cli.utilsr
   r   r   modal.clientr   modal.configr   modal.container_processr   modal.environmentsr   modal.outputr   modal.stream_typer   modal_protor   Typercluster_clirA   create_blockingr2   boolr9   ArgumentOptionintr:   r"   r"   r"   r#   <module>   s>    
