jbc @s ddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlmZmZddlmZmZddlmZ ddl!Z!ddl"m#Z#ddl"m$Z%ddl"m&Z&ddl"m'Z'dd l"m(Z(dd l"m)Z)dd l"m*Z*dd l"m+Z+dd l,m-Z-yddl.m/Z/Wne0k rdZ/nXda2e%j3e4Z5ide j66Z7dej8ej9Z:dZ;dZ<dddgdgdgfZ=e/ddZ>e/dd Z?dd!Z@dd"ZAd#d$ZBd#d%ZCd&ZDd'ZEd(ZFd)ZGid*d+6d,d-6d.d/6d0d16d2d36d4d56d6d76d8d96d:d;6d<d=6d>d?6d@dA6dBdC6dDdE6dFdG6dHdI6dJdK6ZHdLeIfdMYZJdNeKfdOYZLdPeMfdQYZNdReMfdSYZOdTZPddUZQddVZRddWZSdXddYZTddZZUd[ZVd\ZWd]ZXd^ZYeZeZd_Z[d`Z\daZ]eZeZde%j^dbZ_dcZ`e/ddZaebdeZcddfZddgdhZeddiZfe/djZge/dkZhddlZiddmZjdnZkdddoZlddpdpdpdpdqZmebdrZnejodsZpejodtZqduZrdvZseZddwZtdpdpdxdyZuddzZvdewfd{Zxdpdpdxd|dgd}Zyd~ZzdZ{ddZ|ddZ}dZ~ebdZddZdZdZdZdZdZdZdddebddZdebdZdZdZdebeZdZe/dZdZdddZdddZdddZdZdZdZddZdZdZdZddZewfdZdZdZdZddZejodZdZddddZdZdZebdZdZdZdZdZdZddZdZdZddebdZdZdZdddeZebebebddddd ZdddZdZeZdZdddZdZdZd#ddZdZdZdZe5ebdZdZdZdZdZdZdZe5ebdZdZdddZddebdZdZdde jj6dZdebdZdZdZdZddZdZdZddZdZdZdZebddZddpdZdZdddZdZdS(iN(tENOENTtENOEXEC(t b64decodet b64encode(tparse(timporter(tlog(tmergers(tsafeyaml(t temp_utils(t type_utils(t url_helper(tversion(t CFG_BUILTIN(t lru_cachecCs d}|S(s+pass-thru replace for Python3's lru_cache()cSs|S(N((tf((s2/usr/lib/python2.7/site-packages/cloudinit/util.pytwrapper:s((R((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyR8s t_s_-.()ttruet1tontyestofft0tnotfalsessystemd-detect-virts--quiets --containersrunning-in-containerslxc-is-containercCs.tddgdtd|\}}|jS(Ntdpkgs--print-architecturetcapturettarget(tsubptTruetstrip(RtoutR((s2/usr/lib/python2.7/site-packages/cloudinit/util.pytget_architectureRsc Cs;idd6dd6dd6dd6}i}ytd d gd td |\}}xO|jD]A}|jd \}}}||kr\|j|||ls( RRt splitlinest partitionRtvaluestlentLOGtwarningtjointProcessExecutionErrortdict( RtfmaptdataR Rtlinetfnametvaltktmissingterr((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyt _lsb_releaseYs"$ +   cCs#t|dkrt|StS(Nt/(t target_pathR@(R((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyR)qs cCs|dkrd}nSt|tjs:td|n.tjj|}|jdrh|d}n|sr|Sx*t |r|ddkr|d}quWtjj ||S(NtRAsUnexpected input for target: %ss//ii(NRC( tNonet isinstancetsixt string_typest ValueErrortostpathtabspatht startswithR2R5(RRJ((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyRBys   sutf-8cCs#t|tjr|S|j|S(N(RERFRGtdecode(tblobtencoding((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyt decode_binaryscCs#t|tjr|S|j|S(N(RERFt binary_typetencode(ttextRO((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyt encode_textscCs6t|}y|jdSWntk r1|SXdS(Nsutf-8(RRMtUnicodeDecodeError(tsourcetdecoded((s2/usr/lib/python2.7/site-packages/cloudinit/util.pytb64ds   cCs4t|ts!|jd}nt|jdS(Nsutf-8(REtbytesRRRRM(RV((s2/usr/lib/python2.7/site-packages/cloudinit/util.pytb64escCs}|jdt}tjry|jdkryt|try|j}|rc|jrc|j}nd}|j |dS|S(NRMRSsutf-8tsurrogateescape( t get_payloadRRFtPY3tget_content_maintypeRERYt get_charsett input_codecRM(tpartt cte_payloadtcharsetRO((s2/usr/lib/python2.7/site-packages/cloudinit/util.pytfully_decoded_payloads   s/sys/class/dmi/idtboard_asset_tagsbaseboard-asset-tagt board_vendorsbaseboard-manufacturert board_namesbaseboard-product-namet board_serialsbaseboard-serial-numbert board_versionsbaseboard-versiont bios_datesbios-release-datet bios_vendors bios-vendort bios_versions bios-versiontchassis_asset_tagschassis-asset-tagtchassis_vendorschassis-manufacturertchassis_serialschassis-serial-numbertchassis_versionschassis-versiont sys_vendorssystem-manufacturert product_namessystem-product-nametproduct_serialssystem-serial-numbert product_uuids system-uuidtproduct_versionssystem-versionR6cBsGeZdZdZddddddddZdZddZRS(sr%(description)s Command: %(cmd)s Exit code: %(exit_code)s Reason: %(reason)s Stdout: %(stdout)s Stderr: %(stderr)st-c Cs|s|j|_n ||_|sO| rC|tkrCd|_qXd|_n ||_t|tjsy|j|_n ||_|s|dkr|j|_ q||_ n|j ||_ |s|dkr|j|_ q||_ n|j ||_ |r||_ n |j|_ ||_ |ji|j|jd6|j|jd6|j|jd6|j|j d6|j|j d6|j|j d6}tj||dS( Ns(Exec format error. Missing #! in script?s'Unexpected error while running command.R$tcmdt exit_codetstdouttstderrtreason(t empty_attrRwRR$RERFt integer_typesRxRDRzt _indent_textRyR{terrnot MESSAGE_TMPLt_ensure_stringtIOErrort__init__( tselfRyRzRxRwR$R{Rtmessage((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyRsB             cCs t|tjr|jS|S(s1 if data is bytes object, decode (RERFRQRM(RRS((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyRsicCsZd}d|}t|tjr=|j}|j}n|j|j|||S(sY indent text on all but the first line, allowing for easy to read output s t (RERFRQRRtrstriptreplace(RRSt indent_leveltcrtindent((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyR~"s   N(t__name__t __module__RR|RDRRR~(((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyR6s 3 t SeLinuxGuardcBs&eZedZdZdZRS(cCsIytjd|_Wntk r2d|_nX||_||_dS(Ntselinux(Rt import_moduleRt ImportErrorRDRJt recursive(RRJR((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyR2s    cCs$|jr|jjrtStSdS(N(Rtis_selinux_enabledRtFalse(R((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyt __enter__<scCs |j s|jj rdStjj|js7dStjj|j}tjrdt|}ny-tj |}|jj ||t j Wnt k rdSXtjd||jy|jj|d|jWn,t k r}tjd||j|nXdS(Ns,Restoring selinux mode for %s (recursive=%s)Rs,restorecon failed on %s,%s maybe badness? %s(RRRIRJtlexiststrealpathRFtPY2tstrtlstatt matchpathcontstattST_MODEtOSErrorR3tdebugRt restoreconR4(Rt excp_typet excp_valuetexcp_tracebackRJtstatste((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyt__exit__Bs&     (RRRRRR(((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyR1s tMountFailedErrorcBseZRS((RR(((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyR[stDecompressionErrorcBseZRS((RR(((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyR_scOstj}|dkrsy|||tjdWqtk rottdtj|tjdqXntjd|tj|dS(Nis&Failed forking and calling callback %sis(Forked child %s who will run callback %s( RItforkt_exitt ExceptiontlogexcR3R tobj_nameR(tchild_cbtargstkwargstfid((s2/usr/lib/python2.7/site-packages/cloudinit/util.pytfork_cbcs      cCsat|tr|tkSt}|r8t||}ntj|jj|kr]tSt S(N( REtboolRt TRUE_STRINGStlistRFt text_typetlowerRR(R<taddonst check_set((s2/usr/lib/python2.7/site-packages/cloudinit/util.pytis_truers !cCsat|tr|tkSt}|r8t||}ntj|jj|kr]t StS(N( RERRt FALSE_STRINGSRRFRRRR(R<RR((s2/usr/lib/python2.7/site-packages/cloudinit/util.pytis_false}s !cCs*|s tSt|tr|St||S(N(RRERR(R<R((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyttranslate_bools i cCsWtj}|s%tjtj}ndjgtd|D]}|j|^q;S(NRCi(trandomt SystemRandomtstringt ascii_letterstdigitsR5trangetchoice(tstrlent select_fromtrt_x((s2/usr/lib/python2.7/site-packages/cloudinit/util.pytrand_strs cCsG|sd}nx1trBtddd|}||krPqqW|S(NRCRiR(RR(t dictionarytpostfixtnewkey((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyt rand_dict_keys   cCsJytt|diSWn)tk rE}|jtkr?iSnXdS(Ntdefault(t load_yamlt load_fileRRR(R;R((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyt read_confs cGstt|S(N(tsortedt uniq_merge(tlists((s2/usr/lib/python2.7/site-packages/cloudinit/util.pytuniq_merge_sortedscGs}g}xj|D]b}t|tjrb|jjd}g|D]}t|rA|^qA}n|j|q Wt|S(NR+(RERFRGRtsplitR2textendt uniq_list(Rt combined_listta_listta((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyRs (cCsx,tjD]\}}|j||}q Wg}x*|D]"}|tkr<|j|q<q<Wx |D]}|j|d}qiW|j}|S(NRC(tFN_REPLACEMENTStitemsRt FN_ALLOWEDtappendR(tfnR=R.tremovals((s2/usr/lib/python2.7/site-packages/cloudinit/util.pytclean_filenames    cCsyctjt|}tjtjddd|'}|rRt|j S|j SWdQXWn2t k r}|r|St tj |nXdS(Ntrbi( RFtBytesIORTt contextlibtclosingtgziptGzipFileRDRPtreadRRR(R9tquietRMtbuftghR((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyt decomp_gzips$cCs|s dS|jdd}|dj}t|dkrQ|dj}nd}| s||dks||jdkrd}n| s|dks|jdkrd}n||fS(NR*iiis-1tnone(NN(RDRRR2R(tug_pairt ug_partedtutg((s2/usr/lib/python2.7/site-packages/cloudinit/util.pytextract_usergroups% % cCst}xtjtjj|dD]k}tjj|sFq(ntjj|dd!}|j}|r(|jddkr(|||scCs|sd}ntjj|s%iSt|}d}tj||}|r|j}|djjdd|d<|ddkrd|d.+) release (?P[\d\.]+) \((?P[^)]+)\)tnames linuxisred hat enterprisetredhattIDR t VERSION_IDR"tVERSION_CODENAME( RIRJRRtretmatcht groupdictRR0(t release_filetredhat_releaset redhat_regexRtgroup((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyt_parse_redhat_releaseBs   !  c Csd}d}d}i}tjjdr?ttd}n|sQt}n|r |jdd}|jdd}d|ksd|krtj}nR|jdd}|st j d|jd d}|r|j d }qn|d krd }qntjjd rKd}t ddg\}}|j }nrd}z(ytj}Wntk rwnXWdd}x|D]}|rd}qqW|stjdnX|S|||fS(NRCs/etc/os-releaseRRtslestsuseRs[^ ]+ \((?P[^)]+)\)tVERSIONR"trhelRs/bin/freebsd-versionRtunames-risPUnable to determine distribution, template expansion may have unexpected results(RCRCRC(RIRJRtload_shell_contentRR"tgettplatformtmachineRRRRRtdistRRDR3R4( t distro_nametdistro_versiontflavort os_releaseRRR,tfoundtentry((s2/usr/lib/python2.7/site-packages/cloudinit/util.pytget_linux_distro\sN       cCsitjd6tjd6tjd6tjd6tjd6td6}|dj}d}|dkr|dd j}|dkr|}q|dkrd}q|dkrd}q|dkrd}qd}n|dkr|}n||d<|S( NR*tsystemR'tpythonR'R,tunknowntlinuxitarchtcentostdebiantfedoraR&R$tubuntut linuxminttmintRtopensusesopensuse-tumbleweeds opensuse-leapR#twindowstdarwinRR (R8R9R:R;srhelssuse(R<R=R>(R?sopensuse-tumbleweeds opensuse-leapssles(R@sdarwinsfreebsd(R*R4R'tpython_versionR'R3R(tinfoR4tvart linux_dist((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyRs4                   cCs||kr|S||dkr$gS||}t|trZg|D] }|^qD}|St|tjs{t|}n|gS(s 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. N(RDRERRFRGR(RRRR<R.tcval((s2/usr/lib/python2.7/site-packages/cloudinit/util.pytget_cfg_option_lists  cCsYt|tjr$|jd}n|}x(|D] }||krG|S||}q1W|S(sReturn 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.RA(RERFRGR(RtkeypRtcurttok((s2/usr/lib/python2.7/site-packages/cloudinit/util.pytget_cfg_by_paths  cCs,t||\}}t||||fS(N(tget_output_cfgtredirect_output(tcfgtmodetoutfmtterrfmt((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyt fixup_outputs c CsXttjjdr)tjddS|s;tj}n|sMtj}n|rptjd|||j dd\}}|dks|dkrd}|dkrd }nt ||}nF|d krt j |d t d t j}|j}ntd ||r.tj|j|jn||krptjd||tj|j|jdSn|rTtjd|||j dd\}}|dks|dkrd}|dkrd }nt ||}nF|d krt j |d t d t j}|j}ntd||rTtj|j|jqTndS(Nt_CLOUD_INIT_SAVE_STDOUTs5Not redirecting output due to _CLOUD_INIT_SAVE_STDOUTsRedirecting %s to %sRit>s>>tabtwbt|tshelltstdins"Invalid type for output format: %ss!Invalid type for error format: %s(RRItenvironR)R3RRRyRzRRt subprocesstPopenRtPIPERYt TypeErrortdup2tfileno( RPRQto_outto_errROtargtowithtnew_fptproc((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyRMsN            RCc Cs|p dg}d}|r*t|}n|dk rK|dd|7}n|j|pZd|j|pmd|j|pd|j|pd|j|pdtj|S(NRCR*s%s(RRDRturlparset urlunparse( tschemethosttportRJtparamstquerytfragmenttpiecestnetloc((s2/usr/lib/python2.7/site-packages/cloudinit/util.pytmake_url+s cCs~|rt|}ni}x\|D]T}|r"tj|}|sRtj}ntj|}|j||}q"q"W|S(N(treversedRtdict_extract_mergerstdefault_mergerst constructtmerge(tsrcstreverset merged_cfgRNtmergers_to_applytmerger((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyt mergemanydict@s ccs7tj}ztj||VWdtj|XdS(N(RItgetcwdtchdir(tndirtcurr((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyR~Os    ccs-tj|}z |VWdtj|XdS(N(RItumask(tn_msktold((s2/usr/lib/python2.7/site-packages/cloudinit/util.pyRYs cCsdj|d|ddd|S(Ns{0:{fill}{align}{size}}tfilltalignt^tsize(tformat(RSRtmax_len((s2/usr/lib/python2.7/site-packages/cloudinit/util.pytcenterbscCs!tjd|tj|dS(NsRecursively deleting %s(R3Rtshutiltrmtree(RJ((s2/usr/lib/python2.7/site-packages/cloudinit/util.pytdel_dirgsc Csx|rtjj| rdSg}g}|dkr>g}nEt|tr_t|g}n$t|trw|}n tdxttj |D]}tjj ||}tjj |rtj |tj r|j|yt||gdtWq?tk r;}ttd||j|j|q?XqqW|rt|rttdt|t|fndS(Ns%exe_prefix must be None, str, or listRsFailed running %s [%s]s.Runparts: %s failures in %s attempted commands(RIRJtisdirRDRERRR^RtlistdirR5RtaccesstX_OKRRRR6RR3Rxt RuntimeErrorR2( tdirpt skip_no_existt exe_prefixtfailedt attemptedtprefixtexe_nametexe_pathR((s2/usr/lib/python2.7/site-packages/cloudinit/util.pytrunpartsls.    '  icCsjy4t|||\}}||d<||d, . Values can be none when metadata_only is True and no cfg or metadata provides hostname info. tfqdnthostnameRit metadata_only(RRRt get_hostnameR(RNtcloudRRR((s2/usr/lib/python2.7/site-packages/cloudinit/util.pytget_hostname_fqdnKs  "%   s /etc/hostscCsd}yxt|jD]}|jd}|dkrM|d|!}n|j}|seqn|j}t|dkrqn||dkr|d}PqqWWntk rnX|S(s 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). t#iiiiN(RDRR/RRRR2R(RtfilenameRR:thashposR ((s2/usr/lib/python2.7/site-packages/cloudinit/util.pytget_fqdn_from_hostsjs$      c CsWtdkrt}d }i}x|D]}ytj|dddtjtj}g|| LABEL=