o
    O6ufo"  ã                   @   sb   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ ddd„Zdd„ Z	G d	d
„ d
eƒZ
dS )é    N)ÚCluster)Úsos_parse_version©Úsos_get_command_outputc                 C   s   | sg S dd„ |   d¡D ƒS )zParse comma separated string.c                 S   s   g | ]}|  ¡ ‘qS © )Ústrip)Ú.0Ústringr   r   ú=/usr/lib/python3/dist-packages/sos/collector/clusters/juju.pyÚ
<listcomp>   s    z(_parse_option_string.<locals>.<listcomp>ú,)Úsplit)Ústringsr   r   r
   Ú_parse_option_string   s   r   c                 C   s   G dd„ dƒ}|| ƒS )aR  Helper function to get Index.

    The reason why we need Index defined in function is because currently
    the collector.__init__ will load all the classes in this module
    and also Index. This will cause bug because it think Index is
    Cluster type. Also We don't want to provide a customized
    filter to remove Index class.
    c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )z_get_index.<locals>.IndexzüIndex structure to help parse juju status output.

        Attributes apps, units and machines are dict which key
        is the app/unit/machine name
        and the value is list of targets which format are
        {model_name}:{machine_id}.
        c                 S   s(   || _ i | _i | _i | _t d¡| _d S )NÚsos)Ú
model_nameÚappsÚunitsÚmachinesÚloggingÚ	getLoggerÚui_log)Úselfr   r   r   r
   Ú__init__.   s
   z"_get_index.<locals>.Index.__init__c           
      S   s€   |d   ¡ D ]7\}}g }| di ¡}|  ¡ D ]!\}}|d }| j› d|› �}	|	g| j|< |	g| j|< | |	¡ q|| j|< qdS )zAdds principal units to index.Úapplicationsr   Úmachineú:N)ÚitemsÚgetr   r   r   Úappendr   )
r   Újuju_statusÚappÚapp_infoÚnodesr   ÚunitÚ	unit_infor   Únoder   r   r
   Úadd_principals5   s   öz(_get_index.<locals>.Index.add_principalsc                 S   sø   |d   ¡ D ]s\}}| dg ¡}|D ]f}| j |¡s%| j d|› d�¡ q| j|  | j| ¡ d|d | vrC| j d|› d�¡ q|d | d }|  ¡ D ](\}}| j› d|d › �}	| d	i ¡  ¡ D ]\}
}|
 |d
 ¡rv|	g| j|
< qeqOqqdS )z‡Add subordinates to index.

            Since subordinates does not have units they need to be
            manually added.
            r   zsubordinate-tozPrincipal charm z is missingr   z is missing unitsr   r   Úsubordinatesú/N)	r   r   r   r   ÚwarningÚextendr   Ú
startswithr   )r   r    r!   r"   Úsubordinate_toÚparentr   r$   r%   r&   Úsub_keyÚ	sub_valuer   r   r
   Úadd_subordinatesC   s:   
ÿ
ÿÿ
þ€üþïþz*_get_index.<locals>.Index.add_subordinatesc                 S   s2   |d   ¡ D ]}| j› d|› �}|g| j|< qdS )zƒAdd machines to index.

            If model does not have any applications it needs to be
            manually added.
            r   r   N)Úkeysr   r   )r   r    r   r&   r   r   r
   Úadd_machinesd   s   þz&_get_index.<locals>.Index.add_machinesN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r'   r1   r3   r   r   r   r
   ÚIndex%   s    !r8   r   )r   r8   r   r   r
   Ú
_get_index   s   
Ir9   c                   @   s`   e Zd ZdZd ZdZg d¢Zdd„ Zdd„ Zdd	„ Z	d
d„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )Újujua!  
    The juju cluster profile is intended to be used on juju managed clouds.
    It"s assumed that `juju` is installed on the machine where `sos` is called,
    and that the juju user has superuser privilege to the current controller.

    By default, the sos reports will be collected from all the applications in
    the current model. If necessary, you can filter the nodes by models /
    applications / units / machines with cluster options.

    Example:

    sos collect --cluster-type juju -c "juju.models=sos" -c "juju.apps=a,b,c"

    zJuju Managed Clouds))r   Ú z1Filter node list by apps (comma separated regex).)r   r;   z3Filter node list by units (comma separated string).)Úmodelsr;   z4Filter node list by models (comma separated string).)r   r;   z6Filter node list by machines (comma separated string).c                 C   s   t  dd|dt j¡S )z#Remove leading characters before {.z(^[^{]*)(.*)z\2r   )ÚreÚsubÚ	MULTILINE)r   Úoutputr   r   r
   Ú_cleanup_juju_outputŽ   s   zjuju._cleanup_juju_outputc                 C   s6   |   |¡}t|d�}| |¡ | |¡ | |¡ |S )z¯Parse juju status output and return target dict.

        Here are couple helper functions to parse the juju principals units,
        subordinate units and machines.
        )r   )Ú_execute_juju_statusr9   r'   r1   r3   )r   r   r    Úindexr   r   r
   Ú_get_model_info’   s   




zjuju._get_model_infoc                 C   s   t dƒ}|d S )zGrab the version of jujuzjuju versionr@   r   )r   Úresr   r   r
   Ú_get_juju_version¡   s   zjuju._get_juju_versionc           	      C   s¼   |rd|› �nd}d}|   ¡ }t|ƒtdƒkr|d7 }| j› d|› d|› �}|  |¡}|d d	ks=td
|› d|d › �ƒ‚|  |d ¡}d }zt |¡}W |S  tjy]   td|› �ƒ‚w )Nz-m r;   z--format jsonÚ3z --no-colorz status ú Ústatusr   ú'z' returned error: r@   z-Juju output is not valid json format.Output: )	rF   r   ÚcmdÚexec_primary_cmdÚ	ExceptionrA   ÚjsonÚloadsÚJSONDecodeError)	r   r   Úmodel_optionÚformat_optionÚjuju_versionÚ
status_cmdrE   Újuju_json_outputr    r   r   r
   rB   ¦   s*   
ûÿÿÿzjuju._execute_juju_statusc                 C   sF   t ƒ }|D ]}t||ƒ ¡ D ]\}}t ||¡r| |pg ¡ qq|S )zFilter with regex match.)ÚsetÚgetattrr   r=   ÚmatchÚupdate©r   ÚkeyÚpatternsÚ
model_infor#   ÚpatternÚparamÚvaluer   r   r
   Ú_filter_by_pattern¼   s   €þzjuju._filter_by_patternc                 C   sB   t ƒ }|D ]}t||ƒ ¡ D ]\}}||kr| |pg ¡ qq|S )zFilter with fixed match.)rV   rW   r   rY   rZ   r   r   r
   Ú_filter_by_fixedÅ   s   €þzjuju._filter_by_fixedc                 C   s   dS )z'Dynamically change transport to 'juju'.r:   r   )r   r   r   r
   Úset_transport_typeÎ   s   zjuju.set_transport_typec                 C   sÆ   t |  d¡ƒ}t |  d¡ƒ}t |  d¡ƒ}t |  d¡ƒ}|||dœ}t| ¡ ƒs,|s,g S |s1dg}tƒ }|D ](}|  |¡}| ¡ D ]\}	}
|	dkrQ|  |	|
|¡}n|  |	|
|¡}| 	|¡ qAq6t
|ƒS )z+Get the machine numbers from `juju status`.r<   r   r   r   )r   r   r   r;   )r   Ú
get_optionÚanyÚvaluesrV   rD   r   ra   rb   rY   Úlist)r   r<   r   r   r   Úfiltersr#   Úmodelr]   r[   ÚresourceÚ_nodesr   r   r
   Ú	get_nodesÒ   s&   
úzjuju.get_nodesN)r4   r5   r6   r7   rK   Úcluster_nameÚoption_listrA   rD   rF   rB   ra   rb   rc   rl   r   r   r   r
   r:   q   s    		r:   )N)r   rN   r=   Úsos.collector.clustersr   Úsos.utilitiesr   r   r   r9   r:   r   r   r   r
   Ú<module>   s   

V