MINOR: config: add predicates "version_atleast" and "version_before" to cond blocks

These predicates respectively verify that the current version is at least
a given version or is before a specific one. The syntax is exactly the one
reported by "haproxy -v", though each component is optional, so both "1.5"
and "2.4-dev18-88910-48" are supported. Missing components equal zero, and
"dev" is below "pre" or "rc", which are both inferior to no such mention
(i.e. they are negative). Thus "2.4-dev18" is older than "2.4-rc1" which
is older than "2.4".
This commit is contained in:
Willy Tarreau 2021-05-06 16:53:26 +02:00
parent 58ca706e16
commit 0b7c78aa05
2 changed files with 26 additions and 0 deletions

View File

@ -821,6 +821,16 @@ The list of currently supported predicates is the following:
- streq(<str1>,<str2>) : returns true only if the two strings are equal - streq(<str1>,<str2>) : returns true only if the two strings are equal
- strneq(<str1>,<str2>) : returns true only if the two strings differ - strneq(<str1>,<str2>) : returns true only if the two strings differ
- version_atleast(<ver>): returns true if the current haproxy version is
at least as recent as <ver> otherwise false. The
version syntax is the same as shown by "haproxy -v"
and missing components are assumed as being zero.
- version_before(<ver>) : returns true if the current haproxy version is
strictly older than <ver> otherwise false. The
version syntax is the same as shown by "haproxy -v"
and missing components are assumed as being zero.
Example: Example:
.if defined(HAPROXY_MWORKER) .if defined(HAPROXY_MWORKER)
@ -839,6 +849,10 @@ Example:
.endif .endif
.endif .endif
.if version_atleast(2.4-dev19)
profiling.memory on
.endif
Three other directives are provided to report some status: Three other directives are provided to report some status:
- .notice "message" : emit this message at level NOTICE - .notice "message" : emit this message at level NOTICE

View File

@ -140,6 +140,8 @@ enum cond_predicate {
CFG_PRED_FEATURE, // "feature" CFG_PRED_FEATURE, // "feature"
CFG_PRED_STREQ, // "streq" CFG_PRED_STREQ, // "streq"
CFG_PRED_STRNEQ, // "strneq" CFG_PRED_STRNEQ, // "strneq"
CFG_PRED_VERSION_ATLEAST, // "version_atleast"
CFG_PRED_VERSION_BEFORE, // "version_before"
}; };
struct cond_pred_kw { struct cond_pred_kw {
@ -154,6 +156,8 @@ const struct cond_pred_kw cond_predicates[] = {
{ "feature", CFG_PRED_FEATURE, ARG1(1, STR) }, { "feature", CFG_PRED_FEATURE, ARG1(1, STR) },
{ "streq", CFG_PRED_STREQ, ARG2(2, STR, STR) }, { "streq", CFG_PRED_STREQ, ARG2(2, STR, STR) },
{ "strneq", CFG_PRED_STRNEQ, ARG2(2, STR, STR) }, { "strneq", CFG_PRED_STRNEQ, ARG2(2, STR, STR) },
{ "version_atleast", CFG_PRED_VERSION_ATLEAST, ARG1(1, STR) },
{ "version_before", CFG_PRED_VERSION_BEFORE, ARG1(1, STR) },
{ NULL, CFG_PRED_NONE, 0 } { NULL, CFG_PRED_NONE, 0 }
}; };
@ -1753,6 +1757,14 @@ static int cfg_eval_condition(char **args, char **err, const char **errptr)
ret = strcmp(argp[0].data.str.area, argp[1].data.str.area) != 0; ret = strcmp(argp[0].data.str.area, argp[1].data.str.area) != 0;
goto done; goto done;
case CFG_PRED_VERSION_ATLEAST: // checks if the current version is at least this one
ret = compare_current_version(argp[0].data.str.area) <= 0;
goto done;
case CFG_PRED_VERSION_BEFORE: // checks if the current version is older than this one
ret = compare_current_version(argp[0].data.str.area) > 0;
goto done;
default: default:
memprintf(err, "internal error: unhandled conditional expression predicate '%s'", cond_pred->word); memprintf(err, "internal error: unhandled conditional expression predicate '%s'", cond_pred->word);
if (errptr) if (errptr)