2
0
mirror of https://github.com/xcat2/confluent.git synced 2026-11-19 08:52:20 +00:00

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 <name>'.
This commit is contained in:
Markus Hilger
2026-11-11 23:02:02 +01:00
parent c949359199
commit 9d8cb97fb3
2 changed files with 66 additions and 0 deletions
@@ -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 <attribute>" >&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}')
@@ -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 <attribute>" >&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}')