[Debconf-devel] Bug#690390: debconf: Use config file defaults instead of cache

Bas Wijnen wijnen at debian.org
Sun Oct 14 10:16:35 UTC 2012


First of all, I'm happy that we agree on the way things are supposed to
work. However, I think you slightly misunderstood my proposal.

On Sat, Oct 13, 2012 at 01:27:31PM -0400, Joey Hess wrote:
> Bas Wijnen wrote:
> > The proposed solution is to add functionality to debconf, which can read
> > and edit config files.
> 
> debconf already contains this functionality. It's called a config
> script.

That's not exactly what I meant. The config script should be used to
read the config file, set up defaults, and ask questions (or not) using
those defaults. Many packages have simple config files (ini files or
simpler).

If I understand you correctly, you are saying that each of those
packages should implement parsing code for it in its config script. My
point is that this results in needless code duplication. Therefore I
would like to move this parsing code to debconf, so most packages can
simply use a function call. Packages which have more complex config
files must still implement parsing code for it themselves.

As an example, I have quoted the two functions that I use in
pioneers-meta-server (one in the config script, the other in the
postinst). I think it would benefit many packages if functions like
these (but perhaps a bit more eleborate, to support slightly more
complex files) would be in debconf.

> Packages that do not use the config script to read their config files
> before using debconf to ask questions are buggy, yes.

Good to hear that we agree on that. :-)

Thanks,
Bas


----------- Function from config, with usage example ------------
# This function will read variable $1 and set it as default to debconf
# question $2.  If the variable $1 is unset or empty, the template's default
# will be used.
conffile_parse ()
{
	variable="$1"
	question="$2"
	# Read the default from the config file.
	default="`eval 'echo $'"$variable"`"

	# Correct boolean values so debconf understands them.
	if [ "$default" ] ; then
		db_metaget "$question" type
		if [ "$RET" = boolean ] ; then
			case "$default" in
			yes|Yes|YES|true|True|TRUE|1)
				default=true
				;;
			no|No|NO|false|False|FALSE|0)
				default=false
				;;
			*)
				echo "Warning: value '$default' of variable '$variable' not a boolean: ignored."
				default=
				;;
			esac
		fi
	fi

	# If we still have a default to set, do so.
	if [ "$default" ] ; then
		db_set "$question" "$default"
	fi
}

# Parse values from the config file and use them as defaults.
conffile_parse PORT_RANGE pioneers-meta-server/ports
conffile_parse META_SERVER_NAME pioneers-meta-server/name
conffile_parse META_SERVER_ARGS pioneers-meta-server/arguments


----------- Function from postinst, with usage example ------------
# This function will change variable $1 to the result of debconf question $2.
# If it already had the right setting, the file is not touched.
conffile_edit ()
{
	variable="$1"
	question="$2"
	# Get the answer to the debconf question.
	db_get "$question" || return 0
	# Do nothing if the value hasn't changed.
	test "$RET" != "`eval 'echo $'"$variable"`" || return 0

	if grep -q "^$variable=" "$CONFIG_FILE" ; then
		# Edit the conffile to contain the new setting.
		sed -i "s/^\($variable=\).*\$/\\1'$RET'/" "$CONFIG_FILE"
	else
		# Or if the setting isn't in the conffile yet, add it.
		echo "$variable='$RET'" >> "$CONFIG_FILE"
	fi
	return 0
}

conffile_edit PORT_RANGE pioneers-meta-server/ports
conffile_edit META_SERVER_NAME pioneers-meta-server/name
conffile_edit META_SERVER_ARGS pioneers-meta-server/arguments
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://lists.alioth.debian.org/pipermail/debconf-devel/attachments/20121014/c0a8bb46/attachment.pgp>


More information about the Debconf-devel mailing list