Provide access to server configuration in Lua.
---
core/CMakeLists.txt | 4 ++--
core/tarantool.m | 1 +
core/tarantool_lua.m | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
include/tarantool.h | 4 ++++
test/box/lua.result | Bin 6466 -> 8296 bytes
test/box/lua.test | 3 +++
6 files changed, 59 insertions(+), 2 deletions(-)
diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt
index 9757a51..8b590dc 100644
--- a/core/CMakeLists.txt
+++ b/core/CMakeLists.txt
@@ -53,6 +53,7 @@ set_property(DIRECTORY PROPERTY CLEAN_NO_CUSTOM 1)
#
set (recompiled_core_sources
${CMAKE_SOURCE_DIR}/core/tarantool.m
+ ${CMAKE_SOURCE_DIR}/core/tarantool_lua.m
${CMAKE_SOURCE_DIR}/core/say.m
${CMAKE_SOURCE_DIR}/core/admin.m
${CMAKE_SOURCE_DIR}/core/replication.m
@@ -60,8 +61,7 @@ set (recompiled_core_sources
set (common_sources tbuf.m palloc.m util.m
salloc.m pickle.m coro.m stat.m log_io.m
- log_io_remote.m iproto.m exception.m errcode.c latch.m
- tarantool_lua.m)
+ log_io_remote.m iproto.m exception.m errcode.c latch.m)
if (ENABLE_TRACE)
set (common_sources ${common_sources} trace.m)
diff --git a/core/tarantool.m b/core/tarantool.m
index a6d9826..ae0daeb 100644
--- a/core/tarantool.m
+++ b/core/tarantool.m
@@ -165,6 +165,7 @@ reload_cfg(struct tbuf *out)
return -1;
/* All OK, activate the config. */
swap_tarantool_cfg(&cfg, &new_cfg);
+ tarantool_lua_load_cfg(tarantool_L, &cfg);
}
@finally {
destroy_tarantool_cfg(&aux_cfg);
diff --git a/core/tarantool_lua.m b/core/tarantool_lua.m
index 20ace99..f4c2e4a 100644
--- a/core/tarantool_lua.m
+++ b/core/tarantool_lua.m
@@ -36,6 +36,7 @@
#include "pickle.h"
#include "fiber.h"
+#include TARANTOOL_CONFIG
struct lua_State *tarantool_L;
@@ -277,6 +278,7 @@ tarantool_lua_init()
lua_register(L, "print", lbox_print);
L = mod_lua_init(L);
}
+ tarantool_lua_load_cfg(L, &cfg);
lua_settop(L, 0); /* clear possible left-overs of init */
return L;
}
@@ -340,3 +342,50 @@ tarantool_lua(struct lua_State *L,
}
lua_settop(L, 0); /* clear the stack from return values. */
}
+
+static bool quote(const char *str)
+{
+ if (strcmp(str, "true") == 0 || strcmp(str, "false") == 0)
+ return false;
+ char *endptr;
+ (void) strtod(str, &endptr);
+ return *endptr != '\0';
+}
+
+/*
+ * Make a new configuration available in Lua.
+ * We could perhaps make Lua bindings to access the C
+ * structure in question, but for now it's easier and just
+ * as functional to convert the given configuration to a Lua
+ * table and export the table into Lua.
+ */
+void tarantool_lua_load_cfg(struct lua_State *L, struct tarantool_cfg *cfg)
+{
+ luaL_Buffer b;
+ char *key, *value;
+
+ luaL_buffinit(L, &b);
+ tarantool_cfg_iterator_t *i = tarantool_cfg_iterator_init();
+ luaL_addstring(&b, "box.cfg = {}\n");
+ while ((key = tarantool_cfg_iterator_next(i, cfg, &value)) != NULL) {
+ if (value == NULL)
+ continue;
+ if (strchr(key, '.') == NULL) {
+ luaL_addstring(&b, "box.cfg.");
+ luaL_addstring(&b, key);
+ luaL_addstring(&b, " = ");
+ if (quote(value))
+ luaL_addchar(&b, '\'');
+ luaL_addstring(&b, value);
+ if (quote(value))
+ luaL_addchar(&b, '\'');
+ luaL_addstring(&b, "\n");
+ free(value);
+ }
+ }
+ luaL_pushresult(&b);
+ puts(lua_tostring(L, -1));
+ if (luaL_loadstring(L, lua_tostring(L, -1)) == 0)
+ lua_pcall(L, 0, 0, 0);
+ lua_pop(L, 1);
+}
diff --git a/include/tarantool.h b/include/tarantool.h
index 980d171..c92abfe 100644
--- a/include/tarantool.h
+++ b/include/tarantool.h
@@ -71,6 +71,10 @@ extern struct lua_State *tarantool_L;
const char *
tarantool_lua_tostring(struct lua_State *L, int index);
+/* Make a new configuration available in Lua */
+void tarantool_lua_load_cfg(struct lua_State *L,
+ struct tarantool_cfg *cfg);
+
extern struct tarantool_cfg cfg;
extern const char *cfg_filename;
extern bool init_storage, booting;
diff --git a/test/box/lua.result b/test/box/lua.result
index
84cef9371845c966792ada495cd497d1bf0ac5f2..c6dcdecb5e335f0fd68369fa6ef4ed532b7ed7bc
100644
GIT binary patch
delta 1869
zcmcIjy^0$#5azfdU~m@*q*CWQI4}OmnXtu?gKN^bNEZl*S!vc5)=G=Cw$Eww3OVuw
zd4_Z;LmncJkO#;EWOn7Bi$e}K+bk=6Gn#MaoA3MR*PBm2o|VoD^#0-7%X5*>9)5l}
z2eJ74<?Q*_>DliWK`(*j*0IIaiYdcNP|jau_CBHDOC~KVoif!$kM&vailSIAt$$XR
z42|sc`~7Z*AZ59*TBE2qMWb7RPT8P=N+o%&G^VV6l}_;%j1t_Mz}KKTI%i$Va?l<d
z*lWl*Xl(`0K~yz3ItB`5R8iNNylNnjbi+&Qs+5h#gW+{^tL<!tlrEqmo;NqF0tcqD
zRwo2?L(QH}Tw62a)!d<<TP=xv67y?%QaMh50K+9}*wpCom<~8Ja6q<sDNCBb`nYzA
zMneOlL2$1=(nyA3^t!<dECh%-N^YP)UxFZXG>%13SR=u09yJ$R&PYn?g5DX7;!SLq
z8*4He-Xdj{TgH(dD&T#o9P*O5=wNO6DBT+<>vngvg>@*G1%({6adz#SWa=qp*HPsr
zk+#4=g<PvbkqBG;9jWve_t+IN<r>2Z>5<6&z%Yuq&EIYcZ%S-8W#1JwNt2!1iu($T
z%t)I9)5C+?mh+|7+^-R-q48uu1p>+Mu+)n7L)qQ>#LAg6lB*J=q<OrZ-rn84<G0iI
zdp&c%F=D?F&9y_2FsBdAbTS^rcaCZ7DJDr0x*dnwf@Gv|u4YRbAn}OCvWsqqV!EC7
zOd577J?YZ+Od3vdX?rFOjk|a2{y&$tXVP$zOWQN)UjHXu+MY>6pB>wS|LfBBOxioi
HrQOaSlSfLr
delta 33
pcmaFiaL8!Fk%?CYSPBx8vnRfYVJpf{PAx8;EWjkPIhyf{H~;~U4aWcg
diff --git a/test/box/lua.test b/test/box/lua.test
index b46313d..f76a088 100644
--- a/test/box/lua.test
+++ b/test/box/lua.test
@@ -100,3 +100,6 @@ exec admin "lua fifo_top('test')"
exec admin "lua box.delete(0, 'test')"
exec admin "lua fifo_top('test')"
exec admin "lua box.delete(0, 'test')"
+exec admin "lua for k,v in pairs(box.cfg) do print(' - ', k, ': ', v) end"
+exec admin "reload configuration"
+exec admin "lua for k,v in pairs(box.cfg) do print(' - ', k, ': ', v) end"
--
1.7.0.4
_______________________________________________
Mailing list: https://launchpad.net/~tarantool-developers
Post to : [email protected]
Unsubscribe : https://launchpad.net/~tarantool-developers
More help : https://help.launchpad.net/ListHelp