oB]c@sddlZddlZddlZddlZddlZddlmZddlmZddl m Z ddl m Z ddl m Z ddl mZddl mZdd lmZmZdd lmZe jeZd Zied d 6edd6edd6ZdZdZdZdejjfdYZdZ dZ!de j"fdYZ#e#e j$ffgZ%dZ&dS(iN(tHTTPConnection(t PoolManager(tlog(tsources(t url_helper(tutil(tnet(tEphemeralDHCPv4tNoDHCPLeaseError(t EventTypeshttp://169.254.42.42s/conf?format=jsont metadata_urls/user_data/cloud-initt userdata_urls/vendor_data/cloud-inittvendordata_urlii cCsUtjd}|dkrtStjjdr5tStj}d|krQtStS(s There are three ways to detect if you are on Scaleway: * check DMI data: not yet implemented by Scaleway, but the check is made to be future-proof. * the initrd created the file /var/run/scaleway. * "scaleway" is in the kernel cmdline. ssystem-manufacturertScaleways/var/run/scalewaytscaleway(Rt read_dmi_datatTruetostpathtexistst get_cmdlinetFalse(t vendor_nametcmdline((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceScaleway.pyt on_scaleway2s    tSourceAddressAdaptercBs#eZdZdZedZRS(sF Adapter for requests to choose the local address to bind to. cKs#||_tt|j|dS(N(tsource_addresstsuperRt__init__(tselfRtkwargs((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceScaleway.pyRMs c CsPtjtjtjdfg}td|d|d|d|jd||_dS(Nit num_poolstmaxsizetblockRtsocket_options(Rtdefault_socket_optionstsockett SOL_SOCKETt SO_REUSEPORTRRt poolmanager(Rt connectionsR R!R"((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceScaleway.pytinit_poolmanagerQs  (t__name__t __module__t__doc__RRR)(((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceScaleway.pyRIs c CswyDtj|dd d|ddd|dd}tj|jSWn,tjk rr}|jdkrld SnXd S( sQ Retrieve user data or vendor data. Scaleway user/vendor data API returns HTTP/404 if user/vendor data is not set. This function calls `url_helper.readurl` but instead of considering HTTP/404 as an error that requires a retry, it considers it as empty user/vendor data. Also, be aware the user data/vendor API requires the source port to be below 1024 to ensure the client is root (since non-root users can't bind ports below 1024). If requests raises ConnectionError (EADDRINUSE), the caller should retry to call this function on an other port. tdatattimeouttretriesitsessiont exception_cbcSs&|jdko%t|jtjj S(Ni(tcodet isinstancetcausetrequestst exceptionstConnectionError(t_texc((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceScaleway.pytwsiN(RtreadurltNoneRt decode_binarytcontentstUrlErrorR2(t api_addressR.trequests_sessiontrespR9((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceScaleway.pytquery_data_api_once\s c Csxtdt|dD]}yntjd||tj}|jdtdd|ft|d|d|}tjd ||SWqt j k r}tj d ||t j d |}qqXqW|d S( s/Get user or vendor data. Handle the retrying logic in case the source port is used. Scaleway metadata service requires the source port of the client to be a privileged port (<1024). This is done to ensure that only a privileged user on the system can access the metadata service. iis*Trying to get %s data (bind on port %d)...shttp://Rs0.0.0.0R.RAs%s-data downloadeds%Error while trying to get %s data: %siN(trangetmaxtLOGtdebugR5tSessiontmountRRCRR?twarningttimetsleep( tapi_typeR@R/R.tportRAR-R9tlast_exc((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceScaleway.pytquery_data_apis*      tDataSourceScalewaycBseZdZiejejgd6ZdZdZdZ e dZ e dZ dZ dZeeed Ze d Ze d ZRS( R tnetworkcCstt|j|||tjtj|ddgitg|_|jd|_|jd|_ |jd|_ t |jj dt |_t |jj dt|_d|_d|_dS(Nt datasourceR R R R R/R.(RRQRRt mergemanydicttget_cfg_by_pathtBUILTIN_DS_CONFIGtds_cfgtmetadata_addresstuserdata_addresstvendordata_addresstinttgettDEF_MD_RETRIESR/tDEF_MD_TIMEOUTR.R<t_fallback_interfacet_network_config(Rtsys_cfgtdistrotpaths((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceScaleway.pyRs cCstj|jd|jd|j}tjtj|j |_ t d|j |j|j|_ t d|j|j|j|_dS(NR.R/s user-datas vendor-data(RR;RXR.R/tjsontloadsRR=R>tmetadataRPRYt userdata_rawRZtvendordata_raw(RRB((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceScaleway.pyt_crawl_metadatas    c Csts tS|jdkr.tj|_ny<t|j'tjdt j ddd|j WdQXWn*t k r}tj t t|tSXtS(NtlogfunctmsgsCrawl of metadata servicetfunc(RRR_R<Rtfind_fallback_nicRRtlog_timeRFRGRiRtlogexctstrR(Rte((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceScaleway.pyt _get_datas  cCs|jr|jS|jdkr1tj|_nidd6d|jd6}idd6g}|jdr|idd6d|jddd6d|jdd d 6d|jdd d 6g7}n||d s8         ' )l