From 9d8cb97fb30e02bef89bbcf409109dce38aea8b6 Mon Sep 17 00:00:00 2001 From: Markus Hilger Date: Wed, 11 Nov 2026 23:02:02 +0100 Subject: [PATCH] Add confluent_attrib helper to profile functions Profile scripts had to call apiclient and parse /self/myattribs to read a node attribute. confluent_attrib prints one attribute, comma joining lists, and returns 1 when it is unset. Non-shell scripts can run it as 'bash /etc/confluent/functions confluent_attrib '. --- .../common/profile/scripts/functions | 33 +++++++++++++++++++ .../profiles/default/scripts/functions | 33 +++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/confluent_osdeploy/common/profile/scripts/functions b/confluent_osdeploy/common/profile/scripts/functions index 4b17d3ad..92069e6b 100644 --- a/confluent_osdeploy/common/profile/scripts/functions +++ b/confluent_osdeploy/common/profile/scripts/functions @@ -29,6 +29,39 @@ function confluentpython() { fi } +# Print a node attribute, e.g. 'confluent_attrib custom.foo'. Returns 1 if unset. +function confluent_attrib() { + if [ -z "$1" ]; then + echo "Usage: confluent_attrib " >&2 + return 2 + fi + local apiclient=/opt/confluent/bin/apiclient + if [ -f /etc/confluent/apiclient ]; then + apiclient=/etc/confluent/apiclient + fi + # program on stdin, data on fd 3: confluentpython splits its args + confluentpython - "$1" 3< <(confluentpython $apiclient /confluent-api/self/myattribs -j) <<'EOF' +import json +import os +import sys +try: + attribs = json.load(os.fdopen(3)) +except ValueError: + sys.stderr.write('Unable to retrieve node attributes\n') + sys.exit(2) +value = attribs.get(sys.argv[1], None) +if value is None or value == '': + sys.exit(1) +if isinstance(value, list): + value = u','.join(value) +if not isinstance(value, str): + value = u'{0}'.format(value) +if sys.version_info[0] < 3: + value = value.encode('utf8') +sys.stdout.write(value + '\n') +EOF +} + function set_confluent_vars() { if [ -z "$nodename" ]; then nodename=$(grep ^NODENAME: /etc/confluent/confluent.info | awk '{print $2}') diff --git a/confluent_osdeploy/genesis/profiles/default/scripts/functions b/confluent_osdeploy/genesis/profiles/default/scripts/functions index 97c3d54d..5c118165 100644 --- a/confluent_osdeploy/genesis/profiles/default/scripts/functions +++ b/confluent_osdeploy/genesis/profiles/default/scripts/functions @@ -12,6 +12,39 @@ function confluentpython() { fi } +# Print a node attribute, e.g. 'confluent_attrib custom.foo'. Returns 1 if unset. +function confluent_attrib() { + if [ -z "$1" ]; then + echo "Usage: confluent_attrib " >&2 + return 2 + fi + local apiclient=/opt/confluent/bin/apiclient + if [ -f /etc/confluent/apiclient ]; then + apiclient=/etc/confluent/apiclient + fi + # program on stdin, data on fd 3: confluentpython splits its args + confluentpython - "$1" 3< <(confluentpython $apiclient /confluent-api/self/myattribs -j) <<'EOF' +import json +import os +import sys +try: + attribs = json.load(os.fdopen(3)) +except ValueError: + sys.stderr.write('Unable to retrieve node attributes\n') + sys.exit(2) +value = attribs.get(sys.argv[1], None) +if value is None or value == '': + sys.exit(1) +if isinstance(value, list): + value = u','.join(value) +if not isinstance(value, str): + value = u'{0}'.format(value) +if sys.version_info[0] < 3: + value = value.encode('utf8') +sys.stdout.write(value + '\n') +EOF +} + function set_confluent_vars() { if [ -z "$nodename" ]; then nodename=$(grep ^NODENAME: /etc/confluent/confluent.info | awk '{print $2}')