3 \me5 @sUdZddlZddlZddlZddlZddlZddlmZddlm Z m Z m Z ddl m Z ddlmZddlmZmZddlmZmZdd lmZd Zd d d eegeedededgdgdZeeeZdddiiddgddZddgdddgdddgddZejeZejdZ ejdZ!ed d!d"Z"d8d#d$Z#d9d%d&Z$e%ee%d'd(d)Z&d*d+Z'd:d,d-Z(Gd.d/d/Z)d;d0d1Z*e dd2d3d4Z+e,ee e-dd5d6d7Z.dS)[@]{0,2})(([\[](?P[^\]]*)[\]])|(?P[^:]*))([:](?P[0-9]+))?$)distrocCs4t}tjt}|j|kr0tj|||jgdd}|S)zConstruct a distro-specific rsyslog config dictionary by merging distro specific changes into base config. @param distro: String providing the distro class name. @returns: Dict of distro configurations for ntp clients. T)reverse)DISTRO_OVERRIDEScopyRSYSLOG_CONFIGZosfamilyrZ mergemanydictr)rZdcfgcfgr! /usr/lib/python3.6/cc_rsyslog.pydistro_default_rsyslog_configs   r#cCs(tj|rdS|dkrdg}||dS)aiInstall rsyslog package if not already installed. @param install_func: function. This parameter is invoked with the contents of the packages parameter. @param packages: list. This parameter defaults to ['rsyslog']. @param check_exe: string. The name of a binary that indicates the package the specified package is already installed. Nr )rZwhich)Z install_funcrrr!r!r"rs rcCs.|dkr |jdd}|jd|Stj|ddS)Nrrsyslog_svcnamer z try-reloadT)Zcapture) get_optionmanage_servicer)rcommandservicer!r!r" reload_syslogs  r))r rreturnc Cs&|jdi}t|}t|jdtrhtjdddd|jdi}d|krT|d|d<d|krh|d|d <dgtfd |d tfd|dtfd |d tfd |d ttffd |d tfd |d tfd|dtff}xT|D]L\}}}||kr|||<qt|||st d|d|dt ||qW|S)zReturn an updated config. Support converting the old top level format into new format. Raise a `ValueError` if some top level entry has an incorrect type. r z)The rsyslog key with value of type 'list'z22.2)Z deprecatedZdeprecated_versionrZrsyslog_filenamerZ rsyslog_dirrrrrrrzInvalid type for key `z`. Expected type(s): z. Current type: ) getr# isinstancelistrZ deprecatestrdictbool ValueErrortype)r rmycfgZ distro_configZfillupkeydefaultZvtypesr!r!r" load_configs8         $r6c Csg}xt|D]\}}t|trPd|kr:tjd|dq|d}|jd|}n|}|}|j}|svtjd|dqtjj ||}d}||krd}|j |y*d} |j d sd } t j ||| |d Wqtk rt jtd |YqXqW|S) Ncontentz%No 'content' entry in config entry %sfilenamezEntry %s has an empty filenameZabwb )omodezFailed to write to %s) enumerater,r/LOGwarningr+stripospathjoinappendendswithrZ write_file ExceptionZlogexc) r def_fnamecfg_dirfilesZcur_posZentr7r9r=Zendlr!r!r"apply_rsyslog_changess8    rKc Csytj|\}}|j}Wntk r8|d}}YnX|jj}d}t|dkr\|}n"t|dkrr|\}}n td|tj|}|std||jd}|jdp|jd}|jd} |jd r|j d  rtd ||r| r|}t ||||| d } | j | S) Nr8zline had multiple spaces: %szInvalid host specification '%s'protoaddrZ bracket_addrport[]z"host spec had invalid brackets: %s)rmatchrMrNrO) COMMENT_REsplitrAr1len HOST_PORT_RErRgroup startswithrFSyslogRemotesLinevalidate) linerdataZcommentZtoksrRZ host_portrMrNrOtr!r!r"parse_remotes_lines4            r^c@s.eZdZd ddZddZddZdd ZdS) rYNcCs\|sd}||_||_|sd}|dkr*d}n |dkr6d}||_||_|rRt||_nd|_dS)Nz*.*udp@z@@tcp)rrRrMrNintrO)selfrrRrMrNrOr!r!r"__init__&s zSyslogRemotesLine.__init__cCsZ|jrHyt|jWn2tk rF}ztd|j|WYdd}~XnX|jsVtddS)Nzport '%s' is not an integerzaddress is required)rOrbr1rN)rcer!r!r"rZ;s zSyslogRemotesLine.validatecCsd|j|j|j|j|jfS)Nz.[name=%s match=%s proto=%s address=%s port=%s])rrRrMrNrO)rcr!r!r"__repr__Gs zSyslogRemotesLine.__repr__cCs|jd}|jdkr|d7}n|jdkr0|d7}d|jkrN|d|jd7}n ||j7}|jrl|d |j7}|jr|d |j7}|S) N r_r`raz@@:rPrQz:%sz # %s)rRrMrNrOr)rcZbufr!r!r"__str__Ps      zSyslogRemotesLine.__str__)NNNNN)__name__ __module__ __qualname__rdrZrfrir!r!r!r"rY%s   rYcCs|sdSg}|dk r|j|xh|jD]\\}}|s6q(y|jtt||dWq(tk r}ztjd|||WYdd}~Xq(Xq(W|dk r|j|dj|dS)N)rz!failed loading remote %s: %s [%s]r<)rEitemsr.r^r1r?r@rD)rheaderfooterlinesrr[rer!r!r"remotes_to_rsyslog_cfgds & rq)cloudr*cCsvy|jjddWntjk r(dSX|jjdd|jjtjtj|jjddtjdWdQRXdS)z This helper function bundles the necessary steps to disable BSD base syslog ``rc(8)`` reads its configuration on start, so after disabling syslogd, we need to tell rc to reload its config ZenabledZsyslogdNdisableZonestopzOsyslogd is running before cloud-init! Please report this as bug to the porters!) rr&rProcessExecutionErrorZ reload_init contextlibsuppressr?error)rrr!r!r" disable_and_stop_bsd_base_syslogvs rx)rr rrargsr*c Cs^d|krtjd|dSt||j}|d}|drL|jt|dddd|jjdd}|d d krt|jj|d |d d t j r|jj d|t ||dstjddSt |d|d|dd}|stjddSyt|j|dd}Wn:tjk r0} zd}tjdt| WYdd} ~ XnX|rZtjtj|jtjd||dS)Nr z;Skipping module named %s, no 'rsyslog' key in configurationrrz# begin remotesz # end remotes)rnror$rTrr)rrenablez.Empty config rsyslog['configs'], nothing to dorr)rrHrIz0restart of syslog not necessary, no changes mader)r'FzFailed to reload syslog %sz%s configured %s files)r?debugr6rrErqr%rZinstall_packagesrZis_BSDr&rxrKr)rrtr@r.rZ reset_loggingZ setup_loggingr ) rr rrryr3rr(ZchangesZ restartedrer!r!r"handlesR       " r|)Nr )r)N)NN)/__doc__rurZloggingrBretextwraprZ cloudinitrrrZcloudinit.cloudrZcloudinit.configrZcloudinit.config.schemarr Zcloudinit.distrosr r Zcloudinit.settingsr ZMODULE_DESCRIPTIONmetarrZ getLoggerrjr?compilerSrVr#rr)r/r6rKr^rYrqrxr.r-r|r!r!r!r" sn        /) &?