0

Let's say I have some generic INI-type file, covered best by the Samba.lns. I have an entry like:

attribute = value

And I want to transform value to Some-VALUE-x. For a more concrete example:

augtool> print /files/etc/yum.conf/main/cachedir
/files/etc/yum.conf/main/cachedir = "/var/cache/yum/$basearch/$releasever"

And I want to change the value to:

"/var/cache/yum/noarch/$releasever"

That is, I want to replace $basearch with noarch and leave the rest alone. My actual example is a little more complex. Can I even do this within augeas?

Otheus
  • 5,945
  • 1
  • 22
  • 53

2 Answers2

1

You can't do this with a single Augeas API call, but with aug_get and aug_set you can do it in the calling language. e.g. using ruby-augeas:

aug.set("/files/etc/yum.conf/main/cachedir", aug.get("/files/etc/yum.conf/main/cachedir").sub("$basearch", "noarch"))

If you're using augtool per your example, in shell you could do:

cachedir=$(augtool get /files/etc/yum.conf/main/cachedir | sed 's/$basearch/noarch/')
augtool set /files/etc/yum.conf/main/cachedir "$cachedir"
Dominic Cleal
  • 338
  • 1
  • 6
  • What about extending the given lns to add labels in a way so that values are represented as child nodes? – Otheus Sep 28 '16 at 14:27
  • Splitting up the value into more child nodes makes sense sometimes, particularly when a setting has multiple values (e.g. an array of values), but for this file path it would seem excessive for little gain. It isn't generally useful, only for this one use-case. Splitting the path by "/" would indeed allow you to set and replace one component. – Dominic Cleal Sep 28 '16 at 20:47
  • I need to figure out if it makes sense to extend a lens to do this. The actual example is an INI-like file whose RHS contains a comma-separated list of paths. – Otheus Oct 04 '16 at 10:28
  • Yes, that's very common, and makes much more sense than the provided example. e.g. the Subversion module/lens splits `global-ignores` and other properties into separate nodes by whitespace or commas. – Dominic Cleal Oct 04 '16 at 11:19
  • I just noticed the `Shellvars_list` stock lens which does something very very close to what I need. Adding an answer to hilight this possibility. – Otheus Oct 04 '16 at 15:27
1

The Shellvars_list stock lens comes close to providing what I needed.

Given a file such as

GRUB_TIMEOUT=5
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=vgroot/lvswap rd.lvm.lv=vgroot/lvroot"
GRUB_DISABLE_RECOVERY="true"

I want to append arbitrary values in an idempotent way to GRUB_CMDLINE_LINUX. This lens parses this file as follows:

augtool> print $v
/files/home/c14027/default-grub-sample
/files/home/c14027/default-grub-sample/GRUB_TIMEOUT
/files/home/c14027/default-grub-sample/GRUB_TIMEOUT/quote = ""
/files/home/c14027/default-grub-sample/GRUB_TIMEOUT/value = "5"
/files/home/c14027/default-grub-sample/GRUB_DEFAULT
/files/home/c14027/default-grub-sample/GRUB_DEFAULT/quote = ""
/files/home/c14027/default-grub-sample/GRUB_DEFAULT/value = "saved"
/files/home/c14027/default-grub-sample/GRUB_DISABLE_SUBMENU
/files/home/c14027/default-grub-sample/GRUB_DISABLE_SUBMENU/quote = ""
/files/home/c14027/default-grub-sample/GRUB_DISABLE_SUBMENU/value = "true"
/files/home/c14027/default-grub-sample/GRUB_TERMINAL_OUTPUT
/files/home/c14027/default-grub-sample/GRUB_TERMINAL_OUTPUT/quote = "\""
/files/home/c14027/default-grub-sample/GRUB_TERMINAL_OUTPUT/value = "console"
/files/home/c14027/default-grub-sample/GRUB_CMDLINE_LINUX
/files/home/c14027/default-grub-sample/GRUB_CMDLINE_LINUX/quote = "\""
/files/home/c14027/default-grub-sample/GRUB_CMDLINE_LINUX/value[1] = "crashkernel=auto"
/files/home/c14027/default-grub-sample/GRUB_CMDLINE_LINUX/value[2] = "rd.lvm.lv=vgroot/lvsap"
/files/home/c14027/default-grub-sample/GRUB_CMDLINE_LINUX/value[3] = "rd.lvm.lv=vgroot/lvroot"
/files/home/c14027/default-grub-sample/GRUB_DISABLE_RECOVERY
/files/home/c14027/default-grub-sample/GRUB_DISABLE_RECOVERY/quote = "\""
/files/home/c14027/default-grub-sample/GRUB_DISABLE_RECOVERY/value = "true"

We'll do a defvar in augtool so that $v represents our prefix.

Add a new value to this CMDLINE paramater:

set $v/GRUB_CMDLINE_LINUX/value[last()+1] test=142

Remove existing ones:

rm $v/GRUB_CMDLINE_LINUX/value[. =~ regexp("^test=.*")]

Replace the key-pair whose key is test=:

set $v/GRUB_CMDLINE_LINUX/value[. =~ regexp("^test=.*")] test=1234
Otheus
  • 5,945
  • 1
  • 22
  • 53