Hi David,

On Tue, Mar 16, 2010 at 2:40 AM, David Lutterkort <[email protected]> wrote:
> On Sun, 2010-03-14 at 10:48 +0100, Frederik Wagner wrote:
>> On Fri, Feb 26, 2010 at 12:43 AM, David Lutterkort <[email protected]> wrote:
>> > On Thu, 2010-02-25 at 19:19 +0100, Frederik Wagner wrote:
...
>
> What's missing is exposing this call in augtool and the language
> bindings - for puppet, that would mean to enhance ruby-augeas.
>
>> The way to do this - as far as I understand - would be to somehow
>> delete the last node in the path/pattern (the node which value hast to
>> be set), match for the resulting patj and do a set on each match+node
>> to set.  But for this I'm lost in the code, I don't want to introduce
>> arbitrary string manipulation in augeas.c ;-)
>> I'm anyway not sure, if the result would allow for all matching
>> operation, e.g. matches on the last part of the path, like
>> "/file/some/path/*[somematch]"
>
> I agree with your analysis - and that it would be way too complicated to
> get right in a language binding.
>
>> Including this in ruby or the ruby API seems more simple, but since I
>> would like to use it in puppet, I don't want to make a change in all
>> the interfaces for a 'dirty hack'.
>
> Let me know how much you can do with this patch. Would be great if you
> could enhance it with a test or two, either by exposing it in augtool
> and writing a shell script that tests it, or with a test written in C.
> Once we have that, I'll commit all that.

finally I managed to include your patch. It seems to work fine. I
exposed the setm call to augtool and included two tests through
augtool. Find the two patches attached (to be applied on top of
yours).

I also exposed the call through the ruby API. This patch is also
attached, including some tests.

I hope this is how you want them. Only thing missing for me is the
inclusion into the puppet provider (also the enhanced set function for
arrays in the ruby API)... so another TODO...

Bye Frederik
From 72da993f1bc7228545c40e758200306aaf65b0ce Mon Sep 17 00:00:00 2001
From: Frederik Wagner <[email protected]>
Date: Wed, 26 May 2010 22:44:37 +0200
Subject: [PATCH 2/3] Exposed aug_setm to augtool.

---
 src/augtool.c |   34 ++++++++++++++++++++++++++++++++++
 1 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/src/augtool.c b/src/augtool.c
index 92af30f..0968ab5 100644
--- a/src/augtool.c
+++ b/src/augtool.c
@@ -590,6 +590,39 @@ static const struct command_def cmd_set_def = {
     "will appear last\n amongst their siblings"
 };
 
+static void cmd_setm(struct command *cmd) {
+    const char *base = arg_value(cmd, "base");
+    const char *sub  = arg_value(cmd, "sub");
+    const char *val  = arg_value(cmd, "value");
+    int r;
+
+    r = aug_setm(aug, base, sub, val);
+    err_check(cmd);
+    if (r == -1)
+        printf ("Failed\n");
+}
+
+static const struct command_opt_def cmd_setm_opts[] = {
+    { .type = CMD_PATH, .name = "base", .optional = false,
+      .help = "the base node" },
+    { .type = CMD_PATH, .name = "sub", .optional = false,
+      .help = "the subtree relative to the base" },
+    { .type = CMD_STR, .name = "value", .optional = false,
+      .help = "the value for the nodes" },
+    CMD_OPT_DEF_LAST
+};
+
+static const struct command_def cmd_setm_def = {
+    .name = "setm",
+    .opts = cmd_setm_opts,
+    .handler = cmd_setm,
+    .synopsis = "set the value of multiple nodes",
+    .help = "Set multiple nodes  in one operation.  Find or create a "
+    " node matching\n SUB  by interpreting SUB  as a path expression  "
+    "relative to  each node\n matching BASE.  SUB may be NULL,  in "
+    "which case all the nodes matching\n BASE will be modified."
+};
+
 static void cmd_defvar(struct command *cmd) {
     const char *name = arg_value(cmd, "name");
     const char *path = arg_value(cmd, "expr");
@@ -897,6 +930,7 @@ static const struct command_def const *commands[] = {
     &cmd_rm_def,
     &cmd_save_def,
     &cmd_set_def,
+    &cmd_setm_def,
     &cmd_help_def,
     &cmd_def_last
 };
-- 
1.7.1

From 1b6787484fc341cfa55452e9be2846a8f2fcb2f3 Mon Sep 17 00:00:00 2001
From: Frederik Wagner <[email protected]>
Date: Thu, 3 Jun 2010 20:57:13 +0200
Subject: [PATCH 3/3] Tests for new aug_setm function

Test through augtool calls.
---
 tests/grub-setm-newvalues.rb    |   33 +++++++++++++++++++++++++++++++++
 tests/yum.repos-setm-enabled.rb |   27 +++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 0 deletions(-)
 create mode 100644 tests/grub-setm-newvalues.rb
 create mode 100644 tests/yum.repos-setm-enabled.rb

diff --git a/tests/grub-setm-newvalues.rb b/tests/grub-setm-newvalues.rb
new file mode 100644
index 0000000..0397dd7
--- /dev/null
+++ b/tests/grub-setm-newvalues.rb
@@ -0,0 +1,33 @@
+# add console values to multiple kernel entries
+commands="
+setm /files/etc/grub.conf/*/kernel[. =~ regexp('.*2.6.24.*')] console[last()+1] tty0
+setm /files/etc/grub.conf/*/kernel[. =~ regexp('.*2.6.24.*')] console[last()+1] ttyS0,9600n8
+save
+"
+
+diff["/etc/grub.conf"]= <<TXT
+--- /etc/grub.conf
++++ /etc/grub.conf.augnew
+@@ -13,11 +13,11 @@
+ hiddenmenu
+ title Fedora (2.6.24.4-64.fc8)
+ 	root (hd0,0)
+-	kernel /vmlinuz-2.6.24.4-64.fc8 ro root=/dev/vg00/lv00
++	kernel /vmlinuz-2.6.24.4-64.fc8 ro root=/dev/vg00/lv00 console=tty0 console=ttyS0,9600n8
+ 	initrd /initrd-2.6.24.4-64.fc8.img
+ title Fedora (2.6.24.3-50.fc8)
+ 	root (hd0,0)
+-	kernel /vmlinuz-2.6.24.3-50.fc8 ro root=/dev/vg00/lv00
++	kernel /vmlinuz-2.6.24.3-50.fc8 ro root=/dev/vg00/lv00 console=tty0 console=ttyS0,9600n8
+ 	initrd /initrd-2.6.24.3-50.fc8.img
+ title Fedora (2.6.21.7-3.fc8xen)
+ 	root (hd0,0)
+@@ -26,6 +26,6 @@
+ 	module /initrd-2.6.21.7-3.fc8xen.img
+ title Fedora (2.6.24.3-34.fc8)
+ 	root (hd0,0)
+-	kernel /vmlinuz-2.6.24.3-34.fc8 ro root=/dev/vg00/lv00
++	kernel /vmlinuz-2.6.24.3-34.fc8 ro root=/dev/vg00/lv00 console=tty0 console=ttyS0,9600n8
+ 	initrd /initrd-2.6.24.3-34.fc8.img
+         savedefault
+TXT
diff --git a/tests/yum.repos-setm-enabled.rb b/tests/yum.repos-setm-enabled.rb
new file mode 100644
index 0000000..f7eae1a
--- /dev/null
+++ b/tests/yum.repos-setm-enabled.rb
@@ -0,0 +1,27 @@
+# set all existing repos to enabled
+commands="
+setm /files/etc/yum.repos.d/fedora.repo/* enabled 1
+save
+"
+
+diff["/etc/yum.repos.d/fedora.repo"] = <<TXT
+--- /etc/yum.repos.d/fedora.repo
++++ /etc/yum.repos.d/fedora.repo.augnew
+@@ -12,7 +12,7 @@
+ failovermethod=priority
+ #baseurl=http://download.fedora.redhat.com/pub/fedora/linux/releases/$releasever/Everything/$basearch/debug/
+ mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=fedora-debug-$releasever&arch=$basearch
+-enabled=0
++enabled=1
+ gpgcheck=1
+ gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora file:///etc/pki/rpm-gpg/RPM-GPG-KEY
+
+@@ -21,6 +21,6 @@
+ failovermethod=priority
+ #baseurl=http://download.fedora.redhat.com/pub/fedora/linux/releases/$releasever/Everything/source/SRPMS/
+ mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=fedora-source-$releasever&arch=$basearch
+-enabled=0
++enabled=1
+ gpgcheck=1
+ gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora file:///etc/pki/rpm-gpg/RPM-GPG-KEY
+TXT
-- 
1.7.1

From 3d7af6a6bd04d471cf9d93f797f0c863759dfdde Mon Sep 17 00:00:00 2001
From: Frederik Wagner <[email protected]>
Date: Thu, 3 Jun 2010 22:01:09 +0200
Subject: [PATCH] Exposed 'setm' function in ruby API

Added ruby API call for 'setm'.
Added tests for 'setm'.
---
 ext/augeas/_augeas.c |   27 +++++++++++++++++++++++++++
 tests/tc_augeas.rb   |   12 ++++++++++++
 2 files changed, 39 insertions(+), 0 deletions(-)

diff --git a/ext/augeas/_augeas.c b/ext/augeas/_augeas.c
index 74b1c3c..2bdd1e5 100644
--- a/ext/augeas/_augeas.c
+++ b/ext/augeas/_augeas.c
@@ -101,6 +101,32 @@ VALUE augeas_set(VALUE s, VALUE path, VALUE value) {
 
 /*
  * call-seq:
+ *   setm(BASE, SUB, VALUE) -> boolean
+ *
+ *  Set multiple nodes in one operation. Find or create a node matching
+ *  SUB by interpreting SUB as a path expression relative to each node
+ *  matching BASE. SUB may be NULL, in which case all the nodes matching
+ *  BASE will be modified.
+ */
+VALUE augeas_setm(VALUE s, VALUE base, VALUE sub, VALUE value) {
+    augeas *aug = aug_handle(s);
+    const char *cbase = StringValueCStr(base) ;
+    const char *csub = StringValueCStrOrNull(sub) ;
+    const char *cvalue = StringValueCStrOrNull(value) ;
+
+    int callValue = aug_setm(aug, cbase, csub, cvalue) ;
+    VALUE returnValue ;
+
+    if (callValue == 0)
+        returnValue = Qtrue ;
+    else
+        returnValue = Qfalse ;
+
+    return returnValue ;
+}
+
+/*
+ * call-seq:
  *   insert(PATH, LABEL, BEFORE) -> int
  *
  * Make LABEL a sibling of PATH by inserting it directly before or after PATH.
@@ -317,6 +343,7 @@ void Init__augeas() {
     rb_define_method(c_augeas, "save", augeas_save, 0);
     rb_define_method(c_augeas, "load", augeas_load, 0);
     rb_define_method(c_augeas, "set_internal", augeas_set, 2);
+    rb_define_method(c_augeas, "setm", augeas_setm, 3);
     rb_define_method(c_augeas, "close", augeas_close, 0);
 }
 
diff --git a/tests/tc_augeas.rb b/tests/tc_augeas.rb
index 489986f..3edfc01 100644
--- a/tests/tc_augeas.rb
+++ b/tests/tc_augeas.rb
@@ -154,6 +154,18 @@ class TestAugeas < Test::Unit::TestCase
        assert_equal( aug.get("/files/etc/group/disk/user"), nil)
     end
 
+    def test_setm
+        aug = aug_open
+
+        aug.setm("/files/etc/group/*[label() =~ regexp(\"rpc.*\")]","users", "testuser1")
+        assert_equal( aug.get("/files/etc/group/rpc/users"), "testuser1")
+        assert_equal( aug.get("/files/etc/group/rpcuser/users"), "testuser1")
+
+        aug.setm("/files/etc/group/*[label() =~ regexp(\"rpc.*\")]/users",nil, "testuser2")
+        assert_equal( aug.get("/files/etc/group/rpc/users"), "testuser2")
+        assert_equal( aug.get("/files/etc/group/rpcuser/users"), "testuser2")
+    end
+
 
     private
     def aug_open(flags = Augeas::NONE)
-- 
1.7.1

_______________________________________________
augeas-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/augeas-devel

Reply via email to