[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: debconf question



On 12/17/2010 04:27 PM, Picca Frédéric-Emmanuel wrote:
> now my problem is when I generate the configure file from this db entry in the postinst script.
> Here the code :
>
>    1 #!/bin/sh
>    2 CONFIGFILE=/etc/tangorc
>    3 set -e
>    4 . /usr/share/debconf/confmodule
>    5 
>    6 # Generate config file, if it doesn't exist.
>    7 # An alternative is to copy in a template
>    8 # file from elsewhere.
>    9 if [ ! -e $CONFIGFILE ]; then
>   10     echo "# Config file for my package" > $CONFIGFILE
>   11     echo "TANGO_HOST=" >> $CONFIGFILE
>   12 fi
>   13 
>   14 # Substitute in the values from the debconf db.
>   15 # There are obvious optimizations possible here.
>   16 # The cp before the sed ensures we do not mess up
>   17 # the config file's ownership and permissions.
>   18 db_get tango-common/tango-host
>   19 TANGO_HOST=$RET
>   20 cp -a -f $CONFIGFILE $CONFIGFILE.tmp
>   21 
>   22 # If the admin deleted or commented some variables but then set
>   23 # them via debconf, (re-)add them to the conffile.
>   24 test -z "$TANGO_HOST" || grep -Eq '^ *TANGO_HOST=' $CONFIGFILE || \
>   25     echo "TANGO_HOST=" >> $CONFIGFILE
>   26 
>   27 sed -e "s/^ *TANGO_HOST=.*/TANGO_HOST=$TANGO_HOST/" \
>   28     < $CONFIGFILE > $CONFIGFILE.tmp
>   29 mv -f $CONFIGFILE.tmp $CONFIGFILE
>   30 
>   31 #DEBHELPER#
>
> at the end of this script the right configure file should be created, but
> in fact the created file contain the default value `hostname -f`:10000
> instead of host:10001 as enter by the user.
>
> more strange, If I reconfigure the tango-common package, the the entered value
> generate the right config file.
>
> so it seems that there is a problem only during the very first configuration.
>
> I do not understand what is the problem. So I would like some fresh eyes, to help
> me understand this problem
>
> thanks for your attention
>
> Frederic
>
> other related question what should I do to register properly the /etc/tangorc file as a configuration file
> with ucf. I am not sure I did the thing correctly in the package.
>
> [1] http://git.debian.org/?p=debian-science/packages/tango.git;a=summary
>   
If there's only host and port in your config file, don't try
to re-read it in your postinst. What you have done in your
config script should be enough to read the value that was
previously in the config file, or the one that was set using
debconf (with priority to what has been change in debconf,
which is what you've done already).

So instead of the above postinst, just overwrite the config
file this way:

debian/config:
=========
#!/bin/sh

set -e

CONFIGFILE=/etc/tangorc

. /usr/share/debconf/confmodule

# Use what may have been set durring a pre-seed
# operation, so we don't overwrite something that
# may have been previously set.
RET=""
db_get tango-common/tango-host || true
if [ -n "${RET}" ] ; then
	TANGO_HOST=$RET
fi

# Load config file, if it exists.
# This should populate $TANGO_HOST
# if the variable is set in the
# configuration file
if [ -f $CONFIGFILE ]; then
	. $CONFIGFILE || true
fi


# Set $TANGO_HOST with a value if
# we don't find any so far
if [ -z "$TANGO_HOST" ] ; then
	TANGO_HOST=$(hostname -f)":10000"
fi

# Set the value with something, always,
# because at this point, we really should
# have a value in $TANGO_HOST
db_set tango-common/tango-host $TANGO_HOST

# Now that we have either the default value
# or what was set in the config file, let's
# tell Debconf that we want it to prompt the
# user for a change.
db_input high tango-common/tango-host || true
db_go || true


debian/postinst:
================
#!/bin/sh
CONFIGFILE=/etc/tangorc
set -e
. /usr/share/debconf/confmodule

# As we have always set a value in debconf,
# even in non-interactive mode we will have
# somethinga to use there
db_get tango-common/tango-host
TANGO_HOST=$RET

# And as a consequence, we can create a brand
# new config file (provided that there's no
# other things in this config file because in
# that case, we want to make some code to not
# overwrite them...).
echo "# Config file for my package" > $CONFIGFILE
echo "TANGO_HOST=" >> $CONFIGFILE


I hope the above helps. As you see, it's a way
more simple than what you tried to do. I have
to highlight that this is not always the solution,
there might be some other ways, but the above
does worked for me in some of my packages, and
it respects the policy (which is: always read
what is in the config file and keep what the
admin may have change, works also in non-interactive,
provide working defaults).

I'd appreciate another DD comment on this.

Thomas Goirand



Reply to: