[augeas-devel] Lua integration

Raphaël Pinson raphael.pinson at camptocamp.com
Thu Sep 17 15:20:13 UTC 2015


Hello,


It's been years that I've been wanting to do this, and failed in the past
when I tried. This time, I finally have working code to integrate Lua into
Augtool!


# So what's the idea?

Augtool is quite limited, and people often complain of the lack of
conditionals and other niceties of full-blown languages. Instead of
extending augsrun to support conditionals, maths, array operations and the
like, it's much easier to integrate a light, embedded language into Augtool.


# What's the implementation status?

The PoC [0] is really not perfect, but it does what I expect mostly. As of
today:

  - it adds a -l/--lua option to augtool, to switch to the lua interpreter
instead of augrun
  - it maps most Augeas calls to Lua methods
  - it supports both file execution and REPL


# What does it look like?

Here's an example that is currently impossible to write in a clean way
using augtool (you need to code in C or use bindings). It also demonstrates
how environment variables can be used to pass parameters to the script:

    #!/usr/bin/augtool -lAsf
    --- pass the spec as spec=<spec>
    --- pass the file as mount=<mount>

    spec = os.getenv("spec")
    mount = os.getenv("mount")

    transform("Fstab.lns", "/etc/fstab", false)
    load()

    file_path = "etc/fstab/*[file='" .. mount .. "']"
    if (matches(file_path) == 0) then
      defnode("file", "etc/fstab/01", nil)
      set("$file/spec", spec)
      set("$file/file", mount)
    else
      defvar("file", file_path)
    end

    set("$file/spec", spec)
    set("$file/vfstype", "nfs")
    set("$file/opt", "intr")
    set("$file/dump", "0")
    set("$file/passno", "0")

which can be made executable and called with:

    mount=/mnt/ISO spec=nas:/ISO/iso ./augscript.lua

The script will also accept other augtool options (such as -I <lens_dir> or
-r <root>).


# Why am I writing about this?

Besides actually announcing this, I'm looking for:

  - code review (cause my C skills are pretty bad, and my Lua embedding
skills are even worse)
  - opinions on various issues listed in the PR description (how to map
methods, how to map parameters, how to expose Lua in Augeas/augtool, etc.)


Thanks for reading all this, and let me know what you think!



[0] https://github.com/hercules-team/augeas/pull/300


-- 
Raphaël Pinson
Infrastructure Developer & Training Leader
+33 458 482 013

Camptocamp France
Savoie Technolac
BP 352
48, avenue du Lac du Bourget
73372 Le Bourget du Lac, Cedex
www.camptocamp.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/augeas-devel/attachments/20150917/bcd5f0f9/attachment.htm>


More information about the augeas-devel mailing list