I'm implementing a role which can be configured. For the better user experience I'd like to make it's default configuration explicit so that a user could simply edit it in Cartridge WebUI.
But cartridge.config_patch_clusterwide
isn't suitable for this purpose because it calling it from init()
callback results in recursing and thus prohibited.
Is there any other way to accomplist it?
There is on_patch
trigger, which could do it.
Here is API reference: https://www.tarantool.io/en/doc/latest/book/cartridge/cartridge_api/modules/cartridge.twophase/#on-patch-trigger-new-trigger-old
Your role could look like follows:
-- myrole.lua
local twophase = require('cartridge.twophase')
twophase.on_patch(function(conf_new, conf_old)
if conf_old:get_plaintext('myrole.yml') ~= nil then
return
end
conf_new:set_plaintext('myrole.yml', require('yaml').encode({
-- default config
foo = 'bar',
}))
end)
-- local function validate_config()
-- end
-- local function apply_config()
-- end
return {
role_name = 'myrole',
}
Before every update of clusterwide configuration cartridge will call the trigger, so the role can modify it if necessary. Be careful not to spoil it.