
    -_g                     F    d dl Z d dlmZ d dlmZ d dlmZ  G d de      Zy)    N)JujuNotInstalledException)RemoteTransport)sos_get_command_outputc                   ^    e Zd ZdZdZdZd Zd ZddZd Z	e
d        Ze
d	        Zd
 Zd Zy)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

    juju_sshubuntuc                     d}	 t        j                  |t         j                  d       y# t         j                  $ r}| j	                  d       t
        |d }~ww xY w)Nzjuju versionTstderrshellzFailed to check `juju` version)
subprocesscheck_outputSTDOUTCalledProcessError	log_errorr   )selfcmderrs      ?/usr/lib/python3/dist-packages/sos/collector/transports/juju.py_check_juju_installedzJujuSSH._check_juju_installed'   sV    	5##C
0A0AN  ,, 	5NN;<+4	5s   &+ AAAc                     | j                    d| }	 t        j                  |t        j                  d       y# t        j                  $ r | j                  d| d        w xY w)Nz sudo chmod o+r Tr   zFailed to make z world-readable)remote_execr   r   r   r   r   )r   fnamer   s      r   _chmodzJujuSSH._chmod0   sk    !!""25':	##C
0A0AN  ,, 	NN_UG?CD	s	   &: *A$c                 D    | j                         | _        | j                  S N)r   
_connected)r   passwords     r   _connectzJujuSSH._connect9   s    446    c                      y)NT r   s    r   _disconnectzJujuSSH._disconnect=   s    r!   c                     | j                   S r   )r   r$   s    r   	connectedzJujuSSH.connected@   s    r!   c                 h    | j                   j                  d      \  }}|rd| nd}| d| }d| S )N:-m   z	juju ssh )addresssplit)r   modeltarget_optionmodel_optionoptions        r   r   zJujuSSH.remote_execD   sG    #||11#6}(-UG}2 >=/26(##r!   c                     | j                   j                  d      \  }}|rd| nd}d| d| d| d| }t        |d      }|d	   d
k(  S )Nr)   r*   r+   	juju scp z -- r,      )timeoutstatusr   )r-   r.   r   r   r   destr/   unitr1   r   ress           r   _copy_file_to_remotezJujuSSH._copy_file_to_remoteK   sb    ll((-t(-UG}2,tE7!D64&A$S"58}!!r!   c                     | j                  |       | j                  j                  d      \  }}|rd| nd}d| d| d| d| }t        |      }|d   dk(  S )	Nr)   r*   r+   r4   z -- -r r,   r7   r   )r   r-   r.   r   r8   s           r   _retrieve_filezJujuSSH._retrieve_fileR   sl    Ell((-t(-UG}2,wtfAeWAdVD$S)8}!!r!   N)r+   )__name__
__module____qualname____doc__namedefault_userr   r   r    r%   propertyr'   r   r<   r>   r#   r!   r   r   r      sY      DL   $ $""r!   r   )r   sos.collector.exceptionsr   sos.collector.transportsr   sos.utilitiesr   r   r#   r!   r   <module>rI      s!     > 4 0E"o E"r!   