a '¡Dg 2ã @s.ddlZddlZddlZddlmZddlmZddlm Z ddl m Z m Z m Z mZddlmZmZmZddlmZmZddlmZdd lmZmZmZe e¡Zd d d œd dddœddddddgdœZGdd„de ƒZ Gdd„dej!ƒZ"Gdd„de"ƒZ#e#ej$ffe"ej$ej%ffgZ&e ej!dœdd„Z'dS)éN)Ú b64decode)Úsuppress)ÚEnum)ÚAnyÚListÚTupleÚUnion)ÚsourcesÚ url_helperÚutil)Úfind_fallback_nicÚget_interfaces_by_mac)ÚEphemeralIPNetwork)Úget_dmi_configÚget_local_instance_idÚ is_on_akamaizhttp://169.254.169.254zhttp://[fd00:a9fe:a9fe::1]©Úipv4Úipv6z /v1/tokenz /v1/instancez /v1/user-data)ÚtokenÚmetadataÚuserdataTzf2:3)Ú base_urlsÚpathsÚallow_local_stageÚallow_init_stageÚ allow_dhcpÚ allow_ipv4Ú allow_ipv6Úpreferred_mac_prefixesc@seZdZdZdZdZdZdS)ÚMetadataAvailabilityResultzj Used to indicate how this instance should behave based on the availability of metadata to it rééN)Ú__name__Ú __module__Ú __qualname__Ú__doc__Ú NOT_AVAILABLEÚ AVAILABLEÚDEFER©r*r*úF/usr/lib/python3.9/site-packages/cloudinit/sources/DataSourceAkamai.pyr -sr c@sªeZdZdZdZdd„Zdeeedœdd„Ze dœd d „Z e dœd d „Z e dœd d„Z e eeeefefdœdd„Zdeedœdd„Zedœdd„Zedœdd„ZdS)ÚDataSourceAkamaiÚAkamaiFcCsJt d¡tj ||||¡tƒ|_t t ƒt  |ddgi¡t g¡|_ dS)NzSetting up Akamai DataSourceZ datasourcer-) ÚLOGÚdebugr Ú DataSourceÚ__init__Údictrr Z mergemanydictrZget_cfg_by_pathÚBUILTIN_DS_CONFIGÚds_cfg)ÚselfÚsys_cfgÚdistrorr*r*r+r1<s ýùÿzDataSourceAkamai.__init__)Ú path_nameÚuse_v6ÚreturncCsZ||jdvrtd |¡ƒ‚d}|s.|jds2d}|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{}{})r4Ú ValueErrorÚformat)r5r8r9Z version_keyZbase_urlÚpathr*r*r+Ú _build_urlNszDataSourceAkamai._build_url©r:cCsR|jds|jdr(|jds8|jds8t d¡tjS|jrF| ¡S| ¡SdS)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.N)r4r.Úinfor r'Ú local_stageÚ_should_fetch_data_localÚ_should_fetch_data_network©r5r*r*r+Ú_should_fetch_data`sÿÿýü z#DataSourceAkamai._should_fetch_datacCsD|jdst d¡tjS|jds>|jds>t d¡tjStjS)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.©r4r.r@r r)r(rDr*r*r+rBts  ÿz)DataSourceAkamai._should_fetch_data_localcCs |jdst d¡tjStjS)zS Returns whether metadata should be fetched during the init stage. rz1Configuration does not allow for init stage setuprFrDr*r*r+rC‰s  z+DataSourceAkamai._should_fetch_data_networkcsøg}|jrÀtƒ}d}|jd}| ¡D]&\‰}t‡fdd„|Dƒƒr&|}qNq&|durft d¡tƒ}g}|jdr�| t |j |ddd �df¡|jd rô|jd rô| t |j |dd �df¡n4|jdrÚ| 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)Ú.0Úprefix©Zmacr*r+Ú ªózBDataSourceAkamai._get_network_context_managers..zGFailed to find default interface, attempting DHCP on fallback interfacerFTrrr)r) rAr r4ÚitemsÚanyr.Úwarningr Úappendrr7Únoop)r5Únetwork_context_managersZ interfacesZ interfaceZpreferred_prefixesÚinfr*rJr+Ú_get_network_context_managers”sh þ ÿÿ üùÿ ýúÿ þÿ þÿz.DataSourceAkamai._get_network_context_managers)r9r:c Cs:zòtj|jd|d�ddddddid �}|jd krDt d |j¡Wd St|ƒ}tj|jd |d�dddd|dœd�}t t|ƒ¡|_ tj|jd|d�dddd|id�}t|ƒ|_ zt |j ƒ|_ Wn2t j yî}zt d|¡WYd}~n d}~00WnBtj�y4}z&t d|�rdnd|¡WYd}~dSd}~00d S)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. r©r9ZPUTér"ézMetadata-Token-Expiry-SecondsZ300)Zrequest_methodÚtimeoutÚ sec_betweenÚretriesÚheaderséÈz-Fetching token returned %s; not fetching dataTrzapplication/json)ZAcceptúMetadata-Token)rXrYrZr[rr]z*Failed to base64 decode userdata due to %sNz1Failed to retrieve metadata using IPv%s due to %sÚ6Ú4F)r Zreadurlr>Úcoder.r@ÚstrÚjsonÚloadsrZ userdata_rawrÚbinasciiÚErrorrOZUrlError)r5r9Ztoken_responserrrÚer*r*r+Ú_fetch_metadataãs` ÿú þ þû  ÿû & ýz DataSourceAkamai._fetch_metadatac Csât d¡tƒst d¡dStƒ}d|i|_| ¡}|tjkrf|tj krXt d¡dSt d¡dS|  ¡}|D]`\}}|�D|j |d�}|r´|j  d |¡|jd<Wd ƒqÞWd ƒqr1sÈ0Yqrt  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.rUÚidNzHFailed to contact metadata service, falling back to local metadata only.)r.r/rr@rrrEr r(r'rTrgÚgetrO)r5Zlocal_instance_idZ availabilityrRZmanagerr9Zdoner*r*r+Ú _get_data's@  ÿ  ÿÿ  þ 0ÿzDataSourceAkamai._get_datacCst | ¡d¡S)zj A local-only check to see if the instance id matches the id we see on the system zsystem-serial-number)r Zinstance_id_matches_system_uuidZget_instance_id)r5r6r*r*r+Úcheck_instance_id[sÿz"DataSourceAkamai.check_instance_idN)F)F)r#r$r%ZdsnamerAr1raÚboolr>r rErBrCrrrrrrTrgrjrkr*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&rAr*r*r*r+rmesrmr?cCs t |t¡S)N)r Zlist_from_dependsÚ datasources)Zdependsr*r*r+Úget_datasource_listsro)(rdrbZloggingÚbase64rÚ contextlibrrQÚenumrÚtypingrrrrZ cloudinitr r r Z cloudinit.netr r Zcloudinit.net.ephemeralrZ cloudinit.sources.helpers.akamairrrZ getLoggerr#r.r3r r0r,rmZDEP_FILESYSTEMZ DEP_NETWORKrnror*r*r*r+ÚsN     þýÿî / þþü