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

Conversation

skm42
Copy link

@skm42 skm42 commented Mar 15, 2018

@jjethwa
Copy link
Owner

jjethwa commented Mar 16, 2018

Hi @skm42

Thanks so much for the PR! I think it might be better to move this step to the container run script to reduce layer size? I guess we need to see how it interacts with: #141 as well

@skm42
Copy link
Author

skm42 commented Mar 16, 2018

trade-off between image size and container initialization time, I don't mind. like this? skm42@5700e23

Concerning #141 I took a look into icinga2 standalone vagrant box https://github.com/Icinga/icinga-vagrant. It has all locales generated, utf and iso ones. Should be save to follow like this, what's your view? skm42@aaeeebb

@bebehei
Copy link
Contributor

bebehei commented Mar 17, 2018

Thanks so much for the PR! I think it might be better to move this step to the container run script to reduce layer size?

Sorry, but this is pointless. It's absolutely static and not even parametrized. You buy this with container boottime, which is quite valuable on container reboot.

The locales package is ~15 MBs big and all generated locales are about 6MBs in size. Compared to an image, which is about 600MBs big ....


@skm42 Please put the stuff back again into the Dockerfile. But do you see the ARGs in the file?

The stuff you want to add here is currently completely static and has no parameters. Please pack it above the ARGs, as changing ARGs are invalidating the cache and require this layer to get rebuilt, albeit nothing will be different in this layer.

Please squash the commits, too and have a look at the en_US locale it looks like a typo as it doesn't include the actual the charset.


Concerning #141 I took a look into icinga2 standalone vagrant box https://github.com/Icinga/icinga-vagrant. It has all locales generated, utf and iso ones.

Could you please provide a direct link for this? I'm not able to find the part you may refer to.

@skm42
Copy link
Author

skm42 commented Mar 18, 2018

Please see latest commit.
Keep locales to build configurable by ENV: BUILD_LOCALE="locale 1,locale 2"
Locale has to be defined following /etc/locale.gen
What needs to be improved is how to identify whether a locale is already build.

root@icinga2:/# locale -a |grep -i en_us.utf
en_US.utf8
root@icinga2:/# cat /etc/locale.gen |grep -i en_us.utf
en_US.UTF-8 UTF-8

Tested for utf 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, others not yet.

Copy link
Contributor

@bebehei bebehei left a comment

Choose a reason for hiding this comment

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

Sorry, I'm laughing about your code. It looks like a you're a proper C programmer, miss all C idioms and want to implement them in bash 😂

Your code looks like a poem written in swollen language, which tries to express the meaning of a simple sentence.

But in contrast to a poem, you have to write down the simple sentence here. People need to understand you and not have to interpret your peom, what it could mean.

It seems that you had a pretty hard job in programming bash/shell. I guess, I can help you, by commenting your code and showing you, what could get improved.

I'm going to reject this PR, as I've found a welldoing solution.

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.

while [[ $n -le $m ]]
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

echo "BUILD_LOCALES: $BUILD_LOCALES"
typeset -i m=$(echo $BUILD_LOCALES | awk -F ',' '{print NF}')
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

b_locale=$(echo $BUILD_LOCALES | awk -F ',' "{print \$$n}")
cat /etc/locale.gen |grep -q "^$b_locale"
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.

if [[ $? -ne 0 ]]
then
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

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

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

@skm42
Copy link
Author

skm42 commented Mar 18, 2018

fine for me. Going to review your comments for learning better bash and maybe come up with a new PR at a later time.

@skm42 skm42 closed this Mar 18, 2018
@skm42 skm42 deleted the locale-proposal branch March 18, 2018 14:33
@bebehei
Copy link
Contributor

bebehei commented Mar 18, 2018

@skm42 Thanks for firing up this PR. You forced me to finally fix it. #142

@jjethwa
Copy link
Owner

jjethwa commented Mar 19, 2018

Thanks @bebehei and @skm42 Looks like it was a good brainstorming session while I was away 😛

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants