Hcf5ddlZddlZddlZddlZddlZddlZddlZddlmZm Z ddl m Z m Z m Z mZmZmZmZmZddl mZddl mZmZddl mZddl mZmZmZddlmZdd lmZm Z m!Z!m"Z"dd l#m$Z$m%Z%m&Z&m'Z'ejPZ)ejTejVe,Z-d Z.d Z/d e j`dejbdejfdZ2 d/d e j`deejfdejbdejde4f dZ5 d/d e j`de6de4de4ddf dZ7 d0d e j`de j:ddfdZ8dddddddd e j`de6de4de4d e4d!e6de4d"e ee6fd#Z9ddd$d e j`d%e e6d&e4fd'Zd(e6ddfd)Z: d1d(e6d*e ee;ddfd+Zy)2N)ListOptional)apicloudsconfigcontract entitlements event_logger exceptions livepatch)log)messagessecret_manager)status)systemtimerutil)AutoAttachCloudInstance)APPARMOR_PROFILESCLOUD_BUILD_INFODEFAULT_CONFIG_FILEDEFAULT_LOG_PREFIX)AttachmentDataattachment_data_filemachine_id_filetimer_jobs_state_file)zapt-news.servicezesm-cache.servicezua-timer.servicezua-timer.timerzua-auto-attach.pathzua-auto-attach.servicezua-reboot-cmds.servicezubuntu-advantage.service cfgcontract_client attached_atcddlm}tjt |t j ||||jy)Nrupdate_motd_messagesr r)uaclient.timer.update_messagingr#rwriter ua_statusrupdate_activity_token)rrr r#s 2/usr/lib/python3/dist-packages/uaclient/actions.py_handle_partial_attachr+8s= E~+FG ))+Fservices_to_be_enabledsilentc d}g}g} |D]m}t||jdd|j|\} } || z}| s|j|jNtj |jo |st|||tj||rot jt||D cgc]C\} }| tj j#t%|t'j(fEc}} t j*|D cgc]} | tj,fc} y#t j$r3} tjjt|||| d} ~ wt j$r!|jjd}YAt$r9} d}|jj|j| Yd} ~ }d} ~ wwxYwcc}} wcc} w)NT)rname assume_yes allow_betavariantr.)serviceF) error_msglog_path)failed_services)enable_entitlement_by_namer0r3appendeventservice_processedr ConnectivityErrorservice_failedr+UbuntuProError Exceptionservices_failedAttachFailureUnknownErrorziprUNEXPECTED_ERRORformatstrpro_logget_user_or_root_log_file_pathAttachFailureDefaultServices!E_ATTACH_FAILURE_DEFAULT_SERVICES)rr-rr r.retr7unexpected_errorsenable_by_default_serviceent_retreasonexcer0 exceptions r*_enable_default_servicesrREs CO$)? P %8.33199 OGV 7NC&&'@'E'EF''0I0N0N'O P6 sO[A o. 66,/'):, !(i  1188&))n%,%K%K%M9 !  99!0!8EEF! +   ' ' 6;;<sO[A  $ $8==> $8==>  ##$ !!s7A2E?AH #HG=.F3G=<G=.G88G=token allow_enablereturncddlm}ddlm}tj j |tj|}tjjtjj}|j||}|jj| ||t&j(j+|j-dij-dt'j(|} t/j| |r:tj0||jj2} t5|| ||| t7jt9| ||t;j<y#t j"$r!} |jj%| d} ~ wwxYw) aC Common functionality to take a token and attach via contract backend :raise ConnectivityError: On unexpected connectivity issues to contract server or inability to access identity doc from metadata service. :raise ContractAPIError: On unexpected errors when talking to the contract server. r)+check_entitlement_apt_directives_are_uniquer")tz)contract_token attachment_dtNmachineTokenInfo machineId)rr-rr r.r$)uaclient.entitlementsrWr&r#rsecrets add_secretrUAContractClientdatetimenowtimezoneutcadd_contract_machinemachine_token_filer'r %EntitlementsAPTDirectivesAreNotUniquedeleterget_machine_id cache_cleargetrget_enabled_by_default_servicesr rRrrrstart) rrSrTr.rWr#rr new_machine_tokenrP machine_idr-s r*attach_with_tokenrpszE%%e,//4O##''8+<+<+@+@'AK'<<K=  !233C8  %%'"&&'92>BBV**3/J*%!)!I!I ''44"  !#9+#  ~+FG KKM1  ; ; %%'s(FG#F??Gcloudcxtj|}|j|}|d}t|||y)a\ :raise ConnectivityError: On unexpected connectivity issues to contract server or inability to access identity doc from metadata service. :raise ContractAPIError: On unexpected errors when talking to the contract server. :raise NonAutoAttachImageError: If this cloud type does not have auto-attach support. )instance contractToken)rSrTN)rr`%get_contract_token_for_cloud_instancerp)rrqrTr tokenResponserSs r* auto_attachrwsE//4O#IIJM / *Ec\Br,)r1r2 access_onlyr3r. extra_argsr0r1r2ryr3rzctj|||}|||||||} |s=tjtj j | j| jtj\} } | r?|s=tjtjj | j| | fS)z Constructs an entitlement based on the name provided. Passes kwargs onto the entitlement constructor. :raise EntitlementNotFoundError: If no entitlement with the given name is found, then raises this error. )rr0r3)r1r2 called_nameryrz)title) r entitlement_factoryr:infor ENABLING_TMPLrDr}enablerProgressWrapper ENABLED_TMPL) rr0r1r2ryr3r.rzent_cls entitlementrMrNs r*r8r8s".. dGG  K  8))00{7H7H0IJ!(()<)<)>?OGVv 8((//k6G6G/HI F?r,)simulate_with_tokenshow_allrrc~|rtj|||\}}||fStj||}d}||fS)z6 Construct the current Pro status dictionary. )rrSrrrr)r(simulate_statusr)rrrrrJs r*rrsS//%  3;!!cH= 3;r,filenamecgd}d}d} tj|\}}|rfg}|jdD]*}tj||s|j |,tj |dj|yy#tj$rW}tjdt|tj dj|t|Yd}~yd}~wwxYw)z Helper which gets ubuntu_pro apparmor logs from the kernel from the last day and writes them to the specified filename. ) journalctlz-bz-kz--since=1 day agoz7apparmor=\".*(profile=\"ubuntu_pro_|name=\"ubuntu_pro_)N z!Failed to collect kernel logs: %s{}-error)rsubpsplitresearchr9 write_filejoinr ProcessExecutionErrorLOGwarningrErD)rcmd apparmor_re kernel_logs_ apparmor_logs kernel_linerPs r*_write_apparmor_logs_to_filers :CLKK BS) Q M*006 6 99[+6!((5 6   h -(@ A   + +? 8#a&A*++H5s1v>>?sB C5A C00C5 return_codesc tj|j|\}}tj||y#tj $r8}tjdj |t|Yd}~yd}~wwxYw)zCHelper which runs a command and writes output or error to filename.)rcsrN)rrrrr rrDrE)rrroutrrPs r*_write_command_output_to_filer/sm)SYY[l;Q (C(  + +?*++H5s1v>>?s(AB .BB cjxstjtjj t gfdtjDS)Nc3K|]:}t|tjjr|j<ywN) issubclassr repoRepoEntitlement repo_file).0rrs r* z#_get_state_files..Bs: +|'8'8'H'HI   & & sAA) cfg_pathrlog_filerua_filepathrr ENTITLEMENT_CLASSESr%s`r*_get_state_filesr;sM ++ %%**   +??  r, output_dirc  tddj|tdjtjdj|tddj|tddj|td jd j t Dcgc]}d |vsd j|c}d j|t D]1}tdj|dj||ddg3t |d\}}tjdj|tj|tjtj}tjdj|tj|t|}tjrt!j"dt$nt!j&g}t)|D]q\} } tj*tj,| } tjt.j0j |dj| | s|t;j:t<dzzD]} t.j0j?| s# tj,| } tj*| } tjrtj| | tjt.j0j |t.j0jA| | tCdj|tDD]9} t.j0j?| s# tGjH| |;ycc}w#t2$r+} t4j7d| t9| Yd} ~ d} ~ wwxYw#t2$r+} t4j7d| t9| Yd} ~ d} ~ wwxYw#t2$r*} t4j7d| t9| Yd} ~ d} ~ wwxYw)zG Write all relevant Ubuntu Pro logs to the specified directory zcloud-idz{}/cloud-id.txtz {} statusz{}/livepatch-status.txtzsystemctl list-timers --allz{}/systemd-timers.txtzujournalctl --boot=0 -o short-precise -u cloud-init-local.service -u cloud-init-config.service -u cloud-config.servicez{}/cloud-init-journal.txtzjournalctl -o short-precise {} z.servicez-u {}z{}/pro-journal.txtzsystemctl status {}z {}/{}.txtr)rFrz{}/pro-status.json)clsz{}/environment_vars.jsonNz user{}.logz&Failed to collect user log file: %s %s*zFailed to load file: %s %sz{}/apparmor_logs.txtzFailed to copy file: %s %s)%rrDr LIVEPATCH_CMDr UA_SERVICESrrrjsondumpsrDatetimeAwareJSONEncoderget_pro_environmentrwe_are_currently_rootrFget_all_user_log_filesUSER_LOG_COLLECTED_LIMITget_user_log_file enumerateredact_sensitive_logs load_fileosrr?rrrEglobrisfilebasenamerrshutilcopy)rrsr4 pro_statusrenv_vars state_filesuser_log_files log_file_idxrcontentrPfs r* collect_logsrJs"%,,Z8"9223!((4"%&&z2" & $**:6" ,44 HH,7Kq:?"K  ##J/  % ! ( ( 1   z7 3Q  sU3MJ ##J/ :4#@#@A'')H "))*5 8 #3'K  % % ' &&()B*BC'') * #,N";  h 001A1A(1KLG    Z)<)<\)JK  499%7#%=> > 77>>!   **1-009G))+!!!W-    Z)9)9!)<=w (!!7!>!>z!JK 77>>!   Az*CLH  KK98SV     91c!fE  .  91c!fE sU- O7O(A+O!PQ! P* PP Q ! QQ  R Q==R)F)Tr)?rarrloggingrrrtypingrruaclientrrrrr r r r r rFrrrr(rrruaclient.cloudsruaclient.defaultsrrrruaclient.files.state_filesrrrrget_event_loggerr: getLoggerreplace_top_level_logger_name__name__rrrUAConfigr`r+EnableByDefaultServiceboolrRrErprwr8rintrrrr,r*rs  !   $-(((3  & %%'g:::8DE   ,  ,.. ,"" ,$ A A !@!@AA..A"" A  AP 5 5 55  5  5vC C  ) )C C6&*% % % %  %  %% %c#%V*.  "# ,B3B4B4=A ) )&.tCy&9 )  ) &// bfoob3br,