oB]c@sdZddlZddlmZddlZddlZddlZddlmZddlm Z ddl m Z m Z m Z mZmZmZddgZd Zd Zdd Zd Zd ZdZdZddZdZdZedkrejendS(sGDefine 'collect-logs' utility and handler to include in cloud-init cmd.iN(tdatetime(tINSTANCE_JSON_SENSITIVE_FILE(ttempdir(tProcessExecutionErrortchdirtcopyt ensure_dirtsubpt write_files/var/log/cloud-init.logs/var/log/cloud-init-output.logs/run/cloud-inits%/var/lib/cloud/instance/user-data.txtc Cs|s!tjdddd}n|jddddd d d d d d|jddd dd d|jddd tddd dd djt|S(s2Build or extend and arg parser for collect-logs utility. @param parser: Optional existing ArgumentParser instance representing the collect-logs subcommand which will be extended to support the args of this utility. @returns: ArgumentParser with proper argument configuration. tprogs collect-logst descriptions)Collect and tar all cloud-init debug infos --verboses-vtactiontcounttdefaultitdestt verbositythelpsBe more verbose.s --tarfiles-tscloud-init.tar.gzsOThe tarfile to create containing all collected logs. Default: cloud-init.tar.gzs--include-userdatas-ut store_truetuserdatasPOptionally include user-data from {0} which could contain sensitive information.(targparsetArgumentParsert add_argumenttFalsetformattUSER_DATA_FILE(tparser((s</usr/lib/python2.7/site-packages/cloudinit/cmd/devel/logs.pyt get_parsers     cCstjdkrdStfS(s3Return a list of files to ignore if we are non-rooti((tostgetuidR(tcurdirtfiles((s</usr/lib/python2.7/site-packages/cloudinit/cmd/devel/logs.pyt _copytree_ignore_sensitive_files3scCs|yt|\}}Wn:tk rR}t|t|td|d|n&Xt||td|d||SdS(sCHelper which runs a command and writes output or error to filename.scollecting %s failed. is collected %s N(RRRtstrt_debug(tcmdtfilenametmsgRtoutt_te((s</usr/lib/python2.7/site-packages/cloudinit/cmd/devel/logs.pyt_write_command_output_to_file:s cCs#||krtjj|ndS(N(tsyststderrtwrite(R$tlevelR((s</usr/lib/python2.7/site-packages/cloudinit/cmd/devel/logs.pyR!Gs cCsNtjj|r6t||td|d|ntd|d|dS(Nscollected file: %s isfile %s did not exist i(RtpathtisfileRR!(R-tout_dirR((s</usr/lib/python2.7/site-packages/cloudinit/cmd/devel/logs.pyt _collect_fileLs ic Cs}|r,tjdkr,tjjddStjj|}tjj j d}dj |}t dd}tjj ||}tdd gtjj |d d |}td d ddgtjj |dd|}|s|r|nd}ntd|d|tdgtjj |dd|tddddgtjj |dd|xtD]}t|||qrW|rtt||ntjj |d} t| tjjtrtjttjj | ddttdtd|ntdtd|t|+td d!||j|d"d#gWd$QXWd$QXtjjd%|dS(&sCollect all cloud-init logs and tar them up into the provided tarfile. @param tarfile: The path of the tar-gzipped file to create. @param include_userdata: Boolean, true means include user-data. isMTo include userdata, root user is required. Try sudo cloud-init collect-logs is%Y-%m-%dscloud-init-logs-{0}tdirs/tmps cloud-inits --versiontversionscloud-init --versions dpkg-querys--shows-f=${Version} s dpkg-versions dpkg versions not-availables!collected cloud-init version: %s tdmesgs dmesg.txts dmesg outputt journalctls--boot=0s-os short-precises journal.txtssystemd journal of current boottruntignorescollected dir %s sdirectory '%s' did not exist ttartczvft/tNs Wrote %s (RRR)R*R+R-tabspathRtutcnowtdatetstrftimeRRtjoinR(R!tCLOUDINIT_LOGSR0RRtexiststCLOUDINIT_RUN_DIRtshutiltcopytreeRRRtreplace( ttarfiletinclude_userdataRR=tlog_dirttmp_dirR2tdpkg_vertlogtrun_dir((s</usr/lib/python2.7/site-packages/cloudinit/cmd/devel/logs.pyt collect_logsTsX           2cCst|j|j|jS(s:Handle calls to 'cloud-init collect-logs' as a subcommand.(RMRFRR(tnametargs((s</usr/lib/python2.7/site-packages/cloudinit/cmd/devel/logs.pythandle_collect_logs_argsscCst}td|jS(s4Tool to collect and tar all cloud-init related logs.s collect-logs(RRPt parse_args(R((s</usr/lib/python2.7/site-packages/cloudinit/cmd/devel/logs.pytmains t__main__(t__doc__RRRRCR)tcloudinit.sourcesRtcloudinit.temp_utilsRtcloudinit.utilRRRRRRR@RBRtNoneRRR(R!R0RMRPRRt__name__texit(((s</usr/lib/python2.7/site-packages/cloudinit/cmd/devel/logs.pyts*    .      7