[squid-users] Howto make Squid config dependent on hostname?

Alex Rousskov rousskov at measurement-factory.com
Fri Sep 16 18:03:19 UTC 2022


On 9/16/22 10:22, Hildegard Meier wrote:

> I dnot know what "common #line notion" should be (searching on internet matches only the squid man page)

It is a poorly worded reference to "#line" directives used by some 
programming languages for documenting the original location of lines in 
preprocessed files. Here are the corresponding C++ preprocessor docs, 
for example: https://en.cppreference.com/w/cpp/preprocessor/line

Your template preprocessing script may (but does not have to) insert 
#line directives so that Squid can attribute configuration lines to the 
original configuration template rather than its processed result.


> /usr/sbin/squid -k parse -f '|/usr/local/script/squid/pre_process_squid_config_file.sh'
> 
> works, but
> 
> /usr/sbin/squid -k reconfigure -f '|/usr/local/script/squid/pre_process_squid_config_file.sh'
> 
> gives the following line in /var/log/squid/cache.log

> 2022/09/16 16:20:55 kid1| storeDirWriteCleanLogs: Starting...
> 2022/09/16 16:20:55 kid1|   Finished.  Wrote 0 entries.
> 2022/09/16 16:20:55 kid1|   Took 0.00 seconds (  0.00 entries/sec).
> FATAL: parseConfigFile: '|/usr/local/script/squid/pre_process_squid_config_file.sh' failed with exit code -1

 > is that a bug?

I am not sure. I cannot reproduce this problem with a modern/supported 
Squid version, but my test environment may be too different from yours. 
If you are willing to try this with Squid v5, I can help with triaging 
this further, but it may take a few iterations to get to the bottom of 
this (e.g., using "bash -x" and adding some no-output command at the 
very end of the script might provide more clues). Others on this mailing 
list may have better ideas/suspects.


> /usr/local/script/squid/pre_process_squid_config_file.sh
> 
> #!/bin/bash
> 
> readonly SQUID_CONFIG_TEMPLATE_FILE='/etc/squid/squid.conf.TEMPLATE'
> 
> case "${HOSTNAME}" in
>     'node1')  readonly HOSTNAME_PEER='node2' ;;
>     'node2')  readonly HOSTNAME_PEER='node1' ;;
>     *)
>         echo "invalid hostname in script ${0}. Exit"
>         exit 1
>         ;;
> esac
> 
> sed \
> -e "s@{HOSTNAME}@${HOSTNAME}@g" \
> -e "s@{HOSTNAME_PEER}@${HOSTNAME_PEER}@g" \
> "${SQUID_CONFIG_TEMPLATE_FILE}"
> --------------------


FWIW, the "invalid hostname" error should be reported to stderr rather 
than stdout. Otherwise, the error text will be interpreted as Squid 
configuration file contents, producing a somewhat confusing output:

> 2022/09/16 13:49:27| Processing Configuration File: |/tmp/t.sh (depth 0)

> 2022/09/16 13:49:27| /tmp/t.sh(1): unrecognized: 'invalid'



HTH,

Alex.


More information about the squid-users mailing list