U
    L¬÷dö6  ã                	   @   sH  U d 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Zddl	m
Z
mZ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mZ dd
lmZ dZdgZdZdZdddeegeedƒedƒgdgdœZee d< eeƒZ e !e"¡Z#G dd„ dƒZ$ee%dœdd„Z&dd„ Z'edddfedœd d!„Z(e%eee)dd"œd#d$„Z*dS )%z+Puppet: Install, configure and start puppeté    N)ÚStringIO)Údedent)ÚhelpersÚsubpÚ
temp_utilsÚ
url_helperÚutil)ÚCloud)ÚConfig)Ú
MetaSchemaÚget_meta_doc)ÚALL_DISTROSÚDistro)ÚPER_INSTANCEzKhttps://raw.githubusercontent.com/puppetlabs/install-puppet/main/install.shz--test)úpuppet-agentÚpuppeta>  This module handles puppet installation and configuration. If the ``puppet``
key does not exist in global configuration, no action will be taken. If a
config entry for ``puppet`` is present, then by default the latest version of
puppet will be installed. If the ``puppet`` config key exists in the config
archive, this module will attempt to start puppet even if no installation was
performed.

The module also provides keys for configuring the new puppet 4 paths and
installing the puppet package from the puppetlabs repositories:
https://docs.puppet.com/puppet/4.2/reference/whered_it_go.html
The keys are ``package_name``, ``conf_file``, ``ssl_dir`` and
``csr_attributes_path``. If unset, their values will default to
ones that work with puppet 3.x and with distributions that ship modified
puppet 4.x that uses the old paths.
Z	cc_puppetZPuppetz#Install, configure and start puppeta`              puppet:
                install: true
                version: "7.7.0"
                install_type: "aio"
                collection: "puppet7"
                aio_install_url: 'https://git.io/JBhoQ'
                cleanup: true
                conf_file: "/etc/puppet/puppet.conf"
                ssl_dir: "/var/lib/puppet/ssl"
                csr_attributes_path: "/etc/puppet/csr_attributes.yaml"
                exec: true
                exec_args: ['--test']
                conf:
                    agent:
                        server: "puppetserver.example.org"
                        certname: "%i.%f"
                    ca_cert: |
                        -----BEGIN CERTIFICATE-----
                        MIICCTCCAXKgAwIBAgIBATANBgkqhkiG9w0BAQUFADANMQswCQYDVQQDDAJjYTAe
                        Fw0xMDAyMTUxNzI5MjFaFw0xNTAyMTQxNzI5MjFaMA0xCzAJBgNVBAMMAmNhMIGf
                        MA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCu7Q40sm47/E1Pf+r8AYb/V/FWGPgc
                        b014OmNoX7dgCxTDvps/h8Vw555PdAFsW5+QhsGr31IJNI3kSYprFQcYf7A8tNWu
                        1MASW2CfaEiOEi9F1R3R4Qlz4ix+iNoHiUDTjazw/tZwEdxaQXQVLwgTGRwVa+aA
                        qbutJKi93MILLwIDAQABo3kwdzA4BglghkgBhvhCAQ0EKxYpUHVwcGV0IFJ1Ynkv
                        T3BlblNTTCBHZW5lcmF0ZWQgQ2VydGlmaWNhdGUwDwYDVR0TAQH/BAUwAwEB/zAd
                        BgNVHQ4EFgQUu4+jHB+GYE5Vxo+ol1OAhevspjAwCwYDVR0PBAQDAgEGMA0GCSqG
                        SIb3DQEBBQUAA4GBAH/rxlUIjwNb3n7TXJcDJ6MMHUlwjr03BDJXKb34Ulndkpaf
                        +GAlzPXWa7bO908M9I8RnPfvtKnteLbvgTK+h+zX1XCty+S2EQWk29i2AdoqOTxb
                        hppiGMp0tT5Havu4aceCXiy2crVcudj3NFciy8X66SoECemW9UYDCb9T5D0d
                        -----END CERTIFICATE-----
                csr_attributes:
                    custom_attributes:
                        1.2.840.113549.1.9.7: 342thbjkt82094y0uthhor289jnqthpc2290
                    extension_requests:
                        pp_uuid: ED803750-E3C7-44F5-BB08-41A04433FE2E
                        pp_image_name: my_ami_image
                        pp_preshared_key: 342thbjkt82094y0uthhor289jnqthpc2290
            zŒ            puppet:
                install_type: "packages"
                package_name: "puppet"
                exec: false
            r   )ÚidÚnameÚtitleZdescriptionZdistrosZ	frequencyZexamplesZactivate_by_schema_keysÚmetac                   @   s   e Zd Zdd„ ZdS )ÚPuppetConstantsc                 C   s8   || _ || _tj |d¡| _tj | jd¡| _|| _d S )NZcertszca.pem)Ú	conf_pathÚssl_dirÚosÚpathÚjoinÚssl_cert_dirÚssl_cert_pathÚcsr_attributes_path)ÚselfZpuppet_conf_fileZpuppet_ssl_dirr   © r    ú</usr/lib/python3/dist-packages/cloudinit/config/cc_puppet.pyÚ__init__r   s
    zPuppetConstants.__init__N)Ú__name__Ú
__module__Ú__qualname__r"   r    r    r    r!   r   q   s   r   )ÚcloudÚactionc              	   C   sd   d}t D ]>}z"| j ||› d¡ |}W  qHW q tjk
rD   Y qX q|s`t d|d t ¡¡ dS )z8Attempts to perform action on one of the puppet servicesÚ z.servicez0Could not '%s' any of the following services: %sú, N)ÚPUPPET_PACKAGE_NAMESÚdistroZmanage_servicer   ÚProcessExecutionErrorÚLOGÚwarningr   )r&   r'   Zservice_managedÚpuppet_namer    r    r!   Ú_manage_puppet_services   s    
ýr0   c                 C   s   t   | dd|g¡\}}| ¡ S )z›Get the config value for a given setting using `puppet config print`
    :param puppet_bin: path to puppet binary
    :param setting: setting to query
    ÚconfigÚprint)r   Úrstrip)Ú
puppet_binZsettingÚoutÚ_r    r    r!   Úget_config_value‘   s    r7   T)r+   c           	   
   C   s¢   g }|dk	rd|g}|dk	r(|d|g7 }|r6|dg7 }t j|ddj}tj|  ¡ dd@}tj |d	¡}t	j
||d
d tj|g| ddW  5 Q R £ S Q R X dS )a—  Install puppet-agent from the puppetlabs repositories using the one-shot
    shell script

    :param distro: Instance of Distro
    :param url: URL from where to download the install script
    :param version: version to install, blank defaults to latest
    :param collection: collection to install, blank defaults to latest
    :param cleanup: whether to purge the puppetlabs repo after installation
    Nz-vz-cz	--cleanupé   )ÚurlZretriesT)ÚdirZ	needs_exezpuppet-installiÀ  )ÚmodeF©Zcapture)r   ZreadurlÚcontentsr   ZtempdirZget_tmp_exec_pathr   r   r   r   Ú
write_filer   )	r+   r9   ÚversionÚ
collectionÚcleanupÚargsZcontentZtmpdZtmpfr    r    r!   Úinstall_puppet_aioš   s     
 ÿrC   )r   Úcfgr&   rB   Úreturnc              	   C   s  d|krt  d| ¡ d S |d }t |dd¡}t |dd ¡}t |dd ¡}t |dd¡}t |d	d¡}	tj|d
dd}
tj|ddd}tj|dtd}|dkr¬d}d}d}nd}d}d }t |d|¡}|sÚ|rÚt  d¡ nÐ|rªt  d|rî|nd|¡ |dkr||d krjtD ]@}z |j 	||f¡ |}W  qPW n t
jk
rJ   Y nX q|szt  dd t¡¡ n|j 	||f¡ n.|dkršt|j||||	ƒ nt  d|¡ d}
t |dt|dƒ¡}t |dt|dƒ¡}t |dt|dƒ¡}t|||ƒ}d |kr6t |j¡}t ¡ }d!d"„ | ¡ D ƒ}d# |¡}|jt|ƒ|jd$ |d   ¡ D ]è\}}|d%kr¸t |jd&¡ t |j|d¡ t |j¡ t |j|d¡ t |j|¡ t |j|d¡ nT| ¡ D ]J\}}|d'krú| d(t  ¡ ¡}| d)| !¡ ¡}| "¡ }| #|||¡ qÀt $|jd*|j ¡ t |j| %¡ ¡ qLd|kr\t |j&t'j(|d dd+¡ |rlt)|d,ƒ |
rt  d-¡ |d.g}d/|krê|d/ }t*|t+t,fƒr²| -|¡ n6t*|t.ƒrÎ| -| /¡ ¡ nt  d0t0|ƒ¡ | -t1¡ n
| -t1¡ t
j
|dd1 |rt)|d2ƒ d S )3Nr   z9Skipping module named %s, no 'puppet' configuration foundÚinstallTr?   r@   Úinstall_typeZpackagesrA   ÚexecF)ÚdefaultZstart_serviceÚaio_install_urlZaioÚrootz/opt/puppetlabs/bin/puppetr   Úpackage_namez@Puppet install set to false but version supplied, doing nothing.z'Attempting to install puppet %s from %sZlatestz+No installable puppet package in any of: %sr)   z Unknown puppet install type '%s'Ú	conf_filer1   r   Zssldirr   Zcsr_attributesÚconfc                 S   s   g | ]}|  ¡ ‘qS r    )Úlstrip)Ú.0Úir    r    r!   Ú
<listcomp>  s     zhandle.<locals>.<listcomp>Ú
)ÚsourceZca_certiù  Zcertnamez%fz%iz%s.old)Zdefault_flow_styleÚenablezRunning puppet-agentZagentZ	exec_argszOUnknown type %s provided for puppet 'exec_args' expected list, tuple, or stringr<   Ústart)2r-   Údebugr   Zget_cfg_option_boolZget_cfg_option_strÚAIO_INSTALL_URLr.   r*   r+   Zinstall_packagesr   r,   r   rC   r7   r   Z	load_filer   r   ZDefaultingConfigParserÚ
splitlinesZ	read_filer   ÚitemsZ
ensure_dirr   Zchownbynamer   r>   r   ÚreplaceÚsocketZgetfqdnZget_instance_idÚlowerÚsetÚrenameZ	stringifyr   ÚyamlÚdumpr0   Ú
isinstanceÚlistÚtupleÚextendÚstrÚsplitÚtypeÚPUPPET_AGENT_DEFAULT_ARGS)r   rD   r&   rB   Z
puppet_cfgrF   r?   r@   rG   rA   ÚrunZstart_puppetdrJ   Zpuppet_userr4   Zpuppet_packagerL   r/   rM   r   r   Zp_constantsr=   Zpuppet_configZcleaned_linesZcleaned_contentsZcfg_nameÚoÚvÚcmdZcmd_argsr    r    r!   Úhandle¾   s&    ÿ  ÿ  ÿ  ÿ  ÿÿ
ý


þ
    ÿ  ÿ  ÿý

 ÿ
  ÿ
 ÿ
þ


ü
rn   )+Ú__doc__Zloggingr   r\   Úior   Útextwrapr   r`   Z	cloudinitr   r   r   r   r   Zcloudinit.cloudr	   Zcloudinit.configr
   Zcloudinit.config.schemar   r   Zcloudinit.distrosr   r   Zcloudinit.settingsr   rX   ri   r*   ZMODULE_DESCRIPTIONr   Ú__annotations__Z	getLoggerr#   r-   r   rf   r0   r7   rC   rc   rn   r    r    r    r!   Ú<module>	   sX   ÿ)ÿÖ3Æ=
ûÿ$