[augeas-devel] Migrating config file formats

Raphaël Pinson raphink at gmail.com
Sat Feb 11 22:07:32 UTC 2012


After thinking about it some more, I think it could be useful to get
to a standard using subnodes.

Here is an example, unrelated to the suggested problem:


module Migration =

(* There are 3 possible syntaxes for a line
   The last one doesn't have a "@syntax" subnode, it's the default -
newest - syntax *)
let line = [ key Rx.word .
             ( (Sep.colon . store Rx.word . [ label "@syntax" . value "1" ])
             | (Sep.space . store Rx.word . [ label "@syntax" . value "2" ])
             | (Sep.equal . store Rx.word) )
             . Util.eol ]

let lns = line*

(* Now for some tests *)
let full = "foo:bar\nfoo=baz\nfoo baa\n"

(* Get all kind of syntaxes *)
test lns get full =
  { "foo" = "bar"
    { "@syntax" = "1" }
  }
  { "foo" = "baz" }
  { "foo" = "baa"
    { "@syntax" = "2" }
  }

(* Migrate all to new syntax *)
test lns put full after
   rm "//@syntax" =
   "foo=bar\nfoo=baz\nfoo=baa\n"

(* Force syntax 1 for last entry *)
test lns put full after
   set "/foo[3]/@syntax" "1" =
   "foo:bar\nfoo=baz\nfoo:baa\n"



Could that be an interesting approach in general?

Raphaël



2012/1/26 Raphaël Pinson <raphink at gmail.com>:
> Hi Doug,
>
> On Thu, Jan 26, 2012 at 4:07 PM, Doug Warner <doug at warner.fm> wrote:
>> I'm working on a lens to work with Gentoo's new /etc/conf.d/net format and I
>> realized I need to convert their old format to the new format.
>>
>> What's the best way to migrate from one format to another?  I have my lens
>> mostly working for the new format now, but I realized I might need to parse
>> both versions; but then I thought that it might not update when augeas
>> reads/writes the config file.
>>
>> I am using puppet to manage these files; so the values are typically set each
>> run; I'm hoping that would cause augeas to write down the new format rather
>> than leave it the old version.
>>
>> I guess the last option is to wipe out the old file when I upgrade and let
>> puppet/augeas write down the new file, but that seems painful as well.
>>
>> Any recommendations on what to do here?
>>
>
>
> That's a very interesting question. I see two ways of doing this:
> * write a single lens which will map both syntaxes to different nodes
> in the tree, and use the "move" command to migrate;
> * or write two lenses bound to two different files (file.old and
> file.new or something similar) and use the "move" command to migrate.
>
>
> Here is a (minimalistic) proof of concept using the first option:
>
> $ cat myconfig.aug
> module MyConfig =
>
> autoload xfm
>
> let old_record =
>   let entry = [ Util.del_str "\""
>               . label "entry"
>               . store /[0-9.\/]+/
>               . Util.del_str "\"" ]
>   in [ label "old_record" . store Rx.word
>        . Util.del_str "=("
>        . Build.opt_list entry Sep.space
>        . Util.del_str ")" . Util.eol ]
>
> let new_record =
>   let entry = [ label "entry"
>               . store /[0-9.\/]+/ ]
>   in [ label "new_record" . store Rx.word
>        . Util.del_str "=\""
>        . Build.opt_list entry Util.eol
>        . Util.del_str "\"" . Util.eol ]
>
>
> let lns = old_record | new_record
>
> let filter = incl "/file"
>
> let xfm = transform lns filter
>
> $ cat fakeroot/file
> config_eth0=("192.168.0.1/24" "192.168.0.2/24")
>
> $ augtool -I . -r fakeroot
> /files
> /files/file
> /files/file/old_record = "config_eth0"
> /files/file/old_record/entry[1] = "192.168.0.1/24"
> /files/file/old_record/entry[2] = "192.168.0.2/24"
> rpinson at rpinson:~/bas/augeas_migration$ augtool -I . -r fakeroot/
> augtool> print /files/
> /files
> /files/file
> /files/file/old_record = "config_eth0"
> /files/file/old_record/entry[1] = "192.168.0.1/24"
> /files/file/old_record/entry[2] = "192.168.0.2/24"
> augtool> mv /files/file/old_record /files/file/new_record
> augtool> print /files/
> /files
> /files/file
> /files/file/new_record = "config_eth0"
> /files/file/new_record/entry[1] = "192.168.0.1/24"
> /files/file/new_record/entry[2] = "192.168.0.2/24"
> augtool> save
> Saved 1 file(s)
>
> $ cat fakeroot/file
> config_eth0="192.168.0.1/24
> 192.168.0.2/24"
>
>
> Note that you can combine this with the -b flag to keep the old version at hand.
>
>
> Raphaël




More information about the augeas-devel mailing list