Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Global include of LUA scripts #11676

Closed
veitw opened this issue Jun 3, 2022 · 9 comments · Fixed by #11678
Closed

Feature request: Global include of LUA scripts #11676

veitw opened this issue Jun 3, 2022 · 9 comments · Fixed by #11678

Comments

@veitw
Copy link

veitw commented Jun 3, 2022

Description

Please add a feature to authoritative server that allows a common LUA script to be loaded when the server starts.

Primary use case

Creating new LUA functions that work just like the built-in functions.

Example

Switch RR value on a given time for a planned migration scenario.

Solution A so far

Zone:

migratethis     LUA A "os.time() < 1654174980 and '127.0.0.1' or '127.0.1.1'"
migratethat     LUA A "os.time() < 1654174980 and '127.0.0.2' or '127.0.1.2'"
migratewhatever LUA A "os.time() < 1654174980 and '127.0.0.3' or '127.0.1.3'"

Solution B so far

Zone:

switch LUA LUA (" function switchontime(time, before, after) "
                "   if(os.time() < time) then                "
                "     return before                          "
                "   else                                     "
                "     return after                           "
                "   end                                      "
                " end                                        ")

migratethis     LUA A ";include('switch') return switchontime(1654174980, '127.0.0.1', '127.0.1.1')"
migratethat     LUA A ";include('switch') return switchontime(1654174980, '127.0.0.2', '127.0.1.2')"
migratewhatever LUA A ";include('switch') return switchontime(1654174980, '127.0.0.3', '127.0.1.3')"

Proposed solution with new feature

/etc/pdns/pdns.conf:

lua-include-common = /etc/pdns/common-lua.d/*.lua

/etc/pdns/common-lua.d/switchontime.lua

function switchontime(time, before, after)
  if(os.time() < time) then
    return before
  else
    return after
  end
end

Zone:

migratethis     LUA A "switchontime(1654174980, '127.0.0.1', '127.0.1.1')"
migratethat     LUA A "switchontime(1654174980, '127.0.0.2', '127.0.1.2')"
migratewhatever LUA A "switchontime(1654174980, '127.0.0.3', '127.0.1.3')"
@Habbie
Copy link
Member

Habbie commented Jun 3, 2022

lua-include-common = /etc/pdns/common-lua.d/*.lua

we'd probably take a dir, or a file (plus offering a function to include a dir), instead of handling wildcards here, but that's a detail

@Habbie Habbie added this to the auth-4.7.0 milestone Jun 3, 2022
@paddg
Copy link
Contributor

paddg commented Jun 3, 2022

If there are multiple scripts in the directory, the order may matter, which can lead to problems

@veitw
Copy link
Author

veitw commented Jun 3, 2022

If there are multiple scripts in the directory, the order may matter, which can lead to problems

Good point. Sorting alphabetically before loading would be a good idea, so one could prefix files with e.g. "00-" to "99-" to fix the order.

@veitw
Copy link
Author

veitw commented Jun 3, 2022

we'd probably take a dir, or a file (plus offering a function to include a dir), instead of handling wildcards here, but that's a detail

My intention was to use a wildcard to give the admin a configurable possibility to only use files with a given extension.

If including a directory, only files with names ending in .lua should be loaded, so packagers may ship inactive .example files or a README in this directory, and package managers could rename them on uninstall/reinstall or upgrade when one had been altered (for rpm based systems e.g. to *.rpmsave or *.rpmnew), so they are not loaded by default.

@veitw
Copy link
Author

veitw commented Jun 3, 2022

package managers could rename them on uninstall/reinstall or upgrade when one had been altered (for rpm based systems e.g. to *.rpmsave or *.rpmnew), so they are not loaded by default

This is especially important in case this feature leads to LUA files as extension packages. For the example described above I could think of an RPM package pdns-lua-switchontime that includes the code as e.g. /usr/lib/pdns/lua-extensions/switchontime.lua, and a configuration file /etc/pdns/common-lua.d/switchontime.lua that either loads the code as include or is a symlink, or /usr/lib/pdns/lua-extensions/*.lua are loaded by a default configuration directive in pdns.conf. So doing a dnf install pdns-lua-switchontime would be enough to supply this feature.

@Habbie
Copy link
Member

Habbie commented Jun 3, 2022

only files with names ending in .lua should be loaded

yes!

@cmouse cmouse mentioned this issue Jun 3, 2022
7 tasks
@Habbie Habbie modified the milestones: auth-4.7.0, auth-4.8.0 Sep 8, 2022
@Habbie Habbie modified the milestones: auth-4.8.0, auth-4.9.0 Apr 14, 2023
@smck83
Copy link

smck83 commented Oct 19, 2023

I'm looking to do something exactly like this. Currently using recursor as it can reference lua files which has over 100 lines of code but running into issues with Google due to no AA bit. Reading this, it seems all lua is supported (e.f. preresolve) in authorative but without this feature I need to insert row by row in between ""?

@Habbie
Copy link
Member

Habbie commented Oct 19, 2023

but without this feature I need to insert row by row in between ""

you can already use dofile or require

@smck83
Copy link

smck83 commented Oct 19, 2023

Thank you for confirming. That's good news, so I can use the full lua script in a lua records on pdns Authoritative and use
;require("/opt/myscript.lua")

in the record?

@Habbie Habbie modified the milestones: auth-4.9.0, auth-5 Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants