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

Proposal: Automatic query servicing for dpkg installation scripts



This is a proposal for a package which can be used by Debian maintainers
to allow interactive queries in their installation scripts to be answered
non-interactively, or to record interactive responses.

This would have to be an essential package and installed with the base
system.

The package would be particularly useful for administrators who wish to
configure a number of machines the same way, but need to install packages
that have interactive installation scripts.

Proposed name: predict

I'm sure there are a lot of problems I haven't thought of; if the whole
thing is impractical, tell me now!  On the other hand, if it can be fully
worked out, all packages should use it to do interactive queries on
installation.

Here is a first draft of an example of use and of the actual script that
would do the work [not tested]:

# This is a fragment of a question file for package mypackage.
# This file would be installed at /etc/predict/mypackage
#
# Format:
#
#  state:question_number:newline_flag:allowed_responses:question=answer
#
# newline_flag:      1 = print a newline after the question
#                    0 = do not print a newline
# allowed_responses: if blank, the response is not checked
#                    if not blank, the whole field is used in a case
#                    statement - a match results in the answer's being
#                    accepted
# No spaces are allowed at the start of a line. Do not use `:' or `=' 
# except as separators (as in the example)


configure:1:0:A|E|a|e:Do you want to use American or European date format? [a|E] =E

# This is a fragment of an installation script
...
# Do we want American or European date format?
# Use predict-ask and predict-answer to get the response:
#   $1 = package name
#   $2 = state
#   $3 = question number
#   $4 = temporary file path
predict-ask mypackage configure 1 $TMPFILE
answer=`predict-answer`
if [ "$answer" = %%DELAY_CONFIGURATION%% ]
then
	echo Package mypackage must be interactively configured later
	exit 1
fi
...
#!/bin/sh

# This script is to be used by installation scripts which want to give
# installers the chance to automate their answers to interactive questions

# It is of little use on a first installation, since the questions would
# not yet be answered; however it can be set to record answers, that can
# then be reused on subsequent installations.  This would be particularly
# useful for those performing a number of identical installations, which
# include packages that have interactive installation scripts.

# If PREDICT_RECORD environment variable is set, answers will be
# recorded.

# If PREDICT_CLEAR is set, questions will always be asked, even if an
# answer is already recorded.

# If PREDICT_DELAY is set, and a question has no stored answer, the
# answer will be set to "%%DELAY_CONFIGURATION%%"; on receiving this,
# the package should abort its installation script

ANSFILE=/var/run/predict/$PACKAGE
PFILE=/etc/predict/$PACKAGE

PROGNAME=`basename $0`
case $PROGNAME in
    predict-ask)
        # Ask the question (if it is not already answered)
        PACKAGE=$1
        STATE=$2
        QNO=$3
        TMPFILE=$4      # optional

        LINE=`eval grep \''^'$STATE:$QNO:'.*'=\' $PFILE || exit 3`
        ANSWER=`echo $LINE | awk -F = '{print $2}'`

        PARAMS=`echo $LINE |
                awk -F = '{print $1}'`
        NEWLINE=`echo $PARAMS |
                awk -F : '{print $3}'`
        ALLOWED=`echo $PARAMS |
                awk -F : '{print $4}'`
        QUESTION=`echo $PARAMS |
                awk -F : '{print $5}'`

        if [ -z "$ANSWER" -o -n "$PREDICT_CLEAR" ]
        then
            if [ -n "$PREDICT_DELAY" ]
            then
                ANSWER="%%DELAY_CONFIGURATION%%"
            else
                if [ "$NEWLINE" != 1 ]
                then
                    echoopt='-n'
                fi

                bad_response=true
                while [ -n "$bad_response" ]
                do
                    echo $echoopt $QUESTION
                    read ANSWER
                    if [ -n "$ALLOWED" ]
                    then
                        case $ANSWER in
                            $ALLOWED)
                                unset bad_response
                                ;;
                            *)
                                bad_response=true
                                echo '\007'
                                ;;
                        esac
                    else
                        unset bad_response
                    fi
                done

                if [ -n "$PREDICT_RECORD" ]
                then
                    unset must_delete_tmp
                    if [ -z "$TMPFILE" ]
                    then
                        TMPFILE=`mktemp /tmp/predictXXXXX`
                        must_delete_tmp=true
                    fi
                    eval sed -e \'/'^'$STATE:$QNO:/s/='.*$'/=$ANSWER/\' <$PFILE >$TMPFILE
                    cp $TMPFILE $PFILE

                    if [ -n "$must_delete_tmp" ]
                    then
                        rm -f $TMPFILE
                    fi
                fi
            fi
        fi
        echo $ANSWER > $ANSFILE
        ;;
    predict-answer)
        # Give the answer to the last question
        if [ ! -f $ANSFILE ]
        then
            exit 1
        else
            cat $ANSFILE
        fi
        ;;
    *)
        echo "$0: script called with unknown name"
        exit 2
        ;;
esac
Oliver Elphick                                Oliver.Elphick@lfix.co.uk
Isle of Wight                              http://www.lfix.co.uk/oliver
               PGP key from public servers; key ID 32B8FAA1
                 ========================================
     "For to me to live is Christ, and to die is gain."     
                   Philippians 1:21 

Reply to: