Fix broken hosts file updating
authorMartin Schwenke <martin@meltin.net>
Thu, 24 Jul 2014 06:19:02 +0000 (16:19 +1000)
committerMartin Schwenke <martin@meltin.net>
Thu, 24 Jul 2014 06:19:02 +0000 (16:19 +1000)
Replace the hardcoded node name regexp with a generated one.  Detailed
comments are required to describe the construction of the new regexp.

Signed-off-by: Martin Schwenke <martin@meltin.net>
autocluster

index f3f05528b9f6ce6c2c4d5149420346ef7b32990f..94d942e9c3236db2b3a983a10ae394065ff8970d 100755 (executable)
@@ -549,7 +549,24 @@ cluster_update_hosts ()
     [ -n "$hosts_file" ] || hosts_file="tmp/hosts.${CLUSTER}"
     [ -r "$hosts_file" ] || die "Missing hosts file \"${hosts_file}\""
 
-    local pat="# autocluster ${CLUSTER}\$|[[:space:]]${CLUSTER}(n|base)[[:digit:]]+"
+    # Building a general node name regexp is a bit cumbersome.  :-)
+    local name_regexp="("
+    for i in $(set | sed -n -e "s@^\(node_name_format_.*\) ().*@\1@p") ; do
+       # For each node_name_format_* function, get the actual node
+       # name format string
+       fmt=$($i)
+       # fill it with placeholders (remembering that "_" is not valid
+       # in a real cluster name)
+       local t=$(printf "$fmt\n" "_" "0" | head -n 1)
+       # now replace the placeholders with regexps - order is
+       # important here, since the cluster name can contain digits
+       t=$(sed -r -e "s@[[:digit:]]+@[[:digit:]]+@" -e "s@_@${CLUSTER}@" <<<"$t")
+       # add to the regexp
+       name_regexp="${name_regexp}${t}|"
+    done
+    name_regexp="${name_regexp}${CLUSTER}n[[:digit:]]+)"
+
+    local pat="# autocluster ${CLUSTER}\$|[[:space:]]${name_regexp}"
 
     local t="/etc/hosts.${CLUSTER}"
     grep -E "$pat" /etc/hosts >"$t" || true