]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: lua: Add lua-prepend-path configuration option
authorTim Duesterhus <tim@bastelstu.be>
Sun, 12 Jan 2020 12:55:40 +0000 (13:55 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 24 Jan 2020 08:22:03 +0000 (09:22 +0100)
lua-prepend-path allows the administrator to specify a custom Lua library
path to load custom Lua modules that are useful within the context of HAProxy
without polluting the global Lua library folder.

doc/configuration.txt
src/hlua.c

index fbc914d8fbb7c3bf7d51e5cd689b6a5d2f7e909c..89dd4df25129d87102c1d742775477898b57046b 100644 (file)
@@ -605,6 +605,7 @@ The following keywords are supported in the "global" section :
    - log-tag
    - log-send-hostname
    - lua-load
+   - lua-prepend-path
    - mworker-max-reloads
    - nbproc
    - nbthread
@@ -1082,6 +1083,31 @@ lua-load <file>
   This global directive loads and executes a Lua file. This directive can be
   used multiple times.
 
+lua-prepend-path <string> [<type>]
+  Prepends the given string followed by a semicolon to Lua's package.<type>
+  variable.
+  <type> must either be "path" or "cpath". If <type> is not given it defaults
+  to "path".
+
+  Lua's paths are semicolon delimited lists of patterns that specify how the
+  `require` function attempts to find the source file of a library. Question
+  marks (?) within a pattern will be replaced by module name. The path is
+  evaluated left to right. This implies that paths that are prepended later
+  will be checked earlier.
+
+  As an example by specifying the following path:
+
+    lua-prepend-path /usr/share/haproxy-lua/?/init.lua
+    lua-prepend-path /usr/share/haproxy-lua/?.lua
+
+  When `require "example"` is being called Lua will first attempt to load the
+  /usr/share/haproxy-lua/example.lua script, if that does not exist the
+  /usr/share/haproxy-lua/example/init.lua will be attempted and the default
+  paths if that does not exist either.
+
+  See https://www.lua.org/pil/8.1.html for the details within the Lua
+  documentation.
+
 master-worker [no-exit-on-failure]
   Master-worker mode. It is equivalent to the command line "-W" argument.
   This mode will launch a "master" which will monitor the "workers". Using
index 0d943fb620018ecb6288291f6375192343e13395..bcc1d2ff8c7e8b5702328b7aeda32d347a311749 100644 (file)
@@ -7474,8 +7474,36 @@ static int hlua_prepend_path(struct hlua ctx, char *type, char *path)
        return 0;
 }
 
+static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
+                                    struct proxy *defpx, const char *file, int line,
+                                    char **err)
+{
+       char *path;
+       char *type = "path";
+       if (too_many_args(2, args, err, NULL)) {
+               return -1;
+       }
+
+       if (!(*args[1])) {
+               memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
+               return -1;
+       }
+       path = args[1];
+
+       if (*args[2]) {
+               if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
+                       memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
+                       return -1;
+               }
+               type = args[2];
+       }
+
+       return hlua_prepend_path(gL, type, path);
+}
+
 /* configuration keywords declaration */
 static struct cfg_kw_list cfg_kws = {{ },{
+       { CFG_GLOBAL, "lua-prepend-path",         hlua_config_prepend_path },
        { CFG_GLOBAL, "lua-load",                 hlua_load },
        { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
        { CFG_GLOBAL, "tune.lua.task-timeout",    hlua_task_timeout },