HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/cloudinit/config/__pycache__/cc_apt_configure.cpython-310.pyc
o

�AdՁ�@s\UdZddlZddlZddlZddlZddlmZddlmZddl	m
Z
ddl	mZddl	mZm
Z
mZddlmZdd	lmZdd
lmZmZddlmZe�e�adZd
ZdZdZeZddgZ ddded�e ed�gegd�Z!ee"d<ee!�ZdZ#dZ$dZ%dZ&ddd�Z'd d d�Z(d!d"gZ)gd#�Z*dkd$d%�Z+d&e,d'ed(ed)ed*e-d+dfd,d-�Z.d.d/�Z/d0d1�Z0dld2d3�Z1dld4d5�Z2dld6d7�Z3d8d9�Z4d:d;�Z5d<d=�Z6d>d?�Z7d@dA�Z8dBdC�Z9dDdE�Z:dFdG�Z;dmdIdJ�Z<dndKdL�Z=dMdN�Z>	dodOdP�Z?dQdR�Z@dSdT�ZAdUdV�ZBdWdX�ZCdYdZ�ZDd[d\�ZEd]d^�ZFd_d`�ZGdadb�ZHdldcdd�ZIdedf�ZJ	gdpdhdi�ZKdje4iZLdS)qz*Apt Configure: Configure apt for the user.�N)�Logger)�dedent)�gpg)�log)�subp�	templater�util)�Cloud)�Config)�
MetaSchema�get_meta_doc)�PER_INSTANCEz
^[\w-]+:\wz/etc/apt/trusted.gpgz/etc/apt/trusted.gpg.d/z/etc/apt/cloud-init.gpg.d/�ubuntu�debian�cc_apt_configurez
Apt ConfigurezConfigure apt for the usera�        This module handles both configuration of apt options and adding
        source lists.  There are configuration options such as
        ``apt_get_wrapper`` and ``apt_get_command`` that control how
        cloud-init invokes apt-get. These configuration options are
        handled on a per-distro basis, so consult documentation for
        cloud-init's distro support for instructions on using
        these config options.

        .. note::
            To ensure that apt configuration is valid yaml, any strings
            containing special characters, especially ``:`` should be quoted.

        .. note::
            For more information about apt configuration, see the
            ``Additional apt configuration`` example.a-	        apt:
          preserve_sources_list: false
          disable_suites:
            - $RELEASE-updates
            - backports
            - $RELEASE
            - mysuite
          primary:
            - arches:
                - amd64
                - i386
                - default
              uri: 'http://us.archive.ubuntu.com/ubuntu'
              search:
                - 'http://cool.but-sometimes-unreachable.com/ubuntu'
                - 'http://us.archive.ubuntu.com/ubuntu'
              search_dns: false
            - arches:
                - s390x
                - arm64
              uri: 'http://archive-to-use-for-arm64.example.com/ubuntu'

          security:
            - arches:
                - default
              search_dns: true
          sources_list: |
              deb $MIRROR $RELEASE main restricted
              deb-src $MIRROR $RELEASE main restricted
              deb $PRIMARY $RELEASE universe restricted
              deb $SECURITY $RELEASE-security multiverse
          debconf_selections:
              set1: the-package the-package/some-flag boolean true
          conf: |
              APT {
                  Get {
                      Assume-Yes 'true';
                      Fix-Broken 'true';
                  }
              }
          proxy: 'http://[[user][:pass]@]host[:port]/'
          http_proxy: 'http://[[user][:pass]@]host[:port]/'
          ftp_proxy: 'ftp://[[user][:pass]@]host[:port]/'
          https_proxy: 'https://[[user][:pass]@]host[:port]/'
          sources:
              source1:
                  keyid: 'keyid'
                  keyserver: 'keyserverurl'
                  source: 'deb [signed-by=$KEY_FILE] http://<url>/ bionic main'
              source2:
                  source: 'ppa:<ppa-name>'
              source3:
                  source: 'deb $MIRROR $RELEASE multiverse'
                  key: |
                      ------BEGIN PGP PUBLIC KEY BLOCK-------
                      <key data>
                      ------END PGP PUBLIC KEY BLOCK-------
              source4:
                  source: 'deb $MIRROR $RELEASE multiverse'
                  append: false
                  key: |
                      ------BEGIN PGP PUBLIC KEY BLOCK-------
                      <key data>
                      ------END PGP PUBLIC KEY BLOCK-------)�id�name�title�description�distros�examples�	frequency�activate_by_schema_keys�metaz/var/lib/apt/listsz'/etc/apt/apt.conf.d/94cloud-init-configz)/etc/apt/apt.conf.d/90cloud-init-aptproxyzkeyserver.ubuntu.comz!http://archive.ubuntu.com/ubuntu/z"http://security.ubuntu.com/ubuntu/��PRIMARY�SECURITYz$http://ports.ubuntu.com/ubuntu-ports�amd64�i386)�s390x�arm64�armhf�powerpc�ppc64el�riscv64cCs>|dur	t�|�}|tvrt��S|tvrt��Std|��)z�returns the default mirrors for the target. These depend on the
    architecture, for more see:
    https://wiki.ubuntu.com/UbuntuDevelopment/PackageArchive#PortsNz#No default mirror known for arch %s)r�get_dpkg_architecture�PRIMARY_ARCHES�PRIMARY_ARCH_MIRRORS�copy�PORTS_ARCHES�
PORTS_MIRRORS�
ValueError)�arch�target�r.�C/usr/lib/python3/dist-packages/cloudinit/config/cc_apt_configure.py�get_default_mirrors�s
r0r�cfg�cloudr�args�returncCs\d}|dur|at|�}|�di�}t|t�s!tdjt|�d���t||�t	|||�dS)z�process the config for apt_config. This can be called from
    curthooks if a global apt config was provided or via the "apt"
    standalone command.N�aptz9Expected dictionary for 'apt' config, found {config_type})�config_type)
�LOG�convert_to_v3_apt_format�get�
isinstance�dictr+�format�type�apply_debconf_selections�	apply_apt)rr1r2rr3r-�apt_cfgr.r.r/�handle�s
��
rAcCs(t��rdSt�d�st�d�sdSdS)N)Fzsystem is snappy.zapt-getr5)Fzno apt commands.)TzApt is available.)r�system_is_snappyr�whichr.r.r.r/�_should_configure_on_empty_apt�s
rDc	Cs(|st�\}}|st�d|�dSt�d|�tj|d�d}t�|�}t|||d�}t�d|�t�|�dd��rLt	||�t
||||�t|||�zt|t
t�Wnttfydt�d	�Ynwd
|vr�|}||d<|d|d<d}	|�d
t�}
|
r�t�|
�j}	t|d
||||	d�dSdS)Nz#Nothing to do: No apt config and %szhandling apt config: %s�r-�codename)r,zApt Mirror info: %s�preserve_sources_listFz)Failed to apply proxy or apt config info:�sources�RELEASE�MIRROR�add_apt_repo_match)r-�template_params�
aa_repo_match)rDr7�debugr�lsb_releaser%�find_apt_mirror_info�is_falser9�add_mirror_keys�generate_sources_list�rename_apt_lists�apply_apt_config�APT_PROXY_FN�
APT_CONFIG_FN�IOError�OSError�	exception�ADD_APT_REPO_MATCH�re�compile�search�add_apt_sources)r1r2r-�
should_config�msg�releaser,�mirrors�params�matcher�matchcfgr.r.r/r?�sF


�
��r?cCs*|�d�s	|d7}tjdg||dd�dS)N�
zdebconf-set-selectionsT��datar-�capture)�endswithr)�
selectionsr-r.r.r/�debconf_set_selections�s

�rmcCs�g}g}|D]}|tvrt�d|�t||�|�|�q|�|�qt|�r.t�d|�t|�rCtjddgt|�d|dd�dSdS)Nzunconfiguring %szSThe following packages were installed and preseeded, but cannot be unconfigured: %szdpkg-reconfigurez--frontend=noninteractiveTrh)�CONFIG_CLEANERSr7rN�append�len�warningr�list)�packagesr-�	unhandled�	to_config�pkgr.r.r/�dpkg_reconfigures.��
��rwc
s�|�d���st�d�dSd��fdd�t����D��}t|��|d�t�}��	�D]\}}|�
�D]}|�d�r=q5t�
d	d
|�}|�|�q5q-t�|�}t�d|�|�|�}	t|	�dkrht�d
�dSt|	|d�dS)z2apply_debconf_selections - push content to debconf�debconf_selectionsz(debconf_selections was not set in configN�
csg|]}�|�qSr.r.)�.0�key��selsetsr.r/�
<listcomp>4sz,apply_debconf_selections.<locals>.<listcomp>rE�#z[:\s].*�z
pkgs_cfgd: %srzno need for reconfig)r9r7rN�join�sorted�keysrm�encode�set�items�
splitlines�
startswithr\�sub�addr�get_installed_packages�intersectionrprw)
r1r-rl�	pkgs_cfgd�_key�content�linerv�pkgs_installed�
need_reconfigr.r|r/r>)s*

 
�


r>cCs6t�t�|d��}t�d|�|D]}t�|�qdS)z%clean out any local cloud-init configz/etc/cloud/cloud.cfg.d/*dpkg*z#cleaning cloud-init config from: %sN)�globr�target_pathr7rN�os�unlink)r-�flist�dpkg_cfgr.r.r/�clean_cloud_initLs
��r�cCsL|}|�d�r
|dd�}|�d�}|dkr||dd�}|�dd�}|S)z�mirrorurl_to_apt_fileprefix
    Convert a mirror url to the file prefix used by apt on disk to
    store cache information for that mirror.
    To do so do:
    - take off ???://
    - drop tailing /
    - convert in string / to _�/r���z://�N�_)rk�find�replace)�mirror�string�posr.r.r/�mirrorurl_to_apt_fileprefixWs

r�c

Cs�t|�}t�|t�}|��D]\\}}|�|�}|sq|tjjt	|�}|tjjt	|�}	||	kr3qt
|�}
t�d|�D]+}d|	||
d�f}t�
d||�zt�||�Wq>tyitjddd�Yq>wqdS)z>rename_apt_lists - rename apt lists to preserve old cache dataz%s_*z%s%sNzRenaming apt list %s to %szFailed to rename apt list:T)�exc_info)r0rr��	APT_LISTSr�r9r��path�sepr�rpr�r7rN�renamerYrq)
�new_mirrorsr-r,�default_mirrors�prer�omirror�nmirror�oprefix�nprefix�olen�filename�newnamer.r.r/rTis*
���rTcCs"||vrt�d||�|�||�S)z�mirror_to_placeholder
    replace the specified mirror in a template with a placeholder string
    Checks for existance of the expected mirror and warns if not foundz%Expected mirror '%s' not found in: %s)r7rqr�)�tmplr��placeholderr.r.r/�mirror_to_placeholder�sr�cCs8dddddd�}z||}W|Sty|}Y|Sw)z�there are a few default names which will be auto-extended.
    This comes at the inability to use those names literally as suites,
    but on the other hand increases readability of the cfg quite a lotz$RELEASE-updatesz$RELEASE-backportsz$RELEASE-securityz$RELEASE-proposedz$RELEASE)�updates�	backports�security�proposedrb)�KeyError)�suite�mapping�retsuiter.r.r/�map_known_suites�s�
��r�cCs�|s|S|}|D]`}t|�}t�|d|i�}t�d||�d}|�d�D]A}|�d�r0||7}q$|��}t|�dkrad}	|d�d�rW|dd	�D]
}
|	d7}	|
�	d
�rVnqI||	|krad|}||7}q$|}q|S)zRreads the config for suites to be disabled and removes those
    from the templaterIzDisabling suite %s as %sr�Tr���[N�]z"# suite disabled by cloud-init: %s)
r�r�
render_stringr7rNr�r��splitrprk)�disabled�srcrb�retsrcr��releasesuite�newsrcr��cols�pcol�colr.r.r/�disable_suites�s4

�
r�cCs.dD]}|�|g�D]	}t|||d�q
qdS)z=Adds any keys included in the primary/security mirror clauses)�primaryr�)�	file_nameN)r9�add_apt_key)r1r-r{r�r.r.r/rR�s
��rRcCs�d}||d�}|D]}||||<||||��<q	|�dd�}|durGt�d�|�d|jj�}|s9|�d�}|sBt�d�dSt�	|�}t
�||�}	t|�d	�|	|�}
tj
||
d
d�dS)z�generate_sources_list
    create a source.list file based on a custom or default template
    by replacing mirrors and release in the templatez/etc/apt/sources.list)rIrF�sources_listNz1No custom template provided, fall back to builtinzsources.list.%szsources.listz6No template found, not rendering /etc/apt/sources.listr�i�)�mode)�lowerr9r7�info�get_template_filename�distrorrqr�	load_filerr�r��
write_file)r1rbrcr2�aptsrcrd�kr��template_fn�renderedr�r.r.r/rS�s,


�
�
rSFcCsHt�d|�zt�|�j}td|||d�WStjy#t�d��w)zM
    actual adding of a key as defined in key argument
    to the system
    zAdding key:
'%s'r�)�output_fileri�hardenedz(failed to add apt GPG Key to apt keyring)	r7rN�pathlib�Path�stem�apt_keyr�ProcessExecutionErrorrZ)r{r�r�r-rr.r.r/�add_apt_key_raw�s
�r�cCs^d|vrd|vrt}d|vr|d}t�|d|�|d<d|vr-t|d|p)|d|d�SdS)z�
    Add key to the system as defined in ent (if any).
    Supports raw keys or keyid's
    The latter will as a first step fetched to get the raw key
    �keyidr{�	keyserverr��r�N)�DEFAULT_KEYSERVERr�
getkeybyidr�)�entr-r�r�r�r.r.r/r��s��r�cCs|j��dS�N)r��update_package_sources)r2r.r.r/�update_packages	sr�c
Cs�|duri}|durtd��t|t�std|��|D]�}||}t�d|�d|vr/||d<d|vrEd|dvrEt||dd	�}||d
<nt||�}d|vrOq|d}t�||�}|d�	d�skt
j�d|d�|d<|d�
d
�sz|dd
7<||�r�ztjdd|g|d�Wntjy�t�d��wqt�||d�}	zd|}
d}d|vr�|ds�d}tj|	|
|d�Wqty�}zt�d|	|��d}~wwt|�dS)a�
    install keys and repo source .list files defined in 'sources'

    for each 'source' entry in the config:
        1. expand template variables and write source .list file in
                /etc/apt/sources.list.d/
        2. install defined keys
        3. update packages via distro-specific method (i.e. apt-key update)


    @param srcdict: a dict containing elements required
    @param cloud: cloud instance object

    Example srcdict value:
    {
    'rio-grande-repo': {
        'source': 'deb [signed-by=$KEY_FILE] $MIRROR $RELEASE main',
        'keyid': 'B59D 5F15 97A5 04B7 E230  6DCA 0620 BBCF 0368 3F77',
        'keyserver': 'pgp.mit.edu'
        }
    }

    Note: Deb822 format is not supported
    Nz did not get a valid repo matcherzunknown apt format: %szadding source/key '%s'r��sourcez	$KEY_FILETr��KEY_FILEr�z/etc/apt/sources.list.d/z.listzadd-apt-repositoryz--no-updaterEzadd-apt-repository failed.z%s
�aro�w)�omodezfailed write to file %s: %s)r+r:r;�	TypeErrorr7rNr�rr�r�r�r�r�rkrr�rZr�rr�rXr�)
�srcdictr2r-rLrMr�r��key_filer��sourcefn�contentsr��detailr.r.r/r_
sd


�
�
���r_cCsxi}t�d�t|t�r/t�d�|D]}d|vr$d|d<t�|d�}n|d}|||<q|St|t�r8|}|Std��)z1convert v1 apt format to v2 (dict in apt_sources)zIDEPRECATION: 'apt_sources' deprecated config key found. Use 'apt' insteadz9apt config: convert V1 to V2 format (source list to dict)r�zcloud_config_sources.listzunknown apt_sources format)	r7rqr:rrrNr�
rand_dict_keyr;r+)�srclistr��srcentr{r.r.r/�convert_v1_to_v2_apt_formatfs"�



��r�cCs,|�|d�dur|�|�||<||=dSdS)ziconvert an old key to the new one if the old one exists
    returns true if a key was found and convertedNTF�r9)�oldcfg�aptcfg�oldkey�newkeyr.r.r/�convert_key�s
rcCsLgd�}d}ddgi}|D]
\}}t||||�rd}q
|r$|g|d<dSdS)zBconvert old apt_mirror keys into the new more advanced mirror spec))�
apt_mirror�uri)�apt_mirror_searchr^)�apt_mirror_search_dns�
search_dnsF�arches�defaultTr�N)r)r�r��keymap�	converted�newmcfgrrr.r.r/�convert_mirror�s
��r
cCsPddddddddddd	d
�}g}|D]}||vr'||dvr"||=q|�|�q|s,|St�dd
�|��|�dd�}|durtt�d�|D]+}||}||}||=|dus_|�|d�dur`qF|||krqtd||||f��qF|Si}|D]}||dur�t|||||�qxt||�|D]}|�|d�dur�td|��q�||d<|S)z:convert old to new keys and adapt restructured mirror specrHN�proxy�
http_proxy�https_proxy�	ftp_proxyrGr�rK)�apt_sourcesrrr�	apt_proxy�apt_http_proxy�
apt_ftp_proxy�apt_https_proxy�apt_preserve_sources_list�apt_custom_sources_listrK)Nr�zdDEPRECATION apt: converted deprecated config V2 to V3 format for keys '%s'. Use updated config keys.z, r5zODEPRECATION: apt config: deprecated V1/2 and V3 format specified, preferring V3z@Old and New apt format defined with unequal values %s vs %s @ %sz&old apt key '%s' left after conversion)ror7rqr�r9r+rr
)r��
mapoldkeys�
needtoconvertr�	newaptcfgr�verifyr�r.r.r/�convert_v2_to_v3_apt_format�sp�

�������
�rcCs,|�dd�}|durt|�|d<t|�}|S)z�convert the old list based format to the new dict based one. After that
    convert the old dict keys/format to v3 a.k.a 'new apt config'rN)r9r�r)r1rr.r.r/r8�s
r8c
Cs�d}|rZd}g}|dkrd}n|dkrd}ntd��t�||�j}d�|�d�d	d��}|r6|�d
|�|�d�g}	|jj	}
d|
|d
|
f}|D]	}|	�||�qKt�
|	�}|S)zG
    Try to resolve a list of predefines DNS names to pick mirrors
    Nr�r�r�r�zsecurity-mirrorzunknown mirror type�.r�z.%s)z.localdomainr�zhttp://%s-%s%s/%sz%s)r+r�get_hostname_fqdn�fqdnr�r�ro�extendr�r�search_for_mirror)
�
configured�
mirrortyper1r2r��mydom�doms�	mirrordnsr �mirror_listr��	mirrorfmt�postr.r.r/�search_for_mirror_dns�s.�
r+cCsX|dur|dur
|}||d�S|j��}|r(|��}|d|d<|d|d<|St|�S)z^sets security mirror to primary if not defined.
    returns defaults if no mirrors are definedNrr�rr�r)�
datasource�get_package_mirror_infor(r0)�pmirror�smirrorr,r2�mirror_info�mr.r.r/�update_mirror_info s

r2cCsT|�|d�}|durdSd}|D]}|�d�pg}||vr!|Sd|vr'|}q|S)zuout of a list of potential mirror configurations select
    and return the one matching the architecture (or default)Nrr	r�)r1r$r,�mirror_cfg_listr	�mirror_cfg_elemrr.r.r/�get_arch_mirrorconfig8s�r5cCs`t|||�}|durdS|�dd�}|durt�|�dd��}|dur.t|�dd�|||�}|S)z�pass the three potential stages of mirror specification
    returns None is neither of them found anything otherwise the first
    hit is returnedNrr^r)r5r9rr"r+)r1r$r,r2�mcfgr�r.r.r/�
get_mirrorKs�r7cCsn|durt��}t�d|�t|d||�}t�d|�t|d||�}t�d|�t||||�}|d|d<|S)	afind_apt_mirror_info
    find an apt_mirror given the cfg provided.
    It can check for separate config of primary and security mirrors
    If only primary is given security is assumed to be equal to primary
    If the generic apt_mirror is given that is defining for both
    Nz!got arch for mirror selection: %sr�zgot primary mirror: %sr�zgot security mirror: %srrJ)rr%r7rNr7r2)r1r2r,r.r/r0r.r.r/rPesrPcs�d}�fdd�|D�}t|�r!t�d|�t�|d�|�d�ntj�|�r2t�	|�t�d|���
dd�rIt�d	|�t�|��
d��dStj�|�r\t�	|�t�d
|�dSdS)zHapply_apt_config
    Applies any apt*proxy config from if specified
    ))r�Acquire::http::Proxy "%s";)rr8)rzAcquire::ftp::Proxy "%s";)rzAcquire::https::Proxy "%s";cs(g|]\}}��|�r|��|��qSr.r�)rzr�fmt�r1r.r/r~�s(z$apply_apt_config.<locals>.<listcomp>zwrite apt proxy info to %sryz#no apt proxy configured, removed %s�confNzwrite apt config info to %sz$no apt config configured, removed %s)rpr7rNrr�r�r�r��isfile�del_filer9)r1�proxy_fname�config_fname�cfgs�proxiesr.r:r/rU}s

�rUTcsRdd�����fdd�}��fdd�}|dkr|�S|dks"|d	kr%|�Std
��)adapt-key replacement

    commands implemented: 'add', 'list', 'finger'

    @param output_file: name of output gpg file (without .gpg or .asc)
    @param data: key contents
    @param human_output: list keys formatted for human parsing
    @param hardened: write keys to to /etc/apt/cloud-init.gpg.d/ (referred to
    with [signed-by] in sources file)
    cSsTtj�t�r	tgng}t�t�D]}|�d�s|�d�r#|�t|�q|r(|SdS)z�return all apt keys

        /etc/apt/trusted.gpg (if it exists) and all keyfiles (and symlinks to
        keyfiles) in /etc/apt/trusted.gpg.d/ are returned

        based on apt-key implementation
        z.gpgz.ascr�)r�r�r<�APT_LOCAL_KEYS�listdir�APT_TRUSTED_GPG_DIRrkro)�	key_files�filer.r.r/�_get_key_files�s�zapt_key.<locals>._get_key_filesc	s�d}�st�td����|Sz�rtnt}t���}d�|��}t�||�W|St	j
y=t�td����Y|StyOt�td����Y|Sw)ziapt-key add <file>

        returns filepath to new keyring, or '/dev/null' when an error occurs
        z	/dev/nullz)Unknown filename, failed to add key: "{}"z{}{}.gpgz Gpg error, failed to add key: {}z#Decode error, failed to add key: {})r�logexcr7r<�CLOUD_INIT_GPG_DIRrDr�dearmorr�rr��UnicodeDecodeError)r��key_dir�stdout)rir�r�r.r/�apt_key_add�s0
��
�
	�
��
��zapt_key.<locals>.apt_key_addcsfg}��D](}z|�tj|�d��Wqtjy-}z
t�d||�WYd}~qd}~wwd�|�S)zapt-key list

        returns string of all trusted keys (in /etc/apt/trusted.gpg and
        /etc/apt/trusted.gpg.d/)
        )�human_outputzFailed to list key "%s": %sNry)rorrrrr�r7rqr�)�key_listr��error)rGrOr.r/�apt_key_list�s
��
zapt_key.<locals>.apt_key_listr��fingerrrz@apt_key() commands add, list, and finger are currently supported)r+)�commandr�rir�rOrNrRr.)rGrir�rOr�r/r��s�r�z
cloud-init)NNr�)FN)NFN)NNN)NNFT)M�__doc__r�r�r�r\�loggingr�textwrapr�	cloudinitrrrrr�cloudinit.cloudr	�cloudinit.configr
�cloudinit.config.schemarr�cloudinit.settingsr
�	getLogger�__name__r7r[rBrDrIrrr�__annotations__r�rWrVr�r'r*r&r)r0�strrrrArDr?rmrwr>r�r�rTr�r�r�rRrSr�r�r�r_r�rr
rr8r+r2r5r7rPrUr�rnr.r.r.r/�<module>s�
���E�`��

�����
�	
-

!#	&


�Y
K)

�R�