ctdb-scripts: Add function ctdb_get_db_options()
authorMartin Schwenke <martin@meltin.net>
Fri, 20 Apr 2018 02:12:44 +0000 (12:12 +1000)
committerAmitay Isaacs <amitay@samba.org>
Thu, 17 May 2018 02:04:31 +0000 (04:04 +0200)
This pulls database options from the configuration file, caches then
and makes the values available in scripts.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/config/functions

index f3d7c163ec4f92fae079e5bea5bb5960a19cbb74..649b367d4cb974f875af75f2f8a690ee4f28f95a 100755 (executable)
@@ -244,6 +244,43 @@ ctdb_get_ip_address ()
     cat "$_ip_addr_file"
 }
 
+# Cached retrieval of database options for use by event scripts.
+#
+# If the variables are already set then they should not be overwritten
+# - this should only happen during event script testing.
+ctdb_get_db_options ()
+{
+       _db_opts_file="${CTDB_SCRIPT_VARDIR}/db_options.cache"
+
+       if [ ! -f "$_db_opts_file" ] ; then
+               {
+                       ctdb_translate_option "database" \
+                                             "volatile database directory" \
+                                             "CTDB_DBDIR"
+                       ctdb_translate_option "database" \
+                                             "persistent database directory" \
+                                             "CTDB_DBDIR_PERSISTENT"
+                       ctdb_translate_option "database" \
+                                             "state database directory" \
+                                             "CTDB_DBDIR_STATE"
+               } >"$_db_opts_file"
+       fi
+
+       . "$_db_opts_file"
+}
+
+ctdb_translate_option ()
+{
+       _section="$1"
+       _opt="$2"
+       _variable="$3"
+
+       # ctdb-config already prints an error if something goes wrong
+       _t=$("${CTDB_HELPER_BINDIR}/ctdb-config" get "$_section" "$_opt") || \
+               exit $?
+       echo "${_variable}=\"${_t}\""
+}
+
 ######################################################
 # wrapper around /proc/ settings to allow them to be hooked
 # for testing