nuageinit: set the hostname when user-data is missing

This address the situation reported here
https://github.com/freebsd/freebsd-src/pull/1952#issuecomment-3720210259

The user-data file was missing and the `sethostname` function is never
called. This commit adjusts slightly the logic to avoid the `exit()` call
when the `user-data` file is missing.

MFC After:	1 week
Signed-off-by: Gonéri Le Bouder <goneri@lebouder.net>
Differential Revision:	https://github.com/freebsd/freebsd-src/pull/1953
This commit is contained in:
Gonéri Le Bouder
2026-01-07 16:03:34 -05:00
committed by Baptiste Daroussin
parent 9ae43352c8
commit cae280931c
+67 -46
View File
@@ -23,7 +23,7 @@ local function default_user(obj, metadata)
table.insert(ssh_authorized_keys, k) table.insert(ssh_authorized_keys, k)
end end
end end
if type(obj.ssh_authorized_keys) == "table" then if obj and type(obj.ssh_authorized_keys) == "table" then
for _, k in ipairs(obj.ssh_authorized_keys) do for _, k in ipairs(obj.ssh_authorized_keys) do
table.insert(ssh_authorized_keys, k) table.insert(ssh_authorized_keys, k)
end end
@@ -94,13 +94,13 @@ end
local function sethostname(obj, metadata) local function sethostname(obj, metadata)
-- always prefer fqdn if specified over hostname -- always prefer fqdn if specified over hostname
if obj.fqdn then if obj and obj.fqdn then
nuage.sethostname(obj.fqdn) nuage.sethostname(obj.fqdn)
elseif obj.hostname then elseif obj and obj.hostname then
nuage.sethostname(obj.hostname) nuage.sethostname(obj.hostname)
elseif metadata["local-hostname"] then elseif metadata["local-hostname"] then
nuage.sethostname(metadata["local-hostname"]) nuage.sethostname(metadata["local-hostname"])
elseif metadata["local"] then elseif metadata["hostname"] then
nuage.sethostname(metadata["hostname"]) nuage.sethostname(metadata["hostname"])
end end
end end
@@ -130,6 +130,10 @@ end
local function create_default_user(obj, metadata) local function create_default_user(obj, metadata)
local function need_default_user() local function need_default_user()
-- no user data
if not obj then
return true
end
if not obj.users then if not obj.users then
-- default user if "users" is undefined -- default user if "users" is undefined
return true return true
@@ -269,7 +273,7 @@ local function nameservers(interface, obj)
-- Only call resolvconf with interface if interface is provided -- Only call resolvconf with interface if interface is provided
if interface then if interface then
resolvconf_command = "resolvconf -a " .. interface .. " < " .. resolv_conf resolvconf_command = "resolvconf -a " .. interface .. " < " .. resolv_conf
else else
resolvconf_command = "resolvconf -u" resolvconf_command = "resolvconf -u"
end end
if not os.execute(resolvconf_command) then if not os.execute(resolvconf_command) then
@@ -662,7 +666,7 @@ local function parse_network_config()
return netobj return netobj
end end
local function load_metadata() local function load_metadata(citype)
if citype == "config-2" then if citype == "config-2" then
local parser = ucl.parser() local parser = ucl.parser()
local res, err = parser:parse_file(ni_path .. "/meta_data.json") local res, err = parser:parse_file(ni_path .. "/meta_data.json")
@@ -690,46 +694,70 @@ local function load_metadata()
return {} return {}
end end
local function load_userdata()
local ud = nil
local f = nil
local userdatas = {"user-data", "user_data"}
for _, v in pairs(userdatas) do
f = io.open(ni_path .. "/" .. v, "r")
if f then
ud = v
break
end
end
if not f then
return nil, nil
end
local line = f:read("*l")
if not line or #string.gsub(line, "^%s*(.-)%s*$", "%1") == 0 then
f:close()
return
end
if citype ~= "postnet" then
local content = f:read("*a")
if not content or #string.gsub(content, "^%s*(.-)%s*$", "%1") == 0 then
f:close()
return
end
nuage.mkdir_p(root .. "/var/cache/nuageinit")
local tof = assert(io.open(root .. "/var/cache/nuageinit/user_data", "w"))
tof:write(line .. "\n" .. content)
tof:close()
end
f:close()
local obj = nil
if ud then
f = io.open(ni_path .. "/" .. ud)
obj = yaml.load(f:read("*a"))
f:close()
if not obj then
nuage.err("error parsing cloud-config file: " .. ud)
end
end
return line, obj
end
if citype == "config-2" then if citype == "config-2" then
-- network -- network
config2_network(ni_path) config2_network(ni_path)
end end
local metadata = load_metadata() local metadata = load_metadata(citype)
local line, obj = load_userdata()
-- deal with user-data -- No user-data
local ud = nil if line == nil then
local f = nil local calls_table = {
local userdatas = {"user-data", "user_data"} sethostname,
for _, v in pairs(userdatas) do create_default_user,
f = io.open(ni_path .. "/" .. v, "r") }
if f then
ud = v for i = 1, #calls_table do
break calls_table[i](obj, metadata)
end end
end -- YAML user-data
if not f then elseif line == "#cloud-config" then
os.exit(0)
end
local line = f:read("*l")
if not line or #string.gsub(line, "^%s*(.-)%s*$", "%1") == 0 then
f:close()
os.exit(0)
end
if citype ~= "postnet" then
local content = f:read("*a")
if not content or #string.gsub(content, "^%s*(.-)%s*$", "%1") == 0 then
f:close()
os.exit(0)
end
nuage.mkdir_p(root .. "/var/cache/nuageinit")
local tof = assert(io.open(root .. "/var/cache/nuageinit/user_data", "w"))
tof:write(line .. "\n" .. content)
tof:close()
end
f:close()
if line == "#cloud-config" then
local pre_network_calls = { local pre_network_calls = {
sethostname, sethostname,
settimezone, settimezone,
@@ -749,13 +777,6 @@ if line == "#cloud-config" then
write_files_deferred, write_files_deferred,
} }
f = io.open(ni_path .. "/" .. ud)
local obj = yaml.load(f:read("*a"))
f:close()
if not obj then
nuage.err("error parsing cloud-config file: " .. ud)
end
local calls_table = pre_network_calls local calls_table = pre_network_calls
if citype == "postnet" then if citype == "postnet" then
calls_table = post_network_calls calls_table = post_network_calls