Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate UTF locales selectable in icingaweb2 #140

Closed
wants to merge 8 commits into from
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ ENV APACHE2_HTTP=REDIRECT \
ICINGA2_FEATURE_DIRECTOR_KICKSTART="true" \
ICINGA2_FEATURE_DIRECTOR_USER="icinga2-director"

ENV BUILD_LOCALES="ar_SA.UTF-8 UTF-8,de_DE.UTF-8 UTF-8,en_US.UTF-8,fi_FI.UTF-8 UTF-8,it_IT.UTF-8 UTF-8,pt_BR.UTF-8 UTF-8,ru_RU.UTF-8 UTF-8"

RUN export DEBIAN_FRONTEND=noninteractive \
&& apt-get update \
&& apt-get upgrade -y \
Expand All @@ -24,6 +26,7 @@ RUN export DEBIAN_FRONTEND=noninteractive \
curl \
dnsutils \
gnupg \
locales \
lsb-release \
mailutils \
mariadb-client \
Expand Down Expand Up @@ -104,6 +107,7 @@ RUN true \
/bin/ping6 \
/usr/lib/nagios/plugins/check_icmp


EXPOSE 80 443 5665

# Initialize and run Supervisor
Expand Down
37 changes: 37 additions & 0 deletions content/opt/setup/30-locales
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

if [[ ! -f /etc/locale.gen.dist ]]
then
cp /etc/locale.gen /etc/locale.gen.dist
fi

echo "BUILD_LOCALES: $BUILD_LOCALES"
typeset -i m=$(echo $BUILD_LOCALES | awk -F ',' '{print NF}')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no guarantee, that your process will spit out a real number. So typesetting it to an integer doesn't give you any guarantee for this. Also quote the commands output, as it could contain special shell chars or just spaces, which get interpreted.

typeset -i n=1
while [[ $n -le $m ]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're iterating here over an index. Well, that's possible, but in bash this is discouraged. The language is not made for this. Use things like an enhanced for loop.

for elem in $(command_which spits out elements); do
   # do some stuff with "${elem}"
done

Rule of thumb: At the point where you have to use real integers either

  • bash isn't the way to go and another language fits much better
  • you misuse bash
  • you know that you misuse bash, but it's the only language available

do
b_locale=$(echo $BUILD_LOCALES | awk -F ',' "{print \$$n}")
cat /etc/locale.gen |grep -q "^$b_locale"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grep -q "^${b_locale}" /etc/locale.gen

if [[ $? -ne 0 ]]
then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if clause actually executes a command and then tests for its exit code. If the exit code is 0 (==true), it'll execute the codeblock the result else not.

So the [[ (or [ depending on your shell) is called the test function, which is actually a just regular command, getting fed with the logical operators and evaluating them. The "matching" ]] isn't actually a bash syntactic token, it's just an argument to the command and this command can verify that it received all arguments.

So, extratesting for $? -ne 0 is superfluous, as you just could use if grep -q "^$b_locale" /etc/locale.gen; then and three lines are shrunken to a single one.

echo "adding locale $b_locale"
cat /etc/locale.gen | sed "s/# $b_locale/$b_locale/" > /tmp/locale.gen
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the locale, I specified is not there in the file, it won't get enabled. On the one hand side, this is great. But KIM, that you could have a typo in the environment variable (e.g.: using en_us.UTF-8 UTF-8) and then the actual locale gets silently dropped.


sed has got a -i switch. And also like grep it takes a filename as the last parameters.

sed -i "s/# $b_locale/$b_locale/" /etc/locale.gen

mv /tmp/locale.gen /etc/locale.gen
fi

n=$n+1
done
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not further tested, but this would basically do the same:

echo "${BUILD_LOCALES}" | while IFS=, read locale charset; do
    sed -i "s/#\\s*${locale}/${locale}/" /etc/locale.gen
done


n=1
while [[ $n -le $m ]]
do
b_locale=$(echo $BUILD_LOCALES | awk -F ',' "{print \$$n}" |tr ' ', '.' |sed 's/\.UTF-8//' |sed 's/UTF-8/UTF8/')
locale -a |grep -q -i "$b_locale"
if [[ $? -ne 0 ]]
then
echo "$b_locale needs build"
locale-gen
break
fi
n=$n+1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

echo "${BUILD_LOCALES}" | while IFS=, read locale charset; do
    if locale -a | grep -qi "^${locale}$"; do
        locale-gen
        break
    fi
done

done
1 change: 1 addition & 0 deletions content/opt/setup/60-icingaweb2
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

. /opt/helpers


CNFSUFFIX=icingaweb2
mysql_cnf_init_section \
"${CNFSUFFIX}" \
Expand Down