U
    LdB                  	   @   s   U d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZ ddlmZ d	Zd
gZdddeeeedgdgdZeed< eeZ eeZdddZdd Zeee	eddddZdS )z$Fan: Configure ubuntu fan networking    )dedent)log)subputil)Cloud)Config)
MetaSchemaget_meta_doc)PER_INSTANCEa  This module installs, configures and starts the ubuntu fan network system. For
more information about Ubuntu Fan, see:
``https://wiki.ubuntu.com/FanNetworking``.

If cloud-init sees a ``fan`` entry in cloud-config it will:

    - write ``config_path`` with the contents of the ``config`` key
    - install the package ``ubuntu-fan`` if it is not installed
    - ensure the service is started (or restarted if was previously running)

Additionally, the ``ubuntu-fan`` package will be automatically installed
if not present.
ZubuntuZcc_fanZFanzConfigure ubuntu fan networkinga              fan:
              config: |
                # fan 240
                10.0.0.0/8 eth0/16 dhcp
                10.0.0.0/8 eth1/16 dhcp off
                # fan 241
                241.0.0.0/8 eth0/16 dhcp
              config_path: /etc/network/fan
            fan)idnametitleZdescriptiondistrosZ	frequencyZexamplesZactivate_by_schema_keysmetaNz/etc/network/fan)configconfig_pathc              
   C   s   z|  d| d}W n6 tjk
rJ } zd}td|| W 5 d }~X Y nX |ds^|d7 }tj||dd z |  d| |rtd	| W n2 tjk
r } ztd
|| W 5 d }~X Y nX |  d| d S )NstopFTzfailed to stop %s: %s
wZomodestartzsuccess: %s startedzfailed to start %s: %senable)Zmanage_servicer   ZProcessExecutionErrorLOGZwarningendswithr   
write_file)distroserviceconfig_filecontentZstop_failede r!   9/usr/lib/python3/dist-packages/cloudinit/config/cc_fan.pystop_update_startC   s      
 r#   )r   cfgcloudargsreturnc                 C   s   | d}|si }t|tg}| ds:td|  d S tj| d| ddd |j}t	dsr|
dg t|d| d| dd	 d S )
Nr   r   z$%s: no 'fan' config entry. disablingr   r   r   Zfanctlz
ubuntu-fan)r   r   r   )getr   ZmergemanydictBUILTIN_CFGr   debugr   r   r   ZwhichZinstall_packagesr#   )r   r$   r%   r&   ZcfginZmycfgr   r!   r!   r"   handleY   s"    


r+   )__doc__textwrapr   Z	cloudinitr   Zloggingr   r   Zcloudinit.cloudr   Zcloudinit.configr   Zcloudinit.config.schemar   r	   Zcloudinit.settingsr
   ZMODULE_DESCRIPTIONr   r   __annotations__Z	getLogger__name__r   r)   r#   strlistr+   r!   r!   r!   r"   <module>   s8   
