HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/sos/collector/clusters/__pycache__/juju.cpython-310.pyc
o

i��d� �@sJddlZddlZddlZddlmZd	dd�Zdd�ZGdd�de�ZdS)
�N)�ClustercCs|sgSdd�|�d�D�S)zParse comma separated string.cSsg|]}|���qS�)�strip)�.0�stringrr�=/usr/lib/python3/dist-packages/sos/collector/clusters/juju.py�
<listcomp>sz(_parse_option_string.<locals>.<listcomp>�,)�split)�stringsrrr�_parse_option_stringsrcCsGdd�d�}||�S)aRHelper 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@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
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}.
        cSs(||_i|_i|_i|_t�d�|_dS)N�sos)�
model_name�apps�units�machines�logging�	getLogger�ui_log)�selfrrrr�__init__,s
z"_get_index.<locals>.Index.__init__c
Ss�|d��D]7\}}g}|�di�}|��D]!\}}|d}|j�d|��}	|	g|j|<|	g|j|<|�|	�q||j|<qdS)zAdds principal units to index.�applicationsr�machine�:N)�items�getrrr�appendr)
r�juju_status�app�app_info�nodesr�unit�	unit_infor�noderrr�add_principals3s�z(_get_index.<locals>.Index.add_principalscSs�|d��D]s\}}|�dg�}|D]f}|j�|�s%|j�d|�d��q|j|�|j|�d|d|vrC|j�d|�d��q|d|d}|��D](\}}|j�d|d��}	|�d	i���D]\}
}|
�|d
�rv|	g|j|
<qeqOqqdS)z�Add subordinates to index.

            Since subordinates does not have units they need to be
            manually added.
            rzsubordinate-tozPrincipal charm z is missingrz is missing unitsrr�subordinates�/N)	rrrr�warning�extendr�
startswithr)rrrr�subordinate_to�parentrr!r"r#�sub_key�	sub_valuerrr�add_subordinatesAs:
�
��
������z*_get_index.<locals>.Index.add_subordinatescSs2|d��D]}|j�d|��}|g|j|<qdS)z�Add machines to index.

            If model does not have any applications it needs to be
            manually added.
            rrN)�keysrr)rrrr#rrr�add_machinesbs�z&_get_index.<locals>.Index.add_machinesN)�__name__�
__module__�__qualname__�__doc__rr$r.r0rrrr�Index#s!r5r)rr5rrr�
_get_indexs
Ir6c@sXeZdZdZdZdZgd�Zdd�Zdd�Zdd	�Z	d
d�Z
dd
�Zdd�Zdd�Z
dS)�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"

    zJuju Managed Clouds))r�z1Filter node list by apps (comma separated regex).)rr8z3Filter node list by units (comma separated string).)�modelsr8z4Filter node list by models (comma separated string).)rr8z6Filter node list by machines (comma separated string).cCst�dd|dtj�S)z#Remove leading characters before {.z(^[^{]*)(.*)z\2r)�re�sub�	MULTILINE)r�outputrrr�_cleanup_juju_output�szjuju._cleanup_juju_outputcCs6|�|�}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_statusr6r$r.r0)rrr�indexrrr�_get_model_info�s




zjuju._get_model_infocCs�|rd|��nd}d}|j�d|�d|��}|�|�}|ddks-td|�d	|d����|�|d
�}d}zt�|�}W|StjyMtd|����w)Nz-m r8z
--format jsonz status � �statusr�'z' returned error: r=z-Juju output is not valid json format.Output: )�cmd�exec_primary_cmd�	Exceptionr>�json�loads�JSONDecodeError)rr�model_option�
format_option�
status_cmd�res�juju_json_outputrrrrr?�s$
����zjuju._execute_juju_statuscCsFt�}|D]}t||���D]\}}t�||�r|�|pg�qq|S)zFilter with regex match.)�set�getattrrr:�match�update�r�key�patterns�
model_infor �pattern�param�valuerrr�_filter_by_pattern�s��zjuju._filter_by_patterncCsBt�}|D]}t||���D]\}}||kr|�|pg�qq|S)zFilter with fixed match.)rPrQrrSrTrrr�_filter_by_fixed�s��zjuju._filter_by_fixedcCsdS)z'Dynamically change transport to 'juju'.r7r)rrrr�set_transport_type�szjuju.set_transport_typecCs�t|�d��}t|�d��}t|�d��}t|�d��}|||d�}t|���s,|s,gS|s1dg}t�}|D](}|�|�}|��D]\}	}
|	dkrQ|�|	|
|�}n|�|	|
|�}|�	|�qAq6t
|�S)z+Get the machine numbers from `juju status`.r9rrr)rrrr8)r�
get_option�any�valuesrPrArr[r\rS�list)rr9rrr�filtersr �modelrWrU�resource�_nodesrrr�	get_nodes�s&
�zjuju.get_nodesN)r1r2r3r4rE�cluster_name�option_listr>rAr?r[r\r]rfrrrrr7os		r7)N)rrHr:�sos.collector.clustersrrr6r7rrrr�<module>s

V