
    }fl                    $   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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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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! d dl"m#Z# d dlm$Z$m%Z% d dl&m'Z' d dl(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 d d	l6m7Z7 d dl8Z8d d
l9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZD d dlEmFZFmGZG e)rd dlHmIZI daJ ej                  eL      ZMej                  diZOdej                  z   ej                  z   ZRdZSdZT G d de1      ZUd ZV e$       d        ZW e$       d        ZXdde5eYeZf   deYfdZ[dde5eYeZf   deZfdZ\deZdeZfdZ]d Z^ G d d      Z_ G d  d!e`      Za G d" d#e`      Zbd$ Zcdd%Zddd&Zedd'Zfdd(Zgdd)Zhdd*de-fd+Zid, Zjd- Zkd. Zldd0Zmd1 Znd2eYdeofd3Zpd4 Zqd/d/dej                  d/fd5Zs e$       d6        Zt e$       d7        Zu e$       d8        Zv e$       d9        Zw e$       d:        Zx e$       d;        Zydd=Zzdd>Z{dd?Z|dd@Z} e$       dA        Z~dB Z e$       dC        ZddDZddEZdF ZddGZddHe3e0   deofdIZej@                  dJ        Zej@                  dK        ZdL ZdM ZddOZddPZdeoffdQZddRZdd*deofdSZdd*deofdTZddUZddVZdW Z edXg dY      ZddZZdd[Zdefd\Zd] Zd^ Zd_ Zd` Z	 ddaZ	 ddbZ	 ddcZ	 dddZ	 ddeZddfZdg Zdd<dhdie5eYejF                  f   dje2e+egdf      dkedeZfdlZdd<dhdie5eYejF                  f   dje2e+egdf      dkedeYfdmZ e$       dn        Zdo ZdefdpZddqZddrZddsZdt Zdu Zej^                  d/dvdweddfdxZddyeYdeYfdzZd{e
jd                  deZfd|Zd} Zd~ Zd ZddZeoffdZd ZddZej@                  d        Zd Z	 	 	 	 ddZd Zd ZddZd Zd Zd Z e$       d        Zd Zd Z	 dd<ddededdfdZd Zd ZdeYdefdZdeYdefdZdeYdeYfdZdeYdeYfdZdeYde/eY   fdZ	 	 	 dd/ddddZd ZddZddZddZd Zd Zd Zd Z e$       d        Zd ZddZd Zd Zd ZeMd<fdZd Zd Zd Zd ZddZeMd<fdZdeYdefdZ e4d      Z	 	 	 dde+def   defdZd Zddej                  j                  fdZddZd ZddZd Zd Zd ZdÄ ZddĄZddńZdƄ ZdǄ ZddȄZddɄZe% G dʄ d edg d̢                   ZdeYdeYdefdτZddNd<dМdeYdeYde2eY   dededeUfdքZdNdלdeYdeYdefd؄ZdddeofdۄZe de.de*df   fd܄       Zy)    N)	b64decode)deque
namedtuple)contextmanagersuppress)ENOENT)	lru_cachetotal_ordering)Path)TYPE_CHECKINGAnyCallableDequeDict	GeneratorListMapping
NamedTupleOptionalSequenceTypeVarUnion)parse)featuresimporterlogmergersnetsettingssubp
temp_utils
type_utils
url_helperversion)CFG_BUILTINPER_ONCE)Paths_z_-.())true1onyes)off0nofalsec                   "    e Zd ZU eed<   eed<   y)DeprecationLog	log_levelmessageN)__name__
__module____qualname__int__annotations__str     0/usr/lib/python3/dist-packages/cloudinit/util.pyr2   r2   ^   s    NLr<   r2   c                      t        t        t        t        j                         j
                  j                  d      d d             S )N.   )tuplemapr8   osunamereleasesplitr;   r<   r=   kernel_versionrG   c   s2    S"((*,,2237;<==r<   c                  h    t        j                   ddgd      } | j                  j                         S )zReturn the sanitized string output by `dpkg --print-architecture`.

    N.B. This function is wrapped in functools.lru_cache, so repeated calls
    won't shell out every time.
    dpkgz--print-architectureTcapture)r    stdoutstrip)outs    r=   get_dpkg_architecturerO   g   s-     ))V34d
CC::r<   c                  F   ddddd} i }	 t        j                   ddgd	      }|j                  j                         D ]2  }|j                  d
      \  }}}|| v s|j	                         || |   <   4 | j                         D cg c]	  }||vs| }}t        |      r%t        j                  ddj                  |             |S c c}w # t         j                  $ rA}	t        j                  d|	       t        d | j                         D              }Y d }	~	|S d }	~	ww xY w)NcodenamedescriptionidrE   )CodenameDescriptionzDistributor IDReleaselsb_releasez--allTrJ   :z.Missing fields in lsb_release --all output: %s,z#Unable to get lsb_release --all: %sc              3   $   K   | ]  }|d f 
 yw)UNAVAILABLENr;   ).0vs     r=   	<genexpr>zlsb_release.<locals>.<genexpr>   s     >1Q&>s   )r    rL   
splitlines	partitionrM   valueslenLOGwarningjoinProcessExecutionErrordict)
fmapdatarN   linefnamer(   valkmissingerrs
             r=   rW   rW   r   s    $	D D?ii0$?JJ))+ 	0D NN3/ME1c}$'IIKT%[!	0 #kkm=q}1==w<KK@! K > %% ?93?>>>K	?s6   AC *C 	CC2C C D 6DD blobreturnc                 J    t        | t              r| S | j                  |      S N)encoding)
isinstancer:   decode)rp   rt   s     r=   decode_binaryrw      s!    dC(4Ldkk8k.LLr<   textc                 J    t        | t              r| S | j                  |      S rs   )ru   bytesencode)rx   rt   s     r=   encode_textr|      s!    dE*4NX0NNr<   ri   c                     t        | t              st        dt        |       z        	 t	        | d      S # t
        j                  $ r | cY S w xY w)zbase64 decode data

    If data is base64 encoded bytes, return b64decode(data).
    If not, return data unmodified.

    @param data: data as bytes. TypeError is raised if not bytes.
    zdata is '%s', expected bytesT)validate)ru   rz   	TypeErrortyper   binasciiError)ri   s    r=   maybe_b64decoder      sM     dE"6dCDD-->> s   6 AAc                     | j                  d      }| j                         dk(  rOt        |t              r?| j	                         }|r|j
                  r|j
                  }nd}|j                  |d      S |S )NT)rv   rx   utf-8surrogateescape)get_payloadget_content_maintyperu   rz   get_charsetinput_codecrv   )partcte_payloadcharsetrt   s       r=   fully_decoded_payloadr      sy     ""$"/K  "f,U2 ""$w****HH!!(,=>>r<   c                        e Zd ZddZd Zd Zy)SeLinuxGuardc                     	 t        j                  d      | _        || _        || _        y # t        $ r
 d | _        Y !w xY w)Nselinux)r   import_moduler   ImportErrorpath	recursive)selfr   r   s      r=   __init__zSeLinuxGuard.__init__   sA    	 #11)<DL 	"  	 DL	 s   + >>c                 R    | j                   r| j                   j                         ryyNTF)r   is_selinux_enabledr   s    r=   	__enter__zSeLinuxGuard.__enter__   s    <<DLL;;=r<   c                    | j                   r| j                   j                         sy t        j                  j	                  | j                        sy t        j                  j                  | j                        }	 t        j                  |      }| j                   j                  ||t        j                            t        j                  d|| j                         	 | j                   j                  || j                         y # t        $ r Y y w xY w# t        $ r,}t        j                  d|| j                  |       Y d }~y d }~ww xY w)Nz,Restoring selinux mode for %s (recursive=%s)r   z,restorecon failed on %s,%s maybe badness? %s)r   r   rC   r   lexistsrealpathlstatmatchpathconstatST_MODEOSErrorrc   debugr   
restoreconrd   )r   	excp_type
excp_valueexcp_tracebackr   statses          r=   __exit__zSeLinuxGuard.__exit__   s    ||4<<#B#B#Dwwtyy)ww		*	HHTNELL%%dE$,,,?@ 			:NN	

	LL##DDNN#C  		  	KK>	 	s+   <AD  'D 	DD	E "EENF)r5   r6   r7   r   r   r   r;   r<   r=   r   r      s    #r<   r   c                       e Zd Zy)MountFailedErrorNr5   r6   r7   r;   r<   r=   r   r          r<   r   c                       e Zd Zy)DecompressionErrorNr   r;   r<   r=   r   r      r   r<   r   c                 X   t        j                         }|dk(  r	  | |i | t        j                  d       y t
        j                  d|t        j                  |              y # t        $ r< t	        t
        dt        j                  |              t        j                  d       Y y w xY w)Nr   z&Failed forking and calling callback %s   z(Forked child %s who will run callback %s)	rC   fork_exit	Exceptionlogexcrc   r"   obj_namer   )child_cbargskwargsfids       r=   fork_cbr      s    
'')C
ax		d%f%HHQK 			6)	
  	8##H-
 HHQK	s   A$ $AB)(B)c                     t        | t              r| du S t        }|rt        |      |z   }t	        |       j                         j                         |v ryyr   )ru   boolTRUE_STRINGSlistr:   lowerrM   rl   addons	check_sets      r=   is_truer     sN    #d{IOf,	
3x~~9,r<   c                     t        | t              r| du S t        }|rt        |      |z   }t	        |       j                         j                         |v ryy)NFT)ru   r   FALSE_STRINGSr   r:   r   rM   r   s      r=   is_falser     sN    #e|IOf,	
3x~~9,r<   c                 D    | syt        | t              r| S t        | |      S NF)ru   r   r   )rl   r   s     r=   translate_boolr     s&     #
3r<   c                     t        j                         }|s!t        j                  t        j                  z   }dj                  t        |       D cg c]  }|j                  |       c}      S c c}w N )randomSystemRandomstringascii_lettersdigitsre   rangechoice)strlenselect_fromr_xs       r=   rand_strr   )  sQ    A**V]]:77E&MBbAHH[)BCCBs   A/c                 @    |sd}	 t        d      dz   |z   }|| vr	 |S )Nr      )r   r(   )r   )
dictionarypostfixnewkeys      r=   rand_dict_keyr   0  s6    
#c)G3#M	 r<   instance_data_filec          	         ddl m}m}m}m} 	 t        |       }|rAt        j                  j                  |      r"	  ||| |      }t        j                  d||        t        |i       S # t        $ r i cY S w xY w# |$ r!}t        j                  d| |       Y d}~?d}~w|$ r Y I|$ r+}t        j                  d||t        |             Y d}~sd}~ww xY w)z>Read a yaml config with optional template, and convert to dictr   )JinjaLoadErrorJinjaSyntaxParsingExceptionNotJinjaErrorrender_jinja_payload_from_filez?Applied instance data in '%s' to configuration loaded from '%s'z4Failed to render templated yaml config file '%s'. %sNz:Could not apply Jinja template '%s' to '%s'. Exception: %sdefault)!cloudinit.handlers.jinja_templater   r   r   r   load_text_fileFileNotFoundErrorrC   r   existsrc   r   rd   repr	load_yaml)rk   r   r   r   r   r   config_filer   s           r=   	read_confr   :  s     $U+ bggnn-?@	8"K
 II1"	0 ["--E  	  + 	KKF 
  	  	KK "Q 	s:   A) !A: )A76A7:C?BC%C)!CCc                  $    t        t        |        S N)sorted
uniq_merge)listss    r=   uniq_merge_sortedr   n  s    *e$%%r<   c                      g }| D ]T  }t        |t              r1|j                         j                  d      }|D cg c]  }|s|	 }}|j	                  |       V t        |      S c c}w )NrY   )ru   r:   rM   rF   extend	uniq_list)r   combined_lista_listas       r=   r   r   z  sk    M %fc"\\^))#.F!'-A1a-F-V$% ]## .s   A'A'c                     t         j                         D ]  \  }}| j                  ||      }  g }| D ]  }|t        vs|j	                  |        |D ]  }| j                  |d      }  | j                         } | S r   )FN_REPLACEMENTSitemsreplace
FN_ALLOWEDappendrM   )fnrm   r]   removalss       r=   clean_filenamer    s    %%' 1ZZ1H JOOA  ZZ2	BIr<   Tc                    	 t        j                  t        |             5 }t        j                  d dd|      5 }|r+t        |j                               cd d d        cd d d        S |j                         cd d d        cd d d        S # 1 sw Y   nxY wd d d        y # 1 sw Y   y xY w# t        $ r#}|r| cY d }~S t        t        |            |d }~ww xY w)Nrbr   )
ioBytesIOr|   gzipGzipFilerw   readr   r   r:   )ri   quietrv   bufghr   s         r=   decomp_gzipr    s    4ZZD)* 	!c4==$34
 	!$RWWY/		! 	! 	! wwy	! 	! 	! 	! 	! 	! 	!  4K$SV,!3	4so   B* BB		B	B* 'B	6	B?	B* 	B	BB* B'#B* 'B* *	C3C6C<CCc                    | sy| j                  dd      }|d   j                         }t        |      dk(  r|d   j                         }nd }|r|dk(  s|j                         dk(  rd }|r|dk(  s|j                         dk(  rd }||fS )NNNrX   r   r   r@   z-1none)rF   rM   rb   r   )ug_pair	ug_partedugs       r=   extract_usergroupr    s    c1%I!A
9~aL T	QWWY&0T	QWWY&0q6Mr<   root_dirc                 h   t               }t        j                  t        j                  j	                  | d            D ]q  }t        j                  j                  |      s#t        j                  j                  |      dd }|j                         }|sX|j                  d      dk(  sm|||<   s |S )Nz*.pyr   r?   )	rg   globrC   r   re   isfilebasenamerM   find)r  entriesrk   modnames       r=   get_modules_from_dirr)    s    fG277<<&9: %ww~~e$''""5)!B/--/w||C(B.$GEN% Nr<   c                     t        | d      5 }|j                  |       |j                          d d d        y # 1 sw Y   y xY w)Nw)openwriteflush)conpathrx   wfhs      r=   write_to_consoler1    s7    	gs	 s		$		  s	   "8Ac                 
   |rt         j                  j                  |        |rUd}d}t        j                  j                  |      r	 t        ||        d}|r!|st         j                  j                  |        |r3| d   dk(  r |j                  || d d        y  |j                  ||        y y # t        $ rJ d}t         j                  j                  | d       |r! |j                  t        j                  |       Y w xY w)Nz/dev/consoleFTzFailed to write to /dev/console
r"  )sysstderrr-  rC   r   r   r1  r   rL   r   loggingWARNING)	rx   consoler5  r   r3   fallback_to_stdoutr/  writing_to_console_workedconsole_errors	            r=   	multi_logr<    s     

 $)!77>>'"< $/,0) &? JJT"
8tCGGItCRy)CGGIt$	 )  < A

  M?"!56CGGGOO];	<s   B/ /ADDc                  .    dt        j                         v S )NLinuxplatformsystemr;   r<   r=   is_LinuxrB    s    hoo'''r<   c                  b    dt        j                         v ryt        j                         dk(  ryy)NBSDT	DragonFlyFr?  r;   r<   r=   is_BSDrF    s)    !!K'r<   c                  "    t               d   dk(  S )Nvariantfreebsdsystem_infor;   r<   r=   
is_FreeBSDrL        =#y00r<   c                  "    t               d   dk(  S )NrH  	dragonflyrJ  r;   r<   r=   is_DragonFlyBSDrP    s    =#{22r<   c                  "    t               d   dk(  S )NrH  netbsdrJ  r;   r<   r=   	is_NetBSDrS    s    =#x//r<   c                  "    t               d   dk(  S )NrH  openbsdrJ  r;   r<   r=   
is_OpenBSDrV    rM  r<   Fc                 *    || vr|S t        | |         S r   )r   yobjkeyr   s      r=   get_cfg_option_boolr[    s    
$$s)$$r<   c                 R    || vr|S | |   }t        |t              st        |      }|S r   )ru   r:   )rY  rZ  r   rl   s       r=   get_cfg_option_strr]    s/    
$
s)Cc3#hJr<   c                 0    t        t        | ||            S )Nr   )r8   r]  rX  s      r=   get_cfg_option_intr_  !  s    !$W=>>r<   c                 l   | sd} t         j                  j                  |       si S t        |       }d}d|v rd}t	        j
                  ||      }|rc|j                         }d|d   v r|d   |d<   |d   j                         j                  d      d   |d<   |d   d	k(  rd
|d<   |d   |d   |d   dS i S )zReturn a dictionary of distro info fields from /etc/redhat-release.

    Dict keys will align with /etc/os-release keys:
        ID, VERSION_ID, VERSION_CODENAME
    z/etc/redhat-releasezA(?P<name>.+) release (?P<version>[\d\.]+) \((?P<codename>[^)]+)\)	Virtuozzoz)(?P<name>.+) release (?P<version>[\d\.]+)namerQ   z linuxr   zred hat enterpriseredhatr$   )ID
VERSION_IDVERSION_CODENAME)	rC   r   r   r   rematch	groupdictr   r`   )release_fileredhat_releaseredhat_regexrh  groups        r=   _parse_redhat_releasern  %  s     ,77>>,'	#L1N	#  n$CHH\>2E! %-' %fE*f++-77A!Df=00$E&M-	* %j 1
 	

 Ir<   c                     d} d}d}i }d}t         j                  j                  d      rt        t	        d            }|sd}t               }|r|j                  dd      } |j                  dd      }d| v sd| v rt        j                         }n| d	k(  s| d
k(  r|j                  dd      }ni| dk(  r|s|j                  dd      }nO|j                  dd      }|s;t        j                  d|j                  dd            }|r|j                         d   }| dk(  rd} nt               r7t        j                         j                         } t        j                         }n>d}	 t        j                          }d }|D ]  }|sd}	 |st$        j'                  d       |S | ||fS # t"        $ r Y 7w xY w# d }|D ]  }|sd}	 |st$        j'                  d       w w xY w)Nr   F/etc/os-releaseTrd  re  slessusealpinephotonPRETTY_NAME	virtuozzorf  z[^ ]+ \((?P<codename>[^)]+)\)VERSIONrQ   rhelrc  )r   r   r   r   zPUnable to determine distribution, template expansion may have unexpected results)rC   r   r   load_shell_contentr   rn  getr@  machinerg  rh  ri  rF  rA  r   rE   distr   rc   rd   )	distro_namedistro_versionflavor
os_releaseos_release_rhelrh  r|  foundentrys	            r=   get_linux_distror  M  s   KNFJO	ww~~'('7H(IJ
*,
 nnT2.#b9[ Fk$9
 %%'FH$x(?^^M26FK'^^M26F^^$6;F4NN9b1 "__.z:F& "K	oo'--/!))+	==?D E E < 00  		 E E < s*   !F! !	F-*F0 ,F--F0 0
G;Gc                     | d   j                         }d}|dk(  r;| d   d   j                         }|dv r|}|S |dv rd}|S |d	k(  rd
}|S |dv rd}|S d}|S |dv r|}|S )NrA  unknownlinuxr|  r   )	almalinuxrs  arch
azurelinuxcentos
cloudlinuxdebian	eurolinuxfedoramarinermiraclelinux	openeuleropencloudosopenmandrivart  rx  rockyrr  	tencentosrv  )ubuntu	linuxmintmintr  rc  rx  )opensusezopensuse-leapzopensuse-microoszopensuse-tumbleweedsle_hpcz	sle-microrq  rr  )windowsdarwinrI  rR  rU  rO  )r   )inforA  rH  
linux_dists       r=   _get_variantr    s    (^!!#FG&\!_**,
 
 
, !G6 N5 ::G2 N1 8#G. N-  
 
 G N G N 
  
 Nr<   c                  
   t        j                          t        j                         t        j                         t        j                         t	        t        j
                               t               d} t        |       | d<   | S )N)r@  rA  rE   pythonrD   r|  rH  )r@  rA  rE   python_versionr   rD   r  r  )r  s    r=   rK  rK    sc     %%'//###%))+hnn&' "D #4(DOKr<   c                     || vr|S | |   g S | |   }t        |t              r|D cg c]  }| }}|S t        |t              st        |      }|gS c c}w )a  
    Gets the C{key} config option from C{yobj} as a list of strings. If the
    key is present as a single string it will be returned as a list with one
    string arg.

    @param yobj: The configuration object.
    @param key: The configuration key to get.
    @param default: The default to return if key is not found.
    @return: The configuration option as a list of strings or default if key
        is not found.
    )ru   r   r:   )rY  rZ  r   rl   r]   cvals         r=   get_cfg_option_listr    si     $Cy	
s)C#ac3#h5L	  s   	Ac                 t    t        |t              r|j                  d      }| }|D ]  }||vr|c S ||   } |S )a  Return the value of the item at path C{keyp} in C{yobj}.

    example:
      get_cfg_by_path({'a': {'b': {'num': 4}}}, 'a/b/num') == 4
      get_cfg_by_path({'a': {'b': {'num': 4}}}, 'c/d') == None

    @param yobj: A dictionary.
    @param keyp: A path inside yobj.  it can be a '/' delimited string,
                 or an iterable.
    @param default: The default to return if the path does not exist.
    @return: The value of the item at keyp."
    is not found./)ru   r:   rF   )rY  keypr   curtoks        r=   get_cfg_by_pathr    sK     $zz#
C c>N#h Jr<   c                 @    t        | |      \  }}t        ||       ||fS r   )get_output_cfgredirect_output)cfgmodeoutfmterrfmts       r=   fixup_outputr    s(    %c40VVFF#Fr<   c                    t        t        j                  j                  d            rt        j                  d       y |st        j                  }|st        j                  }d }| rt        j                  d||        | j                  dd      \  }}|dk(  s|dk(  rd	}|dk(  rd
}t        ||      }nG|dk(  r4t        j                  |dt        j                  |      }	|	j                  }nt        d| z        |r2t        j                   |j#                         |j#                                || k(  rJt        j                  d||        t        j                   |j#                         |j#                                y |rt        j                  d||       |j                  dd      \  }}|dk(  s|dk(  rd	}|dk(  rd
}t        ||      }nG|dk(  r4t        j                  |dt        j                  |      }	|	j                  }nt        d|z        |r3t        j                   |j#                         |j#                                y y y )N_CLOUD_INIT_SAVE_STDOUTz5Not redirecting output due to _CLOUD_INIT_SAVE_STDOUTc                      t        j                  d       	 t        j                  d      j                  } t        j
                  |        y# t        $ r Y yw xY w)a  Reconfigure umask and group ID to create output files securely.

        This is passed to subprocess.Popen as preexec_fn, so it is executed in
        the context of the newly-created process.  It:

        * sets the umask of the process so created files aren't world-readable
        * if an adm group exists in the system, sets that as the process' GID
          (so that the created file(s) are owned by root:adm)
           admN)rC   umaskgrpgetgrnamgr_gidsetgidKeyError)group_ids    r=   set_subprocess_umask_and_gidz5redirect_output.<locals>.set_subprocess_umask_and_gid#  sJ     		 ||E*11H
 IIh	  		s   A 	AAzRedirecting %s to %s r   >>>abwb|T)shellstdin
preexec_fnz"Invalid type for output format: %sz!Invalid type for error format: %s)r   rC   environrz  rc   r   r4  rL   r5  rF   r,  
subprocessPopenPIPEr  r   dup2fileno)
r  r  o_outo_errr  r  argowithnew_fpprocs
             r=   r  r    s   rzz~~789		IJ



 & 		(%8ll3*s3;$$,Es{#u%FS[## oo7	D ZZF@6IJJGGFMMOU\\^4VII,eV<GGFMMOU\\^4		(%8ll3*s3;$$,Es{#u%FS[## oo7	D ZZF?&HIIGGFMMOU\\^4 ' r<   sourcesc                     |rt        t        |             } i }| D ]W  }|st        j                  |      }|st        j                         }t        j
                  |      }|j                  ||      }Y |S )aF  Merge multiple dicts according to the dict merger rules.

    Dict merger rules can be found in cloud-init documentation. If no mergers
    have been specified, entries will be recursively added, but no values
    get replaced if they already exist. Functionally, this means that the
    highest priority keys must be specified first.

    Example:
    a = {
        "a": 1,
        "b": 2,
        "c": [1, 2, 3],
        "d": {
            "a": 1,
            "b": 2,
        },
    }

    b = {
        "a": 10,
        "c": [4],
        "d": {
            "a": 3,
            "f": 10,
        },
        "e": 20,
    }

    mergemanydict([a, b]) results in:
    {
        'a': 1,
        'b': 2,
        'c': [1, 2, 3],
        'd': {
            'a': 1,
            'b': 2,
            'f': 10,
        },
        'e': 20,
    }
    )r   reversedr   dict_extract_mergersdefault_mergers	constructmerge)r  reverse
merged_cfgr  mergers_to_applymergers         r=   mergemanydictr  h  sz    T x()J 7&;;C@##*#:#:#< &&'78Fj#6J7 r<   c              #      K   t        j                         }	 t        j                  |        |  t        j                  |       y # t        j                  |       w xY wwr   )rC   getcwdchdir)ndircurrs     r=   r  r    s<     99;D


s   A!A A!AA!c              #      K   t        j                  |       }	 | t        j                  |       y # t        j                  |       w xY wwr   )rC   r  )n_mskolds     r=   r  r    s3     
((5/C	
s   A3 AA

Ac                 ,    dj                  | |d|      S )Nz{0:{fill}{align}{size}}^)fillalignsize)format)rx   r  max_lens      r=   centerr    s#    $++4s ,  r<   c                 Z    t         j                  d|        t        j                  |        y )NzRecursively deleting %s)rc   r   shutilrmtreer   s    r=   del_dirr    s    II'.
MM$r<      c                     	 t        |||      \  }}}|| d<   || d<   || d<   y# t        j                  $ r(}|j                  t        j                  k(  rY d}~y d}~ww xY w)z
    returns boolean indicating success or failure (presense of files)
    if files are present, populates 'fill' dictionary with 'user-data' and
    'meta-data' entries
    )baseexttimeout	user-datavendor-data	meta-dataTNF)read_seededr#   UrlErrorcode	NOT_FOUND)r  r  r  r  mdudvdr   s           r=   read_optional_seedr    sj    	 dWE
B[ ][ 66Z)))s   !$ AAAAc                 &   i }| sddg}n^t         j                  j                  | j                  d      d      t         j                  j                  | j	                  d      d      g}t        |      }|D cg c]'  }|st         j                  j                  |      s&|) }}d }|D ]a  }t         j                  j                  t         j                  j                  |d            sAt         j                  j                  |d      } n d }|D ]a  }t         j                  j                  t         j                  j                  |d            sAt         j                  j                  |d      } n |r|r||d<   ||d<   |S |r||d<   |S c c}w )	Nz/var/lib/cloud/data/sslz /var/lib/cloud/instance/data/sslri   sslzcert.pemzkey.pem	cert_filekey_file)rC   r   re   get_ipath_cur	get_cpathr   isdirr$  )pathsssl_detailsssl_cert_pathsdr
  r  s         r=   fetch_ssl_detailsr    s\   K%.
 GGLL,,V4e<GGLL0%8
  /N!/JA1q9IaJNJI 77>>"'',,q*56Q
3I H 77>>"'',,q)45ww||Ay1H X#,K "*J  
#,K ! Ks   7F?FFc                    |}t        |       } 	 t        j                  dt        |       |       t	        j
                  |       }|t        j                  d       |}n1t        ||      s%t        d|dt        j                  |      d      |}|S # t        j                  t        t        f$ r}d}d }t        |d      rt        |d      rt        |d      }n$t        |d      rt        |d      rt        |d      }|r2|d	j                  |j                  d
z   |j                   d
z   |      z  }n|dj                  |      z  }t        j#                  |       Y d }~|S d }~ww xY w)NzKAttempting to load yaml from string of length %s with allowed root types %sz-loaded blob returned None, returning default.zYaml load allows z root types, but got  insteadzFailed loading yaml blobcontext_markproblem_markz5. Invalid format at line {line} column {col}: "{err}"r   )rj   colro   z. {err})ro   )rw   rc   r   rb   yaml	safe_loadru   r   r"   r   	YAMLError
ValueErrorhasattrgetattrr  rj   columnrd   )rp   r   allowedloaded	convertedr   msgmarks           r=   r   r     s^   FD!		6I		
 NN4(	IIEFIIw/J//	:<  " M! NNIz2 (1n%'!^*D1n-DQ'GA~,F1n-DGNNQDKK!O O C 9###**CCM!s   BB E'1B+E""E'c                 j   | j                  d      dk\  r@| j                  dd|z         }| j                  dd|z         }| j                  dd|z         }nWt        j                  r/| d   dk7  r't	        j
                  |       j                  dk(  r| dz  } | d|}| d|}| d|}t        j                  |||	      }d }|j                         r t        t        |j                        i 
      }t        j                  |||	      }	d }
|	j                         r|	j                  }
d }	 t        j                  |||	      }|j                         r|j                  }nt        j                  d       	 ||
|fS # t        j                  $ r }t        j                  d|       Y d }~3d }~ww xY w)Nz%sr   r  r  r  r"  r  r   )r  retriesr   zError in vendor-data responsez!Error in vendor-data response: %s)r&  r  r   %NOCLOUD_SEED_URL_APPEND_FORWARD_SLASHr   urlparsequeryr#   read_file_or_urlokr   rw   contentsrc   r   r  )r  r  r  r&  ud_urlvd_urlmd_urlmd_respr  ud_respr  r  vd_respr   s                 r=   r   r     s   yy!dK#$56dMC$78dK#$5699Bx35>>$#7#=#=#C!;4!=#6!;4))G 
Bzz|}W%5%56C))G 
Bzz|	B
7--GW
 ::<!!BII56B<  :		5q99:s   /E? ?F2F--F2c          	      n   t        t        j                  |       d      }|D cg c]  }|j                  d      s| }}|D cg c]B  }t        j                  j                  t        j                  j                  | |            sA|D }}g }|D ]?  }t        j                  j                  | |      }	 |j                  t        ||             A t        |      S c c}w c c}w # t        $ r t        j                  d|       Y xt        $ r!}t        j                  d||       Y d}~d}~ww xY w)zRead configuration directory.Tr  z.cfgr   1REDACTED config part %s, insufficient permissionsError accessing file %s: [%s]N)r   rC   listdirendswithr   r$  re   r  r   PermissionErrorrc   rd   r   r  )confdr   confsfcfgsr	  r   r   s           r=   read_conf_dr>  D  s    2::e$d3E 41F!3Q4E4 H1UA0F!GQHEH D Bww||E2&	BKK'9B  / 5 I  	KKCT  	BKK7qAA	Bs5   C C AC%	C%7C**D4D4D//D4c                   t               }i }	 t        | |      }|j                  |       d}d|v rU|d   }|rut        |t              s$t        d| dt        j                  |            t        |      j                         }n't        j                  j                  |  d	      r|  d	}|r=t        j                  j                  |      rt!        ||      }|j#                  |       t%        |      S # t        $ r t        j                  d|        Y t        $ r"}t        j                  d| |       Y d}~d}~ww xY w)
a  Read yaml file along with optional ".d" directory, return merged config

    Given a yaml file, load the file as a dictionary. Additionally, if there
    exists a same-named directory with .d extension, read all files from
    that directory in order and return the merged config. The template
    file is optional and will be applied to any applicable jinja file
    in the configs.

    For example, this function can read both /etc/cloud/cloud.cfg and all
    files in /etc/cloud/cloud.cfg.d and merge all configs into a single dict.
    r   r5  r6  Nr   conf_dzConfig file z( contains 'conf_d' with non-string type z.d)r   r   r  r9  rc   rd   r   ru   r:   r   r"   r   rM   rC   r   r  r>  
appendleftr  )cfgfiler   r=  r  r   r:  	confd_cfgs          r=   read_conf_with_confdrD  d  s"    DC	4FG 	CE3HeS)
 3 3E :< 
 E
((*	'"~	&)2u%:LM		"9  
?	
  A3Wa@@As   C8 8EE!D>>Ec                 ,    t        t        |             S )Ncmdline)r   read_cc_from_cmdlinerF  s    r=   read_conf_from_cmdlinerI    s    )':;;r<   c                    | 
t               } d|  } d}d}t        |      }t        |      }t        |       }g }| j                  |      }|dk\  r| j                  |||z         }|dk  r|}|j                  t	        j
                  | ||z   | j                               j                  dd             | j                  |||z         }|dk\  rdj                  |      S )Nr  z cc:end_ccr   z\nr3  )	get_cmdlinerb   r&  r  r   unquotelstripr  re   )	rG  	tag_begintag_endbegin_lend_lclentokensbeginends	            r=   rH  rH    s     -'mGIG)nGLEw<DFLL#E
1*ll7EGO47CMM'%'/C8??ABJJt	

 Ye4 1* 99Vr<   c                 l    | j                  d      }|dk  s| |dz
     dk7  r| S | j                  dd      S )Nr3  r   r   z
)r&  r  )r,  poss     r=   dos2unixrZ    s?    
--
C
ax8C!G$,FD))r<   HostnameFqdnInfo)hostnamefqdn
is_defaultc                 Z   d}d| v r%| d   }t        | d|j                  d      d         }ntd| v r4| d   j                  d      dkD  r| d   }| d   d|j                  d       }n<|j                  d|      j                  }d| v r| d   }n|j                  |	      \  }}t        |||      S )
a  Get hostname and fqdn from config if present and fallback to cloud.

    @param cfg: Dictionary of merged user-data configuration (from init.cfg).
    @param cloud: Cloud instance from init.cloudify().
    @param metadata_only: Boolean, set True to only query cloud meta-data,
        returning None if not present in meta-data.
    @return: a namedtuple of
        <hostname>, <fqdn>, <is_default> (str, str, bool).
        Values can be none when
        metadata_only is True and no cfg or metadata provides hostname info.
        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.
    Fr]  r\  r?   r   NT)r]  metadata_only)r`  )r]  rF   r&  get_hostnamer\  r[  )r  cloudr`  r^  r]  r\  s         r=   get_hostname_fqdnrc    s    " J}6{%c:tzz#q7IJZ!5!5c!:Q!> z?D:'738H %% & h  S z?','9'9"/ (: ($* HdJ77r<   c                 "   d}	 t        |      j                         D ]^  }|j                  d      }|dk\  r|d| }|j                         }|s1|j	                         }t        |      dk  rP| |dd v sX|d   } |S  	 |S # t        $ r Y |S w xY w)a  
    For each host a single line should be present with
      the following information:

        IP_address canonical_hostname [aliases...]

      Fields of the entry are separated by any number of  blanks  and/or  tab
      characters.  Text  from a "#" character until the end of the line is a
      comment, and is ignored. Host  names  may  contain  only  alphanumeric
      characters, minus signs ("-"), and periods (".").  They must begin with
      an  alphabetic  character  and  end  with  an  alphanumeric  character.
      Optional aliases provide for name changes, alternate spellings, shorter
      hostnames, or generic hostnames (for example, localhost).
    N#r      r@   r   )r   r_   r&  rM   rF   rb   IOError)r\  filenamer]  rj   hashpostokss         r=   get_fqdn_from_hostsrk    s     D"8,779 	DiinG!|Ag::<D
 ::<D4y1}48#Aw K)	( K  Ks   A0B 5B =B 	BBc           	      T   t        j                  |       }|j                  }t        t	               }d}i }|D ]{  }	 t        j                  |dddt
        j                  t
        j                        }g ||<   |D ]8  \  }}	}
}}||   j                  |d|d          |j                  |d          : } |a|rt        j                  d|       	 t        t               5  t#        j$                  |j&                  j)                  d            r
	 ddd       y	 ddd       t        j                  |d      }|d   d   d   }|t        vS # t
        j                  t
        j                  f$ r Y 9w xY w# 1 sw Y   \xY w# t
        j                  t
        j                  f$ r Y y	w xY w)
a  determine if a url's network address is resolvable, return a boolean
    This also attempts to be resilent against dns redirection.

    Note, that normal nsswitch resolution is used here.  So in order
    to avoid any utilization of 'search' entries in /etc/resolv.conf
    we have to append '.'.

    The top level 'invalid' domain is invalid per RFC.  And example.com
    should also not exist.  The '__cloud_init_expected_not_found__' entry will
    be resolved inside the search list.
    N)zdoes-not-exist.example.com.zexample.invalid.!__cloud_init_expected_not_found__r   z: zdetected dns redirection: %sz[]T   F)r   r(  r\  _DNS_REDIRECT_IPsetsocketgetaddrinfoSOCK_STREAMAI_CANONNAMEr  addgaierrorerrorrc   r   r   r  r   is_ip_addressnetlocrM   )url
parsed_urlrb  badipsbadnames
badresultsinameresult_fam_stype_protocnamesockaddraddrs                 r=   is_resolvabler    s    $JD

 
 
	E	++4Av'9'96;N;N %'
5!=C ,9D&&%u%,,-LMJJx{+,
	 "II4jA
j! 	  !2!2!8!8!>?	 	?	 ##D$/ay|A+++ OOV\\2 	 	 OOV\\* sB   A8EF  0E5F 0F #E21E25E>:F #F'&F'c                  .    t        j                         } | S r   )rq  gethostname)r\  s    r=   ra  ra  O  s    !!#HOr<   c                 f    	 t        j                  |       d   S # t         j                  $ r Y y w xY wNr   )rq  gethostbyaddrherror)ips    r=   r  r  T  s3    ##B'**== s    00c                 L    t        t        j                  d| z   t        | f      S )z5determine if this url is resolvable (existing or ip).zResolving URL: )logfuncr#  funcr   )log_timerc   r   r  )rz  s    r=   is_resolvable_urlr  [  s&    		#V	 r<   c                     | yt         j                  d|        | D ](  }	 t        |      rt         j                  d|       |c S * y# t        $ r Y 7w xY w)zc
    Search through a list of mirror urls for one that works
    This needs to return quickly.
    Nz%search for mirror in candidates: '%s'zfound working mirror: '%s')rc   r   r  r   )
candidatescands     r=   search_for_mirrorr  e  sj    
 II5zB 	 &		6= '   		s   "A	AAc                    g }| s+t        j                   d      t        j                   d      z   S | j                  d      rI| j                  d      }d|z   d|z   fD cg c]#  }t        j                  j                  |      r|% }}|S | dk(  rt        j                   d      }|S | dk(  rt        j                   d      }|S c c}w )Nz/dev/msdosfs/*z/dev/iso9660/*LABEL=z/dev/msdosfs/z/dev/iso9660/	TYPE=vfatTYPE=iso9660)r#  
startswithrN  rC   r   r   )criteriaoformattagno_cacher   devlistlabelps           r=   find_devs_with_freebsdr  x  s     Gyy)*TYY7G-HHH8$) &-/FG
ww~~a  
 
 N	 
[	 )),- N 
^	#)),-N
s   (Cc                    g }d }d }d}| rD| j                  d      r| j                  d      }| j                  d      r| j                  d      }t        j                  g ddg      }	|	j                  j	                         D ]V  }
|s|rt        j                  d|
gddg      \  }}|rd	|z  |vr/|d
k(  rd|vr9|dk(  rd|v rC|j                  d|
z          X |S )Nr   r  zTYPE=sysctl-nzhw.disknamesr   rcs	mscdlabelr   z
label "%s"iso9660zISO filesystemvfat/dev/)r  rN  r    rL   rF   r  )r  r  r  r  r   r  r  _typemscdlabel_outrN   devr(   s               r=   find_devs_with_netbsdr    s     GEEMx(OOH-Ew'OOG,E
))41#
>Czz! 	&E#yy+s);!QHM1lU*=@I"2-"GF?/=@w}%	& Nr<   c                 v   t        j                   g ddg      }g }|j                  j                         j                  d      D ]Z  }|j	                  d      s|dk(  r|j                  |d d dz          |j                  d	      rD|j                  |d d d
z          \ |D cg c]  }d|z   	 c}S c c}w )Nr  r   r  rY   rX   zfd0:r"  r  cdir  )r    rL   rstriprF   r8  r  r  )	r  r  r  r  r   rN   r  r  r  s	            r=   find_devs_with_openbsdr    s     ))41#
>CG""$**3/ -~~c"F?uSbzC'(%NN5":+,- "))AGaK)))s   'B6c                    t        j                   g ddg      }t        |j                  j                         d      D cg c]&  }|j	                  d      s|j	                  d      s|( }}| dk(  r"|D cg c]  }|j	                  d	      s| }}n>| d
v r"|D cg c]  }|j	                  d	      r| }}n| rt
        j                  d|        |D cg c]  }d|z   	 c}S c c}w c c}w c c}w c c}w )N)r  r  z
kern.disksr   r  Tr4  r  vnr  )r  acd)zLABEL=CONFIG-2r  zUnexpected criteria: %sr  )r    r   rL   rF   r  rc   r   )r  r  r  r  r   rN   r  r  s           r=   find_devs_with_dragonflybsdr    s     ))2
<C 

((*D9||D!!,,t*< 	
G  >!%Em)D1EE	4	4%Kall=.I1KK			+X6!()AGaK)) FK *s$   +C'8C,C,C15C1C6c                    t               rt        | ||||      S t               rt        | ||||      S t	               rt        | ||||      S t               rt        | ||||      S dg}g }| r|j                  d| z         |r|j                  d|z         |r|j                  ddg       |r|j                  d|z         |r|j                  |       ||z   }	 t        j                  |ddg	      \  }}	g }|j                         D ]&  }|j                         }|s|j                  |       ( |S # t        j                  $ r!}
|
j                  t        k(  rd
}n Y d}
~
ld}
~
ww xY w)z
    find devices matching given criteria (via blkid)
    criteria can be *one* of:
      TYPE=<filesystem>
      LABEL=<label>
      UUID=<uuid>
    blkidz-t%sz-s%s-c	/dev/nullz-o%sr   r@   r  r   N)rL  r  rS  r  rV  r  rP  r  r  r   r    rf   errnor   r_   rM   )r  r  r  r  r   
blk_id_cmdoptionscmdrN   _errr   r'  rj   s                r=   find_devs_withr    sr    |%hhMM	$XwXtLL	%hhMM		*gsHd
 	
 JG 	v*+
v~& 	k*+ 	v)*t
w
Cii!Q0d G  !zz|NN4 ! N %% 77fC s   D3 3E'E""E'c                 B   | g } nt        |       } g d}|r|j                  ddg       |j                  |        t        j                  |dd      }i }|j                  j	                         D ]-  }|j                  d      \  }}}t        |      ||<   |||   d<   / |S )	zGet all device tags details from blkid.

    @param devs: Optional list of device paths you wish to query.
    @param disable_cache: Bool, set True to start with clean cache.

    @return: Dict of key value pairs of info for the device.
    )r  -ofullr  r  Tr  )rK   rv   rX   DEVNAME)r   r   r    rL   r_   r`   ry  )	devsdisable_cacher  rN   retrj   r  r(   ri   s	            r=   r  r  
  s     |Dz
!C

D+&'JJt
 ))Ci
8C
C

%%' "~~c*Q%d+C!C"
 Jr<   c                 D    g }| D ]  }||v r|j                  |        |S r   )r  )in_listout_listr  s      r=   r   r   )  s3    H =OOA	
 Or<   read_cbr  rk   r  r  c                j   t         j                  d| |       t        j                         5 }	 t	        | d      5 }t        |||       d d d        |j                         }d d d        t         j                  dt              |        |S # 1 sw Y   CxY w# t        $ r |s Y Tw xY w# 1 sw Y   IxY w)NzReading from %s (quiet=%s)r  )chunk_cbzRead %s bytes from %s)	rc   r   r  r  r,  pipe_in_outr   getvaluerb   )rk   r  r  ofhifhr,  s         r=   load_binary_filer  3  s     II*E59	 "	eT" 8cCw78
 <<>" II%s8}e<O8 8  	 		" "sE   B)BB	BB)B	BB&#B)%B&&B))B2c                0    t        t        | ||            S )Nr  )rw   r  )rk   r  r  s      r=   r   r   F  s     )%NOOr<   c                     t               r#	 t        d      } | j                  dd      d d }|S 	 t        d      j                         }|S # t        $ r#}t        j                  d|       d}Y d }~|S d }~ww xY w# t        $ r d}Y |S w xY w)Nz/proc/1/cmdline r  r"  z"failed reading /proc/1/cmdline: %sr   z/proc/cmdline)is_containerr   r  r   rc   rd   rM   )r,  rG  r   s      r=   _get_cmdliner  O  s    ~	%&78H&&vs3CR8G N	$_5;;=G N  	KK<a@G N	  	GN	s(    A
 A9 
	A6A11A69BBc                  `    dt         j                  v rt         j                  d   S t               S )NDEBUG_PROC_CMDLINE)rC   r  r  r;   r<   r=   rL  rL  b  s&    rzz)zz.//>r<   c                  n    d} 	 t        |       j                         }|dk(  S # t        t        f$ r Y yw xY w)Nz/proc/sys/crypto/fips_enabledr*   F)r   rM   rg  r   )	fips_procr,  s     r=   fips_enabledr  i  sB    /I!),2243W  	s   " 44c                     d}	 | j                  |      }t        |      dk(  rn*|j                  |       |t        |      z  }|r ||       J|j                          |S r  )r  rb   r-  r.  )in_fhout_fh
chunk_sizer  bytes_pipedri   s         r=   r  r  u  sa    K
zz*%t9>LL3t9$K%  LLNr<   c                 t    |dv r|dv ry t         j                  d| ||       t        j                  | ||       y )N)Nr"  z%Changing the ownership of %s to %s:%s)rc   r   rC   chown)rk   uidgids      r=   	chownbyidr    s7    
jSJ.II5uc3GHHUCr<   c                     d}d}	 |rt        j                  |      j                  }|rt        j                  |      j
                  }t        | ||       y # t        $ r}t        d|z        |d }~ww xY w)Nr"  zUnknown user or group: %s)	pwdgetpwnampw_uidr  r  r  r  r   r  )rk   userrm  r  r  r   s         r=   chownbynamer    su    
C
C@,,t$++C,,u%,,C eS#  @1Q78a?@s   AA 	A3A..A3c                    d d g}| rd| vr|S | d   }||v r||   }nd|vr|S |d   }t        |t              r||g}t        |t              r,t        |      dkD  r|d   |d<   t        |      dkD  r|d   |d<   t        |t              rd|v r|d   |d<   d|v r|d   |d<   |d   dk(  r|d   |d<   g d}t        t        |            D ]v  }||   s	||   j                         }d}|D ]8  }	|j                  |	      s|	d	|t        |	      d  j                         }d
} n |sdd	|j                         }|||<   x |S )Noutputallr   r   rw  z&1)r  r  r  Fr  Tr  )	ru   r:   r   rb   rg   r   rN  r  rM   )
r  r  r  outcfgmodecfgswlistr  rl   r  ss
             r=   r  r    s   ,C(#%
]Fv~,J - '3  '4 w<!QZCFw<!QZCF '4 wX&CFgW%CF 1v~QAF3s8_ 1v!fmmo 	A~~a !"CAM$7$7$9:		
 !399;/CA Jr<   c                 x   g }g }| rt        | t              s|S | j                  d      }|r|j                  |       t	        | d      D ]~  }|st        j                  d|      }|s|j                  d      }|j                         }t        |      dk(  r|j                  |       `ddg|dd k(  sk|j                  |d           |D ]V  }t        j                  | d	      D ]9  }	t        j                  j                  |	      s#|	|k7  s)|j                  |	       ; X t        t        ||z               S )
zReturn a list of log file paths from the configuration dictionary.

    @param cfg: The cloud-init merged configuration dictionary.
    def_log_fileNz (?P<type>\||>+)\s*(?P<target>.*)targetr   teez-ar@   *)ru   rg   rz  r  r  rg  rh  rm  rF   rb   r#  rC   r   r$  r   rp  )
r  logsrotated_logsdefault_logfmtrh  r  partslogfilerotated_logfiles
             r=   get_config_logfilesr    s.   
 DLjd+''.)KK c4( "<cBX&u:?KKT]eBQi'KKa!"  5#yyG9A7 	5O ww~~o.?g3M##O4		55 D<'())r<   )r3   exc_infor3   c                X     | j                   ||g|   | j                  |g|d|i y )Nr  )r   r   )r   r#  r3   r  r   s        r=   r   r      s1     CGGIs"T"CIIc,t,H,r<   routinec                     t        j                  |      }|j                  t        |              |j	                         }||d| S |S r  )hashlibnewupdater|   	hexdigest)rp   r	  mlenhasherdigests        r=   	hash_blobr    sF    [[!F
MM+d#$Fa~r<   r<  c                      t        j                         }t         fdd      D ]  }|j                  |        |j	                         S )zHash the content of a binary buffer using SHA1.

    @param f: buffered binary stream to hash.
    @return: digested data as bytes.
    c                  B     j                  t        j                        S r   )r  r  DEFAULT_BUFFER_SIZE)r<  s   r=   <lambda>zhash_buffer.<locals>.<lambda>  s    affR%;%;< r<   r<   )r  sha1iterr  r  )r<  r  chunks   `  r=   hash_bufferr    s?     \\^F<cB e==?r<   c                 P    	 t        j                  |       ryy # t        $ r Y yw xY wr   )r  r  r  rb  s    r=   is_userr    -    <<      	%%c                 P    	 t        j                  |       ryy # t        $ r Y yw xY wr   )r  r  r  r  s    r=   is_groupr!  &  r  r  c                 ^    t         j                  d| |       t        j                  | |       y )NzRenaming %s to %s)rc   r   rC   renamesrcdests     r=   r#  r#  .  s!    II!3-IIc4r<   c                 *    | D ]  }t        ||        y r   )
ensure_dir)dirlistr  r  s      r=   ensure_dirsr*  4  s     1dr<   c                     t        j                  t        |             }t        |t	        |            sCdj                  |D cg c]  }t        |       c}      }t        d|dt        |      d      |S c c}w )N, (z) root types expected, got r  )	jsonloadsrw   ru   rA   re   r:   r   r   )rx   
root_typesdecodedtexpected_typess        r=   	load_jsonr4  9  si    jjt,-GguZ01J#?qCF#?@tG}.
 	
 N $@s   A9c                     t         j                  j                  |       }|t         j                  j                  |      k(  r| S t         j                  j                  |      r| S t	        |      S )zGet the last directory in a path that does not exist.

    Example: when path=/usr/a/b and /usr/a does not exis but /usr does,
    return /usr/a
    )rC   r   dirnamer  get_non_exist_parent_dir)r   p_paths     r=   r7  r7  D  sP     WW__T"F((77== K+F33r<   c                 
   t         j                  j                  |       st        |       }t	        t         j                  j                  |       d      5  t        j                  |        d d d        t        | |       |s|rht        |||       t        | j                  |      d   dd        }t        |      }|j                  D ]   }|j                  |      }t        |||       " y y t        | |       y # 1 sw Y   xY w)NTr   r   )rC   r   r  r7  r   r6  makedirschmodr  r   rF   r  joinpath)r   r  r  rm  non_existed_parent_dirsub_relative_dirsub_pathr   s           r=   r(  r(  U  s    77==!9$!?"''//$/4@ 	KK	dD5.e<  $DJJ/E$Fq$I!"$MN23H(.. 3#,,T2HdE23  	dD	 	s   C99Dc              #      K   	 |  | rd| g}t        j                   |       y y # | rd| g}t        j                   |       w w xY ww)Numount)r    )rA  
umount_cmds     r=   	unmounterrC  l  sL     ""F+JIIj! 6"F+JIIj! s   A% AAAc                     i } 	 t         j                  j                  d      rt        d      j	                         }d}n1t        j
                  d      }|j                  j	                         }d}d}|D ]  }	 |dk(  r|j                         \  }}}}	}
}nZt        j                  ||      }|j                  d      }|j                  d      }|j                  d      }|j                  d      }	|j                  d	d
      }|||	d| |<    t        j                  d| |       | S # t        $ r Y w xY w# t        t         f$ r t#        t        d       Y | S w xY w)Nz/proc/mountsr  mountz*^(/dev/[\S]+) on (/.*) \((.+), .+, (.+)\)$r   r@   rf  rn  z\040r  )fstype
mountpointoptszFetched %s mounts from %szFailed fetching mount points)rC   r   r   r   r_   r    rL   rF   rg  searchrm  r   r  rc   r   rg  r   r   )mounted
mount_locsmethodrN   mountrempliner  mprF  rH  _freq_passnoms                r=   mountsrS  v  sO   G"477>>.)'7BBDJF))G$C..0JF?  	F
V#>Dlln;S"fdE7		'62A''!*CBWWQZF771:D
 GS)B  GCL#	, 			-w? N   W 4s23N4s7   A3D4 8A7D%/4D4 %	D1.D4 0D11D4 4EEc                    t        |t              r|g}nKt        |t        t        f      rt        |      }n)|d}n$t	        dj                  t        |                  t               r|?dg}n;t               r.|g d}t        |      D ]  \  }}|dk(  rd||<   |dv sd	||<    nd
g}t               }t        j                         5 }	d}
t        j                  j                  |       |v r&|t        j                  j                  |          d   }nyd}|D ]\  }d}	 g d}|r|j!                  d|g       |j#                  |        |j#                  |	       t%        j$                  ||       |	}
|	} n st1        d| d|	d|      |j3                  d      s|dz  }t5        |
      5  |	 ||      }n	 |||      }|cddd       cddd       S # t&        t(        f$ r6}|r(t*        j-                  d| |dj/                        |       |}Y d}~d}~ww xY w# 1 sw Y   nxY w	 ddd       y# 1 sw Y   yxY w)a2  
    Mount the device, call method 'callback' passing the directory
    in which it was mounted, then unmount.  Return whatever 'callback'
    returned.  If data != None, also pass data to callback.

    mtype is a filesystem type.  it may be a list, string (a single fsname)
    or a list of fsnames.
    Nz6Unsupported type provided for mtype parameter: {_type})r  auto)ufscd9660msdosr  rW  )r  msdosfsrX  r   FrG  )rE  r  roz-t)
update_envzbFailed to mount device: '%s' with type: '%s' using mount command: '%s', which caused exception: %sr  zFailed mounting z to z	 due to: r  )ru   r:   r   rA   r   r  r   rB  rF  	enumeraterS  r!   tempdirrC   r   r   r   r  r    rg  r   rc   r   re   r   r8  rC  )devicecallbackri   mtypeupdate_env_for_mount	log_errormtypesindexrJ  tmpdrA  rG  failure_reasonmountcmdexcr  s                   r=   mount_cbri    sd   " %	ED%=	)e	DKK5k L 
 	
 z>XF	>/F%f- 	(LE5	! (u++ 'u		( hG				 ,77F#w. !1!1&!9:<HJ!N )!
)4H u6OOF+OOD)IIh3GH!F!%J)0 &t^5  ""3'#Jv 	|z*z40	 	O, ,$  ) ) 		9 #!HHX. &)N)*	 	 	O, , ,sP   	AH4AG3:H4-H	H4H%,HH4HH4H'	#H44H=c                  4    t        j                  t              S r   )obj_copydeepcopyr%   r;   r<   r=   get_builtin_cfgrm    s    [))r<   c                 l    t         j                  d|        t        j                  j	                  |       S )NzTesting if a link exists for %s)rc   r   rC   r   islinkr  s    r=   is_linkrp    s$    II/677>>$r<   c                    t         j                  d||        |rt        j                  j	                  |      rvt        j                  j                  t        j                  j                  |      dt        d      z         }t        j                  | |       t        j                  ||       y t        j                  | |       y )Nz$Creating symbolic link from %r => %rtmpr   )
rc   r   rC   r   r   re   r6  r   symlinkr  )sourcelinkforcetmp_links       r=   sym_linkrx    s~    II4dFC& 77<< 5ux{7JK


68$


8T"JJvtr<   c                 z    t         j                  d|        	 t        j                  |        y # t        $ r Y y w xY w)NzAttempting to remove %s)rc   r   rC   unlinkr   r  s    r=   del_filer{    s3    II'.
		$ s   . 	::c                 ^    t         j                  d| |       t        j                  | |       y )NzCopying %s to %s)rc   r   r  copyr$  s     r=   r}  r}    s!    II #t,
KKTr<   c                  |    	 t        j                  dt        j                               } | S # t        $ r d} Y | S w xY w)Nz%a, %d %b %Y %H:%M:%S %z??)timestrftimegmtimer   )tss    r=   time_rfc2822r    s@    ]]5t{{}E I  Is   (, ;;c                      ddl ddl G fddj                        } j                  j                  j                  d            }j                         }j                  |       |_        dt               sdndg} j                  d	z  | } |        }|j                  |j                  t        |            j                  |      j                  |      dd      d
k7  r|j                  |j                  dz  z   S t!        d      )a  Use sysctl(3) via ctypes to find kern.boottime

    kern.boottime is of type struct timeval. Here we create a
    private class to easier unpack it.
    Use sysctl(3) (or sysctl(2) on OpenBSD) because sysctlbyname(3) does not
    exist on OpenBSD. That complicates retrieval on NetBSD, which #defines
    KERN_BOOTTIME as 83 instead of 21.
    21 on NetBSD is KERN_OBOOTTIME, the kern.boottime up until NetBSD 5.0

    @return boottime: float to be compatible with linux
    r   Nc                   J    e Zd ZdW  j                  fdW  j                  fgZy)boottime.<locals>.timevaltv_sectv_usecN)r5   r6   r7   c_int64_fields_)ctypess   r=   timevalr  7  s    v~~.FNN0KLr<   r  cr      S   r@   r"  g    .Az/Unable to retrieve kern.boottime on this system)r  ctypes.util	StructureCDLLutilfind_libraryc_size_tsizeofvaluerS  c_intr  rb   byrefr  r  RuntimeError)r  libcr  
mib_valuesmibr  r  s         @r=   boottimer  '  s     M&"" M ;;v{{//45D??Dw'DJ	+2J 6<<!j
)C
)CLLZ)LLLL	
 	 zzCKK)333
H
IIr<   c                  2   d} d}	 t         j                  j                  d      r%d}t        d      }|r@|j	                         d   } 	 | S d}t        t        j                         t               z
        } | S # t        $ r t        t        d|z         Y | S w xY w)Nr  r  z/proc/uptimer   r  z&Unable to read uptime using method: %s)rC   r   r   r   rF   r:   r  r  r   r   rc   )
uptime_strrL  r,  s      r=   uptimer  R  s    JFG77>>.)#F%n5H%^^-a0
  FTYY[8:56J   Gs<vEFGs   AA6 
*A6 6BBc                 "    t        | |dd        y )Nr  )omoder  
write_file)r   contents     r=   append_filer  e  s    tWDt4r<   )preserve_moder  r  c                $    t        | dd||       y )Nr   r  )r  r  r  r  r  )r   r  r  s      r=   ensure_filer  i  s     b4}r<   c                 D    	 t        |       S # t        t        f$ r Y y w xY wr   )r8   r  r   )possible_ints    r=   safe_intr  q  s)    <  	" s   
 c                     t        |      }| r.|r+t        |       5  t        j                  | |       d d d        y y y # 1 sw Y   y xY wr   )r  r   rC   r;  )r   r  	real_modes      r=   r;  r;  x  sG    I	$ 	&HHT9%	& 	& t	& 	&s	   =Agrp_namec                     d}	 t        j                  |       j                  }|S # t        $ r t        j                  d|        Y |S w xY w)zt
    Returns the group id of a group name, or -1 if no group exists

    @param grp_name: the name of the group
    r"  z"Group %s is not a valid group name)r  r  r  r  rc   r   )r  r  s     r=   get_group_idr    sQ     CBll8$++ J  B		6AJBs   % AAr   c                 f    t        j                  t        j                   |       j                        S )z
    Returns the octal permissions of the file/folder pointed by the path,
    encoded as an int.

    @param path: The full path of the file/folder.
    )r   S_IMODErC   st_moder  s    r=   get_permissionsr    s!     <<--..r<   c                 ~    t        j                  |       }t        j                  |j                        j
                  S )zw
    Returns the owner of the file/folder pointed by the path.

    @param path: The full path of the file/folder.
    )rC   r   r  getpwuidst_uidpw_namer   sts     r=   	get_ownerr    *     
B<<		"***r<   c                 ~    t        j                  |       }t        j                  |j                        j
                  S )zw
    Returns the group of the file/folder pointed by the path.

    @param path: The full path of the file/folder.
    )rC   r   r  getgrgidst_gidgr_namer  s     r=   	get_groupr    r  r<   usernamec                 *   g }t        j                         D ],  }| |j                  v s|j                  |j                         . t        j                  |       j                  }|j                  t        j                  |      j                         |S )zp
    Returns a list of all groups to which the user belongs

    @param username: the user we want to check
    )	r  getgrallgr_memr  r  r  r  pw_gidr  )r  groupsrm  r  s       r=   get_user_groupsr    sp     F )u||#MM%--() ,,x
 
'
'C
MM#,,s#++,Mr<   )ensure_dir_existsr  rm  c          	      J   |r	 t        |       }|r+t        t        j                  j                  |       ||       d|j                         v rt        |      }d}nt        |      }d}	 d|z  }	t        j                  d| ||	t        |      |       t        |       5  t        | |      5 }
|
j                  |       |
j!                          d	d	d	       d	d	d	       t#        | |       y	# t        $ r Y w xY w# t        $ r d|z  }	Y w xY w# 1 sw Y   AxY w# 1 sw Y   ExY w)
a)  
    Writes a file with the given content and sets the file mode as specified.
    Restores the SELinux context if possible.

    @param filename: The full path of the file to write.
    @param content: The content to write to the file.
    @param mode: The filesystem mode to set on the file.
    @param omode: The open mode used when opening the file (w, wb, a, etc.)
    @param preserve_mode: If True and `filename` exists, preserve `filename`s
                          current mode instead of applying `mode`.
    @param ensure_dir_exists: If True (the default), ensure that the directory
                              containing `filename` exists before writing to
                              the file.
    @param user: The user to set on the file.
    @param group: The group to set on the file.
    )r  rm  brz   
charactersz%oz%rzWriting to %s - %s: [%s] %s %sr  N)r  r   r(  rC   r   r6  r   r|   rw   r   rc   r   rb   r   r,  r-  r.  r;  )rh  r  r  r  r  r  r  rm  
write_typemode_rfhs              r=   r  r    s    8 	"8,D 277??8,4uE
ekkmg&
(!
 II(G 
8	$ (E" 	bHHWHHJ	 
(D7  		  	 	 sG   C* *C9 D+"DD*	C65C69D
	D
D	DD"c                     t        j                  |       D ]X  }t         j                  j                  | |      }t         j                  j	                  |      rt        |       Nt        |       Z y)z
    Deletes all contents of a directory without deleting the directory itself.

    @param dirname: The directory whose contents should be deleted.
    N)rC   r7  r   re   r  r  r{  )r6  nodenode_fullpaths      r=   delete_dir_contentsr    sP     

7# $Wd377=='M"]#$r<   c                     t        j                         }t        |       }|d|j                         d|z  }|dt	               z  z  }|S )Nr  z by cloud-init v. z on %s)r$   version_stringr:   titler  )comment_charr  ci_verheaders       r=   make_headerr  	  sE    ##%FF
4::<@@F
h''FMr<   c           	      $   t        | t        t        f      s!t        dt	        j
                  |       z        d}|r|dz  }ddz  }d}| D ]  }t        |t        t        f      rRg }|D ]/  }|j                  dt        |      j                  d|      z         1 |d	j                  |      d
}|dz  }kt        |t              r||d
}|dz  }|t        dt	        j
                  |      d|       t        j                  d|       |S )Nz8Input to shellify was type '%s'. Expected list or tuple.r   z
#!/bin/sh
z%s%s%s%s)'\r  r  r   z'%s'r  r  r3  r   zUnable to shellify type 'z&'. Expected list, string, tuple. Got: zShellified %s commands.)ru   rA   r   r   r"   r   r  r:   r  re   rc   r   )cmdlist
add_headerr  escaped	cmds_mader   fixedr<  s           r=   shellifyr  	  s$   gt}-F""7+-
 	

 G= 00GI  dT5M*E FVs1v~~c7'CDEF")388E?;GNIc"")40GNI\'006> !* II'3Nr<   c                     |r| j                  |      r| t        |      d  } |r | j                  |      r| d t        |        } | S r   )r  rb   r8  )rj   prefixsuffixs      r=   strip_prefix_suffixr  8	  sE    $//&)CKM"$--'Ns6{l#Kr<   c                     t        j                  | d         y	 t        j                   |        y# t         j                  $ r Y yw xY w)Nr   FT)r    whichrf   )r  s    r=   _cmd_exits_zeror  @	  sG    zz#a&!		#  %% s   1 AAc                      t        g d      S )N)zsystemd-detect-virtz--quietz--containerr  r;   r<   r=   _is_container_systemdr  J	  s    LMMr<   c                      t        dg      S )Nzlxc-is-containerr  r;   r<   r=   _is_container_old_lxcr  N	  s    ./00r<   c                      t               syg d} t        j                  | d         yt        j                  |       \  }}|j                         dk(  S )NF)r  z-qnzsecurity.jail.jailedr   r*   )rL  r    r  rM   )r  rN   r(   s      r=   _is_container_freebsdr  R	  sG    <
3Czz#a&!YYs^FC99;#r<   c                     t         t        t        f} | D ]  } |       s y 	 t        d      }d|v ryd|v ry	 t        j                  j                  d      r t        j                  j                  d      sy	 t        d      j                         }|D ]>  }|j                  d      s|j                         j                  d	d      \  }}|d
k7  s> y 	 y# t        t
        f$ r Y w xY w# t        t
        f$ r Y yw xY w)zH
    Checks to see if this code running in a container of some sort
    Tr   	containerLIBVIRT_LXC_UUIDz/proc/vzz/proc/bcz/proc/self/statuszVxID:rX   r.   F)r  r  r  get_proc_envrg  r   rC   r   r  r   r_   r  rM   rF   )checkshelperpid1envlinesrj   _keyrl   s          r=   r  r  \	  s    	F  8	 q/'!( ) 
ww}}Z z)B	23>>@ 	 Dw'"jjl00a8s#:		  % W  W s:   C C 9/C, )(C, C, C, C)(C),C>=C>c                  @    t         j                  j                  d      S )z2Check to see if we are running in a lxd container.z/dev/lxd/sock)rC   r   r   r;   r<   r=   is_lxdr  	  s    77>>/**r<   c                 L   t         j                  j                  dt        |       d      }	 t	        |      }i }d\  }}|rd\  }}|j                  ||      }|j                  |      D ]"  }|s|j                  |d      \  }	}
|	s|
||	<   $ |S # t
        t        f$ r i cY S w xY w)aH  
    Return the environment in a dict that a given process id was started with.

    @param encoding: if true, then decoding will be done with
                     .decode(encoding, errors) and text will be returned.
                     if false then binary will be returned.
    @param errors:   only used if encoding is true.z/procr  )       =)r  =r   )	rC   r   re   r:   r  rg  r   rv   rF   )pidrt   errorsr	  r,  envnullequalr  rb  rl   s              r=   r  r  	  s     
gs3x	3B#B' C!KD%#e??8V4~~d# iiq)sCI J W 	s   B B#"B#c                     i }| j                         D ]  }	 |j                  dd      \  }}|||<    |S # t        $ r |}d}Y w xY w)Nr  r   T)rF   r  )kvstringr  r  rZ  rl   s        r=   keyval_str_to_dictr  	  sc    
C~~ 	3*JS# C J	  	CC	s   5AAc                 r    | j                  d      r| dd  } t        j                  j                  d| z        S )Nr  r  z/sys/class/block/%s/partition)r  rC   r   r$  )r^  s    r=   is_partitionr  	  s4    !77>>9FBCCr<   c                    t        |t              s|g}g }|D ]  }t        |t              r|j                  |       %t        |t        t        f      rmt        |      dk  st        |      dkD  rt        d      t        |      dk(  r#|d   r|j                  | t	        |      z         |j                  |d          t        d       |S )Nr   r@   z Invalid package & version tuple.r   zInvalid package type.)ru   r   r:   r  rA   rb   r  )version_fmtpkgspkglistpkgs       r=   expand_package_listr  	  s    dD!vG 8c3NN3cE4=)3x!|s3x!|"#EFF3x1}Q{U3Z78NN3q6" 677!8$ Nr<   c                 B   | j                  d      D cg c]  }|s|	 }}d}d}d}d}	t        |      D ]  \  }
}|j                         }t        |      dk  r# |j                  d|
dz   t        |      |        y|d   }|j                  d      D cg c]  }|s|	 }}t        |      t        |      kD  rt	        t        |      t        |            }|d| |d| k7  r|	t        |	      t        |      kD  r	 |j                  d      }
	 ||
dz      }||
d
z      }|}|}	|d   } |r|r|r
|rr||||fS y|r	|r|r|||fS yc c}w c c}w # t        $ r  |j                  d	|
dz   |       Y  yw xY w# t        $ r  |j                  d|
dz   |       Y  yw xY w)zRReturn the mount information for PATH given the lines from
    /proc/$$/mountinfo.r  N
   z$Line %d has two few columns (%d): %sr   rn  r   -z,Did not find column named '-' in line %d: %sr@   z/Too few columns after '-' column in line %d: %sr  )rF   r\  rb   r   minrd  r  
IndexError)r   mountinfo_linesr   get_mnt_optsr   path_elementsdevpthfs_typematch_mount_pointmatch_mount_point_elementsr  rj   r  mount_pointmount_point_elementsxmount_optionss                    r=   parse_mount_infor$  	  s    !%

3511Q5M5FG!%_- 9!4

 u:?CII6As5z4 Ah+6+<+<S+AGaQGG #$s='99 ()3}+=>!$a(:: &1c&7
$%7& 
	C A	AElG1q5\F (%9"as9!v g"3G%6FF
  g"3G%677O 6*  H.  	CII>At 		  	CIIA1q5$ 		s:   E	E	EE?EE: E76E7: FFc                     t        d      j                         D ]%  }|j                         dd \  }}}|| k(  s |||fc S  y)z<On older kernels there's no /proc/$$/mountinfo, so use mtab.	/etc/mtabNrf  )r   r_   rF   )r   rj   r  r   r  s        r=   
parse_mtabr'  )
  sR    {+668 0'+zz|BQ'7$W$7K//0 r<   c                    | j                  d      }t        |      dk(  r|d   S t        |      dk(  r|d   S |d   dv rx| dd  }t        j                  g d      \  }}|j                  d	      D ]:  }|j                         }t        |      dkD  s"|d   |k(  s+|d   } t        |      S  t        |      S t        j                  d
|        y )Nr  r   r   rf  r@   )r  gptgptidrV  ufsidr  )glabelstatusz-sr3  z)Unexpected input in find_freebsd_part: %s)rF   rb   r    r:   rc   rd   )fssplittedtarget_labelr   r  labelsr  s          r=   find_freebsd_partr2  2
  s    xx}H
8}{	X!	{	!A	A!"vyy!;<tjj& 	FLLNE5zA~%(l":Qx4y	
 4y?Dr<   c                     d }|j                  d      D ]K  }|j                         }t        |      dkD  s"t        j                  j	                  |d   | z         sH|} |S  |S )Nr3  r@   r   )rF   rb   rC   r   r   )r   mnt_list
path_foundrj   r  s        r=   get_path_dev_freebsdr6  E
  sb    Jt$ 

u:>bggnnU1X_=J
 r<   c                     t        j                   dd| gddg      \  }}t        |      r+t        j                   ddg      \  }}t        | |      }|y |}|j                         }t	        |d         }d|z   S )NrE  z-pr   r   r  r  )r    rb   r6  rF   r2  )r   r  ro   r4  r5  r  
label_parts          r=   get_freebsd_devpthr9  O
  s|    IIwd3!Q@MVS
3x))WdO43)$9

,,.C"3q6*JZr<   c                    t        j                   dg      \  }}d}| j                  d      D cg c]  }|s|	 }}d}d}d}	d}
|j                         D ]  }t        j                  ||      }|s|j                  d      }|j                  d      }|j                  d      D cg c]  }|s|	 }}t        |      t        |      kD  rxt        t        |      t        |            }|d| |d| k7  r|
t        |
      t        |      kD  r|}	|}
|j                  d      }|j                  d	      }|Ld
j                  |j                  d	      j                  d
      j                         j                  d            }t        j                  d||||       t        j                  d|      }|st               r|dvrt        |      }|}|	| k(  s n |	r|	| vry|rrr|	r	r|||	|fS yyyyrr|	r|||	fS yyyc c}w c c}w )zReturn the mount information for PATH given the lines ``mount(1)``
    This function is compatible with ``util.parse_mount_info()``rE  ze^(?P<devpth>[\S]+?) on (?P<mountpoint>[\S]+?) (\(|type )(?P<type>[^,\(\) ]+)( \()?(?P<options>.*?)\)$r  Nr  rG  r   r   r  rY   r,  zNfound line in mount -> devpth: %s, mount_point: %s, fs_type: %s, options: '%s'z^(/dev/.+)[sp]([0-9])$)zfsnfs)r    rF   r_   rg  rI  rm  rb   r  re   rM   rc   r   rL  r9  )r   r  mountoutputr  regexr   r  r  r   r  r  rj   rR  r!  r"  r  r#  devmmatch_devpths                      r=   parse_mountrA  ]
  s^    ))WI.[$	C 

 !%

3511Q5M5FK!%&&( 8IIeT""ggl++6+<+<S+AGaQGG #$s='99 ()3}+=>!$a(:: &1c&7
$%7& '%9"''&/	*$HH	"((-335;;DAM 				
 yy16:
)F (/F$q8t  1 =G(9m '+<mLL ?L(9G< G(9 '+<== ):G<M 6  Hs   HH'H	/H	c                 (   dt        j                         z  }t         j                  j                  |      r't	        |      j                         }t        | |||      S t         j                  j                  d      rt        |       S t        | |      S )Nz/proc/%s/mountinfor&  )	rC   getpidr   r   r   r_   r$  r'  rA  )r   r   r  mountinfo_pathr  s        r=   get_mount_inforE  
  sp    : *BIIK7N	ww~~n%~.99;eS,??		$$4..r<   optc                 H    t        | d      ^ }}||j                  d      v S )NTr  rY   rE  rF   )r   rF  r(   mnt_optss       r=   has_mount_optrK  
  s&    !$T:LQ(..%%%r<   Tr  .c                    |g }|i }t        j                         }d }|r	 t        t                     }	  ||i |}t        j                         |z
  }	d }
|	 t        t                     |z
  }
d|	z  }|rt        |
t              r	|d|
z  z  }n|dz  }	  | ||z          |S # t        $ r Y yw xY w# t        $ r Y Mw xY w# t        $ r Y |S w xY w# t        j                         |z
  }	d }
|'	 t        t                     |z
  }
n# t        $ r Y nw xY wd|	z  }|rt        |
t              r	|d|
z  z  }n|dz  }	  | ||z          w # t        $ r Y w w xY wxY w)Nz took %0.3f secondsz (%0.2f)z (N/A))r  	monotonicfloatr  r  ru   r   )r  r#  r  r   r   
get_uptimestartustartr  deltaudeltatmsgs               r=   r  r  
  s    |~NNEF	68_FD#F# 5(vx61 %u,&5*
V++ 	C$J J1  		    	J	!  5(vx61  %u,&5*
V++ 	C$J 		s   B# C B2 C #	B/.B/2	B>=B>	CCE.DE	DED(E:EE	EEEEc                 N    | j                  dd      }t        |      dkD  r|S | d fS )Nr?   r   )rsplitrb   )dottedrj  s     r=   expand_dotted_devnamerY    s,    ==a D
4y1}~r<   c                    |g }|g }g }i }||z   D ]  }	 t        | |z   |z   d      ||<    t        |      r*t	        dj                  dj                  |                  |S # t        $ r ||v r|j                  |       Y sw xY w)NFr  zMissing required files: {files}rY   )files)r  r   r  rb   r  r  re   )r  requiredoptionaldelimrn   r  r<  s          r=   pathprefix2dictr`    s     G
C  "	"%dUlQ&6eDCF" 7|-44388G;L4M
 	
 J ! 	"H}q!	"s   A%%BBc                 8   ddddd}dddd	}i }t        |       j                         D ]J  }	 |j                         \  }}}|rt	        |      ||   z  ||<   /||v s4t	        |      ||   z  |||   <   L |S # t        $ r |j                         \  }}d
}Y Ww xY w)N      r      @)kBmBBgBtotalfree	available)z	MemTotal:zMemFree:zMemAvailable:rg  )r   r_   rF   r  r8   )	meminforawmplierskmapr  rj   rZ  r  units	            r=   read_meminforq  1  s     %au=G$D
 Cw'224 	8	#zz|C 5zGDM1CHD[ Z'$-7CS	N	8 J  	JCD	s   A88BBc                 l   | }| j                  d      r| dd } n| j                  d      r| dd } dddd	d
d}| }d}|D ]%  }| j                  |      s|}| dt        |        }' 	 t        |      }|dk  rt        d|z        t	        |||   z        S # t        $ r}t        d|z        |d}~ww xY w)a}  Convert human string or integer to size in bytes

    In the original implementation, SI prefixes parse to IEC values
    (1KB=1024B). Later, support for parsing IEC prefixes was added,
    also parsing to IEC values (1KiB=1024B). To maintain backwards
    compatibility for the long-used implementation, no fix is provided for SI
    prefixes (to make 1KB=1000B may now violate user expectations).

    Future prospective callers of this function should consider implementing a
    new function with more standard expectations (1KB=1000B and 1KiB=1024B)

    Examples:
    10M => 10485760
    10MB => 10485760
    10MiB => 10485760
    iBNrg  r"  r   rb  rc  rd  l        )rg  KMGrL  r   z'%s' is not valid input.z'%s': cannot be negative)r8  rb   rO  r  r8   )r  size_inrn  nummplierrR  r   s          r=   human2bytesr{  I  s    " G}}TCRy	s	CRyEEFG
CF $==FqCF7#C$
FCj Qw3g=>>sWV_$%%  F3g=>AEFs   'B 	B3B..B3c                 j    | t        j                         d   } | dk(  xs | d   dk(  xr | dd dk(  }|S )z$Return True if platform is x86-basedNrn  x86_64r   r  r@   86)rC   rD   )
uname_archx86_arch_matchs     r=   is_x86r  t  sM    XXZ]
8+ 17AB4!7  r<   c                 ,    t        j                  |       S r   )emailmessage_from_string)r   s    r=   r  r  ~  s    $$V,,r<   c                  F   t        j                   ddgd      } t               }| j                  j                         D ]Q  }	 |j	                  d d      \  }}}|j                  d      s,|j                  t        j                  dd|             S |S # t
        $ r Y aw xY w)	Nz
dpkg-queryz--listTrJ   r@   )hiiiz:.*r   )
r    rp  rL   r_   rF   r  r  ru  rg  sub)rN   	pkgs_instrj   stater  r(   s         r=   get_installed_packagesr    s    
))\8,d
;CI

%%' 2	"jjq1OUC L)MM"&&C012   		s   B	B B c                  x   d} 	 t        t        | d            }|j                  dd      j                         dk(  ry	 t               }d|v ryt        d	d      }d|j                         v ryt        j                  j                  d
      ryy# t        $ r!}t
        j                  d| |       Y d }~ud }~ww xY w)Nrp  Tr[  rd  r   zubuntu-corez!Unexpected error loading '%s': %sz
snap_core=z/etc/system-image/channel.iniz/etc/system-image/config.d/F)ry  r   rz  r   r  rc   rd   rL  rC   r   r  )orpathorinfor   rG  r  s        r=   system_is_snappyr    s     FD#N6$FG::dB%%'=8 9
 mGw<DIG'	ww}}23  D7CCDs   9B 	B9B44B9c                    d }| j                         D ]  }|j                  d      s|dd  } n |y |j                  d      r|S |j                  d      rd|t        d      d  z   S |j                  d      rd|t        d      d  j                         z   S |j                  d      rTd	|t        d      d  j                         z   }t        j
                  j                  |      r|S t        |      }|r|d
   S |S d|z   S )Nzroot=r  r  r  z/dev/disk/by-label/zUUID=z/dev/disk/by-uuid/z	PARTUUID=z/dev/disk/by-partuuid/r   )rF   r  rb   r   rC   r   r   r  )rG  r  r  
disks_pathresultss        r=   rootdev_from_cmdliner    s   E}} >>'"GE } !$uS]_'=== #eCLN&;&A&A&CCC$$uS-=-?'@'F'F'HH 	 77>>*% '1: U?r<   c                 p    d }i } ||       D ]%  }|j                  dd      \  }}|s|}|s|s!|||<   ' |S )zGiven shell like syntax (key=value\nkey2=value2\n) in content
    return the data in dictionary form.  If 'add_empty' is True
    then add entries in to the returned dictionary for 'VAR='
    variables.  Set their value to empty_val.c                 0    t        j                  | d      S )NT)comments)shlexrF   )rp   s    r=   _shlex_splitz(load_shell_content.<locals>._shlex_split  s    {{4$//r<   r  r   )rF   )r  	add_empty	empty_valr  ri   rj   rZ  r  s           r=   ry  ry    sS    0 DW% ZZQ'
UEDI Kr<   c           	         t        |       }d}	 |t        |D cg c]$  }t        j                  j                  |      s#|& c}      z  }t	        |      dk(  rt
        j                  d|||        g S |dk(  rt
        j                  d|||        ||z   |kD  rnt        j                  |       ||z  }t
        j                  d|||       |S c c}w )Nr   z)%sAll files appeared after %s seconds: %sz6%sWaiting up to %s seconds for the following files: %sz*%sStill missing files after %s seconds: %s)	rp  rC   r   r   rb   rc   r   r  sleep)flistmaxwaitnaplenlog_preneedwaitedr<  s          r=   wait_for_filesr    s    u:DF
:1q(9Q:;;t9>II;	 IQ;IIH	 F?W$

6&) , II4gw K1 ;s
   $C
C
c                 <    d }| j                  d|g t               y)z-Helper to wait on completion of snap seeding.c                      t        j                  d      st        j                  d       y t        j                   g d       y )Nsnapz+Skipping snap wait, no snap command present)r  waitrA  zseed.loaded)r    r  rc   r   r;   r<   r=   r_  z&wait_for_snap_seeded.<locals>.callback  s+    zz&!IICD		;<r<   zsnap-seeded)freqN)runr&   )rb  r_  s     r=   wait_for_snap_seededr    s    = 
IImXrI9r<   c                 T    t        | d      }|d   j                  d      }|d   dk(  S )z1Check whether the given mount point is mounted rwTrH  r"  rY   r   rwrI  )r   r  
mount_optss      r=   mount_is_read_writer    s1    Kd;F!!#&Ja=D  r<   c                     t        j                  d      syddg}| r5t        j                  j	                  |       ry|j                  d| z  g       |r|j                  d|z  g       t        j                   |      S )zAInvoke udevadm settle with optional exists and timeout parametersudevadmNsettlez--exit-if-exists=%sz--timeout=%s)r    r  rC   r   r   r   )r   r  
settle_cmds      r=   udevadm_settler    sr    ::i  	X&J77>>&!069:;>G34599Z  r<   c                     t        |j                  |       t        j                         |rt        j                  |       |S )a  
    Print error to stderr and return or exit

    @param msg: message to print
    @param rc: return code (default: 1)
    @param fmt: format string for putting message in (default: 'Error:\n {}')
    @param sys_exit: exit when called (default: false)
    )file)printr  r4  r5  exit)r#  rcr  sys_exits       r=   rw  rw     s-     
#**S/

+Ir<   c                        e Zd ZdZ	 ddededededd f
 fdZededd fd	       Zd
 Z	d Z
d Zd Zd Zdd defdZ xZS )Versiona>  A class for comparing versions.

    Implemented as a named tuple with all ordering methods. Comparisons
    between X.Y.N and X.Y always treats the more specific number as larger.

    :param major: the most significant number in a version
    :param minor: next greatest significant number after major
    :param patch: next greatest significant number after minor
    :param rev: the least significant number in a version

    :raises TypeError: If invalid arguments are given.
    :raises ValueError: If invalid arguments are given.

    Examples:
        >>> Version(2, 9) == Version.from_str("2.9")
        True
        >>> Version(2, 9, 1) > Version.from_str("2.9.1")
        False
        >>> Version(3, 10) > Version.from_str("3.9.9.9")
        True
        >>> Version(3, 7) >= Version.from_str("3.7")
        True

    majorminorpatchrevrq   c                 2    t         t        |   | ||||      S )zPDefault of -1 allows us to tiebreak in favor of the most specific
        number)superr  __new__)clsr  r  r  r  	__class__s        r=   r  zVersion.__new__J  s    
 Wc*3ueSIIr<   r$   c           
      Z     | t        t        t         |j                  d                   S )a%  Create a Version object from a string.

        :param version: A period-delimited version string, max 4 segments.

        :raises TypeError: Raised if invalid arguments are given.
        :raises ValueError: Raised if invalid arguments are given.

        :return: A Version object.
        r?   )r   rB   r8   rF   )r  r$   s     r=   from_strzVersion.from_strQ  s&     T#c=7==#56799r<   c                 *    d| j                  |      k(  S )Nr   )_compare_versionr   others     r=   __gt__zVersion.__gt__^  s    D))%000r<   c                     | j                   |j                   k(  xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S r   r  r  r  r  r  s     r=   __eq__zVersion.__eq__a  sX    JJ%++% &

ekk)&

ekk)& EII%		
r<   c              #      K   | j                   | j                  | j                  | j                  fD ]  }|dk7  rt	        |        y yw)z)Iterate over the version (drop sentinels)r"  N)r  r  r  r  r:   )r   ns     r=   __iter__zVersion.__iter__i  sA     **djj$**dhh? 	ABw!f		s   A	Ac                 $    dj                  |       S )Nr?   )re   r   s    r=   __str__zVersion.__str__q  s    xx~r<   c                 *    t        t        |             S r   )hashr:   r   s    r=   __hash__zVersion.__hash__t  s    CIr<   r  c                     | |k(  ry| j                   |j                   kD  ry| j                  |j                  kD  ry| j                  |j                  kD  ry| j                  |j                  kD  ryy)zCompare this Version to another.

        :param other: A Version object.

        :return: -1 if self > other, 1 if self < other, else 0
        r   r   r"  r  r  s     r=   r  zVersion._compare_versionw  s]     5=::#::#::#88eiir<   )r"  r"  r"  r"  )r5   r6   r7   __doc__r8   r  classmethodr:   r  r  r  r  r  r  r  __classcell__)r  s   @r=   r  r  /  s    4 LNJJ%(J69JEHJ	J 
:s 
:y 
: 
:1
i C r<   r  r  r$   boundary_versionc                 f    |dk(  xs+ t         j                  |       t         j                  |      k  S )a  Determine if a deprecation message should be logged.

    :param version: The version in which the thing was deprecated.
    :param boundary_version: The version at which deprecation level is logged.

    :return: True if the message should be logged, else False.
    devel)r  r  )r$   r  s     r=   should_log_deprecationr    s:     w& ,'*:*:+			*	++, ,r<   )extra_messagescheduleskip_log
deprecateddeprecated_versionr  r  r  c                    t        t        d      st        t        dt                      |xs d}t	        | |z   |z   t        |      z         }t        j                  |      }t        |j                  |z   |j                        }|  d| d| d| j                         }	t        |t        j                        st        j                  }
n1t        t         d      rt"        j$                  }
nt        j&                  }
t)        t        d      }|s+||vr'|j+                  |       t         j#                  |
|	       t-        |
|	      S )a  Mark a "thing" as deprecated. Deduplicated deprecations are
    logged.

    @param deprecated: Noun to be deprecated. Write this as the start
        of a sentence, with no period. Version and extra message will
        be appended.
    @param deprecated_version: The version in which the thing was
        deprecated
    @param extra_message: A remedy for the user's problem. A good
        message will be actionable and specific (i.e., don't use a
        generic "Use updated key." if the user used a deprecated key).
        End the string with a period.
    @param schedule: Manually set the deprecation schedule. Defaults to
        5 years. Leave a comment explaining your reason for deviation if
        setting this value.
    @param skip_log: Return log text rather than logging it. Useful for
        running prior to logging setup.
    @return: NamedTuple containing log level and log message
        DeprecationLog(level: int, message: str)

    Note: uses keyword-only arguments to improve legibility
    r   r   z is deprecated in z  and scheduled to be removed in z. r  )r  	deprecatesetattrrp  r  r:   r  r  r  r  r  r  r   DEPRECATION_INFO_BOUNDARYr6  INFOrc   r   
DEPRECATEDWARNr  ru  r2   )r  r  r  r  r  r4   dedupr$   version_removeddeprecate_msglevel	log_caches               r=   r  r    s   < 9e$	5#%(!rGg%(::S]JKE12Ggmmh6FO,(
>
2gY	( fh	 
 "H>> 	l	#	5)IY.e}%%//r<   )r  c                       fd}|S )a~  Mark a "thing" as deprecated. Deduplicated deprecations are
    logged.

    @param deprecated_version: The version in which the thing was
        deprecated
    @param extra_message: A remedy for the user's problem. A good
        message will be actionable and specific (i.e., don't use a
        generic "Use updated key." if the user used a deprecated key).
        End the string with a period.
    @param schedule: Manually set the deprecation schedule. Defaults to
        5 years. Leave a comment explaining your reason for deviation if
        setting this value.

    Note: uses keyword-only arguments to improve legibility
    c                 J     t        j                          fd       }|S )Nc                  J     | i |}t        j                         |S )N)r  r  r  r  )r  r5   )r   r   rN   r  r  r  r  s      r=   	decoratorz2deprecate_call.<locals>.wrapper.<locals>.decorator  s3     ''C#5==+!	 Jr<   )	functoolswraps)r  r  r  r  r  s   ` r=   wrapperzdeprecate_call.<locals>.wrapper  s&    				 
		 r<   r;   )r  r  r  r  s   ``` r=   deprecate_callr    s    & Nr<   r  r'   c                 l   dg i}	 t        j                  t        | j                  d      d            }d|vrg |d<   |S # t        $ r) t
        j                  d| j                  d             Y |S t         j                  $ r0}t
        j                  dt        j                  |       Y d }~|S d }~ww xY w)Nscopeszhotplug.enabledFr[  zFile not found: %sz>Ignoring contents of %s because it is not decodable. Error: %s)r.  r/  r   r  r   rc   r   JSONDecodeErrorrd   r   HOTPLUG_ENABLED_FILE)r  r  r   s      r=   read_hotplug_enabled_filer    s    rNG#**5??+<=UK
 7" "GHN  L		&8I(JK N  
L))	
 	
 N
s   /A   .B31B3%B..B3c               #      K   d yw)ztContext manager that does nothing.

    Note: In python-3.7+, this can be substituted by contextlib.nullcontext
    Nr;   r;   r<   r=   nullcontextr  	  s      
s   )r   r   )    N)TTr   )r   r  )r   r   r  )r   r   r  r  )z
/etc/hosts)Nr^  NFNr   )rb  N)i  )NNN)NNNT)  )r   r  F)re  created)T)r   r  )NNF)z/proc/meminfoF)FN)g      ?r   )r   z	Error:
{}F)r   
contextlibr}  rk  r  r  r#  r  r  r  r  r.  r6  rC   os.pathr@  r  r   rg  r  r  rq  r   r   r  r4  r  base64r   collectionsr   r   r   r   r  r   r	   r
   pathlibr   typingr   r   r   r   r   r   r   r   r   r   r   r   r   urllibr   r  	cloudinitr   r   r   r   r   r   r    r!   r"   r#   r$   cloudinit.settingsr%   r&   cloudinit.helpersr'   ro  	getLoggerr5   rc   sepr  r   r   r  r   r   r2   rG   rO   rW   r:   rz   rw   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  rg   r)  r1  DEBUGr<  rB  rF  rL  rP  rS  rV  r[  r]  r_  rn  r  r  rK  r  r  r  r  r  r  r  r  r  r  r  r   r   r>  rD  rI  rH  rZ  r[  rc  rk  r   r  ra  r  r  r  r  r  r  r  r  r  r   PathLiker8   r  r   r  rL  r  r  r  r  r  r  r7  r   r  BufferedIOBaser  r  r!  r#  r*  r4  r7  r(  rC  rS  ri  rm  rp  rx  r{  r  r  r  r  r  r  r;  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r$  r'  r2  r6  r9  rA  rE  rK  rL  r  rY  r   r`  rq  r{  r  r  r  r  r  ry  r  r  r  r  rw  r  r  r  r  r  r  r;   r<   r=   <module>r     s         
   	   	   
  	       
   ) /  /          5' g! FFC v}}$v';';;
)+Z 
>    :Mc5j) M M
OeCJ' Oe O
% E  &+ +\	y 		 	
* D ,0 ..D ..h&$
4  	3 	4 	 mm)%X ( (   1 1 3 3 0 0 1 1%?%P :1 :1z6r 
 
60&M5`58G, 5 5p    
$B !4' %P(V .2 d @ 9= , ,^<
 F* & (8V%P0$ 0f
( EI* EI6 EI*$ EI*( EI=@> 04	bkk!" hud{+, 	
 , 04	Pbkk!"P hud{+,P 	P
 	P  $	d 	
,7t!*J '.oo- #-	-S  	2$$ 	 	
 !%w 4". " "%V 

[|*
 
	
 'J 'JT&5
 6;/3	&3 3 /# /# /+C +C ++C +C +c d3i & 

: 	
:z$"JN1 * *Z+
<	D6 14% K\E& T>n !u $/N&S &T &
 CL 
+ 36
+ +\ $($bggkk 00(&V-0>(>	:!!$ Xj$FG X Xv
,C 
,3 
,4 
," $(5050 50 C=	50
 50 50 50r EF""/2">A"JW  ( 
YtS$/ 
 
r<   