git: cde6642431bb - main - nuageinit: check for both user_data and user-data

From: Baptiste Daroussin <bapt_at_FreeBSD.org>
Date: Thu, 27 Jun 2024 08:33:39 UTC
The branch main has been updated by bapt:

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

commit cde6642431bb0ca21aeebc7c521e99c681d31ffb
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2024-06-27 08:30:07 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2024-06-27 08:32:14 +0000

    nuageinit: check for both user_data and user-data
    
    openstack, ec2 and other implementation of cloudinit disagrees on the
    name of the file "user-data" or "user_data", test both and use the first
    found
    
    PR:     279876
---
 libexec/nuageinit/nuageinit          | 17 ++++++++++----
 libexec/nuageinit/tests/nuageinit.sh | 44 ++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 4 deletions(-)

diff --git a/libexec/nuageinit/nuageinit b/libexec/nuageinit/nuageinit
index df637bbc5064..f268f9b0f52c 100755
--- a/libexec/nuageinit/nuageinit
+++ b/libexec/nuageinit/nuageinit
@@ -190,18 +190,27 @@ else
 end
 
 -- deal with user-data
-local f = io.open(path..'/user-data', "r")
+local ud = nil
+local f = nil
+userdatas = { "user-data", "user_data" }
+for _,v in pairs(userdatas) do
+	f = io.open(path..'/' .. v, "r")
+	if f then
+		ud = v
+		break
+	end
+end
 if not f then
 	os.exit(0)
 end
 local line = f:read('*l')
 f:close()
 if line == "#cloud-config" then
-	f = io.open(path.."/user-data")
+	f = io.open(path.."/" .. ud)
 	local obj = yaml.eval(f:read("*a"))
 	f:close()
 	if not obj then
-		nuage.err("nuageinit: error parsing cloud-config file: user-data")
+		nuage.err("nuageinit: error parsing cloud-config file: " .. ud)
 	end
 	if obj.groups then
 		for n,g in pairs(obj.groups) do
@@ -298,7 +307,7 @@ if line == "#cloud-config" then
 		routing:close()
 	end
 else
-	local res,err = os.execute(path..'/user-data')
+	local res,err = os.execute(path..'/' .. ud)
 	if not res then
 		nuage.err("nuageinit: error executing user-data script: ".. err)
 	end
diff --git a/libexec/nuageinit/tests/nuageinit.sh b/libexec/nuageinit/tests/nuageinit.sh
index 42c95e1b2abd..54c88ceb539a 100644
--- a/libexec/nuageinit/tests/nuageinit.sh
+++ b/libexec/nuageinit/tests/nuageinit.sh
@@ -1,11 +1,13 @@
 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
 atf_test_case config2_pubkeys
+atf_test_case config2_pubkeys_user_data
 atf_test_case config2_network
 atf_test_case config2_network_static_v4
 
@@ -45,6 +47,16 @@ nocloud_userdata_script_body()
 	atf_check -s exit:0 -o inline:"yeah\n" /usr/libexec/nuageinit ${here}/media/nuageinit nocloud
 }
 
+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
+	chmod 755  ${here}/media/nuageinit/user_data
+	atf_check -s exit:0 -o inline:"yeah\n" /usr/libexec/nuageinit ${here}/media/nuageinit nocloud
+}
+
 nocloud_userdata_cloudconfig_users_body()
 {
 	here=$(pwd)
@@ -199,6 +211,36 @@ EOF
 	atf_check -o inline:"ssh-rsa AAAAB3NzaC1y...== Generated by Nova\n" cat home/freebsd/.ssh/authorized_keys
 }
 
+
+config2_pubkeys_user_data_body()
+{
+	here=$(pwd)
+	export NUAGE_FAKE_ROOTDIR=$(pwd)
+	if [ $(id -u) -ne 0 ]; then
+		atf_skip "root required"
+	fi
+	mkdir -p media/nuageinit
+	touch media/nuageinit/meta_data.json
+	cat > media/nuageinit/user_data << EOF
+#cloud-config
+
+ssh_authorized_keys:
+  - "ssh-rsa AAAAB3NzaC1y...== Generated by Nova"
+EOF
+	mkdir -p etc
+	cat > etc/master.passwd <<EOF
+root:*:0:0::0:0:Charlie &:/root:/bin/csh
+sys:*:1:0::0:0:Sys:/home/sys:/bin/csh
+EOF
+	pwd_mkdb -d etc ${here}/etc/master.passwd
+	cat > etc/group <<EOF
+wheel:*:0:root
+users:*:1:
+EOF
+	atf_check /usr/libexec/nuageinit ${here}/media/nuageinit config-2
+	atf_check -o inline:"ssh-rsa AAAAB3NzaC1y...== Generated by Nova\n" cat home/freebsd/.ssh/authorized_keys
+}
+
 config2_network_body() {
 	here=$(pwd)
 	mkdir -p media/nuageinit
@@ -329,10 +371,12 @@ atf_init_test_cases()
 	atf_add_test_case args
 	atf_add_test_case nocloud
 	atf_add_test_case nocloud_userdata_script
+	atf_add_test_case nocloud_user_data_script
 	atf_add_test_case nocloud_userdata_cloudconfig_users
 	atf_add_test_case nocloud_network
 	atf_add_test_case config2
 	atf_add_test_case config2_pubkeys
+	atf_add_test_case config2_pubkeys_user_data
 	atf_add_test_case config2_network
 	atf_add_test_case config2_network_static_v4
 }