x[hN7JdZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z m Z ddl m Z m Z mZmZmZmZmZddlmZddlmZddlmZmZddlmZdd lmZmZmZeej@ejBe"Z#Gd d eZ$e$d d e$dde$ddgZ%e$dde$dde$dde$dde$dde$dde$dde$d d!e$d"d#e$d$d%e$d&d%e$d'd(g Z& dRd)eejNd*ejNfd+Z(d,ee)d-ejTd.e)d*ee)fd/Z+d,ee)d-ejTd.e)d*dfd0Z,d1ejTd2ejTd3e-d*dfd4Z.d5ejTd3e-d*dfd6Z/d5ejTd*dfd7Z0d5ejTd3e-d*dfd8Z1d9e e)e fd*eejTfd:Z2ejTd;fdejTd*eejTfd?Z4d@ejTd*eejTfdAZ5d5ejTd9e e)e fd@ejTd>ejTd3e-d*df dBZ6ejTdCejTdDdEfdFe)d9e e)e fd@ejTd>ejTd3e-d*df dGZ7dHe8d*dfdIZ9 dSdFe)dHe8dJe-dKe-d*df dLZ:dMe)dNejvd*e8fdOZOs ((C        # *     M    $   Mrcmd file_pathmsgcN|jjdd t|j}t ||t j d||j|S#t$r8}t |t|t j d|d}Yd}~|Sd}~wwxYw)zCHelper which runs a command and writes output or error to filename.Tparentsexist_okcollected %s to file '%s'collecting %s failed.N) parentmkdirrstdoutrLOGdebugstemrr)r?r@rAoutputes r_write_command_output_to_filerPs 4$7Dc!! 9f% -sINNC M !9c!f% )3/ MsA## B$,-BB$c|jjdd |jd5}tj|||dddt j d||jy#1swY+xYw#t$r5}t|t|t j d|Yd}~yd}~wwxYw)zHelper which runs a command and writes output or error to filename. `subprocess.call` is invoked directly here to stream output to the file. Otherwise memory usage can be high for large outputs. TrCw)rJstderrNrFrG) rHrIopen subprocesscallrKrLrMOSErrorrr)r?r@rAfrOs r_stream_command_output_to_filerYs4$7D ^^C  5A OOC! 4 5 -sINNC  5 5 09c!f% )3//0s.A?A3 A?3A<8A?? B=+B88B=rout_dirinclude_sensitivecD|jrz|s+|jjtjzr6|j ddt ||t jd|yt jd|yt jd|y)z-Collect a file into what will be the tarball.TrCzcollected file: %sz#sensitive file %s was not collectedzfile %s did not existN) is_filestatst_modeS_IROTHrIrrKrLtrace)rrZr[s r _collect_filerbsn ||~  3 3dll B MM$M 6 w  II*D 1 II;T B )40rlog_dirctD]Z}tj||jddj}t tj|j|d\|rdt D]Z}tj||jddj}t tj|j|d\yy)z'Obtain subiquity logs and config files.NT)r[)INSTALLER_APPORT_FILESpathlibPathrrHrb INSTALLER_APPORT_SENSITIVE_FILES)rcr[src_filedestination_dirs r_collect_installer_logsrls+ !,,w ab0ABII LL ' "  8 H%ll7HMM!"4EFMMO  X]]+"&  rcbtddg|dz d}tgd|dz d}|s|xsd }y y ) z8Include cloud-init version and dpkg version in the logs. cloud-initz --versionversionzcloud-init --versionr?r@rA)z dpkg-queryz--showz-f=${Version} rnz dpkg-versionz dpkg versionz not-availableN)rP)rcrodpkg_vers r_collect_version_inforrsM+ ; 'I% "G - EN* H -o rc|rtdg|dz dtgd|dz dtgdtj|d d y ) z0Include dmesg and journalctl output in the logs.dmesgz dmesg.txtz dmesg outputrp) journalctlz--boot=0-o short-precisez journal.txtzsystemd journal of current boot)ruz --boot=-1rvrwzjournal-previous.txtz systemd journal of previous bootN)rYrgrh)rcr[s r_collect_system_logsrxsV&  + # =M) - # >,,w(>? .rlog_cfgc#ZKt|D]}tj|yw)z7Get paths for cloud-init.log and cloud-init-output.log.N)rrgrh)ryrs r_get_cloudinit_logsr{s+$G,!ll4  !s)+z /etc/cloud etc_cloud_dirc#lK|dz |dz dgfd|jdDEd{y7w)zGet paths for all files in /etc/cloud. Excludes: /etc/cloud/keys because it may contain non-useful sensitive data. /etc/cloud/templates because we already know its contents keys templatesz99-installer.cfgc3ZK|]"}|jvr|jvr|$ywN)namerH).0rignores r z!_get_etc_cloud..s0  99F "t{{&'@ s(+z**/*Nglob)r|rs @r_get_etc_cloudrsE  # F !&&v.s )424 cloud_dirc tj|jd|jd|jdd|jdD|jdS)zkGet paths for files in /var/lib/cloud. Skip user-provided scripts, semaphores, and old instances. zdata/*z handlers/*zseed/*c3BK|]}|js|ywr)r])rps rrz%_get_var_lib_cloud..s@qAIIK@sz instance/*zinstance/handlers) itertoolschainrrs r_get_var_lib_cloudrsW ??x |$x @INN<0@*+  rrun_dirc$|jdS)zGet all paths under /run/cloud-init except for hook-hotplug-cmd. Note that this only globs the top-level directory as there are currently no relevant files within subdirectories. *rrs r _get_run_dirr$s << rct|t||t||t|D]>}t ||t j |jjdz d@tjtt|t|D]>}t ||t j |jjdz |@y)z8Collect all cloud-init logs into the provided directory./TrrN)rrrxrlr{rbrgrhrH relative_torrrrr)rcryrrr[logfiles r_collect_logs_into_tmp_dirr-s'""34G%67&w/    gll7+22>>sC C   ??Y/W%     gll7+22>>sC C   rz/run/cloud-initz/var/lib/cloudTtarfilec tjj|}tjt j jjd}t|5}tj||}t|||||tdd|d|t|j|ddgd d d t j#d |y #1swY xYw) aCollect all cloud-init logs and tar them up into the provided tarfile. :param tarfile: The path of the tar-gzipped file to create. :param log_cfg: The cloud-init base configuration containing logging cfg. :param run_dir: The path to the cloud-init run directory. :param cloud_dir: The path to the cloud-init cloud directory. :param include_sensitive: Boolean, true means include sensitive data. zcloud-init-logs-%Y-%m-%d)dir)rcryrrr[tarczfz-CrNzWrote %s)osrabspathrnowrutcdatestrftimerrgrhrrrreplacerKinfo)rryrrr[dir_nametmp_dirrcs r collect_logsrNsggoog&G X\\"'')223MN  W  ,,w1"/   G $$y]B7    &HHZ!'  s ,ACC(r4chtj|dk(rtj}n&|dk(rtj}ntj }t j|tj}|jtjdt j|y)zSet up the logger for CLI use. The verbosity controls which level gets printed to stderr. By default, DEBUG and TRACE are hidden. rrez %(message)sN) r reset_loggingloggingINFODEBUGTRACErKsetLevel StreamHandler setFormatter Formatter addHandler)r4levelhandlers r _setup_loggerrwsx  A~  a  LL##%G **=9:NN7rredact_sensitiveinclude_userdatact|tjdk7r td|rtj dt g}|jt||jtj|jjtj|jj| |stj dyy)z:Handle calls to 'cloud-init collect-logs' as a subcommand.rz!This command must be run as root.z;The --include-userdata flag is deprecated and does nothing.)ds_deps)rryrrr[zWARNING: Sensitive data may have been included in the collected logs. Please review the contents of the tarball before sharing or rerun with --redact-sensitive to redact sensitive data.N)rrgetuid RuntimeErrorrKwarningr read_cfgrcfgrgrhpathsrr)rr4rrinits rcollect_logs_clirs) yy{a>?? I   DMMO TZZ//0,,tzz334..    F  r_nameargsctj} t|j|j|j |j y#t$r%}t|tjYd}~yd}~wwxYw)z}Handle the CLI interface to the module. Parse CLI args, redirect all exceptions to stderr, and return an exit code. )r4rrrr)fileNre) r> parse_argsrr4rrr: ExceptionprintsysrS)rrrOs rhandle_collect_logs_argsrse < " " $D nnLL!22!]]    acjj!s7A BA;;B__main__rr)rTF)?__doc__r;rrrrgr^rUrrrtypingrrrrr r r cloudinit.logr cloudinit.stagesr cloudinit.subprrcloudinit.temp_utilsrcloudinit.utilrrrCustomLoggerType getLoggerrrKrrirfr<r>rrhrPrYboolrbrlrrrxr{rrrrrintrr NamespacerexitrrrrrshN   'HHH!!6(@@ 7 # #%6W%6%6x%@A 24I"$568:MN $ @(K79O79O6 D:BDBG" 9=I5}E9;GC"L156 X,, -6 6r c|| c] &D cD&||D25D D& 1 ,, 1!( 1BF 1  1 \\.2 * .7<< .D .  \\.2 ,! #s(^! gll!#/',,|"<<< gll, ',, 8GLL3I ',,8GLL+A \\ #s(^ \\ ||      H)GLL):;*gll+;<" &" &" #s(^&"\\&"|| &"  &"  &"RST*!"         BCx/A/Ac& z CHH %b*,*A*A*C DEr