nuageinit: Lua check and lint files

Mostly white space, style, and luacheck compliance.

Signed-off-by: Jose Luis Duran <jlduran@gmail.com>
This commit is contained in:
Jose Luis Duran
2024-07-29 18:40:52 +00:00
committed by Baptiste Daroussin
parent 99adbd1b3f
commit 504981357a
14 changed files with 259 additions and 209 deletions
+13 -9
View File
@@ -1,7 +1,9 @@
---
-- SPDX-License-Identifier: BSD-2-Clause
--
-- Copyright(c) 2022 Baptiste Daroussin <bapt@FreeBSD.org>
local lfs = require("lfs")
local pu = require("posix.unistd")
local function warnmsg(str)
@@ -36,7 +38,9 @@ local function mkdir_p(path)
end
local function sethostname(hostname)
if hostname == nil then return end
if hostname == nil then
return
end
local root = os.getenv("NUAGE_FAKE_ROOTDIR")
if not root then
root = ""
@@ -49,7 +53,7 @@ local function sethostname(hostname)
warnmsg("Impossible to open " .. hostnamepath .. ":" .. err)
return
end
f:write("hostname=\""..hostname.."\"\n")
f:write('hostname="' .. hostname .. '"\n')
f:close()
end
@@ -92,7 +96,7 @@ local function adduser(pwd)
local extraargs = ""
if pwd.groups then
local list = splitlist(pwd.groups)
extraargs = " -G ".. table.concat(list, ',')
extraargs = " -G " .. table.concat(list, ",")
end
-- pw will automatically create a group named after the username
-- do not add a -g option in this case
@@ -124,7 +128,7 @@ local function adduser(pwd)
local r = os.execute(cmd)
if not r then
warnmsg("nuageinit: fail to add user "..pwd.name);
warnmsg("nuageinit: fail to add user " .. pwd.name)
warnmsg(cmd)
return nil
end
@@ -158,7 +162,7 @@ local function addgroup(grp)
local extraargs = ""
if grp.members then
local list = splitlist(grp.members)
extraargs = " -M " .. table.concat(list, ',')
extraargs = " -M " .. table.concat(list, ",")
end
cmd = "pw "
if root then
@@ -167,7 +171,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("nuageinit: fail to add group " .. grp.name)
warnmsg(cmd)
return false
end
@@ -214,12 +218,12 @@ end
local n = {
warn = warnmsg,
err = errmsg,
dirname = dirname,
mkdir_p = mkdir_p,
sethostname = sethostname,
adduser = adduser,
addgroup = addgroup,
addsshkey = addsshkey,
dirname = dirname,
mkdir_p = mkdir_p,
addsshkey = addsshkey
}
return n
+54 -44
View File
@@ -1,10 +1,11 @@
#!/usr/libexec/flua
---
-- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
--
-- Copyright(c) 2022 Baptiste Daroussin <bapt@FreeBSD.org>
local nuage = require("nuage")
local ucl = require("ucl")
local yaml = require("yaml")
if #arg ~= 2 then
@@ -12,7 +13,6 @@ if #arg ~= 2 then
end
local path = arg[1]
local citype = arg[2]
local ucl = require("ucl")
local default_user = {
name = "freebsd",
@@ -40,7 +40,7 @@ end
local function get_ifaces()
local parser = ucl.parser()
-- grab ifaces
local ns = io.popen('netstat -i --libxo json')
local ns = io.popen("netstat -i --libxo json")
local netres = ns:read("*a")
ns:close()
local res, err = parser:parse_string(netres)
@@ -94,23 +94,27 @@ local function config2_network(p)
for _, v in pairs(obj["networks"]) do
local interface = mylinks[v["link"]]
if v["type"] == "ipv4_dhcp" then
network:write("ifconfig_"..interface.."=\"DHCP\"\n")
network:write("ifconfig_" .. interface .. '="DHCP"\n')
end
if v["type"] == "ipv4" then
network:write("ifconfig_"..interface.."=\"inet "..v["ip_address"].." netmask " .. v["netmask"] .. "\"\n")
network:write(
"ifconfig_" .. interface .. '="inet ' .. v["ip_address"] .. " netmask " .. v["netmask"] .. '"\n'
)
if v["gateway"] then
routing:write("defaultrouter=\""..v["gateway"].."\"\n")
routing:write('defaultrouter="' .. v["gateway"] .. '"\n')
end
if v["routes"] then
for i, r in ipairs(v["routes"]) do
local rname = "cloudinit" .. i .. "_" .. interface
if v["gateway"] and v["gateway"] == r["gateway"] then goto next end
if r["network"] == "0.0.0.0" then
routing:write("defaultrouter=\""..r["gateway"].."\"\n")
if v["gateway"] and v["gateway"] == r["gateway"] then
goto next
end
routing:write("route_".. rname .. "=\"-net ".. r["network"] .. " ")
routing:write(r["gateway"] .. " " .. r["netmask"] .. "\"\n")
if r["network"] == "0.0.0.0" then
routing:write('defaultrouter="' .. r["gateway"] .. '"\n')
goto next
end
routing:write("route_" .. rname .. '="-net ' .. r["network"] .. " ")
routing:write(r["gateway"] .. " " .. r["netmask"] .. '"\n')
ipv4[#ipv4 + 1] = rname
::next::
end
@@ -119,11 +123,11 @@ local function config2_network(p)
if v["type"] == "ipv6" then
ipv6[#ipv6 + 1] = interface
ipv6_routes[#ipv6_routes + 1] = interface
network:write("ifconfig_"..interface.."_ipv6=\"inet6 "..v["ip_address"].."\"\n")
network:write("ifconfig_" .. interface .. '_ipv6="inet6 ' .. v["ip_address"] .. '"\n')
if v["gateway"] then
routing:write("ipv6_defaultrouter=\""..v["gateway"].."\"\n")
routing:write("ipv6_route_"..interface.."=\""..v["gateway"])
routing:write(" -prefixlen 128 -interface "..interface.."\"\n")
routing:write('ipv6_defaultrouter="' .. v["gateway"] .. '"\n')
routing:write("ipv6_route_" .. interface .. '="' .. v["gateway"])
routing:write(" -prefixlen 128 -interface " .. interface .. '"\n')
end
-- TODO compute the prefixlen for the routes
--if v["routes"] then
@@ -131,8 +135,10 @@ local function config2_network(p)
-- local rname = "cloudinit" .. i .. "_" .. mylinks[v["link"]]
-- -- skip all the routes which are already covered by the default gateway, some provider
-- -- still list plenty of them.
-- if v["gateway"] == r["gateway"] then goto next end
-- routing:write("ipv6_route_" .. rname .. "\"\n")
-- if v["gateway"] == r["gateway"] then
-- goto next
-- end
-- routing:write("ipv6_route_" .. rname .. '"\n')
-- ipv6_routes[#ipv6_routes + 1] = rname
-- ::next::
-- end
@@ -140,17 +146,17 @@ local function config2_network(p)
end
end
if #ipv4 > 0 then
routing:write("static_routes=\"")
routing:write(table.concat(ipv4, " ") .. "\"\n")
routing:write('static_routes="')
routing:write(table.concat(ipv4, " ") .. '"\n')
end
if #ipv6 > 0 then
network:write("ipv6_network_interfaces=\"")
network:write(table.concat(ipv6, " ") .. "\"\n")
network:write("ipv6_default_interface=\""..ipv6[1].."\"\n")
network:write('ipv6_network_interfaces="')
network:write(table.concat(ipv6, " ") .. '"\n')
network:write('ipv6_default_interface="' .. ipv6[1] .. '"\n')
end
if #ipv6_routes > 0 then
routing:write("ipv6_static_routes=\"")
routing:write(table.concat(ipv6, " ") .. "\"\n")
routing:write('ipv6_static_routes="')
routing:write(table.concat(ipv6, " ") .. '"\n')
end
network:close()
routing:close()
@@ -158,7 +164,7 @@ end
if citype == "config-2" then
local parser = ucl.parser()
local res,err = parser:parse_file(path..'/meta_data.json')
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)
@@ -184,9 +190,9 @@ elseif citype == "nocloud" then
if not obj then
nuage.err("nuageinit: error parsing nocloud meta-data")
end
local hostname = obj['local-hostname']
local hostname = obj["local-hostname"]
if not hostname then
hostname = obj['hostname']
hostname = obj["hostname"]
end
if hostname then
nuage.sethostname(hostname)
@@ -200,7 +206,7 @@ local ud = nil
local f = nil
userdatas = {"user-data", "user_data"}
for _, v in pairs(userdatas) do
f = io.open(path..'/' .. v, "r")
f = io.open(path .. "/" .. v, "r")
if f then
ud = v
break
@@ -209,7 +215,7 @@ end
if not f then
os.exit(0)
end
local line = f:read('*l')
local line = f:read("*l")
f:close()
if line == "#cloud-config" then
f = io.open(path .. "/" .. ud)
@@ -230,7 +236,7 @@ if line == "#cloud-config" then
nuage.addgroup({name = k, members = v})
end
else
nuage.warn("nuageinit: invalid type : "..type(g).." for users entry number "..n);
nuage.warn("nuageinit: invalid type: " .. type(g) .. " for users entry number " .. n)
end
end
end
@@ -254,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("nuageinit: invalid type : " .. type(u) .. " for users entry number " .. n)
end
::unext::
end
@@ -275,45 +281,49 @@ if line == "#cloud-config" then
local routing = open_config("routing")
local ipv6 = {}
for _, v in pairs(obj.network.ethernets) do
if not v.match then goto next end
if not v.match.macaddress then goto next end
if not v.match then
goto next
end
if not v.match.macaddress then
goto next
end
if not ifaces[v.match.macaddress] then
nuage.warn("nuageinit: not interface matching: " .. v.match.macaddress)
goto next
end
local interface = ifaces[v.match.macaddress]
if v.dhcp4 then
network:write("ifconfig_"..interface.."=\"DHCP\"\n")
network:write("ifconfig_" .. interface .. '="DHCP"\n')
elseif v.addresses then
for _, a in pairs(v.addresses) do
if a:match("^(%d+)%.(%d+)%.(%d+)%.(%d+)") then
network:write("ifconfig_"..interface.."=\"inet "..a.."\"\n")
network:write("ifconfig_" .. interface .. '="inet ' .. a .. '"\n')
else
network:write("ifconfig_"..interface.."_ipv6=\"inet6 "..a.."\"\n")
network:write("ifconfig_" .. interface .. '_ipv6="inet6 ' .. a .. '"\n')
ipv6[#ipv6 + 1] = interface
end
end
end
if v.gateway4 then
routing:write("defaultrouter=\""..v.gateway4.."\"\n")
routing:write('defaultrouter="' .. v.gateway4 .. '"\n')
end
if v.gateway6 then
routing:write("ipv6_defaultrouter=\""..v.gateway6.."\"\n")
routing:write("ipv6_route_"..interface.."=\""..v.gateway6)
routing:write(" -prefixlen 128 -interface "..interface.."\"\n")
routing:write('ipv6_defaultrouter="' .. v.gateway6 .. '"\n')
routing:write("ipv6_route_" .. interface .. '="' .. v.gateway6)
routing:write(" -prefixlen 128 -interface " .. interface .. '"\n')
end
::next::
end
if #ipv6 > 0 then
network:write("ipv6_network_interfaces=\"")
network:write(table.concat(ipv6, " ") .. "\"\n")
network:write("ipv6_default_interface=\""..ipv6[1].."\"\n")
network:write('ipv6_network_interfaces="')
network:write(table.concat(ipv6, " ") .. '"\n')
network:write('ipv6_default_interface="' .. ipv6[1] .. '"\n')
end
network:close()
routing:close()
end
else
local res,err = os.execute(path..'/' .. ud)
local res, err = os.execute(path .. "/" .. ud)
if not res then
nuage.err("nuageinit: error executing user-data script: " .. err)
end
+5 -5
View File
@@ -2,12 +2,12 @@ PACKAGE= tests
ATF_TESTS_SH= nuage utils nuageinit
${PACKAGE}FILES+= warn.lua
${PACKAGE}FILES+= err.lua
${PACKAGE}FILES+= dirname.lua
${PACKAGE}FILES+= sethostname.lua
${PACKAGE}FILES+= addgroup.lua
${PACKAGE}FILES+= addsshkey.lua
${PACKAGE}FILES+= adduser.lua
${PACKAGE}FILES+= addgroup.lua
${PACKAGE}FILES+= dirname.lua
${PACKAGE}FILES+= err.lua
${PACKAGE}FILES+= sethostname.lua
${PACKAGE}FILES+= warn.lua
.include <bsd.test.mk>
+1
View File
@@ -1,6 +1,7 @@
#!/usr/libexec/flua
local n = require("nuage")
if n.addgroup() then
n.err("addgroup should not accept empty value")
end
+3
View File
@@ -1,2 +1,5 @@
#!/usr/libexec/flua
local n = require("nuage")
n.addsshkey(".", "mykey")
+1
View File
@@ -1,6 +1,7 @@
#!/usr/libexec/flua
local n = require("nuage")
if n.adduser() then
n.err("adduser should not accept empty value")
end
+5 -2
View File
@@ -1,8 +1,11 @@
#!/usr/libexec/flua
local n = require("nuage")
print(n.dirname("/my/path/path1"))
if n.dirname("path") then
nuage.err("Expecting nil for n.dirname(\"path\")")
n.err('Expecting nil for n.dirname("path")')
end
if n.dirname() then
nuage.err("Expecting nil for n.dirname")
n.err("Expecting nil for n.dirname")
end
+1
View File
@@ -1,4 +1,5 @@
#!/usr/libexec/flua
local n = require("nuage")
n.err("plop")
+16 -5
View File
@@ -1,9 +1,16 @@
#-
# Copyright (c) 2022 Baptiste Daroussin <bapt@FreeBSD.org>
#
# SPDX-License-Identifier: BSD-2-Clause
#
atf_test_case sethostname
atf_test_case addsshkey
atf_test_case adduser
atf_test_case addgroup
sethostname_body() {
sethostname_body()
{
export NUAGE_FAKE_ROOTDIR="$(pwd)"
atf_check /usr/libexec/flua $(atf_get_srcdir)/sethostname.lua
if [ ! -f etc/rc.conf.d/hostname ]; then
@@ -12,7 +19,8 @@ sethostname_body() {
atf_check -o inline:"hostname=\"myhostname\"\n" cat etc/rc.conf.d/hostname
}
addsshkey_body() {
addsshkey_body()
{
atf_check /usr/libexec/flua $(atf_get_srcdir)/addsshkey.lua
if [ ! -f .ssh/authorized_keys ]; then
atf_fail "ssh key not added"
@@ -24,7 +32,8 @@ addsshkey_body() {
atf_check -o inline:"mykey\nmykey\n" cat .ssh/authorized_keys
}
adduser_body() {
adduser_body()
{
export NUAGE_FAKE_ROOTDIR="$(pwd)"
if [ $(id -u) -ne 0 ]; then
atf_skip "root required"
@@ -38,7 +47,8 @@ adduser_body() {
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
}
addgroup_body() {
addgroup_body()
{
export NUAGE_FAKE_ROOTDIR="$(pwd)"
mkdir etc
printf "wheel:*:0:root\n" > etc/group
@@ -46,7 +56,8 @@ addgroup_body() {
atf_check -o inline:"impossible_groupname:*:1001:\n" grep impossible_groupname etc/group
}
atf_init_test_cases() {
atf_init_test_cases()
{
atf_add_test_case sethostname
atf_add_test_case addsshkey
atf_add_test_case adduser
+21 -18
View File
@@ -1,8 +1,13 @@
#-
# Copyright (c) 2022 Baptiste Daroussin <bapt@FreeBSD.org>
#
# SPDX-License-Identifier: BSD-2-Clause
#
atf_test_case args
atf_test_case nocloud
atf_test_case nocloud_userdata_script
atf_test_case nocloud_user_data_script
atf_test_case nocloud_userdata_cloudconfig
atf_test_case nocloud_userdata_cloudconfig_users
atf_test_case nocloud_network
atf_test_case config2
@@ -12,7 +17,6 @@ atf_test_case config2_pubkeys_meta_data
atf_test_case config2_network
atf_test_case config2_network_static_v4
args_body()
{
atf_check -s exit:1 -e inline:"Usage /usr/libexec/nuageinit <cloud-init directory> [config-2|nocloud]\n" /usr/libexec/nuageinit
@@ -43,7 +47,7 @@ nocloud_userdata_script_body()
here=$(pwd)
mkdir -p media/nuageinit
printf "instance-id: iid-local01\n" > ${here}/media/nuageinit/meta-data
printf "#!/bin/sh\necho "yeah"\n" > ${here}/media/nuageinit/user-data
printf "#!/bin/sh\necho yeah\n" > ${here}/media/nuageinit/user-data
chmod 755 ${here}/media/nuageinit/user-data
atf_check -s exit:0 -o inline:"yeah\n" /usr/libexec/nuageinit ${here}/media/nuageinit nocloud
}
@@ -53,7 +57,7 @@ nocloud_user_data_script_body()
here=$(pwd)
mkdir -p media/nuageinit
printf "instance-id: iid-local01\n" > ${here}/media/nuageinit/meta-data
printf "#!/bin/sh\necho "yeah"\n" > ${here}/media/nuageinit/user_data
printf "#!/bin/sh\necho yeah\n" > ${here}/media/nuageinit/user_data
chmod 755 ${here}/media/nuageinit/user_data
atf_check -s exit:0 -o inline:"yeah\n" /usr/libexec/nuageinit ${here}/media/nuageinit nocloud
}
@@ -137,14 +141,13 @@ EOF
printf "instance-id: iid-local01\n" > ${here}/media/nuageinit/meta-data
cat > media/nuageinit/user-data << EOF
#cloud-config
#
network:
version: 2
ethernets:
# opaque ID for physical interfaces, only referred to by other stanzas
id0:
match:
macaddress: '${myaddr}'
macaddress: "$myaddr"
addresses:
- 192.168.14.2/24
- 2001:1::1/64
@@ -176,7 +179,7 @@ config2_body()
atf_check /usr/libexec/nuageinit ${here}/media/nuageinit config-2
cat > media/nuageinit/meta_data.json << EOF
{
"hostname": "cloudimg",
"hostname": "cloudimg"
}
EOF
export NUAGE_FAKE_ROOTDIR=$(pwd)
@@ -195,7 +198,6 @@ config2_pubkeys_body()
touch media/nuageinit/meta_data.json
cat > media/nuageinit/user-data << EOF
#cloud-config
ssh_authorized_keys:
- "ssh-rsa AAAAB3NzaC1y...== Generated by Nova"
EOF
@@ -225,7 +227,6 @@ config2_pubkeys_user_data_body()
touch media/nuageinit/meta_data.json
cat > media/nuageinit/user_data << EOF
#cloud-config
ssh_authorized_keys:
- "ssh-rsa AAAAB3NzaC1y...== Generated by Nova"
EOF
@@ -289,7 +290,8 @@ EOF
atf_check -o inline:"ssh-ed25519 my_key_id tdb@host\n" cat home/freebsd/.ssh/authorized_keys
}
config2_network_body() {
config2_network_body()
{
here=$(pwd)
mkdir -p media/nuageinit
printf "{}" > media/nuageinit/meta_data.json
@@ -306,7 +308,7 @@ cat > media/nuageinit/network_data.json <<EOF
{
"ethernet_mac_address": "$myaddr",
"id": "iface0",
"mtu": null,
"mtu": null
}
],
"networks": [
@@ -321,7 +323,7 @@ cat > media/nuageinit/network_data.json <<EOF
"link": "iface0",
// supports condensed IPv6 with CIDR netmask
"ip_address": "2001:cdba::3257:9652/24",
"gateway": "fd00::1"
"gateway": "fd00::1",
"routes": [
{
"network": "::",
@@ -332,11 +334,11 @@ cat > media/nuageinit/network_data.json <<EOF
"network": "::",
"netmask": "ffff:ffff:ffff::",
"gateway": "fd00::1:1"
},
}
],
"network_id": "da5bb487-5193-4a65-a3df-4a0055a8c0d8"
},
],
}
]
}
EOF
export NUAGE_FAKE_ROOTDIR=$(pwd)
@@ -356,7 +358,8 @@ EOF
atf_check -o file:routing cat ${here}/etc/rc.conf.d/routing
}
config2_network_static_v4_body() {
config2_network_static_v4_body()
{
here=$(pwd)
mkdir -p media/nuageinit
printf "{}" > media/nuageinit/meta_data.json
@@ -373,14 +376,14 @@ cat > media/nuageinit/network_data.json <<EOF
{
"ethernet_mac_address": "$myaddr",
"id": "iface0",
"mtu": null,
"mtu": null
}
],
"networks": [
{
"id": "network0",
"link": "iface0",
"type": "ipv4"
"type": "ipv4",
"ip_address": "10.184.0.244",
"netmask": "255.255.240.0",
"routes": [
+1
View File
@@ -1,4 +1,5 @@
#!/usr/libexec/flua
local n = require("nuage")
n.sethostname("myhostname")
+14 -4
View File
@@ -1,20 +1,30 @@
#-
# Copyright (c) 2022 Baptiste Daroussin <bapt@FreeBSD.org>
#
# SPDX-License-Identifier: BSD-2-Clause
#
atf_test_case warn
atf_test_case err
atf_test_case dirname
warn_body() {
warn_body()
{
atf_check -e "inline:plop\n" -s exit:0 /usr/libexec/flua $(atf_get_srcdir)/warn.lua
}
err_body() {
err_body()
{
atf_check -e "inline:plop\n" -s exit:1 /usr/libexec/flua $(atf_get_srcdir)/err.lua
}
dirname_body() {
dirname_body()
{
atf_check -o "inline:/my/path/\n" -s exit:0 /usr/libexec/flua $(atf_get_srcdir)/dirname.lua
}
atf_init_test_cases() {
atf_init_test_cases()
{
atf_add_test_case warn
atf_add_test_case err
atf_add_test_case dirname
+1
View File
@@ -1,4 +1,5 @@
#!/usr/libexec/flua
local n = require("nuage")
n.warn("plop")
+1
View File
@@ -1,3 +1,4 @@
---
-- SPDX-License-Identifier: MIT
--
-- Copyright (c) 2017 Dominic Letz dominicletz@exosite.com