Added support for loading remote yaml configuration files over http/https (#254)

This commit is contained in:
Francois Dupoux 2022-01-30 09:17:14 +00:00
parent fcac204d3c
commit c15d70c558
5 changed files with 46 additions and 3 deletions

View file

@ -11,6 +11,7 @@ SystemRescue ChangeLog
* Remove Q-Logic Fibrechannel/Infiniband HBA firmware to save space (#256)
* Implemented a script to determine the effective configuration from yaml files (#251)
* Added boot option 'sysrescuecfg' to control how the configuration is loaded (#254)
* Added support for loading remote yaml configuration files over http/https (#254)
-------------------------------------------------------------------------------
9.00 (2022-01-16):

View file

@ -1,6 +1,8 @@
[Unit]
Description=Determine SystemRescue effective configuration
Before=sysrescue-initialize
After=network.target network-online.target
Before=sysrescue-initialize.service
Wants=network-online.target
[Service]
Type=oneshot

View file

@ -1,6 +1,6 @@
[Unit]
Description=SystemRescue Initialization
Before=sshd.service getty-pre.target iptables.service ip6tables.service
Before=getty-pre.target
Wants=getty-pre.target
[Service]

View file

@ -15,7 +15,7 @@
-- Shell scripts can read values from the JSON file using a command such as:
-- jq --raw-output '.global.copytoram' /etc/sysrescue/sysrescue-effective-config.json
-- This script requires the following lua packages to run on Arch Linux:
-- sudo pacman -Sy lua lua-yaml lua-dkjson
-- sudo pacman -Sy lua lua-yaml lua-dkjson lua-http
-- ==============================================================================
-- Import modules
@ -23,6 +23,7 @@
local lfs = require('lfs')
local yaml = require('yaml')
local json = require("dkjson")
local request = require("http.request")
-- ==============================================================================
-- Utility functions
@ -120,6 +121,32 @@ function process_yaml_config(curconfig)
return true
end
-- Download a file over http/https and return the contents of the file or nil if it fails
function download_file(fileurl)
local req_timeout = 10
local req = request.new_from_uri(fileurl)
local headers, stream = req:go(req_timeout)
if headers == nil then
io.stderr:write(string.format("Failed to download %s: Could not connect\n", fileurl))
return nil
end
status = headers:get(":status")
if status ~= '200' then
io.stderr:write(string.format("Failed to download %s: Received HTTP code %s\n", fileurl, status))
return nil
end
local body, err = stream:get_body_as_string()
if not body and err then
io.stderr:write(string.format("Failed to download %s: Error %s\n", fileurl, tostring(err)))
return nil
end
return body
end
-- ==============================================================================
-- Initialisation
-- ==============================================================================
@ -182,6 +209,18 @@ for _, curdir in ipairs(confdirs) do
end
end
-- Process remote yaml configuration files
print("Searching for remote yaml configuration files ...")
for _, curfile in ipairs(conffiles) do
if string.match(curfile, "^https?://") then
print(string.format("Processing remote yaml configuration file: %s ...", curfile))
local contents = download_file(curfile)
if (contents == nil) or (process_yaml_config(yaml.load(contents)) == false) then
errcnt = errcnt + 1
end
end
end
-- ==============================================================================
-- Override the configuration with values passed on the boot command line
-- ==============================================================================

View file

@ -114,6 +114,7 @@ lsof
lsscsi
lua
lua-dkjson
lua-http
lua-yaml
lvm2
lz4