
    fK                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
mZ d dlmZmZmZmZmZ d dlmZmZmZmZ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 dl&m'Z' d dl(m)Z) dZ*dZ+dZ,dZ-e*e+e,gZ.dZ/dZ0dZ1dZ2dZ3dZ4dZ5dZ6 ejn                  e8      Z9dd fdd fdd  fd!Z:e G d" d#e
             Z; G d$ d%e<      Z= G d& d'e<      Z> G d( d)e<      Z?dEd*Z@e3fd+ZA ed,g d-      ZB ed.d/d0g      ZC G d1 de'e j                  2      ZEd3 ZFd4eeEeGf   fd5ZHd6 ZI	 dFd7eGd4eJfd8ZKd9 ZLdGd:ZM G d; d<eN      ZOd= ZPd>eEd?eGd4eJfd@ZQd?eGd4eeE   fdAZRd4eGfdBZSdCeGd4eGfdDZTy)H    N)
namedtuple)Enumunique)AnyDictListOptionalTuple)atomic_helperdmiimporternet
type_utils)	user_data)util)
write_json)Distro)
EventScope	EventType)launch_index)Paths)CloudInitPickleMixin)eventsdisabledlocalr   pass
FILESYSTEMNETWORK
DataSourcez|EXPERIMENTAL: The structure and format of content scoped under the 'ds' key may change in subsequent releases of cloud-init.zredacted for non-root user
cloud-name_unsetunknownz	aws-chinac                     | dk(  S Naws cs    </usr/lib/python3/dist-packages/cloudinit/sources/__init__.py<lambda>r*   ?   s
    1:     zaws-govc                     | dk(  S r$   r&   r'   s    r)   r*   r*   @   s
    Q%Z r+   zazure-chinac                     | dk(  S )Nazurer&   r'   s    r)   r*   r*   A   s
    qG| r+   )zcn-zus-gov-chinac                   0    e Zd ZdZdZdZdZdZdZde	fdZ
y	)
NetworkConfigSourcezb
    Represents the canonical list of network config sources that cloud-init
    knows about.
    cmdlineds
system_cfgfallback	initramfsreturnc                     | j                   S N)valueselfs    r)   __str__zNetworkConfigSource.__str__R   s    zzr+   N)__name__
__module____qualname____doc__CMD_LINEDS
SYSTEM_CFGFALLBACK	INITRAMFSstrr=   r&   r+   r)   r1   r1   E   s.    
 H	BJHI r+   r1   c                       e Zd ZdZy)DatasourceUnpickleUserDataErrorzERaised when userdata is unable to be unpickled due to python upgradesNr>   r?   r@   rA   r&   r+   r)   rI   rI   V   s    Or+   rI   c                       e Zd Zy)DataSourceNotFoundExceptionNr>   r?   r@   r&   r+   r)   rL   rL   Z       r+   rL   c                       e Zd ZdZy)InvalidMetaDataExceptionz8Raised when metadata is broken, unavailable or disabled.NrJ   r&   r+   r)   rP   rP   ^   s    Br+   rP   c                 x   t        j                  |       }g }g }| j                         D ]  \  }}|r	|dz   |z   }n|}|j                         |v s|j                         |v r|j	                  |       t        |t              r7|j                  d      r&|j	                  |       |j                  dd      ||<   t        |t              st        |||      }	|j                  |	j                  d             |j                  |	j                  d             |	||<    t        |      |d<   t        |      |d<   |S )zProcess all instance metadata cleaning it up for persisting as json.

    Strip ci-b64 prefix and catalog any 'base64_encoded_keys' as a list

    @return Dict copy of processed metadata.
    /zci-b64: base64_encoded_keyssensitive_keys)copydeepcopyitemslowerappend
isinstancerG   
startswithreplacedictprocess_instance_metadataextendpopsorted)
metadatakey_pathrU   md_copyrT   	sens_keyskeyvalsub_key_path
return_vals
             r)   r_   r_   b   s/    mmH%GINN$ &S#c>C/LLIIK>)!!#~5\*c3CNN9$=&&|4;;y"5GCLc4 2\>J  &&z~~6K'LMZ^^,<=>%GCL'&( &,,?%@G!" &y 1GNr+   c                    | j                  dg       s| S t        j                  |       }| j                  d      D ]M  }|j                  d      }|}|D ])  }||v st	        ||   t
              s||d   k7  s%||   }+ |v sI|||<   O |S )zRedact any sensitive keys from to provided metadata dictionary.

    Replace any keys values listed in 'sensitive_keys' with redact_value.
    rU   rR   )getrV   rW   splitr[   r^   )rc   redact_valuere   rd   
path_partsobjpaths          r)   redact_sensitive_keysrs      s     <<("-mmH%GLL!12 %^^C(
 	 Ds4y$/JrN*$i	  3;$CI% Nr+   	URLParams)max_wait_secondstimeout_secondsnum_retriessec_between_retriesDataSourceHostnamehostname
is_defaultc            	       z   e Zd ZU eZdZdZdZdZdZ	e
j                  e
j                  e
j                  e
j                  fZee
df   ed<   dZdZdZd	Zej.                  ej2                  ej4                  ej6                  ej8                  hiZej.                  ej2                  hiZd
efdefdi fddddddf	Z eee!e"f   df   ed<   dZ#dZ$ee!df   ed<   dZ%dZ&e'e!   ed<   d	Z(dIde)de*fdZ+de,ddfdZ-d Z.de/fdZ0de/fd Z1d! Z2d" Z3dJd#Z4de/fd$Z5dKd%Z6de/fd&Z7d' Z8dLd(Z9d) Z:d* Z;e<d+        Z=e<d,        Z>d- Z?e<d.        Z@d/ ZAe<d0        ZBd1 ZCe<d2        ZDd3 ZEd4 ZFd5 ZGd6 ZHd7 ZId8 ZJd9 ZKd: ZLd; ZMe<d<        ZNe<d=        ZOd> ZPdMd?ZQd@ ZRdAeSe   fdBZTdAeSe   de/fdCZUdD ZVeWdNdE       ZXe<dF        ZYdG ZZdH Z[y)Or   zen_US.UTF-8_undefN.network_config_sourcesrl   
         ec2_metadatanetwork_jsonrc   )userdataN)userdata_rawN)
vendordataN)vendordata_rawN)vendordata2N)vendordata2_rawNcached_attr_defaultsF)
combined_cloud_config
merged_cfgmerged_system_cfgzsecurity-credentialsr   	user-datar   r   vendor-datazds/vendor_datasensitive_metadata_keysextra_hotplug_udev_rulesdistropathsc                 v   || _         || _        || _        d | _        i | _        d | _        d | _        d | _        d | _        d | _	        t        j                  | j                   d| j                  fi       | _        | j                  si | _        |s%t        j                  | j                        | _        y || _        y )N
datasource)sys_cfgr   r   r   rc   r   r   r   r   r   r   get_cfg_by_pathdsnameds_cfgudUserDataProcessorud_proc)r<   r   r   r   r   s        r)   __init__zDataSource.__init__0  s    
 +/"#**LL<5r
 {{DK//

;DL"DLr+   ci_pkl_versionr7   c                 ^   t        | d      sd| _        t        | d      sd| _        t        | d      sd| _        t        | d      r"| j                  	 t        | j                         t        | d      sd| _
        yy# t        $ r&}t        j                  d|       t               |d}~ww xY w)	z(Perform deserialization fixes for Paths.r   Nr   skip_hotplug_detectFr   z:Unable to unpickle datasource: %s. Ignoring current cache.r   )hasattrr   r   r   r   rG   AttributeErrorLOGdebugrI   r   )r<   r   es      r)   	_unpicklezDataSource._unpickleG  s    t]+#Dt./#'D t23',D$4$)B?DMM" t78,0D) 9 " ?		/
 67Q>?s   A= =	B,!B''B,c                 ,    t        j                  |       S r9   )r   obj_namer;   s    r)   r=   zDataSource.__str__a  s    ""4((r+   c                      y)z#Check if running on this datasourceTr&   r;   s    r)   	ds_detectzDataSource.ds_detectd      r+   c                    | j                   j                         t               j                         k(  rt        j	                  d|        y| j
                  j                  dg       | j                   gk(  rt        j	                  d|        yy)aI  Override if either:
        - only a single datasource defined (nothing to fall back to)
        - commandline argument is used (ci.ds=OpenStack)

        Note: get_cmdline() is required for the general case - when ds-identify
        does not run, _something_ needs to detect the kernel command line
        definition.
        zOMachine is configured by the kernel commandline to run on single datasource %s.Tdatasource_listz5Machine is configured to run on single datasource %s.F)r   rY   parse_cmdliner   r   r   rm   r;   s    r)   override_ds_detectzDataSource.override_ds_detecth  sv     ;;-/"7"7"99II(
 \\/4EIIG r+   c                     | j                         r| j                         S | j                         r&t        j	                  d|        | j                         S t        j	                  d|        y)z&Overrides runtime datasource detectionz8Detected platform: %s. Checking for active instance dataz#Datasource type %s is not detected.F)r   	_get_datar   r   r   r;   s    r)   _check_and_get_datazDataSource._check_and_get_data  sX    ""$>>##^^IIJ >>##II;TBr+   c                    | j                         j                  }| j                         }| j                  }|d   }di ddgd|d|dt	        | j
                  | j                  | j                        d| j
                  d	| j
                  d
|d   d   d|d   d   d|d   d   d| j                  d| j                         d|d   d|d|d|d   d   d|d||d   d   | j                  | j                  |d   |d   diS )z2Return a dictionary of standardized metadata keys.sys_infov1
_beta_keyssubplatformavailability-zoneavailability_zonecloud_idr    
cloud_namer   distr   distro_versionr   distro_release   platformpublic_ssh_keyspython_versionpythoninstance-idinstance_idkernel_releaseunamelocal-hostnamelocal_hostname   variant)machineregionr   system_platformr   )
get_hostnamerz   get_instance_idr   canonical_cloud_idr   r   platform_typeget_public_ssh_keysr   )r<   instance_datar   r   r   sysinfos         r)   _get_standardized_metadataz%DataSource._get_standardized_metadata  s   **,55**, 22  
+ }o#%6 $%6 .OOT[[$2D2D	 doo doo '&/!, !'&/!"4 !'&/!"4 D.. "4#;#;#= !'("3 {  {!" !''"21"5#$ !.%& !.'( #7+A.++#//#*:#6"9-1
 	
r+   c                     | j                   sy|r|}n| j                  }|D ]  \  }}t        | |      st        | ||       ! |sd| _         yy)zReset any cached metadata attributes to datasource defaults.

        @param attr_defaults: Optional tuple of (attr, value) pairs to
           set instead of cached_attr_defaults.
        NF)_dirty_cacher   r   setattr)r<   attr_defaultsattr_values	attributer:   s        r)   clear_cached_attrszDataSource.clear_cached_attrs  s_       'K33K + 	0IutY'i/	0  %D r+   c                 \    d| _         | j                         }|s|S | j                          |S )zDatasources implement _get_data to setup metadata and userdata_raw.

        Minimally, the datasource should return a boolean True on success.
        T)r   r   persist_instance_data)r<   return_values     r)   get_datazDataSource.get_data  s5    
 !//1""$r+   c                    |rXt         j                  j                  | j                  j                        r%t        | | j                  j                  d             t        | d      rHt        j                  t        | d            }|j                  dd       |j                  dd       d|i}nbdd| j                  ii}t        | d      rt        | d      }|t        k7  r||d   d<   t        | d	      rt        | d	      }|t        k7  r||d   d	<   t        |d   d
<   t        j                  | j                        |d<   d|d   d
<   t        j                  |d         |d<   d|d   d
<   t!        j"                         |d<   |j%                  | j'                  |             	 t)        j*                  |      }t-        t/        j0                  |      | j2                        }| j                  j?                  d      }	|d   jA                  dd      }
t         j                  jC                  | j                  jD                  d      }t!        jF                  | d|
 |
 d       d}| d|
 }t         j                  jI                  |      rt         j                  jK                  |      }t!        jL                  ||d       |r||k7  rt!        jN                  |       tQ        |	|d       | j                  j?                  d      }tQ        |tS        |             y# t4        $ r)}t6        j9                  dt;        |             Y d}~yd}~wt<        $ r)}t6        j9                  dt;        |             Y d}~yd}~ww xY w)aP  Process and write INSTANCE_JSON_FILE with all instance metadata.

        Replace any hyphens with underscores in key names for use in template
        processing.

        :param write_cache: boolean set True to persist obj.pkl when
            instance_link exists.

        @return True on successful write, False otherwise.
        obj_pkl_crawled_metadatar   Nr   r3   	meta_datar   r   _docr   z<DEPRECATED: Use merged_system_cfg. Will be dropped from 24.1r   zUMerged cloud-init system config from /etc/cloud/cloud.cfg and /etc/cloud/cloud.cfg.d/r   )rU   z'Error persisting instance-data.json: %sFinstance_data_sensitiver   r   nonezcloud-id-
T)forcei  )moder   )*osrr   lexistsr   instance_link	pkl_storeget_ipath_curr   rV   rW   getattrra   rc   UNSETEXPERIMENTAL_TEXTr   r   system_infoupdater   r   
json_dumpsr_   jsonloadsr   	TypeErrorr   warningrG   UnicodeDecodeErrorget_runpathrm   joinrun_dir
write_fileexistsrealpathsym_linkdel_filer   rs   )r<   write_cachecrawled_metadatar   r   r   contentprocessed_datar   json_sensitive_filer   cloud_id_fileprev_cloud_id_filenew_cloud_id_file	json_files                  r)   r   z DataSource.persist_instance_data  s    277??4::+C+CDdDJJ44Y?@4,-  $}}12    d3  5!#34M!K#?@Mt^,&t^<5(:FM$'7t^,&t^<5(:FM$'7&7dF#&*mmDLL&Al# K 	l#	
 .2]],'.
)*' 	)*62 %)$4$4$6j!T<<]KL	#..}=G6

7##;;N #jj445NO &**:v>TZZ%7%7D=/8*5(2G!,oQxj977>>-(!#!1!1-!@'dC"48I"IMM,-&UCJJ**?;	93NCD1  	KKA3q6J! 	KKA3q6J	s$   "?L 	M?&M

M?M::M?c                     t        d      )z@Walk metadata sources, process crawled data and save attributes.zlSubclasses of DataSource must implement _get_data which sets self.metadata, vendordata_raw and userdata_raw.)NotImplementedErrorr;   s    r)   r   zDataSource._get_data$  s    !D
 	
r+   c           	      R   | j                   }	 t        | j                  j                  d| j                               }| j                  }	 t        dt        | j                  j                  d| j                                    }| j                  }	 t        | j                  j                  d| j                              }| j                  }	 t        | j                  j                  d| j                              }t        ||||      S # t        $ r9 t        j                  t        d| j                  j                  d      |       Y w xY w# t        $ rE | j                  }t        j                  t        d| j                  j                  d      |       Y w xY w# t        $ r9 t        j                  t        d| j                  j                  d      |       Y !w xY w# t        $ r9 t        j                  t        d	| j                  j                  d      |       Y *w xY w)
zReturn the Datasource's preferred url_read parameters.

        Subclasses may override url_max_wait, url_timeout, url_retries.

        @return: A URLParams object with max_wait_seconds, timeout_seconds,
            num_retries.
        max_waitz6Config max_wait '%s' is not an int, using default '%s'r   timeoutz5Config timeout '%s' is not an int, using default '%s'retriesz5Config retries '%s' is not an int, using default '%s'rx   zAConfig sec_between_retries '%s' is not an int, using default '%s')url_max_waitintr   rm   
ValueErrorr   logexcr   url_timeoutmaxurl_retries	Exceptionurl_sec_between_retriesrt   )r<   r  r  r  rx   s        r)   get_url_paramszDataSource.get_url_params+  s    $$	4;;??:t7H7HIJH ""		!SD<L<L!MNOG ""	$++//)T5E5EFGG #::	"%)4+G+G# 7G5HII_  	KKH
+		  	&&GKKG	*		  	KKG	*		  	KK& 56#	sH   /D	 
9E /F /G$ 	>E
EA
FF>G! G!$>H&%H&c                     | j                   .| j                  j                  | j                               | _         |r| j	                  | j                         S | j                   S r9   )r   r   processget_userdata_raw_filter_xdata)r<   apply_filters     r)   get_userdatazDataSource.get_userdatag  sM    ==  LL001F1F1HIDM%%dmm44}}r+   c                     | j                   .| j                  j                  | j                               | _         | j                   S r9   )r   r   r  get_vendordata_rawr;   s    r)   get_vendordatazDataSource.get_vendordatan  s5    ??""ll2243J3J3LMDOr+   c                     | j                   .| j                  j                  | j                               | _         | j                   S r9   )r   r   r  get_vendordata2_rawr;   s    r)   get_vendordata2zDataSource.get_vendordata2s  s:    ##||33D4L4L4NODr+   c                     t        | d      s| j                  j                         | _        | j                  s| j                  j                         | _        | j                  S )N_platform_type)r   r   rY   r+  r;   s    r)   r   zDataSource.platform_typex  sL    t-."&++"3"3"5D"""&++"3"3"5D"""r+   c                     t        | d      s| j                         | _        | j                  s| j                         | _        | j                  S )a  Return a string representing subplatform details for the datasource.

        This should be guidance for where the metadata is sourced.
        Examples of this on different clouds:
            ec2:       metadata (http://169.254.169.254)
            openstack: configdrive (/dev/path)
            openstack: metadata (http://169.254.169.254)
            nocloud:   seed-dir (/seed/dir/path)
            lxd:   nocloud (/seed/dir/path)
        _subplatform)r   _get_subplatformr-  r;   s    r)   r   zDataSource.subplatform  sE     t^, $ 5 5 7D   $ 5 5 7D   r+   c                 D    t        | d      rdt        | d      z  S t        S )z?Subclasses should implement to return a "slug (detail)" string.metadata_addresszmetadata (%s))r   r   METADATA_UNKNOWNr;   s    r)   r.  zDataSource._get_subplatform  s&    4+,"WT3E%FFFr+   c                 ,   | j                   r| j                   S | j                  r| j                  j                  t              r| j                  j                  t              }t	        |t
              r!|j                         | _         | j                   S | j                         j                         | _         t        j                  dt        t        |             | j                   S | j                         j                         | _         | j                   S )zReturn lowercase cloud name as determined by the datasource.

        Datasource can determine or define its own cloud product name in
        metadata.
        z5Ignoring metadata provided key %s: non-string type %s)_cloud_namerc   rm   METADATA_CLOUD_NAME_KEYr[   rG   rY   _get_cloud_namer   r   type)r<   r   s     r)   r   zDataSource.cloud_name  s     ###==T]]../FG**+BCJ*c*#-#3#3#5   $(#7#7#9#?#?#A 		K+$   $335;;=Dr+   c                     | j                   S )zReturn the datasource name as it frequently matches cloud name.

        Should be overridden in subclasses which can run on multiple
        cloud names, such as DatasourceEc2.
        )r   r;   s    r)   r5  zDataSource._get_cloud_name  s     {{r+   c                 X    | j                   sy d| j                   v r| j                   d   S y )Nzlaunch-index)rc   r;   s    r)   r   zDataSource.launch_index  s*    }}T]]*==00r+   c                     t        j                  t        j                  | j                               g}|}|D ]  }|j	                  |      } |S r9   )r   Filterr   safe_intapply)r<   processed_udfiltersnew_udfs        r)   r!  zDataSource._filter_xdata  sN    d.?.? @A
  	%AWWV_F	%r+   c                      yNFr&   r;   s    r)   is_disconnectedzDataSource.is_disconnected      r+   c                     | j                   S r9   )r   r;   s    r)   r   zDataSource.get_userdata_raw  s       r+   c                     | j                   S r9   )r   r;   s    r)   r%  zDataSource.get_vendordata_raw  s    """r+   c                     | j                   S r9   )r   r;   s    r)   r(  zDataSource.get_vendordata2_raw  s    ###r+   c                     i S r9   r&   r;   s    r)   get_config_objzDataSource.get_config_obj  s    	r+   c                 J    t        | j                  j                  d            S )Nzpublic-keys)normalize_pubkey_datarc   rm   r;   s    r)   r   zDataSource.get_public_ssh_keys  s    $T]]%6%6}%EFFr+   c                      y)a5  Publish the public SSH host keys (found in /etc/ssh/*.pub).

        @param hostkeys: List of host key tuples (key_type, key_value),
            where key_type is the first field in the public key file
            (e.g. 'ssh-rsa') and key_value is the key itself
            (e.g. 'AAAAB3NzaC1y...').
        Nr&   )r<   hostkeyss     r)   publish_host_keyszDataSource.publish_host_keys  s    r+   c                     ddi}|j                         D ]V  \  }}|j                  |      s|D ]:  }d||t        |      d  }t        j                  j                  |      s6|c c S  X y )Nsd)vdxvdvtbz/dev/)rX   r\   lenr   rr   r  )r<   
short_namemappingsnfromtlistntocands          r)   _remap_devicezDataSource._remap_device  sx     ./&nn. 	 NUE((/  &):c%jl+CD77>>$'K 	  r+   c                      y r9   r&   )r<   _names     r)   device_name_to_devicez DataSource.device_name_to_device  s     r+   c                 t    | j                   }	 | j                  j                         }|S # t        $ r Y |S w xY w)z<Default locale is en_US.UTF-8, but allow distros to override)default_localer   
get_localer  )r<   locales     r)   ra  zDataSource.get_locale  sD    $$	[[++-F  # 		s   * 	77c                     | j                   j                  d| j                   j                  d            }|r|S | j                   j                  di       j                  d      S )Nr   r   	placementrc   rm   )r<   top_level_azs     r)   r   zDataSource.availability_zone  sY    }}((!2!23F!G
 }}  b1556IJJr+   c                 8    | j                   j                  d      S )Nr   re  r;   s    r)   r   zDataSource.region  s    }}  **r+   c                 h    | j                   rd| j                   vryt        | j                   d         S )Nr   ziid-datasource)rc   rG   r;   s    r)   r   zDataSource.get_instance_id  s+    }}T]] B#4==/00r+   c                 N   d}d}|}d}| j                   r| j                   j                  d      s|rt        d|      S g }t        j                         }	|	dk(  rd}t        j
                  |	      }
|
r/|
j                  d      dkD  rt        |
      j                  d      }n|	r/|	j                  d      dkD  rt        |	      j                  d      }n|	r|	|g}n||g}n| j                   d   }t        j                  |      rMg }|rt        j                  |      }|rt        |      j                  d      }n(d	|j                  dd
      z  g}n|j                  d      }t        |      dkD  r|d   }	dj                  |dd       }n|d   }	|r||k7  r|	d|}	t        |	|      S )a  Get hostname or fqdn from the datasource. Look it up if desired.

        @param fqdn: Boolean, set True to return hostname with domain.
        @param resolve_ip: Boolean, set True to attempt to resolve an ipv4
            address provided in local-hostname meta-data.
        @param metadata_only: Boolean, set True to avoid looking up hostname
            if meta-data doesn't have local-hostname present.

        @return: a DataSourceHostname namedtuple
            <hostname or qualified hostname>, <is_default> (str, bool).
            is_default is a bool and
            it's true only if hostname is localhost and was
            returned by util.get_hostname() as a default.
            This is used to differentiate with a user-defined
            localhost hostname.
            Optionally return (None, False) when
            metadata_only is True and local-hostname data is not available.
        localdomain	localhostFr   NT.r   zip-%sr   r   )rc   rm   ry   r   r   get_fqdn_from_hostsfindrG   rn   r   is_ipv4_addressgethostbyaddrr]   rT  r   )r<   fqdn
resolve_ipmetadata_only	defdomaindefhostdomainr{   toksrz   
hosts_fqdnlhosts               r)   r   zDataSource.get_hostname  s   & "	
}}DMM$5$56F$G)$
;;
 D((*H;&!
11(;Jjooc2Q6:,,S1hmmC0148}**3/ ),+ MM"23E""5)--e4Dt9??3/D#emmC&==>D{{3't9q=AwHXXd12h'FAwHFi'"*F3H!(J77r+   c                 :    | j                   j                  |       S )N)data_source)r   get_package_mirror_infor;   s    r)   r|  z"DataSource.get_package_mirror_infof  s    {{22t2DDr+   source_event_typesc                     i }|D ][  }| j                   j                         D ]<  \  }}||v s|j                  |      st               ||<   ||   j	                  |       > ] |S r9   )supported_update_eventsrX   rm   setadd)r<   r}  supported_eventseventupdate_scopeupdate_eventss         r)   get_supported_eventszDataSource.get_supported_eventsi  s~    24' 	>E --335> M)+//=9<(6$\266u=>	>  r+   c                    | j                  |      }|j                         D ]h  \  }}t        j                  d|j                  dj                  |D cg c]  }|j                   c}             | j                  d|z  t        ff       j |r#| j                          | j                         }|ryt        j                  d| dj                  |D cg c]  }|j                   c}             yc c}w c c}w )a  Refresh cached metadata if the datasource supports this event.

        The datasource has a list of supported_update_events which
        trigger refreshing all cached metadata as well as refreshing the
        network configuration.

        @param source_event_types: List of EventTypes which may trigger a
            metadata update.

        @return True if the datasource did successfully update cached metadata
            due to source_event_type.
        z:Update datasource metadata and %s config due to events: %s, z
_%s_configTz(Datasource %s not updated for events: %sF)	r  rX   r   r   r:   r   r   r   r   )r<   r}  r  scopematched_eventsr  results          r)   update_metadata_if_supportedz'DataSource.update_metadata_if_supportedv  s      445GH%5%;%;%= 		F!E>IIL		NC55;;CD ##lU&:E%B$DE		F ##%]]_F		6II0BCuu{{CD	

 ! D Ds   C1C6c                      yrB  r&   )r<   r   s     r)   check_instance_idzDataSource.check_instance_id  rD  r+   c                 ~    |t         }|t        }| D ]'  }|||v r|c S t        j                  d||       |c S  |S )Nz%invalid dsmode '%s', using default=%s)DSMODE_NETWORKVALID_DSMODESr   r   )
candidatesdefaultvalid	candidates       r)   _determine_dsmodezDataSource._determine_dsmode  s_     ?$G=!E# 		I E!  ;Y 		 r+   c                      y r9   r&   r;   s    r)   network_configzDataSource.network_config  r   r+   c                      y)a(  setup(is_new_instance)

        This is called before user-data and vendor-data have been processed.

        Unless the datasource has set mode to 'local', then networking
        per 'fallback' or per 'network_config' will have been written and
        brought up the OS at this point.
        Nr&   )r<   is_new_instances     r)   setupzDataSource.setup  s     	r+   c                      y)a  activate(cfg, is_new_instance)

        This is called before the init_modules will be called but after
        the user-data and vendor-data have been fully processed.

        The cfg is fully up to date config, it contains a merged view of
           system config, datasource config, user config, vendor config.
        It should be used rather than the sys_cfg passed to __init__.

        is_new_instance is a boolean indicating if this is a new instance.
        Nr&   )r<   cfgr  s      r)   activatezDataSource.activate  s     	r+   r9   )r&   T)F)FFF)NN)\r>   r?   r@   r  dsmoder`  r   r3  r+  r-  r1   rB   rF   rD   rC   r~   r
   __annotations__r  r  r  r  r   r   r   BOOT_NEW_INSTANCEBOOTBOOT_LEGACYHOTPLUGr  default_update_eventsr   r   rG   r   r   r   r   r   r	   _ci_pkl_versionr   r   r   r  r   r=   boolr   r   r   r   r   r   r   r   r  r#  r&  r)  propertyr   r   r.  r   r5  r   r!  rC  r   r%  r(  rI  r   rN  r[  r^  ra  r   r   r   r   r|  r   r  r  r  staticmethodr  r  r  r  r&   r+   r)   r   r      s*   F"N F K N
 L 	$$%%&&	?E"5s":;  LKK@ 	''NN!!	
 	''
 
		R !
9%c3h 45 
 L0U38_ "   /3hsm2O# #u #.1 1 14)4 D .$
L&&
$ 
Pd
4 
:Jx
 
 # # ! !$     .    !#$G  K K + +1F8PE tI  $"&y/$	$L  *  	r+   )	metaclassc                 n   g }| s|S t        | t              r| j                         S t        | t        t        f      rt        |       S t        | t
              r]| j                         D ]J  \  }}t        |t              r|g}t        |t        t        f      s0|D ]  }|s|j                  |        L |S r9   )r[   rG   
splitlineslistr  r^   rX   rZ   )pubkey_datakeys_keynameklistpkeys        r)   rK  rK    s    D+s#%%''+c{+K  +&!,!2!2!4 	*Xu %%%$-! *D D)	*	* Kr+   r7   c           	         t        |||      }|D cg c]  }t        j                  |       }	}t        |v rdnd}
t        j                  d|
|	       t        |	|      D ]  \  }}t        j                  d|j                  dd      z  d|
d|d	|
d
||      }	 |5  t        j                  d|        || ||      }|j                  t        j                  g      r/d|
d||_        |t        j                  |      fcd d d        c S 	 d d d         ddj#                  |	      z  }t%        |      c c}w # 1 sw Y   /xY w# t        $ r t        j                   t        d|       Y w xY w)Nnetworkr   z#Searching for %s data source in: %sz	search-%sr   rS   zsearching for z data from zno z data found from )namedescriptionmessageparentz%Seeing if we can get any data from %szfound zGetting data from %s failedz4Did not find any data source, searched classes: (%s)r  )list_sourcesr   r   DEP_NETWORKr   r   zipr   ReportEventStackr]   r  r   r  r  r  r   r  r   rL   )r   r   r   ds_depscfg_listpkg_listreporterds_listr@  ds_namesr   r  clsmyrepsmsgs                   r)   find_sourcer    sz    8Wh7G0781
##A&8H8#w.9GDII3T8D7+ A	c''t||L"==;?F26=	

	A 9		A3G/11001 @DT$JEMz223789 99A& A499D C &c
**5 99 9  	AKK:C@	As6   D:E A$D?	EE?E	E$E32E3c                 b   g }t         j                  d| ||       | D ]  }t        j                  |      }t        j                  ||dg      \  }}|st         j                  d|       |D ]@  }t        j                  |      }	t        |	d      }
 |
|      }|s/|j                  |          |S )zReturn a list of classes that have the same depends as 'depends'
    iterate through cfg_list, loading "DataSource*" modules
    and calling their "get_datasource_list".
    Return an ordered list of classes that match (if any)
    zLLooking for data source in: %s, via packages %s that matches dependencies %sget_datasource_listzDCould not import %s. Does the DataSource exist and is it importable?)	r   r   r   "match_case_insensitive_module_namefind_moduleerrorimport_moduler   r`   )r  dependsr  src_listr3   ds_namem_locs_looked_locsm_locmodlistermatchess               r)   r  r    s     HII	8  ==bA'33X 56 
 II$
  	E((/CS"78FWoG(	$ Or+   fieldc                 z    | syt        j                  |      }|sy| j                         |j                         k(  S rB  )r   read_dmi_datarY   )r   r  	dmi_values      r)   instance_id_matches_system_uuidr  6  s;    
 !!%(I)//"333r+   c                     | st         } |st         }|t         k(  r| t         k7  r| S |S t        j                         D ](  \  }}|\  }}|j                  |      s ||       s&|c S  | t         k7  r| S |S )z@Lookup the canonical cloud-id for a given cloud_name and region.)r1  CLOUD_ID_REGION_PREFIX_MAPrX   r\   )r   r   r   prefixcloud_id_testr   valid_clouds          r)   r   r   D  s    %
!!!))!;!A!A!C "/;V$Z)@O %%Or+   c                    | syt        | t              r| S t        | t              rt        j                  |       S t        | t
              r+|du rt        | j                  d      d      S t        d      t        dt        |       z        )aL  data: a loaded object (strings, arrays, dicts).
    return something suitable for cloudinit vendordata_raw.

    if data is:
       None: return None
       string: return string
       list: return data
             the list is then processed in UserDataProcessor
       dict: return convert_vendordata(data.get('cloud-init'))
    NTz
cloud-initF)recursez'vendordata['cloud-init'] cannot be dictz$Unknown data type for vendordata: %s)
r[   rG   r  rV   rW   r^   convert_vendordatarm   r  r6  )datar  s     r)   r  r  W  sz     $$}}T""$d?%dhh|&<eLLBCC
;d4jH
IIr+   c                       e Zd Zy)BrokenMetadataNrM   r&   r+   r)   r  r  o  rN   r+   r  c                 t    g }t        |       }|D ]%  \  }}|t        |      k(  s|j                  |       ' |S r9   )r  rZ   )r  r  ret_listdepsetr  depss         r)   list_from_dependsr  z  sB    H\F !dSYOOC ! Or+   rq   fnamec                    	 t        j                  |       }	 t        j                  ||dd       y# t        $ r t        j                  t
        d|        Y yw xY w# t        $ r t        j                  t
        d|       Y yw xY w)z[Use pickle to serialize Datasource to a file as a cache.

    :return: True on success
    zFailed pickling datasource %sFwb   )omoder   z Failed pickling datasource to %sT)pickledumpsr  r   r  r   r   )rq   r  pk_contentss      r)   r   r     sz    
ll3'{$UC   C8#>
  C;UCs    2 A $AA$BBc                 d   d}	 t        j                  |       }|sy	 t        j                  |      S # t        $ r@}t        j                  j                  |       rt        j                  d| |       Y d}~]d}~ww xY w# t        $ r Y yt        $ r t        j                  t        d|        Y yw xY w)zBUse pickle to deserialize a instance Datasource from a cache file.Nzfailed loading pickle in %s: %sz#Failed loading pickled blob from %s)r   load_binary_filer  r   rr   isfiler   r   r  r   rI   r  )r  pickle_contentsr   s      r)   pkl_loadr    s    OE//6 ||O,,  E77>>% KK95!DE +  C>Fs+   2 A> 	A;6A66A;>	B/	#B/.B/c                  <    t        t        j                               S )zCheck if command line argument for this datasource was passed
    Passing by command line overrides runtime datasource detection
    )parse_cmdline_or_dmir   get_cmdliner&   r+   r)   r   r     s       0 0 233r+   inputc                 v   t        j                  d|       }t        j                  d|       }t        j                  d|       }|xs |xs |}|xs |}|rA|j                  d      j                         }t	        j
                  d| d| dd| d	
       |r"|j                  d      r|j                  d      S y)Nz(?:^|\s)ds=([^\s;]+)z(?:^|\s)ci\.ds=([^\s;]+)z (?:^|\s)ci\.datasource=([^\s;]+)r   z7Defining the datasource on the commandline using ci.ds=z or ci.datasource=z23.2zUse ds=z instead)
deprecateddeprecated_versionextra_messagerS   )researchgroupstripr   	deprecate)r  
ds_parse_0
ds_parse_1
ds_parse_2r3   r  r   s          r)   r  r    s    2E:J6>J>FJ		/z	/ZB)zJ!!!$**, !!!'*  &#F884	
 
bhhqkxx{r+   )rS   r&   )zsystem-uuidr  )UabcrV   r   loggingr   r  r  collectionsr   enumr   r   typingr   r   r   r	   r
   	cloudinitr   r   r   r   r   r   r   r   cloudinit.atomic_helperr   cloudinit.distrosr   cloudinit.eventr   r   cloudinit.filtersr   cloudinit.helpersr   cloudinit.persistencer   cloudinit.reportingr   DSMODE_DISABLEDDSMODE_LOCALr  DSMODE_PASSr  DEP_FILESYSTEMr  	DS_PREFIXr   REDACT_SENSITIVE_VALUEr4  r   r1  	getLoggerr>   r   r  r1   r  rI   rL   rP   r_   rs   rt   ry   ABCMetar   rK  rG   r  r  r  r  r   r  IOErrorr  r  r   r  r   r  r&   r+   r)   <module>r      s       	  	 "  3 3 C C %  . $ 1 * # 6 & ,?	<  6  '  g!
 -./034  $   Pi P	) 	Cy C F 2H : 	   _% _D:+
:s?+B!J ,44	4&J0	W 	: c d $C HZ0 *4s 4  r+   