o x[h2 @s.ddlZddlZddlZddlmZddlmZddlm Z ddl m Z m Z m Z mZddlmZmZmZddlmZmZddlmZdd lmZmZmZeeZd d d d dddddddddgdZGddde Z Gdddej!Z"Gddde"Z#e#ej$ffe"ej$ej%ffgZ&de ej!fddZ'dS)N) b64decode)suppress)Enum)AnyListTupleUnion)sources url_helperutil)find_fallback_nicget_interfaces_by_mac)EphemeralIPNetwork)get_dmi_configget_local_instance_id is_on_akamaizhttp://169.254.169.254zhttp://[fd00:a9fe:a9fe::1]ipv4ipv6z /v1/tokenz /v1/instancez /v1/user-data)tokenmetadatauserdataTzf2:3) base_urlspathsallow_local_stageallow_init_stage allow_dhcp allow_ipv4 allow_ipv6preferred_mac_prefixesc@seZdZdZdZdZdZdS)MetadataAvailabilityResultzj Used to indicate how this instance should behave based on the availability of metadata to it rN)__name__ __module__ __qualname____doc__ NOT_AVAILABLE AVAILABLEDEFERr*r*D/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceAkamai.pyr -s r c@seZdZdZdZddZddededefdd Zde fd d Z de fd d Z de fddZ de eeeefeffddZddedefddZdefddZdefddZdS)DataSourceAkamaiAkamaiFcCsJtdtj||||t|_tt t |ddgit g|_ dS)NzSetting up Akamai DataSource datasourcer-) LOGdebugr DataSource__init__dictrr mergemanydictrget_cfg_by_pathBUILTIN_DS_CONFIGds_cfg)selfsys_cfgdistrorr*r*r+r2<s  zDataSourceAkamai.__init__ path_nameuse_v6returncCsZ||jdvrtd|d}|s|jdsd}|jd|}|jd|}d||S)z Looks up the path for a given name and returns a full url for it. If use_v6 is passed in, the IPv6 base url is used; otherwise the IPv4 url is used unless IPv4 is not allowed in ds_cfg rzUnknown path name {}rrrrz{}{})r7 ValueErrorformat)r8r;r< version_keybase_urlpathr*r*r+ _build_urlNs zDataSourceAkamai._build_urlcCsN|jds |jdr|jds|jdstdtjS|jr#|S|S)z Returns whether metadata should be retrieved at this stage, at the next stage, or never, in the form of a MetadataAvailabilityResult. rrrrz*Configuration prohibits fetching metadata.)r7r/infor r' local_stage_should_fetch_data_local_should_fetch_data_networkr8r*r*r+_should_fetch_data`s z#DataSourceAkamai._should_fetch_datacCsD|jds tdtjS|jds|jdstdtjStjS)z Returns whether metadata should be retrieved during the local stage, or if it should wait for the init stage. rz)Configuration prohibits local stage setuprrz9Configuration does not allow for ephemeral network setup.r7r/rDr r)r(rHr*r*r+rFts  z)DataSourceAkamai._should_fetch_data_localcCs |jds tdtjStjS)zS Returns whether metadata should be fetched during the init stage. rz1Configuration does not allow for init stage setuprJrHr*r*r+rGs  z+DataSourceAkamai._should_fetch_data_networkcsg}|jrat}d}|jd}|D]\}tfdd|Dr&|}nq|dur3tdt}g}|jdrH|t |j |ddd df|jd r_|jd r_|t |j |dd df|S|jdrn|t df|jd r{|t df|S) z Returns a list of context managers which should be tried when setting up a network context. If we're running in init mode, this return a noop since networking should already be configured. Nrcsg|]}|qSr*) startswith).0prefixmacr*r+ szBDataSourceAkamai._get_network_context_managers..zGFailed to find default interface, attempting DHCP on fallback interfacerFTrrr)r) rEr r7itemsanyr/warningr appendrr:noop)r8network_context_managers interfaces interfacepreferred_prefixesinfr*rNr+_get_network_context_managerssl      z.DataSourceAkamai._get_network_context_managersc Cs6z{tj|jd|dddddddid }|jd kr"td |jWd St|}tj|jd |ddddd|dd}tt||_ tj|jd|ddddd|id}t||_ z t |j |_ WWd St j y{}ztd|WYd}~Wd Sd}~wwtjy}ztd|rdnd|WYd}~dSd}~ww)z Runs through the sequence of requests necessary to retrieve our metadata and user data, creating a token for use in doing so, capturing the results. rr<PUTr"zMetadata-Token-Expiry-Seconds300)request_methodtimeout sec_betweenretriesheadersz-Fetching token returned %s; not fetching dataTrzapplication/json)AcceptMetadata-Token)rbrcrdrerrhz*Failed to base64 decode userdata due to %sNz1Failed to retrieve metadata using IPv%s due to %s64F)r readurlrCcoder/rDstrjsonloadsr userdata_rawrbinasciiErrorrSUrlError)r8r<token_responserrrer*r*r+_fetch_metadatasj        z DataSourceAkamai._fetch_metadatac CstdtstddSt}d|i|_|}|tjkr3|tj kr,tddStddS| }|D]2\}}|$|j |d}|r\|j d ||jd< Wd dSWd n1sfwYq9t d dS) zW Overrides _get_data in the DataSource class to actually retrieve data z#Getting data from Akamai DataSourcez#Not running on Akamai, not running.Fz instance-idz5Metadata is not available, returning local data only.TzFConfigured not to fetch data at this stage; waiting for a later stage.r\idNzHFailed to contact metadata service, falling back to local metadata only.)r/r0rrDrrrIr r(r'r[rvgetrS)r8local_instance_id availabilityrVmanagerr<doner*r*r+ _get_data'sJ       zDataSourceAkamai._get_datacCst|dS)zj A local-only check to see if the instance id matches the id we see on the system zsystem-serial-number)r instance_id_matches_system_uuidget_instance_id)r8r9r*r*r+check_instance_id[sz"DataSourceAkamai.check_instance_idN)F)r#r$r%dsnamerEr2rmboolrCr rIrFrGrrrrrr[rvr}rr*r*r*r+r,8s  OD4r,c@seZdZdZdZdS)DataSourceAkamaiLocalz A subclass of DataSourceAkamai that runs the same functions, but during the init-local stage. This allows configuring networking via cloud-init, as networking hasn't been configured yet. TN)r#r$r%r&rEr*r*r*r+resrr=cCs t|tS)N)r list_from_depends datasources)dependsr*r*r+get_datasource_lists r)(rqrnloggingbase64r contextlibrrUenumrtypingrrrr cloudinitr r r cloudinit.netr r cloudinit.net.ephemeralr cloudinit.sources.helpers.akamairrr getLoggerr#r/r6r r1r,rDEP_FILESYSTEM DEP_NETWORKrrr*r*r*r+sP      /