Automating Installation of Debian Packages

When installing packages, you may notice that you often get prompts similar to the one shown below:

These are useful for grabbing details from the user where necessary, but are annoying when trying to automate the installation of many packages, or when one needs to ensure the same details are used across multiple systems and the user does not make a mistake.

Luckily, one can preconfigure the answers to the prompts through the use of debconf. For example, if one wants to automatically install mysql server on debian without seeing any pompts, you simply run the following commands before running

sudo apt-get install mysql-server

sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password $DATABASE_PASS"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $DATABASE_PASS"

However, how does one know what to pass to debconf-set-selections? Here is the script that I use. Make sure to update the PACKAGE_NAME variable

PACKAGE_NAME=""

cd /tmp
aptitude download $PACKAGE_NAME
mv $PACKAGE_NAME* $PACKAGE_NAME.deb

mkdir control_files
dpkg-deb -e $PACKAGE_NAME.deb control_files/

grep -e '^Template:' -e '^Type:' -e '^Default:' \
 control_files/templates | xargs | sed \
 -e 's/\s*Template: /\npackagename\t/g' \
 -e 's/\s*Type: */\t/g' \
 -e 's/\s*Default: */\t/g'

# cleanup
rm -rf control_files
rm $PACKAGE_NAME.deb
Example output:
packagename     glance/register-endpoint        boolean false
packagename     glance/keystone-ip      string
packagename     glance/keystone-auth-token      password
packagename     glance/endpoint-ip      string
packagename     glance/region-name      string  regionOne

Clearing Settings

If you ever remove a package, the debconf settings will still be there. This means that if you were to reinstall something like MySQL, it would re-use the original password settings rather than ask you for new ones!


To clear the debconf settings for a package, run the following command.

echo PURGE | sudo debconf-communicate $PACKAGE_NAME
To show any debconf settings for a package, use:
sudo debconf-show $PACKAGE_NAME

References

No comments:

Post a Comment