[augeas-devel] Re: A few lenses... [logrotate.aug]

Raphaël Pinson raphink at gmail.com
Fri Jul 11 10:15:42 UTC 2008


On Fri, Jul 11, 2008 at 12:13 PM, Raphaël Pinson <raphink at gmail.com> wrote:

>
> As for other lenses, I sometimes fail to insert values before paths. E.g.
>
> ====================================================
> augtool> print /files/etc/logrotate.d/apache2/
> /files/etc/logrotate.d/apache2
> /files/etc/logrotate.d/apache2/rule = "/var/log/apache2/*.log"
> /files/etc/logrotate.d/apache2/rule/schedule = "weekly"
> /files/etc/logrotate.d/apache2/rule/missingok = "missingok"
> /files/etc/logrotate.d/apache2/rule/rotate = "52"
> /files/etc/logrotate.d/apache2/rule/compress = "compress"
> /files/etc/logrotate.d/apache2/rule/delaycompress = "delaycompress"
> /files/etc/logrotate.d/apache2/rule/ifempty = "notifempty"
> /files/etc/logrotate.d/apache2/rule/create
> /files/etc/logrotate.d/apache2/rule/create/mode = "640"
> /files/etc/logrotate.d/apache2/rule/create/owner = "root"
> /files/etc/logrotate.d/apache2/rule/create/group = "adm"
> /files/etc/logrotate.d/apache2/rule/sharedscripts
> /files/etc/logrotate.d/apache2/rule/postrotate
> /files/etc/logrotate.d/apache2/rule/postrotate/line[1] = "if [ -f
> /var/run/apache2.pid ]; then"
> /files/etc/logrotate.d/apache2/rule/postrotate/line[2] =
> "/etc/init.d/apache2 restart > /dev/null"
> /files/etc/logrotate.d/apache2/rule/postrotate/line[3] = "fi"
> augtool> ins schedule before /files/etc/logrotate.d/apache2/rule/postrotate
> augtool> print /files/etc/logrotate.d/apache2/
> /files/etc/logrotate.d/apache2
> /files/etc/logrotate.d/apache2/rule = "/var/log/apache2/*.log"
> /files/etc/logrotate.d/apache2/rule/schedule[1] = "weekly"
> /files/etc/logrotate.d/apache2/rule/missingok = "missingok"
> /files/etc/logrotate.d/apache2/rule/rotate = "52"
> /files/etc/logrotate.d/apache2/rule/compress = "compress"
> /files/etc/logrotate.d/apache2/rule/delaycompress = "delaycompress"
> /files/etc/logrotate.d/apache2/rule/ifempty = "notifempty"
> /files/etc/logrotate.d/apache2/rule/create
> /files/etc/logrotate.d/apache2/rule/create/mode = "640"
> /files/etc/logrotate.d/apache2/rule/create/owner = "root"
> /files/etc/logrotate.d/apache2/rule/create/group = "adm"
> /files/etc/logrotate.d/apache2/rule/sharedscripts
> /files/etc/logrotate.d/apache2/rule/schedule[2]
> /files/etc/logrotate.d/apache2/rule/postrotate
> /files/etc/logrotate.d/apache2/rule/postrotate/line[1] = "if [ -f
> /var/run/apache2.pid ]; then"
> /files/etc/logrotate.d/apache2/rule/postrotate/line[2] =
> "/etc/init.d/apache2 restart > /dev/null"
> /files/etc/logrotate.d/apache2/rule/postrotate/line[3] = "fi"
> augtool> set /files/etc/logrotate.d/apache2/rule/schedule[2] "daily"
> augtool> print /files/etc/logrotate.d/apache2/
> /files/etc/logrotate.d/apache2
> /files/etc/logrotate.d/apache2/rule = "/var/log/apache2/*.log"
> /files/etc/logrotate.d/apache2/rule/schedule[1] = "weekly"
> /files/etc/logrotate.d/apache2/rule/missingok = "missingok"
> /files/etc/logrotate.d/apache2/rule/rotate = "52"
> /files/etc/logrotate.d/apache2/rule/compress = "compress"
> /files/etc/logrotate.d/apache2/rule/delaycompress = "delaycompress"
> /files/etc/logrotate.d/apache2/rule/ifempty = "notifempty"
> /files/etc/logrotate.d/apache2/rule/create
> /files/etc/logrotate.d/apache2/rule/create/mode = "640"
> /files/etc/logrotate.d/apache2/rule/create/owner = "root"
> /files/etc/logrotate.d/apache2/rule/create/group = "adm"
> /files/etc/logrotate.d/apache2/rule/sharedscripts
> /files/etc/logrotate.d/apache2/rule/schedule[2] = "daily"
> /files/etc/logrotate.d/apache2/rule/postrotate
> /files/etc/logrotate.d/apache2/rule/postrotate/line[1] = "if [ -f
> /var/run/apache2.pid ]; then"
> /files/etc/logrotate.d/apache2/rule/postrotate/line[2] =
> "/etc/init.d/apache2 restart > /dev/null"
> /files/etc/logrotate.d/apache2/rule/postrotate/line[3] = "fi"
>
> This works so far. Now if I try to insert in the beginning of the file ...
>
> augtool> ins schedule before /files/etc/logrotate.d/apache2/rule
> augtool> print /files/etc/logrotate.d/apache2/
> /files/etc/logrotate.d/apache2
> /files/etc/logrotate.d/apache2/rule = "/var/log/apache2/*.log"
> /files/etc/logrotate.d/apache2/rule/schedule[1] = "weekly"
> /files/etc/logrotate.d/apache2/rule/missingok = "missingok"
> /files/etc/logrotate.d/apache2/rule/rotate = "52"
> /files/etc/logrotate.d/apache2/rule/compress = "compress"
> /files/etc/logrotate.d/apache2/rule/delaycompress = "delaycompress"
> /files/etc/logrotate.d/apache2/rule/ifempty = "notifempty"
> /files/etc/logrotate.d/apache2/rule/create
> /files/etc/logrotate.d/apache2/rule/create/mode = "640"
> /files/etc/logrotate.d/apache2/rule/create/owner = "root"
> /files/etc/logrotate.d/apache2/rule/create/group = "adm"
> /files/etc/logrotate.d/apache2/rule/sharedscripts
> /files/etc/logrotate.d/apache2/rule/schedule[2] = "daily"
> /files/etc/logrotate.d/apache2/rule/postrotate
> /files/etc/logrotate.d/apache2/rule/postrotate/line[1] = "if [ -f
> /var/run/apache2.pid ]; then"
> /files/etc/logrotate.d/apache2/rule/postrotate/line[2] =
> "/etc/init.d/apache2 restart > /dev/null"
> /files/etc/logrotate.d/apache2/rule/postrotate/line[3] = "fi"
>
> I don't see the schedule entry, but in the end of the file :
>
> augtool> ins schedule after /files/etc/logrotate.d/apache2/rule
> augtool> print /files/etc/logrotate.d/apache2/
> /files/etc/logrotate.d/apache2
> /files/etc/logrotate.d/apache2/rule = "/var/log/apache2/*.log"
> /files/etc/logrotate.d/apache2/rule/schedule[1] = "weekly"
> /files/etc/logrotate.d/apache2/rule/missingok = "missingok"
> /files/etc/logrotate.d/apache2/rule/rotate = "52"
> /files/etc/logrotate.d/apache2/rule/compress = "compress"
> /files/etc/logrotate.d/apache2/rule/delaycompress = "delaycompress"
> /files/etc/logrotate.d/apache2/rule/ifempty = "notifempty"
> /files/etc/logrotate.d/apache2/rule/create
> /files/etc/logrotate.d/apache2/rule/create/mode = "640"
> /files/etc/logrotate.d/apache2/rule/create/owner = "root"
> /files/etc/logrotate.d/apache2/rule/create/group = "adm"
> /files/etc/logrotate.d/apache2/rule/sharedscripts
> /files/etc/logrotate.d/apache2/rule/schedule[2] = "daily"
> /files/etc/logrotate.d/apache2/rule/postrotate
> /files/etc/logrotate.d/apache2/rule/postrotate/line[1] = "if [ -f
> /var/run/apache2.pid ]; then"
> /files/etc/logrotate.d/apache2/rule/postrotate/line[2] =
> "/etc/init.d/apache2 restart > /dev/null"
> /files/etc/logrotate.d/apache2/rule/postrotate/line[3] = "fi"
> /files/etc/logrotate.d/apache2/schedule
> augtool> set /files/etc/logrotate.d/apache2/schedule "weekly"
> augtool> print /files/etc/logrotate.d/apache2/
> /files/etc/logrotate.d/apache2
> /files/etc/logrotate.d/apache2/rule = "/var/log/apache2/*.log"
> /files/etc/logrotate.d/apache2/rule/schedule[1] = "weekly"
> /files/etc/logrotate.d/apache2/rule/missingok = "missingok"
> /files/etc/logrotate.d/apache2/rule/rotate = "52"
> /files/etc/logrotate.d/apache2/rule/compress = "compress"
> /files/etc/logrotate.d/apache2/rule/delaycompress = "delaycompress"
> /files/etc/logrotate.d/apache2/rule/ifempty = "notifempty"
> /files/etc/logrotate.d/apache2/rule/create
> /files/etc/logrotate.d/apache2/rule/create/mode = "640"
> /files/etc/logrotate.d/apache2/rule/create/owner = "root"
> /files/etc/logrotate.d/apache2/rule/create/group = "adm"
> /files/etc/logrotate.d/apache2/rule/sharedscripts
> /files/etc/logrotate.d/apache2/rule/schedule[2] = "daily"
> /files/etc/logrotate.d/apache2/rule/postrotate
> /files/etc/logrotate.d/apache2/rule/postrotate/line[1] = "if [ -f
> /var/run/apache2.pid ]; then"
> /files/etc/logrotate.d/apache2/rule/postrotate/line[2] =
> "/etc/init.d/apache2 restart > /dev/null"
> /files/etc/logrotate.d/apache2/rule/postrotate/line[3] = "fi"
> /files/etc/logrotate.d/apache2/schedule = "weekly"
>
>
> It works... Any idea anyone ?
>
>
I should precise that my apache2 file has no empty line in the beginning.

If I add an empty line in the beginning of the file, I am able to insert
data before the rule...




>
> ====================================================
>
> (* Logrotate module for Augeas                *)
> (* Author: Raphael Pinson <raphink at gmail.com> *)
> (*                                            *)
> (* Supported :                                *)
> (*   - defaults                               *)
> (*   - rules                                  *)
> (*   - postrotate entries                     *)
> (*                                            *)
> (* Todo :                                     *)
> (*                                            *)
>
> module Logrotate =
>    autoload xfm
>
>    let sep_spc = Util.del_ws_spc
>    let eol = Util.del_str "\n"
>    let num = /[0-9]+/
>    let word = /[^,# \n\t{}]+/
>
>
>    (* define comments and empty lines *)
>    let comment (indent:string) = [ label "comment" . Util.del_str indent .
> del /#[ \t]*/ "# " .  store /([^ \t\n][^\n]*)?/ . eol ]
>    let empty   = [ del /[ \t]*\n/ "" ]
>
>
>    (* Useful functions *)
>
>    let select_to_eol (kw:string) (select:regexp) (indent:string) = [
> Util.del_str indent . label kw . store select . eol ]
>    let value_to_eol (kw:string) (value:regexp) (indent:string )  = [
> Util.del_str indent . key kw . sep_spc . store value . eol ]
>    let flag_to_eol (kw:string) (indent:string)                   = [
> Util.del_str indent . key kw . eol ]
>
>
>    (* Defaults *)
>
>    let create (indent:string ) = [ Util.del_str indent . key "create" .
>                        ( sep_spc . [ label "mode" . store num ] . sep_spc .
>                        [ label "owner" . store word ] . sep_spc .
>                        [ label "group" . store word ])?
>                     . eol ]
>
>    let attrs (indent:string) = select_to_eol "schedule"
> /(daily|weekly|monthly)/ indent
>                 | value_to_eol "rotate" num indent
>                 | create indent
>                 | flag_to_eol "nocreate" indent
>                 | value_to_eol "include" word indent
>                 | select_to_eol "missingok" /(no)?missingok/ indent
>                 | select_to_eol "compress" /(no)?compress/ indent
>                 | select_to_eol "delaycompress" /(no)?delaycompress/ indent
>                 | select_to_eol "ifempty" /(not)?ifempty/ indent
>                 | flag_to_eol "sharedscripts" indent
>                 | value_to_eol "size" word indent
>                 | value_to_eol "tabooext" word indent
>                 | value_to_eol "olddir" word indent
>                 | flag_to_eol "noolddir" indent
>                 | value_to_eol "mail" word indent
>                 | flag_to_eol "mailfirst" indent
>                 | flag_to_eol "maillast" indent
>                 | flag_to_eol "nomail" indent
>                 | value_to_eol "errors" word indent
>                 | value_to_eol "extension" word indent
>
>
>    (* Define hooks *)
>
>    let endscript_re = /[ \t]*endscript[ \t]*/
>    let hook_func (func_type:string) = [
>         del /[ \t]*/ "\t" . key func_type . eol .
>           [ label "line" . del /[ \t]*/ "" . store (/([^ \t\n][^\n]*)?/ -
> endscript_re) . eol ]* .
>         del (endscript_re . "\n") "\tendscript\n" ]
>
>    let hooks = hook_func "postrotate"
>              | hook_func "prerotate"
>
>
>    (* Define rule *)
>
>    let body = Util.del_str "{\n"
>                        . ( comment "\t" | attrs "\t" | hooks | empty )*
>                        . Util.del_str "}\n"
>
>    let rule =
>      [ label "rule" . store word . (sep_spc)? . eol? . body ]
>
>
>    let lns = ( comment "" | empty | attrs "" | rule )*
>
>    let filter = incl "/etc/logrotate.d/*"
>               . incl "/etc/logrotate.conf"
>               . Util.stdexcl
>
>    let xfm = transform lns filter
>
> ====================================================
>
>
>
> Raphaël
>
>
>
>
> On Wed, Jul 9, 2008 at 11:00 PM, David Lutterkort <lutter at watzmann.net>
> wrote:
>
>> On Wed, 2008-07-09 at 11:17 +0200, Raphaël Pinson wrote:
>> > Here are the new versions of logrotate.aug and test_logrotate.aug
>> >
>> > Notes :
>> >    - I switched back from seq to label for the hook_func lines,
>> > because seq would keep increasing the line numbers outside of the
>> > context, i.e. postrotate/1, postrotate/2, prerotate/3, prerotate/4,
>> > etc., which doesn't make sense. label on the other hand doesn't act
>> > this way. Is this a bug in seq ?
>>
>> When you say 'seq "foo"' you are using a global sequence "foo" that
>> keeps increasing while a file is being read. You can reset that sequence
>> by using 'counter "foo"', so if you have something like
>>
>>        [ counter "foo" . stuff . [ seq "foo" . more stuff ]* ]*
>>
>> the inner [ .. ] will start counting from 1 everytime (without the
>> 'counter' it keeps increasing globally)
>>
>> Generally, using a fixed label is preferrable IMHO; the difference
>> between one and the other is subtle and has to do with how deleted stuff
>> is restored: if you use 'seq', the deleted stuff is tied to that exact
>> numeric label, and if you delete node 5, the other nodes will keep their
>> exact formatting. When you use 'label "bar"', then deleting 'bar[5]'
>> will restore the old bar[6] with the deleted things from the deleted
>> bar[5] etc.
>>
>> >    - I didn't touch the sep_tab issue yet
>>
>> Ok .. that should be a relatively easy fix. Once that's done, I think
>> it's ready to be committed.
>>
>>
>> >    (* Define hooks *)
>> >
>> >    let endscript_re = /[ \t]*endscript[ \t]*/
>> >    let hook_func (func_type:string) = [
>> >         del /[ \t]*/ "\t" . key func_type . eol .
>> >           [ label "line" . del /[ \t]*/ "" . store (/([^ \t\n][^
>> > \n]*)?/ - endscript_re) . eol ]* .
>> >         del (endscript_re . "\n") "\tendscript\n" ]
>>
>> Actually, thinking about this more, why not suck the whole script into
>> one node instead of splitting it into individual lines ?
>>
>> David
>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/augeas-devel/attachments/20080711/2c0e2e9c/attachment.htm>


More information about the augeas-devel mailing list