From c58d582ffb8c03f67b6799a9783fe8061b404d5e Mon Sep 17 00:00:00 2001 From: Martin Schwenke Date: Mon, 17 Aug 2015 13:39:10 +1000 Subject: [PATCH] ctdb-scripts: Factor out possible creation of rt_tables file Signed-off-by: Martin Schwenke Reviewed-by: Amitay Isaacs Reviewed-by: Jose A. Rivera Reviewed-by: Michael Adam --- ctdb/config/events.d/13.per_ip_routing | 44 ++++++++++++++------------ 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/ctdb/config/events.d/13.per_ip_routing b/ctdb/config/events.d/13.per_ip_routing index cd0020e559e..60c1dd08edd 100755 --- a/ctdb/config/events.d/13.per_ip_routing +++ b/ctdb/config/events.d/13.per_ip_routing @@ -93,6 +93,19 @@ ipv4_host_addr_to_net () ###################################################################### +ensure_rt_tables () +{ + rt_tables="$CTDB_ETCDIR/iproute2/rt_tables" + + # This file should always exist. Even if this didn't exist on the + # system, adding a route will have created it. What if we startup + # and immediately shutdown? Let's be sure. + if [ ! -f "$rt_tables" ] ; then + mkdir -p "${rt_tables%/*}" # dirname + touch "$rt_tables" + fi +} + # Setup a table id to use for the given IP. We don't need to know it, # it just needs to exist in /etc/iproute2/rt_tables. Fail if no free # table id could be found in the configured range. @@ -100,12 +113,7 @@ ensure_table_id_for_ip () { _ip=$1 - _f="$CTDB_ETCDIR/iproute2/rt_tables" - # This file should always exist, but... - if [ ! -f "$_f" ] ; then - mkdir -p $(dirname "$_f") - touch "$_f" - fi + ensure_rt_tables # Maintain a table id for each IP address we've ever seen in # rt_tables. We use a "ctdb." prefix on the label. @@ -117,7 +125,7 @@ ensure_table_id_for_ip () ( # Note that die() just gets us out of the subshell... flock --timeout 30 0 || \ - die "ensure_table_id_for_ip: failed to lock file $_f" + die "ensure_table_id_for_ip: failed to lock file $rt_tables" _new=$CTDB_PER_IP_ROUTING_TABLE_ID_LOW while read _t _l ; do @@ -140,44 +148,38 @@ ensure_table_id_for_ip () # If the new table id is legal then add it to the file and # print it. if [ $_new -le $CTDB_PER_IP_ROUTING_TABLE_ID_HIGH ] ; then - printf "%d\t%s\n" "$_new" "$_label" >>"$_f" + printf "%d\t%s\n" "$_new" "$_label" >>"$rt_tables" return 0 else return 1 fi - ) <"$_f" + ) <"$rt_tables" } # Clean up all the table ids that we might own. clean_up_table_ids () { - _f="$CTDB_ETCDIR/iproute2/rt_tables" - # Even if this didn't exist on the system, adding a route will - # have created it. What if we startup and immediately shutdown? - if [ ! -f "$_f" ] ; then - mkdir -p $(dirname "$_f") - touch "$_f" - fi + ensure_rt_tables ( # Note that die() just gets us out of the subshell... flock --timeout 30 0 || \ - die "clean_up_table_ids: failed to lock file $_f" + die "clean_up_table_ids: failed to lock file $rt_tables" # Delete any items from the file that have a table id in our # range or a label matching our label. Preserve comments. - _tmp="${_f}.$$.ctdb" + _tmp="${rt_tables}.$$.ctdb" awk -v min="$CTDB_PER_IP_ROUTING_TABLE_ID_LOW" \ -v max="$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH" \ -v pre="$table_id_prefix" \ '/^#/ || \ !(min <= $1 && $1 <= max) && \ !(index($2, pre) == 1) \ - { print $0 }' "$_f" >"$_tmp" + { print $0 }' "$rt_tables" >"$_tmp" - mv "$_tmp" "$_f" + mv "$_tmp" "$rt_tables" # The lock is gone - don't do anything else here - ) <"$_f" + ) <"$rt_tables" } ###################################################################### -- 2.34.1