diff --git a/doc/lua-api/index.rst b/doc/lua-api/index.rst index 7f9627452..a4c32a41b 100644 --- a/doc/lua-api/index.rst +++ b/doc/lua-api/index.rst @@ -1089,6 +1089,14 @@ Core class perform the heavy job in a dedicated task and allow remaining events to be processed more quickly. +.. js:function:: core.use_native_mailers_config() + + **context**: body + + Inform haproxy that the script will make use of the native "mailers" + config section (although legacy). In other words, inform haproxy that + :js:func:`Proxy.get_mailers()` will be used later in the program. + .. _proxy_class: Proxy class @@ -1216,8 +1224,14 @@ Proxy class **LEGACY** - Returns a table containing mailers config for the current proxy or nil - if mailers are not available for the proxy. + Returns a table containing legacy mailers config (from haproxy configuration + file) for the current proxy or nil if mailers are not available for the proxy. + + .. warning:: + When relying on :js:func:`Proxy.get_mailers()` to retrieve mailers + configuration, :js:func:`core.use_native_mailers_config()` must be called + first from body or init context to inform haproxy that Lua makes use of the + legacy mailers config. :param class_proxy px: A :ref:`proxy_class` which indicates the manipulated proxy. diff --git a/examples/lua/mailers.lua b/examples/lua/mailers.lua index b1201fffd..6f1620cad 100644 --- a/examples/lua/mailers.lua +++ b/examples/lua/mailers.lua @@ -364,6 +364,10 @@ local function srv_event_add(event, data) mailers_track_server_events(data.reference) end +-- tell haproxy that we do use the legacy native "mailers" config section +-- which allows us to retrieve mailers configuration using Proxy:get_mailers() +core.use_native_mailers_config() + -- event subscriptions are purposely performed in an init function to prevent -- email alerts from being generated too early (when process is starting up) core.register_init(function() diff --git a/include/haproxy/mailers.h b/include/haproxy/mailers.h index 509224ac6..ea2c2d8af 100644 --- a/include/haproxy/mailers.h +++ b/include/haproxy/mailers.h @@ -31,6 +31,7 @@ #include #include +extern int mailers_used_from_lua; extern struct mailers *mailers; int init_email_alert(struct mailers *mailers, struct proxy *p, char **err); diff --git a/src/hlua.c b/src/hlua.c index 25df95690..d4734e0af 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -86,6 +86,8 @@ static uint hlua_log_opts = HLUA_LOG_LOGGERS_ON | HLUA_LOG_STDERR_AUTO; /* set to 1 once some lua was loaded already */ static uint8_t hlua_loaded = 0; +/* set to 1 during body evaluation (very early lua file loading), then 0 */ +static uint8_t hlua_body = 1; #define HLUA_BOOL_SAMPLE_CONVERSION_UNK 0x0 #define HLUA_BOOL_SAMPLE_CONVERSION_NORMAL 0x1 @@ -2384,6 +2386,20 @@ __LJMP static int hlua_core_get_var(lua_State *L) return MAY_LJMP(hlua_smp2lua(L, &smp)); } +/* This function informs haproxy that native mailers config section is + * actually being used from Lua. + * + * It may not be used outside of body context. + */ +__LJMP static int hlua_use_native_mailers_config(lua_State *L) +{ + if (!hlua_body) + WILL_LJMP(luaL_error(L, "use_native_mailers_config: " + "not available outside of body context")); + mailers_used_from_lua = 1; + return 0; +} + /* A class is a lot of memory that contain data. This data can be a table, * an integer or user data. This data is associated with a metatable. This * metatable have an original version registered in the global context with @@ -13811,6 +13827,8 @@ int hlua_post_init() struct hlua_function *fcn; struct hlua_reg_filter *reg_flt; + hlua_body = 0; + #if defined(USE_OPENSSL) /* Initialize SSL server. */ if (socket_ssl->xprt->prepare_srv) { @@ -14108,6 +14126,7 @@ lua_State *hlua_init_state(int thread_num) hlua_class_function(L, "Warning", hlua_log_warning); hlua_class_function(L, "Alert", hlua_log_alert); hlua_class_function(L, "done", hlua_done); + hlua_class_function(L, "use_native_mailers_config", hlua_use_native_mailers_config); hlua_fcn_reg_core_fcn(L); lua_setglobal(L, "core"); diff --git a/src/mailers.c b/src/mailers.c index 7d3fe3839..451b1e2ee 100644 --- a/src/mailers.c +++ b/src/mailers.c @@ -23,6 +23,7 @@ #include #include +int mailers_used_from_lua = 0; struct mailers *mailers = NULL; @@ -48,3 +49,19 @@ void free_email_alert(struct proxy *p) ha_free(&p->email_alert.to); ha_free(&p->email_alert.myhostname); } + +static int mailers_post_check(void) +{ + struct mailers *cur; + + for (cur = mailers; cur != NULL; cur = cur->next) { + if (cur->users && !mailers_used_from_lua) { + ha_warning("mailers '%s' is referenced on at least one proxy but Lua " + "mailers are not configured so the setting will be ignored. " + "Use 'examples/lua/mailers.lua' file for basic mailers support.\n", cur->id); + return ERR_WARN; + } + } + return ERR_NONE; +} +REGISTER_POST_CHECK(mailers_post_check);