(KXc @s(ddlmZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"dd lm#Z#dd l$m%Z%da'ej(e)Z*id e j+6Z,d ej-ej.Z/d dgZ0de1fdYZ2de3fdYZ4de5fdYZ6de5fdYZ7dZ8dZ9ddZ:ddZ;ddZ<dddZ=dZ>dZ?d Z@d!ZAeBd"ZCd#ZDd$ZEeBeBdejFd%ZGeHfd&ZId'ZJeKd(ZLdd)ZMd*ZNdd+ZOdd,ZPd-ZQddd.ZRdd/d/d/d/d0ZSeKd1ZTejUd2ZVejUd3ZWejUd4ZXd5ZYd6ZZeBdd7Z[d/d/d8d9Z\dd:Z]d8d;ddd<dddd=Z^deHfd>Z_d/d/d8d;d?d@Z`dAZadBZbddCZcdDZddEZedFdGZfddJddKZgdLZhdMZidNZjdOZkdPZldQZmddRdeKddSZndTZodUZpdeKdVZqdWZrdXddYZsdddZZtddd[Zud\Zvd]Zwdd^Zxd_Zyd`ZzdaZ{dbdcZ|ddZ}deZ~ddfZejUdgZdhZdeKdeBdiZdjZeKdkZdlZdmZdnZdoZdpZdqdrZdsZdtZdqdudvZdwZdddeBeKeKdxZdydzd{Zd|ZeBd}Zddd~ZdZdZdZdZdZe*dZdZdZe*dZdZddeKdZdZdde jj+dZdS(i(tStringION(timporter(tlog(tmergers(tsafeyaml(t type_utils(t url_helper(tversion(t CFG_BUILTINt_s_-.()srunning-in-containerslxc-is-containertProcessExecutionErrorcBs)eZdZdddddddZRS(sr%(description)s Command: %(cmd)s Exit code: %(exit_code)s Reason: %(reason)s Stdout: %(stdout)r Stderr: %(stderr)rcCs|sd|_n ||_|s-d|_n ||_t|ttfsWd|_n ||_|srd|_n ||_|sd|_n ||_|r||_n d|_|j i|jd6|jd6|jd6|jd6|jd6|jd 6}t j ||dS( Nt-s'Unexpected error while running command.tt descriptiontcmdt exit_codetstdouttstderrtreason( RR t isinstancetlongtintRRRRt MESSAGE_TMPLtIOErrort__init__(tselfRRRRR Rtmessage((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyRWs4                  N(t__name__t __module__RtNoneR(((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyR Nst SeLinuxGuardcBs&eZedZdZdZRS(cCsIytjd|_Wntk r2d|_nX||_||_dS(Ntselinux(Rt import_moduleRt ImportErrorRtpatht recursive(RR"R#((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyRs    cCs$|jr|jjrtStSdS(N(Rtis_selinux_enabledtTruetFalse(R((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyt __enter__scCs|jr|jjrtjjtjj|j}t|}t}yEtj|}t j |kr|jj ||t j t }nWnt k rnX|rtjd||j|jj|d|jqndS(Ns,Restoring selinux mode for %s (recursive=%s)R#(RR$tosR"trealpatht expandusertstrR&tlstattstattST_MODEt matchpathconR%tOSErrortLOGtdebugR#t restorecon(Rt excp_typet excp_valuetexcp_tracebackR"t do_restoretstats((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyt__exit__s!     (RRR&RR'R9(((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyRs tMountFailedErrorcBseZRS((RR(((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyR:stDecompressionErrorcBseZRS((RR(((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyR;sc sNtj|tjdjt_fd}td|S(NsCreated temporary file %scsjjdS(N(tunlinktname((tfh(s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyt unlink_nowsR?(ttempfiletNamedTemporaryFileR1R2R=tdel_fileR<tsetattr(tkwargsR?((R>s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pytExtendedTemporaryFiles  cGstj}|dkrfy||tjdWqttdtj|tjdqXntjd|tj|dS(Nis&Failed forking and calling callback %sis(Forked child %s who will run callback %s(R(tforkt_exittlogexcR1Rtobj_nameR2(tchild_cbtargstfid((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pytfork_cbs     cCsdt|tr|tkSddddg}|r>||}nt|jj|kr`tStS(Nttruet1tontyes(RtboolR%R+tlowertstripR&(tvaltaddonst check_set((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pytis_trues  cCsdt|tr|tkSddddg}|r>||}nt|jj|kr`tStS(Ntofft0tnotfalse(RRRR&R+RSRTR%(RURVRW((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pytis_falses  cCs*|s tSt|tr|St||S(N(R&RRRRX(RURV((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyttranslate_bools i cCsK|stjtj}ndjgtd|D]}tj|^q/S(NR i(tstringtletterstdigitstjointrangetrandomtchoice(tstrlent select_fromt_x((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pytrand_strscCsMytt|diSWn,tk rH}|jtjkrBiSnXdS(Ntdefault(t load_yamlt load_fileRterrnotENOENT(tfnamete((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyt read_confs cGstt|S(N(tsortedt uniq_merge(tlists((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pytuniq_merge_sortedscGsg}xm|D]e}t|ttfre|jjd}g|D]}t|rD|^qD}n|j|q Wt|S(Nt,(RR+t basestringRTtsplittlentextendt uniq_list(Rtt combined_listta_listta((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyRss (cCsx,tjD]\}}|j||}q Wg}x*|D]"}|tkr<|j|q<q<Wx |D]}|j|d}qiW|j}|S(NR (tFN_REPLACEMENTSt iteritemstreplacet FN_ALLOWEDtappendRT(tfntktvtremovals((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pytclean_filenames    cCsyJtt|}tjtjddd|}|jSWdQXWn/tk r{}|rf|St t|nXdS(Ntrbi( RR+t contextlibtclosingtgziptGzipFileRtreadt ExceptionR;(tdatatquiettbuftghRp((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyt decomp_gzip+s$cCs|s dS|jdd}|dj}t|dkrQ|dj}nd}| s||dks||jdkrd}n| s|dks|jdkrd}n||fS(Nt:iiis-1tnone(NN(RRxRTRyRS(tug_pairt ug_partedtutg((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pytextract_usergroup7s% % cCst}xtjtjj|dD]k}tjj|sFq(ntjj|dd!}|j}|r(|jddkr(|||t|}n|S(N(RR+Rw(RRRjRU((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pytget_cfg_option_strs   cCsEitjd6tjd6tjd6tjd6tjd6S(Ntplatformtreleasetpythontunametdist(RRtpython_versionRtlinux_distribution(((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyt system_infos     cCs||kr|S||dkr$gS||}t|trZg|D] }|^qD}|St|tsxt|}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(RRtlistRwR+(RRRjRURtcval((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pytget_cfg_option_lists  cCs5|}x(|D] }||kr#|S||}q W|S(N((RtkeypRjtcurttok((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pytget_cfg_by_paths   cCs,t||\}}t||||fS(N(tget_output_cfgtredirect_output(tcfgtmodetoutfmtterrfmt((s;/opt/freeware/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 %st it>s>>tabRt|tshelltstdins"Invalid type for output format: %ss!Invalid type for error format: %s(RXR(tenvirontgetR1R2RRRRxRt subprocesstPopenR%tPIPERRtdup2tfileno( RRto_outto_errRtargtowithtnew_fptproc((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyRsN            R c Csg}|j|pdd}|r4t|}n|dk rU|dd|7}n|j|pdd|j|pwd|j|pd|j|pd|j|pdtj|S(NR Rs%s(RR+Rturlparset urlunparse( tschemethosttportR"tparamstquerytfragmenttpiecestnetloc((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pytmake_urls cCs~|rt|}ni}x\|D]T}|r"tj|}|sRtj}ntj|}|j||}q"q"W|S(N(treversedRtdict_extract_mergerstdefault_mergerst constructtmerge(tsrcstreverset merged_cfgRtmergers_to_applytmerger((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyt mergemanydicts ccs7tj}ztj||VWdtj|XdS(N(R(tgetcwdtchdir(tndirtcurr((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyR-s    ccs-tj|}z |VWdtj|XdS(N(R(tumask(tn_msktold((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyR7s cks*tj|}z |VWdt|XdS(N(R@tmkdtemptdel_dir(RDttdir((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyttempdir@s cCsdj|d|ddd|S(Ns{0:{fill}{align}{size}}tfilltalignt^tsize(tformat(RRtmax_len((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pytcenterLscCs!tjd|tj|dS(NsRecursively deleting %s(R1R2tshutiltrmtree(R"((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyR Qsc 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 listtcapturesFailed running %s [%s]s.Runparts: %s failures in %s attempted commands(R(R"tisdirRRR+RRRrtlistdirRbRtaccesstX_OKRtsubpR&R RHR1Rt RuntimeErrorRy( tdirpt skip_no_existt exe_prefixtfailedt attemptedtprefixtexe_nametexe_pathRp((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pytrunpartsVs.    '  icCsjy4t|||\}}||d<||dRRBR8RARCt file_pathR?RpR*((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pytread_file_or_urls0    " cCs|}yot|}tjdt||tj|}t||sntd|tj |fn|}WnLt j tt fk rt|dkrtjdqt tdnX|S(NsKAttempting to load yaml from string of length %s with allowed root types %ss2Yaml load allows %s root types, but got %s insteadis/load_yaml given empty string, returning defaultsFailed loading yaml blob(R+R1R2RyRtloadRRRRItyamlt YAMLErrort ValueErrorRH(tblobRjtallowedtloadedt converted((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyRks   ic Cs|jdrd|}n|jdr4|}n|jddkrh|d|}|d|}n&d|d|f}d|d|f}t||||}d}|jrt|} t| d i}nt||||} d} | jrt| } | } n|| fS( NR<s file://%ssfile://s%sis user-datas meta-datas%s%s%sRj(RERRKRtokR+Rk( R,R-R.R@t file_retriestud_urltmd_urltmd_respR/tmd_strtud_respR0tud_str((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyR(s(       cCsttj|dt}g|D]}|jdr"|^q"}g|D]-}tjjtjj||rJ|^qJ}g}x0|D](}|jt tjj||qWt |S(NRs.cfg( RrR(RR%tendswithR"RRbRRqR(tconfdtconfstftcfgsR((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pyt read_conf_ds( - &cCst|}t}d|kr}|d}|rt|ttfsetd|tj|fqzt|j}qn#t j j d|rd|}n| st j j | r|St |}t ||gS(Ntconf_ds8Config file %s contains 'conf_d' with non-string type %ss%s.d(RqR&RR+RwRRRIRTR(R"RRaR(tcfgfileRR]t confd_cfg((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pytread_conf_with_confd s     c Cs|dkrt}nd}d}t|}t|}t|}g}|j|}x{|dkr|j|||}|dkr|}n|j||||!jjdd|j|||}q`Wdj|S(Nscc:tend_ccis\ns (Rt get_cmdlineRyRRRDRRb( tcmdlinet tag_beginttag_endtbegin_ltend_ltclenttokenstbegintend((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pytread_cc_from_cmdline"s"         cCsC|jd}|dks/||ddkr3|S|jddS(Ns iis s (RR(R?tpos((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pytdos2unixAs cCsd|kr8|d}t|d|jdd}nd|kr|djddkr|d}|d|jd }n7|jdt}d|kr|d}n |j}||fS(NtfqdnthostnameRi(RRxRt get_hostnameR%(RtcloudRtRu((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pytget_hostname_fqdnIs  "%    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(RRlt splitlinesRRTRxRyR(RutfilenameRttlinethashposR((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pytget_fqdn_from_hostsas$      scloud-config-urlRIs #cloud-configcCs|dkrt}nt|}d}d}x(|D] }||kr7||}Pq7q7W|sedSt|}|jj|r|jr||t|fS||dfS(N(NNN(RRgtkeyval_str_to_dictRKR?RERTR+(tnameststartsRhRRIRtresp((s;/opt/freeware/lib/python2.7/site-packages/cloudinit/util.pytget_cmdline_urls       c Csctdkrt}ddtf}i}x|D]}ytj|dddtjtj}g|| LABEL=