Skip to content

Commit

Permalink
fix: improve mender-inventory-provides's device_type extraction log…
Browse files Browse the repository at this point in the history
…ic from the Mender configuration file

- Add support for reading the paths from env variables `MENDER_CONF_DIR` and `MENDER_DATASTORE_DIR` when relocating the device_type file.
- Implement configuration parsing to extract `DeviceTypeFile` from `mender.conf` despite its location.
- Fallback to the default `device_type` file if `DeviceTypeFile` is not set or the extracted file path does not exist.

Changelog: Fix inventory reporting of device_type to correctly select
the file based on Mender configuration and Mender environment variables

Ticket: None

Signed-off-by: Luis Ramirez <[email protected]>
Signed-off-by: Lluis Campos <[email protected]>
  • Loading branch information
MuchoLucho authored and lluiscampos committed Dec 12, 2024
1 parent 9b047a9 commit adf65a9
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion support/mender-inventory-provides
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,33 @@ set -e
# Also submit device_type, which is in a separate file, not in the database. We don't know if the
# file contains a newline, so it's important that this is done last, to avoid corrupting other
# entries.
grep '^device_type=' /var/lib/mender/device_type

# Set default values for configuration directories
mender_conf_dir=${MENDER_CONF_DIR:-/etc/mender}
mender_datastore_dir=${MENDER_DATASTORE_DIR:-/var/lib/mender}

# Set default value for DeviceTypeFile
default_device_type_file="$mender_datastore_dir/device_type"

# Poor man's case insensitive match.
# Read mender.conf file to find the DeviceTypeFile key if it exists prioritizing /etc over /var/lib
match="[Dd][Ee][Vv][Ii][Cc][Ee][Tt][Yy][Pp][Ee][Ff][Ii][Ll][Ee]"
if [ -f "$mender_conf_dir/mender.conf" ]; then
device_type_file=$(sed -ne '/"'"$match"'" *: *"[^"]*"/ { s/.*"'"$match"'" *: *"\([^"]*\)".*/\1/; p }' "$mender_conf_dir/mender.conf" || true)
fi

if [ -f "$mender_datastore_dir/mender.conf" ]; then
if [ -z "$device_type_file" ] || [ ! -f "$device_type_file" ]; then
device_type_file=$(sed -ne '/"'"$match"'" *: *"[^"]*"/ { s/.*"'"$match"'" *: *"\([^"]*\)".*/\1/; p }' "$mender_datastore_dir/mender.conf" || true)
fi
fi

# Use default if DeviceTypeFile is not set, empty, or the file does not exist
if [ -z "$device_type_file" ] || [ ! -f "$device_type_file" ]; then
device_type_file="$default_device_type_file"
fi

# Extract the device_type value from the file specified by DeviceTypeFile
grep '^device_type=' "$device_type_file"

exit 0

0 comments on commit adf65a9

Please sign in to comment.