git: 945632ca7611 - main - nuageinit: Standardize warning/error messages

From: Baptiste Daroussin <bapt_at_FreeBSD.org>
Date: Thu, 26 Sep 2024 11:06:18 UTC
The branch main has been updated by bapt:

URL: https://cgit.FreeBSD.org/src/commit/?id=945632ca76117029e7bd1f46d17ccb378973daf7

commit 945632ca76117029e7bd1f46d17ccb378973daf7
Author:     Jose Luis Duran <jlduran@gmail.com>
AuthorDate: 2024-07-30 01:18:11 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2024-09-26 10:59:14 +0000

    nuageinit: Standardize warning/error messages
    
    Standardize the utilities from nuage.lua, to return nil on failure, plus
    an error message as a second result, and some value different from nil
    on success.
    
    Make warnmsg() and errmsg() append "nuageinit: " by default.  Pass an
    optional second parameter as false to avoid printing this tag.
    
    Signed-off-by: Jose Luis Duran <jlduran@gmail.com>
---
 libexec/nuageinit/nuage.lua          | 21 ++++++++++++++-------
 libexec/nuageinit/nuageinit          | 26 +++++++++++++-------------
 libexec/nuageinit/tests/nuage.sh     |  4 ++--
 libexec/nuageinit/tests/nuageinit.sh |  4 ++--
 libexec/nuageinit/tests/utils.sh     |  4 ++--
 5 files changed, 33 insertions(+), 26 deletions(-)

diff --git a/libexec/nuageinit/nuage.lua b/libexec/nuageinit/nuage.lua
index 1f035036f802..c27b66b23be2 100644
--- a/libexec/nuageinit/nuage.lua
+++ b/libexec/nuageinit/nuage.lua
@@ -7,12 +7,19 @@ local unistd = require("posix.unistd")
 local sys_stat = require("posix.sys.stat")
 local lfs = require("lfs")
 
-local function warnmsg(str)
-	io.stderr:write(str .. "\n")
+local function warnmsg(str, prepend)
+	if not str then
+		return
+	end
+	local tag = ""
+	if prepend ~= false then
+		tag = "nuageinit: "
+	end
+	io.stderr:write(tag .. str .. "\n")
 end
 
-local function errmsg(str)
-	io.stderr:write(str .. "\n")
+local function errmsg(str, prepend)
+	warnmsg(str, prepend)
 	os.exit(1)
 end
 
@@ -129,7 +136,7 @@ local function adduser(pwd)
 
 	local r = os.execute(cmd)
 	if not r then
-		warnmsg("nuageinit: fail to add user " .. pwd.name)
+		warnmsg("fail to add user " .. pwd.name)
 		warnmsg(cmd)
 		return nil
 	end
@@ -172,7 +179,7 @@ local function addgroup(grp)
 	cmd = cmd .. "groupadd -n " .. grp.name .. extraargs
 	local r = os.execute(cmd)
 	if not r then
-		warnmsg("nuageinit: fail to add group " .. grp.name)
+		warnmsg("fail to add group " .. grp.name)
 		warnmsg(cmd)
 		return false
 	end
@@ -201,7 +208,7 @@ local function addsshkey(homedir, key)
 
 	local f = io.open(ak_path, "a")
 	if not f then
-		warnmsg("nuageinit: impossible to open " .. ak_path)
+		warnmsg("impossible to open " .. ak_path)
 		return
 	end
 	f:write(key .. "\n")
diff --git a/libexec/nuageinit/nuageinit b/libexec/nuageinit/nuageinit
index 50d77dfef9f5..8c289fea6b1d 100755
--- a/libexec/nuageinit/nuageinit
+++ b/libexec/nuageinit/nuageinit
@@ -9,7 +9,7 @@ local ucl = require("ucl")
 local yaml = require("yaml")
 
 if #arg ~= 2 then
-	nuage.err("Usage " .. arg[0] .. " <cloud-init directory> [config-2|nocloud]")
+	nuage.err("Usage " .. arg[0] .. " <cloud-init directory> [config-2|nocloud]", false)
 end
 local path = arg[1]
 local citype = arg[2]
@@ -32,7 +32,7 @@ local function open_config(name)
 	nuage.mkdir_p(root .. "/etc/rc.conf.d")
 	local f, err = io.open(root .. "/etc/rc.conf.d/" .. name, "w")
 	if not f then
-		nuage.err("nuageinit: unable to open " .. name .. " config: " .. err)
+		nuage.err("unable to open " .. name .. " config: " .. err)
 	end
 	return f
 end
@@ -69,14 +69,14 @@ local function config2_network(p)
 	f:close()
 	local res, err = parser:parse_file(p .. "/network_data.json")
 	if not res then
-		nuage.warn("nuageinit: error parsing network_data.json: " .. err)
+		nuage.warn("error parsing network_data.json: " .. err)
 		return
 	end
 	local obj = parser:get_object()
 
 	local ifaces = get_ifaces()
 	if not ifaces then
-		nuage.warn("nuageinit: no network interfaces found")
+		nuage.warn("no network interfaces found")
 		return
 	end
 	local mylinks = {}
@@ -167,7 +167,7 @@ if citype == "config-2" then
 	local res, err = parser:parse_file(path .. "/meta_data.json")
 
 	if not res then
-		nuage.err("nuageinit: error parsing config-2: meta_data.json: " .. err)
+		nuage.err("error parsing config-2 meta_data.json: " .. err)
 	end
 	local obj = parser:get_object()
 	if obj.public_keys then
@@ -183,12 +183,12 @@ if citype == "config-2" then
 elseif citype == "nocloud" then
 	local f, err = io.open(path .. "/meta-data")
 	if err then
-		nuage.err("nuageinit: error parsing nocloud meta-data: " .. err)
+		nuage.err("error parsing nocloud meta-data: " .. err)
 	end
 	local obj = yaml.eval(f:read("*a"))
 	f:close()
 	if not obj then
-		nuage.err("nuageinit: error parsing nocloud meta-data")
+		nuage.err("error parsing nocloud meta-data")
 	end
 	local hostname = obj["local-hostname"]
 	if not hostname then
@@ -222,21 +222,21 @@ if line == "#cloud-config" then
 	local obj = yaml.eval(f:read("*a"))
 	f:close()
 	if not obj then
-		nuage.err("nuageinit: error parsing cloud-config file: " .. ud)
+		nuage.err("error parsing cloud-config file: " .. ud)
 	end
 	if obj.groups then
 		for n, g in pairs(obj.groups) do
 			if (type(g) == "string") then
 				local r = nuage.addgroup({name = g})
 				if not r then
-					nuage.warn("nuageinit: failed to add group: " .. g)
+					nuage.warn("failed to add group: " .. g)
 				end
 			elseif type(g) == "table" then
 				for k, v in pairs(g) do
 					nuage.addgroup({name = k, members = v})
 				end
 			else
-				nuage.warn("nuageinit: invalid type: " .. type(g) .. " for users entry number " .. n)
+				nuage.warn("invalid type: " .. type(g) .. " for users entry number " .. n)
 			end
 		end
 	end
@@ -260,7 +260,7 @@ if line == "#cloud-config" then
 					end
 				end
 			else
-				nuage.warn("nuageinit: invalid type : " .. type(u) .. " for users entry number " .. n)
+				nuage.warn("invalid type : " .. type(u) .. " for users entry number " .. n)
 			end
 			::unext::
 		end
@@ -288,7 +288,7 @@ if line == "#cloud-config" then
 				goto next
 			end
 			if not ifaces[v.match.macaddress] then
-				nuage.warn("nuageinit: not interface matching: " .. v.match.macaddress)
+				nuage.warn("not interface matching: " .. v.match.macaddress)
 				goto next
 			end
 			local interface = ifaces[v.match.macaddress]
@@ -325,6 +325,6 @@ if line == "#cloud-config" then
 else
 	local res, err = os.execute(path .. "/" .. ud)
 	if not res then
-		nuage.err("nuageinit: error executing user-data script: " .. err)
+		nuage.err("error executing user-data script: " .. err)
 	end
 end
diff --git a/libexec/nuageinit/tests/nuage.sh b/libexec/nuageinit/tests/nuage.sh
index 4e2353176b16..6d8a2746de1f 100644
--- a/libexec/nuageinit/tests/nuage.sh
+++ b/libexec/nuageinit/tests/nuage.sh
@@ -42,7 +42,7 @@ adduser_body()
 	printf "root:*:0:0::0:0:Charlie &:/root:/bin/csh\n" > etc/master.passwd
 	pwd_mkdb -d etc etc/master.passwd
 	printf "wheel:*:0:root\n" > etc/group
-	atf_check -e inline:"Argument should be a table\nArgument should be a table\n" /usr/libexec/flua $(atf_get_srcdir)/adduser.lua
+	atf_check -e inline:"nuageinit: Argument should be a table\nnuageinit: Argument should be a table\n" /usr/libexec/flua $(atf_get_srcdir)/adduser.lua
 	test -d home/impossible_username || atf_fail "home not created"
 	atf_check -o inline:"impossible_username::1001:1001::0:0:impossible_username User:/home/impossible_username:/bin/sh\n" grep impossible_username etc/master.passwd
 }
@@ -52,7 +52,7 @@ addgroup_body()
 	export NUAGE_FAKE_ROOTDIR="$(pwd)"
 	mkdir etc
 	printf "wheel:*:0:root\n" > etc/group
-	atf_check -e inline:"Argument should be a table\nArgument should be a table\n" /usr/libexec/flua $(atf_get_srcdir)/addgroup.lua
+	atf_check -e inline:"nuageinit: Argument should be a table\nnuageinit: Argument should be a table\n" /usr/libexec/flua $(atf_get_srcdir)/addgroup.lua
 	atf_check -o inline:"impossible_groupname:*:1001:\n" grep impossible_groupname etc/group
 }
 
diff --git a/libexec/nuageinit/tests/nuageinit.sh b/libexec/nuageinit/tests/nuageinit.sh
index 64ac066fefbf..f3fc55e87523 100644
--- a/libexec/nuageinit/tests/nuageinit.sh
+++ b/libexec/nuageinit/tests/nuageinit.sh
@@ -22,7 +22,7 @@ args_body()
 	atf_check -s exit:1 -e inline:"Usage /usr/libexec/nuageinit <cloud-init directory> [config-2|nocloud]\n" /usr/libexec/nuageinit
 	atf_check -s exit:1 -e inline:"Usage /usr/libexec/nuageinit <cloud-init directory> [config-2|nocloud]\n" /usr/libexec/nuageinit bla
 	atf_check -s exit:1 -e inline:"Usage /usr/libexec/nuageinit <cloud-init directory> [config-2|nocloud]\n" /usr/libexec/nuageinit bla meh plop
-	atf_check -s exit:1 -e inline:"Unknown cloud init type: meh\n" /usr/libexec/nuageinit bla meh
+	atf_check -s exit:1 -e inline:"nuageinit: Unknown cloud init type: meh\n" /usr/libexec/nuageinit bla meh
 }
 
 nocloud_body()
@@ -174,7 +174,7 @@ config2_body()
 {
 	here=$(pwd)
 	mkdir -p media/nuageinit
-	atf_check -s exit:1 -e match:"nuageinit: error parsing config-2: meta_data.json.*" /usr/libexec/nuageinit ${here}/media/nuageinit config-2
+	atf_check -s exit:1 -e match:"nuageinit: error parsing config-2 meta_data.json:.*" /usr/libexec/nuageinit ${here}/media/nuageinit config-2
 	printf "{}" > media/nuageinit/meta_data.json
 	atf_check /usr/libexec/nuageinit ${here}/media/nuageinit config-2
 	cat > media/nuageinit/meta_data.json << EOF
diff --git a/libexec/nuageinit/tests/utils.sh b/libexec/nuageinit/tests/utils.sh
index c3b5b8212938..26f117d81d60 100644
--- a/libexec/nuageinit/tests/utils.sh
+++ b/libexec/nuageinit/tests/utils.sh
@@ -10,12 +10,12 @@ atf_test_case dirname
 
 warn_body()
 {
-	atf_check -e "inline:plop\n" -s exit:0 /usr/libexec/flua $(atf_get_srcdir)/warn.lua
+	atf_check -e "inline:nuageinit: plop\n" -s exit:0 /usr/libexec/flua $(atf_get_srcdir)/warn.lua
 }
 
 err_body()
 {
-	atf_check -e "inline:plop\n" -s exit:1 /usr/libexec/flua $(atf_get_srcdir)/err.lua
+	atf_check -e "inline:nuageinit: plop\n" -s exit:1 /usr/libexec/flua $(atf_get_srcdir)/err.lua
 }
 
 dirname_body()