
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
\mec8              	   @   sd  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m	Z	 ddl
mZ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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e%Z ej&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)suppress)StringIO)dedent)ListUnion)helperssubp
temp_utils
url_helperutil)Cloud)Config)
MetaSchemaget_meta_doc)ALL_DISTROSDistroPackageInstallerError)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
            )idnametitledescriptionZdistrosZ	frequencyZexamplesZactivate_by_schema_keysc               @   s   e Zd Zdd ZdS )PuppetConstantsc             C   s8   || _ || _tjj|d| _tj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.6/cc_puppet.py__init__t   s
    zPuppetConstants.__init__N)__name__
__module____qualname__r&   r$   r$   r$   r%   r   s   s   r   )cloudactionc             C   sd   d}xBt D ]:}y| jj|| d |}P W q
 tjk
rB   Y q
X q
W |s`tjd|djt  dS )z8Attempts to perform action on one of the puppet services z.servicez0Could not '%s' any of the following services: %sz, N)PUPPET_PACKAGE_NAMESdistroZmanage_servicer   ZProcessExecutionErrorLOGwarningr   )r*   r+   Zservice_managedpuppet_namer$   r$   r%   _manage_puppet_services   s    

r2   c             C   s   t j | dd|g\}}|j S )zGet 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    r9   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| j dd4}tjj|d	}t	j
||d
d tj|g| dd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)capture)r
   Zreadurlcontentsr	   ZtempdirZget_tmp_exec_pathr   r   r   r   
write_filer   )	r.   r;   version
collectioncleanupargsZcontentZtmpdZtmpfr$   r$   r%   install_puppet_aio   s    
rE   )r   cfgr*   rD   returnc           
   C   s8  d|krt jd|  d S |d }tj|dd}tj|dd }tj|dd }tj|dd}tj|d	d}	tj|d
dd}
tj|ddd}tj|dtd}|dkrd}d}d}nd}d}d }tj|d|}| r|rt jd n|rt jd|r|nd| |dkr|d krrxJtD ]B}tt	. |r0||ggn|g}|j
j| |}P W d Q R X qW |st jddjt n"|r||ggn|g}|j
j| n.|dkrt|j
||||	 nt jd| d}
tj|dt|d}tj|dt|d}tj|dt|d}t|||}d |krXtj|j}tj }d!d" |j D }d#j|}|jt||jd$ x|d  j D ]\}}|d%krtj|jd& tj|j|d tj|j tj|j|d tj|j| tj|j|d nXxV|j D ]J\}}|d'kr|jd(tj  }|jd)|j! }|j" }|j#||| qW tj$|jd*|j  tj|j|j%  qhW d|kr~tj|j&t'j(|d dd+ |rt)|d, |
r$t jd- |d.g}d/|kr|d/ }t*|t+t,fr|j-| n6t*|t.r|j-|j/  nt jd0t0| |j-t1 n
|j-t1 t2j2|dd1 |r4t)|d2 d S )3Nr   z9Skipping module named %s, no 'puppet' configuration foundinstallTrA   rB   install_typeZpackagesrC   execF)defaultZstart_serviceaio_install_urlZaiorootz/opt/puppetlabs/bin/puppetzpuppet-agent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: %sz, z Unknown puppet install type '%s'	conf_filer3   r   Zssldirr"   Zcsr_attributesZconfc             S   s   g | ]}|j  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 string)r>   start)3r/   debugr   Zget_cfg_option_boolZget_cfg_option_strAIO_INSTALL_URLr0   r-   r   r   r.   Zinstall_packagesr   rE   r9   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dumpr2   
isinstancelisttupleextendstrsplittypePUPPET_AGENT_DEFAULT_ARGSr   ) r   rF   r*   rD   Z
puppet_cfgrH   rA   rB   rI   rC   ZrunZstart_puppetdrL   Zpuppet_userr6   Zpuppet_packagerN   r1   Z
to_installrO   r   r"   Zp_constantsr?   Zpuppet_configZcleaned_linesZcleaned_contentsZcfg_nameovcmdZcmd_argsr$   r$   r%   handle   s    


















rn   )r   r   )0__doc__Zloggingr   r]   
contextlibr   ior   textwrapr   Ztypingr   r   ra   Z	cloudinitr   r   r	   r
   r   Zcloudinit.cloudr   Zcloudinit.configr   Zcloudinit.config.schemar   r   Zcloudinit.distrosr   r   r   Zcloudinit.settingsr   rY   rj   r-   ZMODULE_DESCRIPTIONmetaZ	getLoggerr'   r/   r   rg   r2   r9   rE   rd   rn   r$   r$   r$   r%   <module>	   sN   '
