2025-04-27 17:20:02 +01:00
|
|
|
local cjson = require('cjson')
|
|
|
|
local http = require('socket.http')
|
|
|
|
local redis = require('redis')
|
2025-04-28 15:13:52 +01:00
|
|
|
local socket = require('socket')
|
|
|
|
local stdlib = require('posix.stdlib')
|
|
|
|
|
|
|
|
-- TODO: Logging formatting
|
|
|
|
-- TODO: Monitoring
|
2025-04-27 17:20:02 +01:00
|
|
|
|
|
|
|
local client = redis.connect('redis', 6379)
|
|
|
|
|
|
|
|
local function get_mirrors(source)
|
|
|
|
local body, _, _, _ = http.request(source)
|
|
|
|
return cjson.decode(body)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function prepare_pool_map(mirrors)
|
|
|
|
local pool_map = {}
|
|
|
|
for _, site in pairs(mirrors['sites']) do
|
|
|
|
for _, alternative in pairs(site['available_alternatives']) do
|
|
|
|
if alternative['type'] == 'mirror' then
|
|
|
|
local canonical_host = site['main_domain']
|
|
|
|
if select(2, canonical_host:gsub("%.", "")) == 1 then
|
|
|
|
canonical_host = "www." .. canonical_host
|
|
|
|
end
|
|
|
|
pool_map[canonical_host] = alternative['url']:sub(9)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return pool_map
|
|
|
|
end
|
|
|
|
|
|
|
|
local function redis_set(client, key, data)
|
|
|
|
local res, err = client:set(key, data)
|
|
|
|
if not res then
|
|
|
|
print("Error setting key in Redis:", err)
|
|
|
|
else
|
|
|
|
print("Data successfully stored in Redis under '" .. key .. "'")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_config(host)
|
|
|
|
local function read_file(file_path)
|
|
|
|
local file = io.open(file_path, "r")
|
|
|
|
if file then
|
|
|
|
local content = file:read("*a")
|
|
|
|
file:close()
|
|
|
|
return content
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local file_paths = {
|
|
|
|
"/configs/" .. host .. ".json", -- Check host-specific file
|
|
|
|
"/configs/default.json" -- Check default file
|
|
|
|
}
|
|
|
|
for _, file_path in ipairs(file_paths) do
|
|
|
|
local content = read_file(file_path)
|
|
|
|
if content then
|
|
|
|
print("Using " .. file_path .. " for " .. host .. " configuration.")
|
|
|
|
return content
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return "{}"
|
|
|
|
end
|
|
|
|
|
|
|
|
local function update_pool_data(pool_name, pool_source)
|
|
|
|
local pool_map = prepare_pool_map(get_mirrors(pool_source))
|
|
|
|
redis_set(client, 'jasima:poolmap:' .. pool_name, cjson.encode(pool_map))
|
|
|
|
for host, _ in pairs(pool_map) do
|
|
|
|
local redis_key = 'jasima:config:' .. host
|
|
|
|
local config = get_config(host)
|
|
|
|
redis_set(client, redis_key, config)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local interval = 5 * 60 -- 5 minutes in seconds
|
|
|
|
local last_update_time = os.clock() - interval
|
|
|
|
|
|
|
|
while true do
|
|
|
|
local current_time = os.clock()
|
|
|
|
if current_time - last_update_time >= interval then
|
|
|
|
for pool_name, pool_source in pairs(stdlib.getenv()) do
|
|
|
|
if pool_name:match("^JASIMA_MIRRORS_POOL_") then
|
|
|
|
local pool_name = pool_name:sub(21)
|
|
|
|
update_pool_data(pool_name, pool_source)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
last_update_time = current_time
|
|
|
|
end
|
2025-04-28 15:13:52 +01:00
|
|
|
socket.sleep(1)
|
2025-04-27 17:20:02 +01:00
|
|
|
end
|