o
    Ìv›a›  ã                
   @   s¤   d Z ddlZddlmZmZ ddlmZ ddlmZ zddl	Z	W n e
y< Z zdZe d e¡¡ W Y dZ[ndZ[ww dZG d	d
„ d
eƒZG dd„ dejƒZdS )zkCloud plugin.

Supported Cloud API:
- OpenStack meta data (class ThreadOpenStack, see bellow): AWS, OVH...
é    N)Ú	iteritemsÚto_ascii)ÚGlancesPlugin)ÚloggerTz1Missing Python Lib ({}), Cloud plugin is disabledFc                       sL   e Zd ZdZd‡ fdd„	Z‡ fdd„Zejejdd„ ƒƒZ	dd	d
„Z
‡  ZS )ÚPluginzèGlances' cloud plugin.

    The goal of this plugin is to retreive additional information
    concerning the datacenter where the host is connected.

    See https://github.com/nicolargo/glances/issues/1029

    stats is a dict
    Nc                    s8   t t| ƒj||d d| _|  ¡  tƒ | _| j ¡  dS )zInit the plugin.)ÚargsÚconfigTN)Úsuperr   Ú__init__Zdisplay_curseÚresetÚThreadOpenStackÚ	OPENSTACKÚstart)Úselfr   r   ©Ú	__class__© ú?/usr/lib/python3/dist-packages/glances/plugins/glances_cloud.pyr
   6   s
   zPlugin.__init__c                    s   | j  ¡  tt| ƒ ¡  dS )z+Overwrite the exit method to close threads.N)r   Ústopr	   r   Úexit©r   r   r   r   r   F   s   
zPlugin.exitc                 C   s.   |   ¡ }tr|S | jdkr| jj}|| _| jS )zAUpdate the cloud stats.

        Return the stats (dict)
        Zlocal)Zget_init_valueÚimport_error_tagZinput_methodr   Ústats)r   r   r   r   r   ÚupdateL   s   
	zPlugin.updatec                 C   sˆ   g }| j r| j i ks|  ¡ r|S d| j v rBd| j v rBd| j v rBd}| |  |d¡¡ d | j d | j d | j d ¡}| |  |¡¡ |S )z4Return the string to display in the curse interface.úinstance-typeúinstance-idÚregionzCloud ZTITLEz{} instance {} ({}))r   Zis_disabledÚappendZcurse_add_lineÚformat)r   r   Z	max_widthÚretÚmsgr   r   r   Ú	msg_cursei   s    
ÿþþzPlugin.msg_curse)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r   Z_check_decoratorZ_log_result_decoratorr   r!   Ú__classcell__r   r   r   r   r   +   s    
r   c                       sf   e Zd ZdZdZdddddœZ‡ fdd	„Zd
d„ Zedd„ ƒZ	e	j
dd„ ƒZ	ddd„Zdd„ Z‡  ZS )r   zG
    Specific thread to grab OpenStack stats.

    stats is a dict
    z'http://169.254.169.254/latest/meta-dataúami-idr   r   zplacement/availability-zone)r'   r   r   r   c                    s,   t  d¡ tt| ƒ ¡  t ¡ | _i | _dS )zInit the class.z3cloud plugin - Create thread for OpenStack metadataN)	r   Údebugr	   r   r
   Ú	threadingZEventÚ_stopperÚ_statsr   r   r   r   r
      s   


zThreadOpenStack.__init__c                 C   sš   t r|  ¡  dS t| jƒD ]=\}}d | j|¡}z	tj|dd}W n ty> } zt	 
d ||¡¡ W Y d}~ dS d}~ww |jrJt|jƒ| j|< qdS )zdGrab plugin's stats.

        Infinite loop, should be stopped by calling the stop() method
        Fz{}/{}é   )ÚtimeoutzBcloud plugin - Cannot connect to the OpenStack metadata API {}: {}NT)r   r   r   ÚOPENSTACK_API_METADATAr   ÚOPENSTACK_API_URLÚrequestsÚgetÚ	Exceptionr   r(   Úokr   Zcontentr+   )r   ÚkÚvZr_urlÚrÚer   r   r   Úrun˜   s"   €ù€zThreadOpenStack.runc                 C   s   | j S )zStats getter.©r+   r   r   r   r   r   ¯   s   zThreadOpenStack.statsc                 C   s
   || _ dS )zStats setter.Nr9   )r   Úvaluer   r   r   r   ´   s   
Nc                 C   s   t  d¡ | j ¡  dS )zStop the thread.z2cloud plugin - Close thread for OpenStack metadataN)r   r(   r*   Úset)r   r-   r   r   r   r   ¹   s   
zThreadOpenStack.stopc                 C   s
   | j  ¡ S )z%Return True is the thread is stopped.)r*   ZisSetr   r   r   r   Ústopped¾   s   
zThreadOpenStack.stopped)N)r"   r#   r$   r%   r/   r.   r
   r8   Úpropertyr   Úsetterr   r<   r&   r   r   r   r   r      s     ý	


r   )r%   r)   Zglances.compatr   r   Zglances.plugins.glances_pluginr   Zglances.loggerr   r0   ÚImportErrorr7   r   Zwarningr   r   ZThreadr   r   r   r   r   Ú<module>   s   €ýV