o
    O6ufÎ
  ã                   @   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                   @   sZ   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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

    Újuju_sshÚubuntuc                 C   s<   d}zt j|t jdd� W dS  t jy   |  d¡ t‚w )Nzjuju versionT©ÚstderrÚshellzFailed to check `juju` version)Ú
subprocessÚcheck_outputÚSTDOUTÚCalledProcessErrorÚ	log_errorr   )ÚselfÚcmd© r   ú?/usr/lib/python3/dist-packages/sos/collector/transports/juju.pyÚ_check_juju_installed'   s   ý
þzJujuSSH._check_juju_installedc                 C   sN   | j › d|› �}ztj|tjdd� W dS  tjy&   |  d|› d�¡ ‚ w )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   Ú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   ÚmodelÚtarget_optionÚmodel_optionÚoptionr   r   r   r   D   s   
zJujuSSH.remote_execc                 C   s\   |   |¡ | j d¡\}}|rd|› �nd}d|› d|› d|› d|› �}t|ƒ}|d dkS )	Nr    r!   r   z	juju scp z -- -r r"   Ústatusr   )r   r#   r$   r   )r   r   Údestr%   Úunitr'   r   Úresr   r   r   Ú_retrieve_fileK   s   
zJujuSSH._retrieve_fileN)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚnameÚdefault_userr   r   r   r   Úpropertyr   r   r-   r   r   r   r   r      s    	
	

r   )r   Úsos.collector.exceptionsr   Úsos.collector.transportsr   Úsos.utilitiesr   r   r   r   r   r   Ú<module>   s
   