From da4c31554ca322a4e0ee59568f86b06b4bb7efc8 Mon Sep 17 00:00:00 2001 From: Makarov Andrey Date: Tue, 15 Nov 2022 15:49:01 +0300 Subject: [PATCH] Update hbase-site.xml properties If we stop HBase container and start it again, duplicate entries are appended to hbase-site.xml. This change allows to overwrite existing values. --- base/entrypoint.sh | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/base/entrypoint.sh b/base/entrypoint.sh index 8a6f42f..82a71ba 100644 --- a/base/entrypoint.sh +++ b/base/entrypoint.sh @@ -1,13 +1,24 @@ #!/bin/bash -function addProperty() { +function setProperty() { + # Sets/unsets property value in an XML configuration file + # Usage: + # * Append/update: `setProperty filePath propName propValue` + # * Delete: `setProperty filePath propName` local path=$1 local name=$2 local value=$3 - local entry="$name${value}" - local escapedEntry=$(echo $entry | sed 's/\//\\\//g') - sed -i "/<\/configuration>/ s/.*/${escapedEntry}\n&/" $path + local wrappedName="$name" + local escapedName=${wrappedName//\//\\/} # https://stackoverflow.com/a/27788661 + if [ -z "$value" ]; then + sed -i "/${escapedName}/d" $path + else + local entry="$wrappedName$value" + local escapedEntry=${entry//\//\\/} + # https://superuser.com/a/590666 + grep -q "${wrappedName}" $path && sed -i "/${escapedName}/c ${escapedEntry}" $path || sed -i "/<\/configuration>/ s/.*/${escapedEntry}\n&/" $path + fi } function configure() { @@ -24,7 +35,7 @@ function configure() { var="${envPrefix}_${c}" value=${!var} echo " - Setting $name=$value" - addProperty /etc/hbase/$module-site.xml $name "$value" + setProperty /etc/hbase/$module-site.xml $name "$value" done }