o
    w7e:                  
   @   s   U 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 d dlmZmZ d dlmZ d d	lmZ d
ZdZ	 dZd
ddeegeg g dZeed< eeZe eZdedede	deddf
ddZdS )    N)parse_qs)
url_helper)util)Cloud)Config)
MetaSchemaget_meta_doc)ALL_DISTROS)PER_INSTANCEcc_rightscale_userdataCLOUD_INIT_REMOTE_HOOKaI  This module adds support for RightScale configuration hooks to cloud-init.
RightScale adds an entry in the format ``CLOUD_INIT_REMOTE_HOOK=http://...`` to
ec2 user-data. This module checks for this line in the raw userdata and
retrieves any scripts linked by the RightScale user data and places them in the
user scripts configuration directory, to be run later by ``cc_scripts_user``.

.. note::
    the ``CLOUD_INIT_REMOTE_HOOK`` config variable is present in the raw ec2
    user data only, not in any cloud-config parts

**Raw user data schema**::

    CLOUD_INIT_REMOTE_HOOK=<url>
zRightScale Userdataz&Support rightscale configuration hooks)idnametitledescriptiondistros	frequencyexamplesactivate_by_schema_keysmetar   cfgcloudargsreturnc                 C   s  t |dd }|rt|std|  d S | }zt|}|r"t|vr,td| t W d S W n ty<   ttd|  w g }g }|	d}	|t }
t
|
D ]H\}}tj|	d| }zt|}| ru|jrutj||dd || W qN ty } z|| ttd	t|| W Y d }~qNd }~ww |rtd
t| t|t|
krt|
t| }td| |rtdt| |d d S )Nget_userdata_rawz'Failed to get raw userdata in module %sz:Skipping module %s, did not find %s in parsed raw userdataz1Failed to parse query string %s into a dictionaryscriptszrightscale-%02ii  )modez!%s failed to read %s and write %sz)Wrote out rightscale userdata to %s filesz%s urls were skipped or failedz2%s failed with exceptions, re-raising the last one)getattrcallableLOGdebugr   MY_HOOKNAME	Exceptionr   logexcget_ipath_cur	enumerateospathjoinuhelpreadurlokcontents
write_fileappendMY_NAMElenwarning)r   r   r   r   r   udmdict	wrote_fnscaptured_excps	scripts_durlsiurlfnamerespeskipped r?   I/usr/lib/python3/dist-packages/cloudinit/config/cc_rightscale_userdata.pyhandleJ   sh   




rA   )loggingr'   urllib.parser   	cloudinitr   r*   r   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar   r   cloudinit.distrosr	   cloudinit.settingsr
   r0   r"   MODULE_DESCRIPTIONr   __annotations____doc__	getLogger__name__r    strlistrA   r?   r?   r?   r@   <module>   s4   

"