o Hcf@s\ddlZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z m Z ddl m Z mZmZmZmZmZmZddlZddlmZddlmZmZmZmZmZmZmZddlm Z ddl!m"Z"dZ#d Z$d Z%d Z&d Z'd Z(dZ)dZ*dZ+dZ,dZ-dZ.dZ/dZ0dZ1dZ2dZ3dZ4ej56e dej56e dgej56e dej56e dej56e dej56e dgd Z7gd!Z8e9Z:e;eej?Gd"d#d#ej@ZAed$d%eBfd&eBfd'eBfgZCd(d)ZDeDd*eBd+eBfd,d-ZEd.d/ZFd0eBd1eeBfd2d3ZG  dd4eeBd5eeBd6ee eBeBfd1eBfd7d8ZHe dd9  dd5eeBd6ee eBeBfd1eBfd:d;ZIGdd?ZKd@dAZLdBeBd1eeBfdCdDZM EddBeBdFeNd1eeBfdGdHZO dd6ee eBeBfd1eBfdIdJZPeQejRejSfe8dKeBfdLdMZT  ddNeeBdOeeeBd6ee eBeBfd1eBfdPdQZUdReBd1eejVfdSdTZW ddUejVdVeeBd1eejXfdWdXZYdYeeBdZeBd[eNd\eBd1eBf d]d^ZZ EddYeeBdZeBd[eNd\eBd_eBd`eNd1eBfdadbZ[dceBd\eBddeBdYeeBd_eBd1df dedfZ\dgdhZ]didjZ^ ddceBd\eBd_eeBd1dfdkdlZ_dmdnZ`dodpZadqdrZbdsdtZcdueBd1eNfdvdwZdd1eeCfdxdyZed1eeBfdzd{ZfddeAjgfd|eeBd}eeBd~eeAd1dfddZhd1eeifddZjd1eejfddZkddZldddZmdeeBdeBfddZndeeBdeBfddZodeeBfddZpddZqddZrdeeBd1e eBeeBeeBfffddZsd1eBfddZtdS)N) lru_cachewraps)DictIterableList NamedTupleOptionalSetUnion)AcquireProgress) event_logger exceptionsgpgmessagessecret_managersystemutil)ESM_APT_ROOTDIR)status_cache_filegN@z # ubuntu-pro-clientzDir::Etc::netrc/zDir::Etc::netrcparts/zDir::State::lists/a/* * Autogenerated by ubuntu-pro-client * Do not edit this file directly * * To change what ubuntu-pro-client sets, use the `pro config set` * or the `pro config unset` commands to set/unset either: * global_apt_http_proxy and global_apt_https_proxy * for a global apt proxy * or * ua_apt_http_proxy and ua_apt_https_proxy * for an apt proxy that only applies to Ubuntu Pro related repos. */ z$Acquire::http::Proxy "{proxy_url}"; z%Acquire::https::Proxy "{proxy_url}"; z4Acquire::http::Proxy::esm.ubuntu.com "{proxy_url}"; z5Acquire::https::Proxy::esm.ubuntu.com "{proxy_url}"; z/etc/apt/trusted.gpg.d/z/usr/share/keyringsz/usr/lib/apt/methods/httpsz /usr/sbin/update-ca-certificatesz//etc/apt/apt.conf.d/90ubuntu-advantage-aptproxyz*/var/lib/apt/periodic/update-success-stamp)xenialbionicfocaljammymanticz# Written by ubuntu-pro-client Types: deb{deb_src} URIs: {url} Suites: {suites} Components: main Signed-By: {keyrings_dir}/{keyring_file} zetc/apt/sources.listzvar/lib/dpkg/statuszetc/apt/apt.conf.dzetc/apt/preferences.dzvar/cache/apt/archives/partialzvar/lib/apt/lists/partial)filesfolders)g?g@g$@c@seZdZeZeZdS) AptProxyScopeN)__name__ __module__ __qualname__objectGLOBALUACLIENTr#r#./usr/lib/python3/dist-packages/uaclient/apt.pyrbs rInstalledAptPackagenameversionarchcstfdd}|S)z*Decorator ensuring apt_pkg is initialized.cs&tjddkr t|i|S)NDir)apt_pkgconfiggetinit)argskwargsfr#r$new_fpsz"ensure_apt_pkg_init..new_f)r)r2r3r#r1r$ensure_apt_pkg_initmsr4abcCs t||SN)r+version_compare)r5r6r#r#r$r8{s r8c Cs|d\}}tjdsdSz0t!}tjddd||||tj |dgt t dWdWdS1s8wYWdSt j yx}z,|jdkrlt|j}td |r`t j|d td |rlt j|d t jt|d d}~wtjyt jt |d w)aValidate apt credentials for a PPA. @param repo_url: private-ppa url path @param username: PPA login username. @param password: PPA login password or resource token. @raises: UbuntuProError for invalid credentials, timeout or unexpected errors. ://z/usr/lib/apt/apt-helperNz download-filez{}://{}:{}@{}/pool/zapt-helper-output)timeout retry_sleepsdz401\s+unauthorized|httperror401)repozconnection timed outdetail)secondsr=)splitospathexiststempfileTemporaryDirectoryrsubpformatjoinAPT_HELPER_TIMEOUT APT_RETRIESr ProcessExecutionError exit_codestrstderrlowerresearchAPTInvalidCredentials APTTimeoutAPTUnexpectedError subprocessTimeoutExpiredAPTCommandTimeout)repo_urlusernamepasswordprotocol repo_pathtmpderOr#r#r$assert_valid_apt_credentialss>   &     r` apt_errorreturncCsVt}|dD]}|r(td|}|r(d|ddd}||q |S)aAParse apt update errors for invalid apt config in user machine. This functions parses apt update errors regarding the presence of invalid apt config in the system, for example, a ppa that cannot be reached, for example. In that scenario, apt will output a message in the following formats: The repository 'ppa 404 Release' ... Failed to fetch ppa 404 ... On some releases, both of these errors will be present in the apt error message. :param apt_error: The apt error string :return: a NamedMessage containing the error message  z2(Failed to fetch |The repository .)(?P[^\s]+)z- urlz/distsr)setstriprArQrR groupdictadd)ra failed_reposline pattern_matchrepo_url_matchr#r#r$(_parse_apt_update_for_invalid_apt_configs rmcmd error_msgoverride_env_varsc Csztj|dt|d\}}W|StjyG}z,dt|jvr"t t|j}|r5tj d t |d|r9|nt|}tj |dd}~ww)aRun an apt command, retrying upon failure APT_RETRIES times. :param cmd: List containing the apt command to run, passed to subp. :param error_msg: The string to raise as UbuntuProError when all retries are exhausted in failure. :param override_env_vars: Passed directly as subp's override_env_vars arg :return: stdout from successful run of the apt command. :raise UbuntuProError: on issues running apt-cache policy. T)capturer;rpz%Could not get lock /var/lib/dpkg/lockrc)rir>N) rrGrKr rLrNrOAPTProcessConflictErrorrmAPTInvalidRepoErrorrIsortedrU)rnrorpout_errr_rimsgr#r#r$run_apt_commands*    rx)maxsizecCstddg||dS)N apt-cachepolicy)rnrorp)rx)rorpr#r#r$get_apt_cache_policys r|c@s$eZdZddZddZddZdS)PreserveAptCfgcCs||_i|_dSr7)apt_funccurrent_apt_cfg)selfr~r#r#r$__init__s zPreserveAptCfg.__init__cs&tjfddD|_|S)Ncsi|] }|t|qSr#)copydeepcopyr-).0keycfgr#r$ sz,PreserveAptCfg.__enter__..)r+r,keysrr~)rr#rr$ __enter__ s  zPreserveAptCfg.__enter__cCs4tj}|jD] }|||j|qtdSr7)r+r,rrre init_system)rtypevalue tracebackrrr#r#r$__exit__s zPreserveAptCfg.__exit__N)rrrrrrr#r#r#r$r}s r}cCs.tjD]}tj|qttdSr7)r+r,rclearr.Cacherr#r#r$get_apt_pkg_caches rcCsbz%tjD]}td|stj|qtjdttt dWSt y0iYSw)Nz^Acquirer)) r+r,rrQrRrrerr.r Exceptionrr#r#r$get_esm_apt_pkg_cache#s    rpkg_namec Cshtt}z||}WntyYWddSwWdn1s&wY|jr2|jjSdSr7)r}rKeyError current_verver_str)rcachepackager#r#r$get_pkg_version6s   rFcheck_esm_cachec CsTtt7}z||}WntyYWddSwt|}||}|s1 WddS|j}Wdn1s>wY|sG|SttU}|rz||}Wntyf|YWdSwt|} | |} | s||WdS| j} t| |dkr| WdSWd|SWd|S1swY|S)Nr) r}rrr+DepCacheget_candidate_verrrr8) rrrr dep_cache candidatecandidate_version esm_cache esm_package esm_dep_cache esm_candidateesm_candidate_versionr#r#r$get_pkg_candidate_versionCsZ               rc CszDz tddg|d}Wn2tjyttjy*}ztj|jdd}~wtjy=}ztj|jdd}~wwWt |St w)Napt-getupdaternrprepo_msgr>) rxr rrAPTUpdateProcessConflictErrorrsAPTUpdateInvalidRepoErrorrwUbuntuProErrorAPTUpdateFailedr| cache_clear)rprur_r#r#r$run_apt_update_commandos"   rsources_list_pathc Cs*tt}tjdtj|tjddtjddt}| t }tj tj dd}t |}zDz||||dWdn1sOwYWn"ttd d ydttyw}ztjt|d d}~wwWtntwWddS1swYdS) NzDir::Etc::sourcelistzDir::Etc::sourcepartszN.O.F.O.L.D.E.RzAPT::List-Cleanup0zDir::State::ListslockrErrorr#r>)r}rr+r,rerBrCabspath SourceListread_main_listr rIfind_dirFileLockrgetattrr rr SystemErrorrrNr|r)rr sources_listfetch_progress lock_filerr_r#r#r$update_sources_lists6    "rpackages apt_optionsc Csj|durg}ztgd|||d}W|Stjy!ttjy4}ztj|jdd}~ww)N)rinstall --assume-yesrr)rxr rrAPTInstallProcessConflictErrorrsAPTInstallInvalidRepoErrorrw)rrrprur_r#r#r$run_apt_install_commands$ rorigincCsvt}tt(}|jD]}|j}|r$|jD]\}}|j|kr#||qq Wdt|S1s2wYt|Sr7) rer}rrr file_listrrhlist)rresultrrinstalled_versionfile_r#r#r$ get_installed_packages_by_origins     rrexclude_origincs6g}|jD]}fdd|jD}|r||q|S)Ncs(g|]\}}|jdkr|jkr|qS)now) componentr)rrrrr#r$ s z3get_remote_versions_for_package..) version_listrappend)rrvalid_versionsr' valid_originsr#rr$get_remote_versions_for_packages   rsuitesseriesupdates_enabledrYcCsRd}|D]"}||vr qd}d|vr|std||d}|dj|||d7}q|S)Nr*-updatesz?Not enabling apt suite "%s" because "%s-updates" is not enabledz# zC{maybe_comment}deb {url} {suite} main # deb-src {url} {suite} main ) maybe_commentrdsuite)LOGwarningrH)rrrrYcontentrrr#r#r$_get_list_file_contents& r keyring_fileinclude_deb_srcc sZfdd|D}|stddd|D}|rdnd}tj|d|t||d}|S) Ncsg|]}|vr|qSr#r#rrrr#r$r z-_get_sources_file_content..zPNot enabling service-related -updates suites because "%s-updates" is not enabledcSsg|]}d|vr|qS)rr#rr#r#r$rsz deb-srcr* )rdr keyrings_dirrdeb_src)rrDEB822_REPO_FILE_CONTENTrHrI KEYRINGS_DIR) rrrrYrrappliable_suitesrrr#rr$_get_sources_file_contents$ r repo_filename credentialscCsz |d\}}Wn tyd}|}Ynwtj|tj}|dr-|dd}t |||d}t ddgt j } | D]} d || vrKqAd | vrPqAd }t||||tvrytjt|} tjt|} t| | t||||} nt|||||} t|| dS) zAdd an authenticated apt repo and credentials to the system. @raises: InvalidAPTCredentialsError when the token provided can't access the repo PPA. :bearer/NFrzr{z a={}-updatesz o=Ubuntu,T)rA ValueErrorrsecrets add_secretrget_release_inforendswithr`rxrAPT_POLICY_FAILED splitlinesrHadd_apt_auth_conf_entrySERIES_NOT_USING_DEB822rBrCrIr APT_KEYS_DIRrexport_gpg_keyrr write_file)rrYrrrrZr[rrr{rjsource_keyring_filedestination_keyring_filerr#r#r$add_auth_apt_repo!sD            rc Cst}|d\}}|ds|d7}tj|rt|}nd}dj|||t d}d}g} | D].} |s[| } t | dkr[| d} | |krP| |d }q2| |vr[| |d }| | q2|sh| || dtj |d | d d d S)zBAdd or replace an apt auth line in apt's auth.conf file or conf.d.r9rr*z:machine {repo_path} login {login} password {password}{cmt})r]loginr[cmtFTrcmodeN)!get_apt_auth_file_from_apt_configrArrBrCrDr load_filerHAPT_AUTH_COMMENTrlenrrrI) rYrr[ apt_auth_file _protocolr] orig_contentrepo_auth_lineadded_new_auth new_linesrj split_linecurr_line_repor#r#r$rYsD           rcs|d\}}|dr|dd}t}tj|rGt|}dj|dd fdd | D}|s=t |dStj ||d d dSdS) z+Remove a repo from the shared apt auth filer9rNrzmachine {repo_path}/ login)r]rccsg|]}|vr|qSr#r#)rrj auth_prefixr#r$rrz2remove_repo_from_apt_auth_file..r r ) rArrrBrCrDrrrHrIrensure_file_absentr)rYrr]rapt_authrr#rr$remove_repo_from_apt_auth_files     rcCsNt||drtt|d|r!tjt|}t|t |dS)z>Remove an authenticated apt repo and credentials to the systemz.sourcesrN) rrrrset_filename_extensionrBrCrIrr)rrYrr#r#r$remove_auth_apt_repos     r cCsB|d\}}|dr|dd}dj||d}t||dS)z.Add an apt preferences file and pin for a PPA.r9rNrz.)rrGr)rur package_listr#r#r$get_installed_packagess r7cCst}dd|D}|S)NcSsg|]}|jqSr#)r&)rr1r#r#r$rsz0get_installed_packages_names..)r7)r6 pkg_namesr#r#r$r2sr2 http_proxy https_proxy proxy_scopecCs|s|r!|r!d}|tjkrd}n|tjkrd}ttjj|dd}|r@|tjkr3|tj|d7}n |tjkr@|t j|d7}|r]|tjkrP|t j|d7}n |tjkr]|t j|d7}|dkret |}|dkrpt tdSt t|dS)a Writes an apt conf file that configures apt to use the proxies provided as args. If both args are None, then no apt conf file is written. If this function previously wrote a conf file, and was run again with both args as None, the existing file is removed. :param http_proxy: the url of the http proxy apt should use, or None :param https_proxy: the url of the https proxy apt should use, or None :return: None r*z UA-scopedglobal)scope) proxy_urlN)rr"r!eventinforSETTING_SERVICE_PROXY_SCOPErHAPT_CONFIG_UA_PROXY_HTTPAPT_CONFIG_GLOBAL_PROXY_HTTPAPT_CONFIG_UA_PROXY_HTTPSAPT_CONFIG_GLOBAL_PROXY_HTTPSAPT_PROXY_CONFIG_HEADERrrAPT_PROXY_CONF_FILEr)r9r:r;messageapt_proxy_configr#r#r$setup_apt_proxysD       rJcCs d}tjtrttj}|Sr7)rBrCrDAPT_UPDATE_SUCCESS_STAMP_PATHstatst_mtime cache_timer#r#r$get_apt_cache_time.s  rPcCs$t}|dur dStj|tjjSr7)rPdatetime fromtimestamptimezoneutcrNr#r#r$get_apt_cache_datetime5srUcstjtjtdddtdtd}tfdd|Dr!dStttdD]}t |q*tdD] }tj |ddd q6dS) Nz**/*T) recursiverrc3s|]}|vVqdSr7r#)rrexisting_filesr#r$ Dsz._ensure_esm_cache_structure..i)exist_okr ) r,rBrCrIrESM_BASIC_FILE_STRUCTUREallrensure_folder_absent create_filemakedirs) desired_filesrfolderr#rWr$_ensure_esm_cache_structure<s    rbc CstsdStddlm}ddlm}ddlm}m }d}d}t }|dur/||d}| dgD]"}| ddd krG| d d d k}| ddd krW| d d d k}q5||} |rl| d|jkrl| n| tr||} |r| d|jkr| n| ttJ} t} | Gdddt} | }z | || dWnty}ztdt|WYd}~n d}~wwWddSWddS1swYdS)Nr)status)ApplicationStatus)ESMAppsEntitlementESMInfraEntitlementFservicesr&r*zesm-apps availablenoyesz esm-infrac@s,eZdZdejfddZdejfddZdS)z-update_esm_caches..EsmAcquireProgressitemcStd|jdS)NzFetched ESM Apt Cache item: {})rdebugrHurirrkr#r#r$donesz2update_esm_caches..EsmAcquireProgress.donecSrl)Nz&Failed to fetch ESM Apt Cache item: {})rrrHrnror#r#r$fails z2update_esm_caches..EsmAcquireProgress.failN)rrrr+AcquireItemDescrprqr#r#r#r$EsmAcquireProgresssrsz%Failed to fetch the ESM Apt Cache: {})ris_current_series_ltsrbuaclient.actionsrc(uaclient.entitlements.entitlement_statusrduaclient.entitlements.esmrerfrreadr-application_statusDISABLEDsetup_local_esm_repodisable_local_esm_repois_current_series_active_esmr}rr+rrr rrrrrHrN)rrcrdrerfapps_availableinfra_availablecurrent_statusserviceappsinfrarrrsrr_r#r#r$update_esm_cachesQsZ        "r package_names error_messagecC"tgdt||ddiddS)a Remove APT packages from the system. Setting DEBIAN_FRONTEND to noninteractive makes sure no prompts will appear during the operation. In this case, --force-confdef will automatically pick the default option when some debconf should appear. In the absence of a default option, --force-confold will automatically choose to keep the old configuration file. )rremover$-o Dpkg::Options::="--force-confdef"$-o Dpkg::Options::="--force-confold"DEBIAN_FRONTENDnoninteractiverpNrxrrrr#r#r$remove_packages  rcCr)a Purge APT packages from the system - remove everything. Setting DEBIAN_FRONTEND to noninteractive makes sure no prompts will appear during the operation. In this case, --force-confdef will automatically pick the default option when some debconf should appear. In the absence of a default option, --force-confold will automatically choose to keep the old configuration file. )rpurgerrrrrrNrrr#r#r$purge_packagesrrcCst|gdddiddS)a6 Install packages, allowing downgrades. The --allow downgrades flag is needed because sometimes we need to reinstall the packages to a lower version (passed in the package_name string, as package=version). Setting DEBIAN_FRONTEND to noninteractive makes sure no prompts will appear during the operation. In this case, --force-confdef will automatically pick the default option when some debconf should appear. In the absence of a default option, --force-confold will automatically choose to keep the old configuration file. )z--allow-downgradesrrrr)rrpN)r)rr#r#r$reinstall_packagess  rcCs*tjD]}tj|qttjSr7)r+r,rr init_configrr#r#r$_get_apt_configsrcCs8tt}||}Wd|S1swY|Sr7)r}rr)base_keyapt_cfg apt_cfg_keysr#r#r$get_apt_config_keyss   r cfg_namescCsdi}tt"}|D]}||}t|s||pd}|||<q Wd|S1s+wY|S)z Get all APT configuration values for the given config names. If one of the config names is not present on the APT config, that config name will have a value of None N)r}rr-rN value_list)r apt_cfg_dictrcfg_name cfg_valuer#r#r$get_apt_config_valuess      rcCsd}d}tj|r |S|S)Nz/etc/apt/sources.listz&/etc/apt/sources.list.d/ubuntu.sources)rBrCrD)old_sources_pathnew_sources_pathr#r#r$get_system_sources_files r)NN)Fr7)rbN)urrQenumr,loggingrBrQrVrE functoolsrrtypingrrrrrr r r+apt.progress.baser uaclientr r rrrrruaclient.defaultsruaclient.files.state_filesrrJrr(r'r*rFrCrErBrDrrAPT_METHOD_HTTPS_FILECA_CERTIFICATES_FILErGrKrrrCrIr[rKget_event_loggerr? getLoggerreplace_top_level_logger_namerruniqueEnumrrNr%r4r8r`rmrxr|r}rrrboolrrretryrrrrrPackagerVersionrrrrrrr r#rr/r0r3r7r2r!rJfloatrPrUrbrrrrrrrrr#r#r#r$s$ $      ) ( *   -  '        8,    8 E