f2 ddlZddlZddlZddlmZddlmZddlm Z ddl m Z m Z m Z mZddlmZmZmZddlmZmZddlmZdd lmZmZmZej8eZd d d d dddddddddgdZGdde Z GddejBZ"Gdde"Z#e#ejHffe"ejHejJffgZ&de ejBfdZ'y)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_prefixesceZdZdZdZdZdZy)MetadataAvailabilityResultzj Used to indicate how this instance should behave based on the availability of metadata to it rN)__name__ __module__ __qualname____doc__ NOT_AVAILABLE AVAILABLEDEFERD/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceAkamai.pyr"r"-s MI Er-r"ceZdZdZdZdZddededefdZde fdZ de fd Z de fd Z de eeeefeffd Zddedefd Zdefd ZdefdZy)DataSourceAkamaiAkamaiFctjdtjj ||||t |_tjttj|ddgitg|_ y)NzSetting up Akamai DataSource datasourcer1) LOGdebugr DataSource__init__dictrr mergemanydictrget_cfg_by_pathBUILTIN_DS_CONFIGds_cfg)selfsys_cfgdistrors r.r7zDataSourceAkamai.__init__<sr 01##D'65A (( $$!8, "    r- path_nameuse_v6returnc||jdvrtdj|d}|s|jdsd}|jd|}|jd|}dj||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{}{})r< ValueErrorformat)r=r@rA version_keybase_urlpaths r. _build_urlzDataSourceAkamai._build_urlNs{ DKK0 03::9EF F \2 K;;{+K8{{7#I.}}Xt,,r-c|jds|jdr|jds4|jds%tjdtjS|j r|j S|jS)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.)r<r4infor"r) local_stage_should_fetch_data_local_should_fetch_data_networkr=s r._should_fetch_dataz#DataSourceAkamai._should_fetch_data`sw  L)$++l2K /0KK 23 HHA B-;; ;   002 2224 4r-c|jds%tjdtjS|jds4|jds%tjdtjStj S)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.r<r4rKr"r+r*rOs r.rMz)DataSourceAkamai._should_fetch_data_localtsl {{./ HH@ A-33 3{{<(\1J HHK .33 3)333r-c|jds%tjdtjStj S)zS Returns whether metadata should be fetched during the init stage. rz1Configuration does not allow for init stage setuprRrOs r.rNz+DataSourceAkamai._should_fetch_data_networks6{{-. HHH I-33 3)333r-c g}|jrt}d}|jd}|jD]1\}}t |Dcgc]}|j |c}s/|}n|t jdt}g}|jdr*|jt|j|dddf|jdr8|jd r)|jt|j|d df|S|jdr|jtdf|jdr|jtdf|Scc}w) 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. Nr zGFailed to find default interface, attempting DHCP on fallback interfacerFTrrr)r) rLrr<itemsany startswithr4warningr appendrr?noop)r=network_context_managers interfaces interfacepreferred_prefixesmacinfprefixs r._get_network_context_managersz.DataSourceAkamai._get_network_context_managerss  !    /0JI!%-E!F &,,. S:LMS^^F+M!$I    )./ ') ${{<((//* KK%!&!%   {{<(T[[-F(//* KK%!%   6('!{{<((//{{<((//('oNs E* c ( tj|jd|ddddddi }|jd k7r!tj d |jy t |}tj|jd |dddd|d}tjt ||_ tj|jd|dddd|i}t ||_ t|jj|_ y #tj$r }tjd|Yd}~y d}~wwxYw#tj $r%}tjd|rdnd|Yd}~yd}~wwxYw)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. rrAPUTr$zMetadata-Token-Expiry-Seconds300)request_methodtimeout sec_betweenretriesheadersz-Fetching token returned %s; not fetching dataTrzapplication/json)AcceptMetadata-Token)rjrkrlrmrrpz*Failed to base64 decode userdata due to %sNz1Failed to retrieve metadata using IPv%s due to %s64F)r readurlrIcoder4rKstrjsonloadsr userdata_rawrdecodebinasciiErrorrXUrlError)r=rAtoken_responserrres r._fetch_metadataz DataSourceAkamai._fetch_metadatas : '//7$3U N""c)C"'''E")) 6:0&+ H!JJs8}5DM")) 6:$e H!$H D  M$-d.?.?$@$G$G$I!>> M H!LL M""  KKC3   sCAE BE:(D##E6E EEEF,F  Fcvtjdtstjdyt }d|i|_|j }|tjk7r?|tjk(rtjdytjdy|j}|D]Y\}}|5|j|}|r4|j jd ||j d< d d d y d d d [tjd y#1swY{xYw) 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.rdidNzHFailed to contact metadata service, falling back to local metadata only.)r4r5rrKrrrPr"r*r)rbrgetrX)r=local_instance_id availabilityr[managerrAdones r. _get_datazDataSourceAkamai._get_data's? 78~ HH: ;13 ,  ..0 5?? ?9GGGK HH! #'#E#E#G 7 OGV ++6+:37==3D3D)4DMM-0  &#    KK!  '  s ?D//D8 cJtj|jdS)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)r=r>s r.check_instance_idz"DataSourceAkamai.check_instance_id[s& 66  "$:  r-N)F)r%r&r'dsnamerLr7ruboolrIr"rPrMrNrrr rrrbrrrr,r-r.r0r08s FK $-C--#-$5$>5(4*D4* 4,F 4M( eE#112D89 :M(^BdBtBH242h D r-r0ceZdZdZdZy)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(rLr,r-r.rres Kr-rrBc6tj|tS)N)r list_from_depends datasources)dependss r.get_datasource_listrs  $ $Wk ::r-)(rzrvloggingbase64r contextlibrrZenumrtypingrrrr cloudinitr r r cloudinit.netr rcloudinit.net.ephemeralr cloudinit.sources.helpers.akamairrr getLoggerr%r4r;r"r6r0rDEP_FILESYSTEM DEP_NETWORKrrr,r-r.rs '**//B6 g! ), "#  %0j w))j Z ,W3356   " "      ;D););$<;r-