o Hcfc@sddlZddlZddlZddlZddlmZddlmZmZm Z m Z m Z m Z ddl mZmZmZmZmZmZmZmZmZddlmZddlmZmZmZddlmZeZ e!e"e#Z$dZ%Gd d d ej&Z'dS) N)exists)AnyDictListOptionalTupleUnion) apiaptcontract event_logger exceptionshttpmessagessystemutil)base)ApplicationStatusCanDisableFailureCanDisableFailureReason)status_cache_filez<^linux-image-([\d]+[.-][\d]+[.-][\d]+-[\d]+-[A-Za-z0-9_-]+)$c seZdZdZdZdZdZdZdZdZ e de e e dffdd Ze de fd d Ze de fd d Ze dee fddZe dee fddZe dee fddZdefddZe ejde fddZ d>dedeeeefffdd Zde fddZdej defddZ!de fd d!Z"dej fd"d#Z#dej fd$d%Z$dej fd&d'Z%d(d)Z&d*d+Z'dee(ee)j*ffd,d-Z+d.d/Z, d>d0e-e e.fd1e-e e.fd2edeffd3d4 Z/  d?dej d5eee d6eddfd7d8Z0dej ddfd9d:Z1 d@dej d;efd s z6RepoEntitlement.purge_kernel_check..Fmessage_operationmsgT)research RE_KERNEL_PKGr"rngroupis_interactiver #NonInteractiveKernelPurgeDisallowedr`rPURGE_KERNEL_REMOVALr(rFrbrget_kernel_info uname_releasePURGE_CURRENT_KERNELget_installed_ubuntu_kernelsPURGE_NO_ALTERNATIVE_KERNELrprompt_for_confirmationPURGE_KERNEL_CONFIRMATION)r package_listrWrymcurrent_kernelinstalled_kernelsalternative_kernelsrrrrlsH   z"RepoEntitlement.purge_kernel_checkcCsd}|r|dtj|dtdd|Dd}|r4|dtj|dtdd|Dd}|rC|dtjdtjifgdS) NFrZcSsg|]}|jqSrr"r~ryrrrr-sz4RepoEntitlement.prompt_for_purge..TcSsg|]\}}|jqSrr)r~ry_rrrr7srr)r`rWARN_PACKAGES_REMOVALrcreate_package_list_strWARN_PACKAGES_REINSTALLrPROCEED_YES_NO)rrxrwrWpromptrrrrp!s8   z RepoEntitlement.prompt_for_purgecs<tfdd|D}|rt|tjj|ddSdS)Ncsg|] }|jvr|jqSrrrinstalled_packagesrrrNs  z3RepoEntitlement.execute_removal..r\)r get_installed_packages_namespurge_packagesrUNINSTALLING_PACKAGES_FAILEDr()rrx to_removerrrruHs zRepoEntitlement.execute_removalcs0tfdd|D}|rt|dSdS)Ncs*g|]\}}|jvrd|j|jqS)z{}={})r"r(ver_str)r~ryrrrrr`s  z5RepoEntitlement.execute_reinstall..)r rreinstall_packages)rrw to_reinstallrrrrt[s z!RepoEntitlement.execute_reinstallc Cstjtjj|jdf}|j}|didi}|d}|s*tjtjj|jdfS|d}|ss  z. FT)r)allr load_filestripsplitbool)rr7apt_filerrr_check_apt_url_is_appliedsz)RepoEntitlement._check_apt_url_is_applied orig_accessdeltas allow_enablec s0t|||r dS|di}|di}|d}|d}t}|r-|r-|} n|\} } | tjkr:dS| |sxt d|j |t tjj|j d|di} | did} | rjt|j| |t|t|rt d |t tjjd |d |jt|d dS) a1Process any contract access deltas for this entitlement. :param orig_access: Dictionary containing the original resourceEntitlement access details. :param deltas: Dictionary which contains only the changed access keys and values. :param allow_enable: Boolean set True if allowed to perform the enable operation. When False, a message will be logged to inform the user about the recommended enabled service. :return: True when delta operations are processed; False when noop. Tr,r-r5r.Fz.New aptURL, updating %s apt sources list to %srXz%New additionalPackages, installing %r, r\)r)rGprocess_contract_deltasr0rread"_check_application_status_on_cacherrrrLOGrZr"r>rREPO_UPDATING_APT_SOURCESr(r remove_auth_apt_repor)rrr ProgressWrapperr^ REPO_REFRESH_INSTALLING_PACKAGESrbrc) rrrrdelta_entitlementdelta_directives delta_apt_urldelta_packages status_cacherrorig_entitlementold_urlrOrrrsL          z'RepoEntitlement.process_contract_deltasrcleanup_on_failurecCs|s|j}|s dS|d|jdz||Wntjy,|r+|t w| t j j |jd|jrDddi}gd}nd}g}z tj|||dWdStjyn|rmtd |j|t w) zInstall contract recommended packages for the entitlement. :param package_list: Optional package list to use instead of self.packages. :param cleanup_on_failure: Cleanup apt files if apt install fails. Nr pre_installriDEBIAN_FRONTENDnoninteractive)z--allow-downgradesz$-o Dpkg::Options::="--force-confdef"z$-o Dpkg::Options::="--force-confold")r2 apt_optionsoverride_env_varsz.Apt install failed, removing apt config for {})r2r` messagingr0_update_sources_listr UbuntuProErrorrrr rrWrINSTALLING_SERVICE_PACKAGESr(rFapt_noninteractiver run_apt_install_commandrrZr")rrWrrrrrrrrcsH   z RepoEntitlement.install_packagescCsd}d}d}|jjs|jjr'td|jjtj}td|jjtj}tjj }n |jj s/|jj rGtd|jj tj}td|jj tj}tjj }tj |||d|j}|j}|ddi}|ddi}|d} | s|jjd } |d st|j} | | |j} | r| d} | s| } td |j|d } | stj|jd |d}|stj|jd |d}|stj|jd |jr|jstj|j|jd|j j!|jd}t"|||j|jg}t#tj$s|%dt#tj&s|%d|r"|'dt(j)j!d*|dztj+|dWntj,y!|-t./wt0||j1!|| ||j2|3t(j4j!|jdzt5|WdStj,yV|j-t./ddw)zSetup apt config based on the resourceToken and directives. Also sets up apt proxy if necessary. :raise UbuntuProError: on failure to setup any aspect of this apt configuration Nrhttps) http_proxy https_proxy proxy_scoper,r- obligations resourceToken machineTokenenableByDefaultzWNo resourceToken present in contract for service %s. Using machine token as credentialsaptKeyrEr5r9rDrzapt-transport-httpszca-certificatesrZrr\F)run_apt_update)6cfgglobal_apt_http_proxyglobal_apt_https_proxyrvalidate_proxyPROXY_VALIDATION_APT_HTTP_URLPROXY_VALIDATION_APT_HTTPS_URLr AptProxyScopeGLOBALua_apt_http_proxyua_apt_https_proxyUACLIENTsetup_apt_proxyr)r/r0 machine_tokenr UAContractClientget_resource_machine_accessr"rwarningrFr RepoNoAptKeyMissingAptURLDirective RepoNoSuitesrrIRepoPinFailNoOriginrepo_pref_file_tmplr(add_ppa_pinningrAPT_METHOD_HTTPS_FILErnCA_CERTIFICATES_FILEr`rINSTALLING_PACKAGESrbrrrrr radd_auth_apt_repor*rBrWAPT_UPDATING_LISTupdate_sources_list)rrWrrscope repo_filename resource_cfgr-rtokenrclientmachine_accessrrrrepo_pref_fileprerequisite_pkgsrrrr^1s               z RepoEntitlement.setup_apt_configrc Cstj}|j}|jjj|jdi}|di}|d}|s(t j |jd| t j j|jdt|||jt|||jrQ|jj|jd}t||r_| t jtdSdS)zRemove any repository apt configuration files. :param run_apt_update: If after removing the apt update command after removing the apt files. r,r-r5rrirN)rr$r%r)rmachine_token_file entitlementsr"r0r rrWrREMOVING_APT_CONFIGURATIONr(rFr rrBremove_apt_list_filesrrensure_file_absentAPT_UPDATING_LISTSrun_apt_update_command) rrWrr%rr,access_directivesrrrrrrrs*       z!RepoEntitlement.remove_apt_config)F)NT)T)4__name__ __module__ __qualname__r'rr*rIrrsupports_purgepropertyrintstrrr)r+rr2rr7r:rrAabcabstractmethodrBrrrHrVr rrdrer{rlrprurtrr NamedMessagerrrrrrcr^rr __classcell__rrrOrr#s  ,A '  1  F  ;|r)(r%r1loggingros.pathrtypingrrrrrruaclientr r r r r rrrruaclient.entitlementsr(uaclient.entitlements.entitlement_statusrrruaclient.files.state_filesrget_event_loggerr> getLoggerreplace_top_level_logger_namerrr UAEntitlementrrrrrs  ,