U
    -_g                     @   s@   d dl Z d dlmZ d dlmZ d dlmZ G dd deZdS )    N)JujuNotInstalledException)RemoteTransport)sos_get_command_outputc                   @   sb   e Zd ZdZdZdZdd Zdd Zdd	d
Zdd Z	e
dd Ze
dd Zdd Zdd ZdS )JujuSSHa  
    A "transport" that leverages `juju ssh` to perform commands on the remote
    hosts.

    This transport is expected to be used in juju managed environment, and the
    user should have the necessary credential for accessing the controller.
    When using this transport, the --nodes option will be expected to be a
    comma separated machine IDs, **not** IP addr, since `juju ssh` identifies
    the ssh target by machine ID.

    Examples:

    sos collect --nodes 0,1,2 --no-local --transport juju --batch

    Zjuju_sshZubuntuc              
   C   sT   d}zt j|t jdd W n4 t jk
rN } z| d t|W 5 d }~X Y nX dS )Nzjuju versionTstderrshellzFailed to check `juju` version)
subprocesscheck_outputSTDOUTCalledProcessError	log_errorr   )selfcmderr r   ?/usr/lib/python3/dist-packages/sos/collector/transports/juju.py_check_juju_installed'   s    
zJujuSSH._check_juju_installedc                 C   sV   | j  d| }ztj|tjdd W n* tjk
rP   | d| d  Y nX dS )Nz sudo chmod o+r Tr   zFailed to make z world-readable)remote_execr	   r
   r   r   r   )r   fnamer   r   r   r   _chmod0   s    zJujuSSH._chmod c                 C   s   |   | _| jS N)r   
_connected)r   Zpasswordr   r   r   _connect9   s    
zJujuSSH._connectc                 C   s   dS )NTr   r   r   r   r   _disconnect=   s    zJujuSSH._disconnectc                 C   s   | j S r   )r   r   r   r   r   	connected@   s    zJujuSSH.connectedc                 C   s:   | j d\}}|rd| nd}| d| }d| S )N:-m r    z	juju ssh )addresssplit)r   modelZtarget_optionmodel_optionZoptionr   r   r   r   D   s    zJujuSSH.remote_execc                 C   sV   | j d\}}|rd| nd}d| d| d| d| }t|dd}|d	 d
kS )Nr   r   r   	juju scp z -- r       )Ztimeoutstatusr   )r!   r"   r   r   r   destr#   Zunitr$   r   resr   r   r   _copy_file_to_remoteK   s
    zJujuSSH._copy_file_to_remotec                 C   s\   |  | | jd\}}|r(d| nd}d| d| d| d| }t|}|d dkS )	Nr   r   r   r%   z -- -r r    r'   r   )r   r!   r"   r   r(   r   r   r   _retrieve_fileR   s    
zJujuSSH._retrieve_fileN)r   )__name__
__module____qualname____doc__nameZdefault_userr   r   r   r   propertyr   r   r+   r,   r   r   r   r   r      s   		


r   )r	   Zsos.collector.exceptionsr   Zsos.collector.transportsr   Zsos.utilitiesr   r   r   r   r   r   <module>   s   