U
    ¯AdÕ  ã                	   @   sV  U d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
 ddl	mZ ddl	mZmZmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZ e e¡adZdZdZdZeZddgZ dddedƒe edƒgeg dœZ!ee"d< ee!ƒZ dZ#dZ$dZ%dZ&dddœZ'd d dœZ(d!d"gZ)d#d$d%d&d'd(gZ*dkd)d*„Z+e,eeee-dd+œd,d-„Z.d.d/„ Z/d0d1„ Z0dld2d3„Z1dmd4d5„Z2dnd6d7„Z3d8d9„ Z4d:d;„ Z5d<d=„ Z6d>d?„ Z7d@dA„ Z8dBdC„ Z9dDdE„ Z:dFdG„ Z;dodIdJ„Z<dpdKdL„Z=dMdN„ Z>dqdOdP„Z?dQdR„ Z@dSdT„ ZAdUdV„ ZBdWdX„ ZCdYdZ„ ZDd[d\„ ZEd]d^„ ZFd_d`„ ZGdadb„ ZHdrdcdd„ZIdedf„ ZJdsdhdi„ZKdje4iZLdS )tz*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/ZubuntuZdebianZcc_apt_configurezApt ConfigurezConfigure 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ÚtitleZdescriptionÚdistrosZexamplesÚ	frequencyZactivate_by_schema_keysÚmetaz/var/lib/apt/listsz'/etc/apt/apt.conf.d/94cloud-init-configz)/etc/apt/apt.conf.d/90cloud-init-aptproxyzkeyserver.ubuntu.comz!http://archive.ubuntu.com/ubuntu/z"http://security.ubuntu.com/ubuntu/©ÚPRIMARYÚSECURITYz$http://ports.ubuntu.com/ubuntu-portsZamd64Zi386Zs390xZarm64ZarmhfZpowerpcZppc64elZriscv64c                 C   sB   | dkrt  |¡} | tkr"t ¡ S | tkr2t ¡ S td|  ƒ‚dS )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    
r"   )r   ÚcfgÚcloudr   ÚargsÚreturnc                 C   s\   d}|dk	r|a t|ƒ}| di ¡}t|tƒsBtdjt|ƒdƒ‚t||ƒ t	|||ƒ dS )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})Zconfig_type)
ÚLOGÚconvert_to_v3_apt_formatÚgetÚ
isinstanceÚdictr   ÚformatÚtypeÚapply_debconf_selectionsÚ	apply_apt)r   r#   r$   r   r%   r   Zapt_cfgr    r    r!   Úhandle­   s    
ÿÿ
r1   c                   C   s(   t  ¡ rdS t d¡s$t d¡s$dS dS )N)Fzsystem is snappy.zapt-getr'   )Fzno apt commands.)TzApt is available.)r   Zsystem_is_snappyr   Zwhichr    r    r    r!   Ú_should_configure_on_empty_aptÇ   s
    r2   c              	   C   s*  | s"t ƒ \}}|s"t d|¡ d S t d| ¡ tj|dd }t |¡}t| ||d}t d|¡ t |  dd¡¡r˜t	| |ƒ t
| |||ƒ t|||ƒ zt| ttƒ W n" ttfk
rÊ   t d	¡ Y nX d
| kr&|}||d< |d |d< d }	|  dt¡}
|
rt |
¡j}	t| d
 ||||	d d S )Nz#Nothing to do: No apt config and %szhandling apt config: %s©r   Úcodename)r   zApt 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)r2   r(   Údebugr   Zlsb_releaser   Úfind_apt_mirror_infoZis_falser*   Ú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)r#   r$   r   Zshould_configÚmsgÚreleaser   ÚmirrorsÚparamsZmatcherZmatchcfgr    r    r!   r0   Ð   sB    



ûr0   c                 C   s*   |   d¡s| d7 } tjdg| |dd d S )Nó   
zdebconf-set-selectionsT©Údatar   Zcapture)Úendswithr   )Ú
selectionsr   r    r    r!   Údebconf_set_selectionsý   s    
ürU   c                 C   s†   g }g }| D ]:}|t kr<t d|¡ t | |ƒ | |¡ q| |¡ qt|ƒr\t d|¡ t|ƒr‚tjddgt|ƒ d |dd d S )Nzunconfiguring %szSThe following packages were installed and preseeded, but cannot be unconfigured: %szdpkg-reconfigurez--frontend=noninteractiveTrQ   )ÚCONFIG_CLEANERSr(   r<   ÚappendÚlenÚwarningr   Úlist)Zpackagesr   Z	unhandledZ	to_configÚpkgr    r    r!   Údpkg_reconfigure  s,    ýÿûr\   c           
         sà   |   d¡‰ ˆ st d¡ dS d ‡ fdd„tˆ  ¡ ƒD ƒ¡}t| ¡ |d tƒ }ˆ  	¡ D ]:\}}| 
¡ D ](}| d¡rzqjt d	d
|¡}| |¡ qjqZt |¡}t d|¡ | |¡}	t|	ƒdkrÐt d¡ dS t|	|d dS )z2apply_debconf_selections - push content to debconfZdebconf_selectionsz(debconf_selections was not set in configNÚ
c                    s   g | ]}ˆ | ‘qS r    r    )Ú.0Úkey©Zselsetsr    r!   Ú
<listcomp>4  s     z,apply_debconf_selections.<locals>.<listcomp>r3   ú#z[:\s].*Ú zpkgs_cfgd: %sr   zno need for reconfig)r*   r(   r<   ÚjoinÚsortedÚkeysrU   ÚencodeÚsetÚitemsÚ
splitlinesÚ
startswithrH   ÚsubÚaddr   Zget_installed_packagesÚintersectionrX   r\   )
r#   r   rT   Z	pkgs_cfgdZ_keyZcontentÚliner[   Zpkgs_installedZneed_reconfigr    r`   r!   r/   )  s(    

 



r/   c                 C   s6   t   t | d¡¡}t d|¡ |D ]}t |¡ q"dS )z%clean out any local cloud-init configz/etc/cloud/cloud.cfg.d/*dpkg*z#cleaning cloud-init config from: %sN)Úglobr   Útarget_pathr(   r<   ÚosÚunlink)r   ZflistZdpkg_cfgr    r    r!   Úclean_cloud_initL  s    
ÿrt   c                 C   sL   | }|  d¡r|dd… }| d¡}|dkr<||d d… }| dd¡}|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Ú_)rS   ÚfindÚreplace)ÚmirrorÚstringÚposr    r    r!   Úmirrorurl_to_apt_fileprefixW  s    

r~   c              
   C   sÞ   t |ƒ}t |t¡}| ¡ D ]¼\}}|  |¡}|s4q|tjj t	|ƒ }|tjj t	|ƒ }	||	krfqt
|ƒ}
t d| ¡D ]Z}d|	||
d… f }t d||¡ zt ||¡ W q| tk
rÔ   tjddd Y q|X q|qdS )z>rename_apt_lists - rename apt lists to preserve old cache dataz%s_*z%s%sNzRenaming apt list %s to %szFailed to rename apt list:T)Úexc_info)r"   r   rq   Ú	APT_LISTSri   r*   rr   ÚpathÚsepr~   rX   rp   r(   r<   ÚrenamerE   rY   )Znew_mirrorsr   r   Zdefault_mirrorsZprer   ZomirrorZnmirrorZoprefixZnprefixZolenÚfilenameZnewnamer    r    r!   r@   i  s$    
r@   c                 C   s"   || krt  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)r(   rY   rz   )Útmplr{   Zplaceholderr    r    r!   Úmirror_to_placeholder‚  s    r†   c                 C   s:   ddddddœ}z||  }W n t k
r4   | }Y nX |S )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)ZupdatesZ	backportsÚsecurityZproposedrM   )ÚKeyError)ÚsuiteÚmappingZretsuiter    r    r!   Úmap_known_suites‹  s    û
r‹   c                 C   sÖ   | s|S |}| D ]À}t |ƒ}t |d|i¡}t d||¡ d}| d¡D ]‚}| d¡r`||7 }qH| ¡ }t|ƒdkrÂd}	|d  d¡r®|dd	… D ]}
|	d7 }	|
 	d
¡r’ q®q’||	 |krÂd| }||7 }qH|}q|S )zRreads the config for suites to be disabled and removes those
    from the templater7   zDisabling suite %s as %src   Trb   é   é   ú[Nú]z"# suite disabled by cloud-init: %s)
r‹   r   Úrender_stringr(   r<   rj   rk   ÚsplitrX   rS   )ÚdisabledÚsrcrM   Zretsrcr‰   ZreleasesuiteZnewsrcro   ZcolsZpcolÚcolr    r    r!   Údisable_suites  s2    


r•   c                 C   s.   dD ]$}|   |g ¡D ]}t|||d qqdS )z=Adds any keys included in the primary/security mirror clauses)Úprimaryr‡   )Ú	file_nameN)r*   Úadd_apt_key)r#   r   r_   r{   r    r    r!   r>   Ã  s    r>   c                 C   sÀ   d}||dœ}|D ] }|| ||< || ||  ¡ < q|  dd¡}|dkrŽt d¡ | d|jj ¡}|sr| d¡}|s„t d¡ dS t 	|¡}t
 ||¡}	t|  d	¡|	|ƒ}
tj||
d
d dS )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)r7   r4   Úsources_listNz1No custom template provided, fall back to builtinzsources.list.%szsources.listz6No template found, not rendering /etc/apt/sources.listr•   i¤  )Úmode)Úlowerr*   r(   ÚinfoZget_template_filenameÚdistror   rY   r   Z	load_filer   r   r•   Ú
write_file)r#   rM   rN   r$   ZaptsrcrO   Úkr…   Ztemplate_fnZrenderedr’   r    r    r!   r?   Ê  s,    


ÿ
ÿ
r?   Fc                 C   sR   t  d| ¡ zt |¡j}td|| |dW S  tjk
rL   t  d¡ ‚ Y nX dS )zM
    actual adding of a key as defined in key argument
    to the system
    zAdding key:
'%s'rm   )Úoutput_filerR   Úhardenedz(failed to add apt GPG Key to apt keyringN)	r(   r<   ÚpathlibÚPathZstemÚapt_keyr   ÚProcessExecutionErrorrF   )r_   r—   r¡   r   r   r    r    r!   Úadd_apt_key_rawè  s    
r¦   c                 C   s^   d| kr8d| kr8t }d| kr$| d }t | d |¡| d< d| krZt| d |pR| d |dS dS )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
    Zkeyidr_   Ú	keyserverr„   ©r¡   N)ÚDEFAULT_KEYSERVERr   Z
getkeybyidr¦   )Úentr   r¡   r—   r§   r    r    r!   r˜   ö  s     
 ÿr˜   c                 C   s   | j  ¡  d S )N)r   Zupdate_package_sources)r$   r    r    r!   Úupdate_packages	  s    r«   c                 C   sÌ  |dkri }|dkrt dƒ‚t| tƒs2td|  ƒ‚| D ]†}| | }t d|¡ d|kr`||d< d|krŒd|d krŒt||dd	}||d
< n
t||ƒ}d|kr q6|d }t ||¡}|d  	d¡sØt
j d|d ¡|d< |d  d¡sö|d  d7  < ||ƒr@ztjdd|g|d W q6 tjk
r<   t d¡ ‚ Y q6X q6t ||d ¡}	z8d| }
d}d|krv|d svd}tj|	|
|d W q6 tk
r¼ } zt d|	|¡ ‚ W 5 d}~X Y q6X q6t|ƒ dS )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 matcherzunknown apt format: %szadding source/key '%s'r„   Úsourcez	$KEY_FILETr¨   ZKEY_FILEru   z/etc/apt/sources.list.d/z.listzadd-apt-repositoryz--no-updater3   zadd-apt-repository failed.z%s
ÚarW   Úw)Úomodezfailed write to file %s: %s)r   r+   r,   Ú	TypeErrorr(   r<   r˜   r   r   rk   rr   r   rd   rS   r   r¥   rF   rq   r   rž   rD   r«   )Úsrcdictr$   r   r:   r;   r„   rª   Úkey_filer¬   ZsourcefnÚcontentsr¯   Zdetailr    r    r!   rK     s`    



 ÿ
þ

rK   c                 C   sx   i }t  d¡ t| tƒr\t  d¡ | D ]2}d|krHd|d< t |d¡}n|d }|||< q&nt| tƒrl| }ntdƒ‚|S )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„   zcloud_config_sources.listzunknown apt_sources format)	r(   rY   r+   rZ   r<   r   Zrand_dict_keyr,   r   )Zsrclistr±   Zsrcentr_   r    r    r!   Úconvert_v1_to_v2_apt_formatf  s     ÿ


r´   c                 C   s,   |   |d¡dk	r(|   |¡||< | |= dS dS )ziconvert an old key to the new one if the old one exists
    returns true if a key was found and convertedNTF©r*   )ÚoldcfgÚaptcfgÚoldkeyÚnewkeyr    r    r!   Úconvert_key  s
    rº   c                 C   sJ   dddg}d}ddgi}|D ]\}}t | |||ƒrd}q|rF|g|d< d	S )
zBconvert old apt_mirror keys into the new more advanced mirror spec)Ú
apt_mirrorÚuri)Úapt_mirror_searchrJ   )Úapt_mirror_search_dnsÚ
search_dnsFÚarchesÚdefaultTr–   N)rº   )r¶   r·   ZkeymapZ	convertedZnewmcfgr¸   r¹   r    r    r!   Úconvert_mirror‹  s    ý
rÂ   c                 C   sT  ddddddddddd	d
œ}g }|D ]*}|| kr$| | dkrD| |= q$|  |¡ q$|sX| S t dd |¡¡ |  dd¡}|dk	rèt d¡ |D ]V}|| }| | }| |= |dksŒ| |d¡dkrÀqŒ||| krŒtd||| |f ƒ‚qŒ| S i }|D ]"}|| dk	rðt| |||| ƒ qðt| |ƒ |D ]$}|  |d¡dk	r"td| ƒ‚q"|| d< | S )z:convert old to new keys and adapt restructured mirror specr6   NÚproxyÚ
http_proxyÚhttps_proxyÚ	ftp_proxyr5   r™   r9   )Úapt_sourcesr»   r½   r¾   Z	apt_proxyZapt_http_proxyZapt_ftp_proxyZapt_https_proxyZapt_preserve_sources_listZapt_custom_sources_listr9   )Nrc   zdDEPRECATION apt: converted deprecated config V2 to V3 format for keys '%s'. Use updated config keys.z, r'   zODEPRECATION: 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)rW   r(   rY   rd   r*   r   rº   rÂ   )r¶   Z
mapoldkeysZneedtoconvertr¸   Z	newaptcfgr¹   Zverifyr·   r    r    r!   Úconvert_v2_to_v3_apt_format  sh    õýÿþÿ
rÈ   c                 C   s,   |   dd¡}|dk	r t|ƒ| 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'rÇ   N)r*   r´   rÈ   )r#   rÇ   r    r    r!   r)   è  s
    r)   c                 C   s¸   d}| r´d}g }|dkrd}n|dkr,d}nt dƒ‚t ||¡j}d | d¡d	d… ¡}|rl| d
| ¡ | d¡ g }	|jj	}
d|
|d|
f }|D ]}|	 || ¡ q–t 
|	¡}|S )zG
    Try to resolve a list of predefines DNS names to pick mirrors
    Nrc   r–   r{   r‡   zsecurity-mirrorzunknown mirror typeÚ.rŒ   z.%s)z.localdomainrc   zhttp://%s-%s%s/%sz%s)r   r   Zget_hostname_fqdnÚfqdnrd   r‘   rW   Úextendr   r   Úsearch_for_mirror)Z
configuredÚ
mirrortyper#   r$   r{   ZmydomZdomsZ	mirrordnsrÊ   Zmirror_listr   Z	mirrorfmtZpostr    r    r!   Úsearch_for_mirror_dns÷  s.    ÿ
rÎ   c                 C   sX   | dk	r|dkr| }| |dœS |j  ¡ }|rP| ¡ }|d |d< |d |d< |S t|ƒS )z^sets security mirror to primary if not defined.
    returns defaults if no mirrors are definedNr   r–   r   r‡   r   )Z
datasourceZget_package_mirror_infor   r"   )ÚpmirrorÚsmirrorr   r$   Úmirror_infoÚmr    r    r!   Úupdate_mirror_info   s    

rÓ   c                 C   sT   |   |d¡}|dkrdS d}|D ].}|  d¡p0g }||krB|  S d|kr |}q |S )zuout of a list of potential mirror configurations select
    and return the one matching the architecture (or default)NrÀ   rÁ   rµ   )r#   rÍ   r   Zmirror_cfg_listrÁ   Zmirror_cfg_elemrÀ   r    r    r!   Úget_arch_mirrorconfig8  s    rÔ   c                 C   s`   t | ||ƒ}|dkrdS | dd¡}|dkr>t | dd¡¡}|dkr\t| dd¡|| |ƒ}|S )z’pass the three potential stages of mirror specification
    returns None is neither of them found anything otherwise the first
    hit is returnedNr¼   rJ   r¿   )rÔ   r*   r   rÌ   rÎ   )r#   rÍ   r   r$   Zmcfgr{   r    r    r!   Ú
get_mirrorK  s    
   ÿrÕ   c                 C   sn   |dkrt  ¡ }t d|¡ t| d||ƒ}t d|¡ t| d||ƒ}t d|¡ t||||ƒ}|d |d< |S )	a  find_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–   zgot primary mirror: %sr‡   zgot security mirror: %sr   r8   )r   r   r(   r<   rÕ   rÓ   )r#   r$   r   rÏ   rÐ   rÑ   r    r    r!   r=   e  s    r=   c                    s¶   d}‡ fdd„|D ƒ}t |ƒrBt d|¡ t |d |¡d ¡ n"tj |¡rdt 	|¡ t d|¡ ˆ  
dd¡rt d	|¡ t |ˆ  
d¡¡ n"tj |¡r²t 	|¡ t d
|¡ dS )zHapply_apt_config
    Applies any apt*proxy config from if specified
    ))rÃ   úAcquire::http::Proxy "%s";)rÄ   rÖ   )rÆ   zAcquire::ftp::Proxy "%s";)rÅ   zAcquire::https::Proxy "%s";c                    s(   g | ] \}}ˆ   |¡r|ˆ   |¡ ‘qS r    rµ   )r^   r   Zfmt©r#   r    r!   ra   ‰  s     
 z$apply_apt_config.<locals>.<listcomp>zwrite apt proxy info to %sr]   z#no apt proxy configured, removed %sÚconfNzwrite apt config info to %sz$no apt config configured, removed %s)rX   r(   r<   r   rž   rd   rr   r   ÚisfileZdel_filer*   )r#   Zproxy_fnameZconfig_fnameZcfgsZproxiesr    r×   r!   rA   }  s    

rA   Tc                    sV   dd„ ‰ ‡‡‡fdd„}‡ ‡fdd„}| dkr4|ƒ S | dksD| d	krJ|ƒ S t d
ƒ‚dS )ad  apt-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)
    c                  S   sT   t j t¡rtgng } t  t¡D ]&}| d¡s8| d¡r |  t| ¡ q | rP| S dS )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.ascrc   )rr   r   rÙ   ÚAPT_LOCAL_KEYSÚlistdirÚAPT_TRUSTED_GPG_DIRrS   rW   )Z	key_filesÚfiler    r    r!   Ú_get_key_files§  s
    zapt_key.<locals>._get_key_filesc               	      s    d} ˆst  td ˆ ¡¡ n€z2ˆr&tnt}t ˆ ¡}d |ˆ¡} t  | |¡ W nL t	j
k
rv   t  td ˆ ¡¡ Y n& tk
rš   t  td ˆ ¡¡ Y nX | S )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   Zlogexcr(   r-   ÚCLOUD_INIT_GPG_DIRrÜ   r   Zdearmorrž   r   r¥   ÚUnicodeDecodeError)r—   Zkey_dirÚstdout)rR   r¡   r    r    r!   Úapt_key_add¶  s.     ÿ
ÿ
 ÿ ÿ
zapt_key.<locals>.apt_key_addc                     sf   g } ˆ ƒ D ]P}z|   tj|ˆd¡ W q
 tjk
rX } zt d||¡ W 5 d}~X Y q
X q
d | ¡S )zapt-key list

        returns string of all trusted keys (in /etc/apt/trusted.gpg and
        /etc/apt/trusted.gpg.d/)
        )Úhuman_outputzFailed to list key "%s": %sNr]   )rW   r   rZ   r   r¥   r(   rY   rd   )Zkey_listr²   Úerror)rÞ   rã   r    r!   Úapt_key_listÒ  s    
"zapt_key.<locals>.apt_key_listrm   ZfingerrZ   z@apt_key() commands add, list, and finger are currently supportedN)r   )Zcommandr    rR   r¡   rã   râ   rå   r    )rÞ   rR   r¡   rã   r    r!   r¤   ™  s    ÿr¤   z
cloud-init)NN)N)N)N)FN)NFN)NNN)N)NNFT)MÚ__doc__rp   rr   r¢   rH   Zloggingr   Útextwrapr   Z	cloudinitr   r   r   r   r   Zcloudinit.cloudr	   Zcloudinit.configr
   Zcloudinit.config.schemar   r   Zcloudinit.settingsr   Z	getLoggerÚ__name__r(   rG   rÚ   rÜ   rß   r   r   r   Ú__annotations__r€   rC   rB   r©   r   r   r   r   r"   ÚstrrZ   r1   r2   r0   rU   r\   r/   rt   r~   r@   r†   r‹   r•   r>   r?   r¦   r˜   r«   rK   r´   rº   rÂ   rÈ   r)   rÎ   rÓ   rÔ   rÕ   r=   rA   r¤   rV   r    r    r    r!   Ú<module>	   s¼   
ÿÿÿE£`þþ
    þ	-

!
#	&

     ÿ
Y
K)
       ÿ
R ÿ