
    fi8              
          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&d<    ee%      Z  ejN                  e(      Z) G d d      Z*dede+fdZ,d Z-e!dddfd efd!Z.d"e+d#eded$e/d%df
d&Z0y)'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.
	cc_puppet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titledescriptiondistros	frequencyexamplesactivate_by_schema_keysmetac                       e Zd Zd Zy)PuppetConstantsc                     || _         || _        t        j                  j	                  |d      | _        t        j                  j	                  | j
                  d      | _        || _        y )Ncertszca.pem)	conf_pathssl_dirospathjoinssl_cert_dirssl_cert_pathcsr_attributes_path)selfpuppet_conf_filepuppet_ssl_dirr-   s       </usr/lib/python3/dist-packages/cloudinit/config/cc_puppet.py__init__zPuppetConstants.__init__t   sN     *%GGLLAWW\\$*;*;XF#6     N)__name__
__module____qualname__r2    r3   r1   r#   r#   s   s    
7r3   r#   cloudactionc                     d}t         D ]%  }	 | j                  j                  || d       |} n |s+t
        j                  d|dj                  t                      yy# t        j                  $ r Y kw xY w)z8Attempts to perform action on one of the puppet services z.servicez0Could not '%s' any of the following services: %s, N)PUPPET_PACKAGE_NAMESdistromanage_servicer	   ProcessExecutionErrorLOGwarningr*   )r8   r9   service_managedpuppet_names       r1   _manage_puppet_servicesrE      s    O+ 	LL'';-x0HI)O	 >II*+	
  )) 		s   !AA54A5c                 Z    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_binsettingout_s       r1   get_config_valuerN      s+    
 YY
Hgw?@FC::<r3   Tr>   c                    g }|d|g}||d|gz  }|r|dgz  }t        j                  |d      j                  }t        j                  | j                         d      5 }t        j                  j                  |d	      }t        j                  ||d
       t        j                  |g|z   d      cddd       S # 1 sw Y   yxY w)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   )urlretriesT)dir	needs_exezpuppet-installi  )modeFcapture)r   readurlcontentsr
   tempdirget_tmp_exec_pathr(   r)   r*   r   
write_filer	   )	r>   rQ   version
collectioncleanupargscontenttmpdtmpfs	            r1   install_puppet_aiord      s      Dgz""   S!4==G 
		$$&$
 7	ww||D"23gE2yy$$67 7 7s    AB==Cr   cfgr`   returnc                 f   d|vrt         j                  d|        y |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      }
t        j                  |dd      }t        j                  |dt
              }|dk(  rd}d}d}nd}d}d }t        j                  |d|      }|s|rt         j                  d       n|rt         j                  d|r|nd|       |dk(  r|xt        D ]C  }t        t              5  |r||ggn|g}|j                  j                  |       |}	 d d d         n |st         j                  ddj                  t                     n]|r||ggn|g}|j                  j                  |       n7|dk(  rt        |j                  ||||	       nt         j                  d|       d}
t        j                  |dt        |d            }t        j                  |dt        |d            }t        j                  |dt        |d            }t        |||      }d |v rYt        j                   |j"                        }t%        j&                         }|j)                         D cg c]  }|j+                          }}d!j                  |      }|j-                  t/        |      |j"                  "       |d    j1                         D ]  \  }}|d#k(  rt        j2                  |j4                  d$       t        j6                  |j4                  |d       t        j2                  |j8                         t        j6                  |j8                  |d       t        j:                  |j<                  |       t        j6                  |j<                  |d       n|j1                         D ]q  \  }}|d%k(  rT|j?                  d&tA        jB                               }|j?                  d'|jE                               }|jG                         }|jI                  |||       s t        jJ                  |j"                  d(|j"                  z         t        j:                  |j"                  |jM                                 d|v r8t        j:                  |jN                  tQ        jR                  |d   d)             |rtU        |d*       |
rt         j                  d+       |d,g}d-|v r|d-   } tW        | tX        tZ        f      r|j]                  |        nztW        | t^              r |j]                  | ja                                nJt         j                  d.tc        |              |j]                  td               n|j]                  td               tg        jf                  |d/       |rtU        |d0       y y # 1 sw Y   xY wc c}w )1Nr   z9Skipping module named %s, no 'puppet' configuration foundinstallTr]   r^   install_typepackagesr_   execF)defaultstart_serviceaio_install_url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 %slatestz+No installable puppet package in any of: %sr<   z Unknown puppet install type '%s'	conf_filerG   r'   ssldirr-   csr_attributesconf
)sourceca_certi  certnamez%fz%iz%s.old)default_flow_styleenablezRunning puppet-agentagent	exec_argszOUnknown type %s provided for puppet 'exec_args' expected list, tuple, or stringrV   start)4rA   debugr   get_cfg_option_boolget_cfg_option_strAIO_INSTALL_URLrB   r=   r   r   r>   install_packagesr*   rd   rN   r#   load_text_filer&   r   DefaultingConfigParser
splitlineslstrip	read_filer   items
ensure_dirr'   chownbynamer+   r\   r,   replacesocketgetfqdnget_instance_idlowersetrename	stringifyr-   yamldumprE   
isinstancelisttupleextendstrsplittypePUPPET_AGENT_DEFAULT_ARGSr	   )!r   re   r8   r`   
puppet_cfgrh   r]   r^   ri   r_   runstart_puppetdrn   puppet_userrJ   puppet_packagerq   rD   
to_installrs   r'   r-   p_constantsrY   puppet_configicleaned_linescleaned_contentscfg_nameovcmdcmd_argss!                                    r1   handler      s   s		G	
 	XJ&&z9dCG%%j)TBG((\4HJ**NJL &&z9dCG

"
":vu
EC,,OTM --%O
 u1
'
**NNL wN	
 
		5GH	
 :%##7 	K!"78   ' *734"- #
 55jA'2 	 $KKE		"67 29lG,-|n  --j9U"ow
G KK:LIC''K!1*h!GI %%I/
HEG 11%56 ")W6IJK &&{'<'<=668 .6-@-@-BCCC99]3%&{/D/D 	  	
 (/557 !	NMHc 9$  3 3U;  !4!4k6J 8 89  !9!9;O 9 93?  --{F  IIK 	6DAqJ IIdFNN,<=IIdE,A,A,CDGGI!%%h15	6 KK%%x;3H3H'I OOK11=3J3J3LMC!	NF :%++IIj!12uM	

 x0 		()7#*$!+.H(T5M2

8$Hc*

8>>+,! N	 

45JJ01		#u%w/ w f Ds   )X!X.!X+	)1__doc__loggingr(   r   
contextlibr   ior   textwrapr   typingr   r   r   	cloudinitr   r	   r
   r   r   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar   r   cloudinit.distrosr   r   r   cloudinit.settingsr   r   r   r=   MODULE_DESCRIPTIONr!   __annotations__	getLoggerr4   rA   r#   r   rE   rN   rd   r   r   r7   r3   r1   <module>r      s0   2  	       A A ! # < H H +_%J 1  $ 2%}&(	
R 		
U2f !)zu;j ;z t
g!7 7
5 
# 
$ 	!7!7Hr0 r06 r0% r0t r0 r0r3   