U
    Ad                  	   @   s  U d 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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ZdZeeZdZdgZdddeeedgedgdZeed< eeZ eeefddZ e!eeee"ddddZ#dS )z6 Mcollective: Install, configure and start mcollective    N)Logger)dedent)	ConfigObj)log)subputil)Cloud)Config)
MetaSchemaget_meta_doc)PER_INSTANCEz&/etc/mcollective/ssl/server-public.pemz'/etc/mcollective/ssl/server-private.pemz/etc/mcollective/server.cfga  This module installs, configures and starts mcollective. If the ``mcollective``
key is present in config, then mcollective will be installed and started.

Configuration for ``mcollective`` can be specified in the ``conf`` key under
``mcollective``. Each config value consists of a key value pair and will be
written to ``/etc/mcollective/server.cfg``. The ``public-cert`` and
``private-cert`` keys, if present in conf may be used to specify the public and
private certificates for mcollective. Their values will be written to
``/etc/mcollective/ssl/server-public.pem`` and
``/etc/mcollective/ssl/server-private.pem``.

.. note::
    The ec2 metadata service is readable by non-root users.
    If security is a concern, use include-once and ssl urls.
allZcc_mcollectiveZMcollectivez(Install, configure and start mcollectivea              # Provide server private and public key and provide the following
            # config settings in /etc/mcollective/server.cfg:
            # loglevel: debug
            # plugin.stomp.host: dbhost

            # WARNING WARNING WARNING
            # The ec2 metadata service is a network service, and thus is
            # readable by non-root users on the system
            # (ie: 'ec2metadata --user-data')
            # If you want security for this, please use include-once + SSL urls
            mcollective:
              conf:
                loglevel: debug
                plugin.stomp.host: dbhost
                public-cert: |
                    -------BEGIN CERTIFICATE--------
                    <cert data>
                    -------END CERTIFICATE--------
                private-cert: |
                    -------BEGIN CERTIFICATE--------
                    <cert data>
                    -------END CERTIFICATE--------
            mcollective)idnametitleZdescriptiondistrosZexamplesZ	frequencyZactivate_by_schema_keysmetac              
   C   s  z"t j|ddd}tt|}W nD tk
rf } z&|jtjkrD nt	d| t }W 5 d }~X Y nX | 
 D ]\}}|dkrt j||dd ||d< d|d	< qp|d
krt j||dd ||d< d|d	< qpt|tr|||< qpt|tr ||jkri ||< |
 D ]\}	}
|
|| |	< qqpt|||< qpzt |d|  W n6 tk
rx } z|jtjkrfn W 5 d }~X Y nX t }|| t j|| dd d S )NF)quietdecodez4Did not find file %s (starting with an empty config)zpublic-certi  )modezplugin.ssl_server_publicZsslZsecurityproviderzprivate-certi  zplugin.ssl_server_privatez%s.old)r   Z	load_filer   ioBytesIOIOErrorerrnoZENOENTLOGdebugitemsZ
write_file
isinstancestrdictZsectionscopywritegetvalue)configZ
server_cfgZpubcert_fileZpricert_fileZold_contentsZmcollective_configeZcfg_namecfgovcontents r*   A/usr/lib/python3/dist-packages/cloudinit/config/cc_mcollective.py	configure^   sJ    





r,   )r   r&   cloudr   argsreturnc                 C   sZ   d|kr| d|  d S |d }|jd d|krBt|d d tjdddgdd	 d S )
Nr   z?Skipping module named %s, no 'mcollective' key in configuration)r   conf)r$   serviceZrestartF)Zcapture)r   ZdistroZinstall_packagesr,   r   )r   r&   r-   r   r.   Zmcollective_cfgr*   r*   r+   handle   s    r2   )$__doc__r   r   Zloggingr   textwrapr   Z	configobjr   Z	cloudinitr   r   r   Zcloudinit.cloudr   Zcloudinit.configr	   Zcloudinit.config.schemar
   r   Zcloudinit.settingsr   ZPUBCERT_FILEZPRICERT_FILEZ
SERVER_CFGZ	getLogger__name__r   ZMODULE_DESCRIPTIONr   r   __annotations__r,   r   listr2   r*   r*   r*   r+   <module>
   sR   
&
>    