[augeas-devel] Migrating config file formats

Raphaël Pinson raphink at gmail.com
Thu Jan 26 15:47:20 UTC 2012


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