Files
kumomta/assets/policy-extras/sources.lua
T
Wez Furlong 1d5180a221 sources.lua: tolerate empty pool or source sections in a file
We can compose multiple files, so it is OK for there to not
be any sources or pools in any given file.
2023-06-16 08:46:35 -07:00

112 lines
2.3 KiB
Lua

local mod = {}
local kumo = require 'kumo'
local utils = require 'policy-extras.policy_utils'
local function load_data_from_file(file_name, target)
local data = utils.load_json_or_toml_file(file_name)
for source, params in pairs(data.source or {}) do
target.sources[source] = target.sources[source]
or {
name = source,
}
utils.merge_into(params, target.sources[source])
end
for pool, pool_def in pairs(data.pool or {}) do
for pool_source, params in pairs(pool_def) do
target.pools[pool] = target.pools[pool]
or {
name = pool,
entries = {},
}
params.name = pool_source
table.insert(target.pools[pool].entries, params)
end
end
end
local function load_data(data_files)
local target = {
sources = {},
pools = {},
}
for _, file_name in ipairs(data_files) do
load_data_from_file(file_name, target)
end
-- print(kumo.json_encode_pretty(target))
return target
end
--[[
Usage:
Create a `/opt/kumomta/etc/sources.toml` file with
contents like:
```toml
[source."ip-1"]
source_address = "10.0.0.1"
[source."ip-2"]
source_address = "10.0.0.2"
[source."ip-3"]
source_address = "10.0.0.3"
# Pool containing just ip-1, which has weight=1
[pool."BestReputation"]
[pool."BestReputation"."ip-1"]
# Pool with multiple ips
[pool."MediumReputation"]
[pool."MediumReputation"."ip-2"]
weight = 2
# We're warming up ip-3, so use it less frequently than ip-2
[pool."MediumReputation"."ip-3"]
weight = 1
```
Then in your policy:
```
local sources = require 'policy-extras.sources'
sources:setup({'/opt/kumomta/etc/sources.toml'})
```
]]
function mod:setup(data_files)
local cached_load_data = kumo.memoize(load_data, {
name = 'sources_data',
ttl = '5 minutes',
capacity = 10,
})
kumo.on('get_egress_source', function(source_name)
local data = cached_load_data(data_files)
local params = data.sources[source_name]
--[[
print(
string.format('source %s: %s', source_name, kumo.json_encode(params))
)
]]
return kumo.make_egress_source(params)
end)
kumo.on('get_egress_pool', function(pool_name)
local data = cached_load_data(data_files)
local params = data.pools[pool_name]
-- print(string.format('pool %s: %s', pool_name, kumo.json_encode(params)))
return kumo.make_egress_pool(params)
end)
end
return mod