Sindbad~EG File Manager

Current Path : /lib/python3.6/site-packages/sos/collector/clusters/__pycache__/
Upload File :
Current File : //lib/python3.6/site-packages/sos/collector/clusters/__pycache__/juju.cpython-36.pyc

3

�fo"�@sbddlZddlZddlZddlmZddlmZddlmZddd�Zdd�Z	Gd	d
�d
e�Z
dS)�N)�Cluster)�sos_parse_version)�sos_get_command_outputcCs|sgSdd�|jd�D�S)zParse comma separated string.cSsg|]}|j��qS�)�strip)�.0�stringrr�/usr/lib/python3.6/juju.py�
<listcomp>sz(_parse_option_string.<locals>.<listcomp>�,)�split)Zstringsrrr	�_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|_tjd�|_dS)NZsos)�
model_name�apps�units�machines�loggingZ	getLogger�ui_log)�selfrrrr	�__init__.s
z"_get_index.<locals>.Index.__init__c
Ss�x�|dj�D]r\}}g}|jdi�}xN|j�D]B\}}|d}|j�d|��}	|	g|j|<|	g|j|<|j|	�q0W||j|<qWdS)zAdds principal units to index.�applicationsr�machine�:N)�items�getrrr�appendr)
r�juju_status�app�app_info�nodesr�unit�	unit_infor�noderrr	�add_principals5sz(_get_index.<locals>.Index.add_principalscSs
�x|dj�D]�\}}|jdg�}x�|D]�}|jj|�sP|jjd|�d��q*|j|j|j|�d|d|kr�|jjd|�d��q*|d|d}x`|j�D]T\}}|j�d|d��}	x6|jd	i�j�D]"\}
}|
j|d
�r�|	g|j|
<q�Wq�Wq*WqWdS)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 unitsrrZsubordinates�/N)	rrrrZwarning�extendr�
startswithr)rrrrZsubordinate_to�parentrr r!r"Zsub_keyZ	sub_valuerrr	�add_subordinatesCs&
z*_get_index.<locals>.Index.add_subordinatescSs6x0|dj�D] }|j�d|��}|g|j|<qWdS)z�Add machines to index.

            If model does not have any applications it needs to be
            manually added.
            rrN)�keysrr)rrrr"rrr	�add_machinesdsz&_get_index.<locals>.Index.add_machinesN)�__name__�
__module__�__qualname__�__doc__rr#r(r*rrrr	�Index%s
!r/r)rr/rrr	�
_get_indexs
Ir0c@sdeZdZdZdZdZdddd gZdd
�Zdd�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�ZdS)!�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 Cloudsr��1Filter node list by apps (comma separated regex).r�3Filter node list by units (comma separated string).�models�4Filter node list by models (comma separated string).r�6Filter node list by machines (comma separated string).cCstjdd|dtj�S)z#Remove leading characters before {.z(^[^{]*)(.*)z\2r)�re�sub�	MULTILINE)r�outputrrr	�_cleanup_juju_output�szjuju._cleanup_juju_outputcCs6|j|�}t|d�}|j|�|j|�|j|�|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_statusr0r#r(r*)rrr�indexrrr	�_get_model_info�s




zjuju._get_model_infocCstd�}|dS)zGrab the version of jujuzjuju versionr;)r)r�resrrr	�_get_juju_version�szjuju._get_juju_versionc	Cs�|rd|��nd}d}|j�}t|�td�kr6|d7}|j�d|�d|��}|j|�}|dd	ksztd
|�d|d����|j|d�}d}ytj|�}Wn$tjk
r�td
|����YnX|S)Nz-m r2z
--format json�3z --no-colorz status � Zstatusr�'z' returned error: r;z-Juju output is not valid json format.Output: )	rAr�cmdZexec_primary_cmd�	Exceptionr<�json�loadsZJSONDecodeError)	rrZmodel_optionZ
format_optionZjuju_versionZ
status_cmdr@Zjuju_json_outputrrrr	r=�s"
zjuju._execute_juju_statuscCsNt�}xB|D]:}x4t||�j�D]"\}}tj||�r |j|p>g�q WqW|S)zFilter with regex match.)�set�getattrrr8�match�update)r�key�patterns�
model_infor�pattern�param�valuerrr	�_filter_by_pattern�s
zjuju._filter_by_patterncCsJt�}x>|D]6}x0t||�j�D]\}}||kr |j|p:g�q WqW|S)zFilter with fixed match.)rIrJrrL)rrMrNrOrrPrQrRrrr	�_filter_by_fixed�s
zjuju._filter_by_fixedcCsdS)z'Dynamically change transport to 'juju'.r1r)rrrr	�set_transport_type�szjuju.set_transport_typecCs�t|jd��}t|jd��}t|jd��}t|jd��}|||d�}t|j��r\|r\gS|sfdg}t�}x\|D]T}|j|�}xD|j�D]8\}	}
|	dkr�|j|	|
|�}n|j|	|
|�}|j	|�q�WqrWt
|�S)z+Get the machine numbers from `juju status`.r5rrr)rrrr2)r
Z
get_option�any�valuesrIr?rrSrTrL�list)rr5rrr�filtersrZmodelrOrMZresourceZ_nodesrrr	�	get_nodes�s$

zjuju.get_nodesN)rr2r3)rr2r4)r5r2r6)rr2r7)r+r,r-r.rEZcluster_nameZoption_listr<r?rAr=rSrTrUrZrrrr	r1qs"		r1)N)rrGr8Zsos.collector.clustersrZ
sos.utilitiesrrr
r0r1rrrr	�<module>s
V

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists