From 279744cf6e4f6096507953f1c1f956e0b9ed8532 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 13 Nov 2016 18:16:42 +0100 Subject: Adding debconf support for image selection in curl container create script (FIXME). Signed-off-by: Daniel Baumann --- share/scripts/curl | 70 +++++++++++++++++++++++- share/scripts/curl.d/0001-images | 85 ++++++++++++++++++++++++++++++ share/scripts/curl.d/0001-images.templates | 9 ++++ 3 files changed, 163 insertions(+), 1 deletion(-) create mode 100755 share/scripts/curl.d/0001-images create mode 100644 share/scripts/curl.d/0001-images.templates (limited to 'share/scripts') diff --git a/share/scripts/curl b/share/scripts/curl index 49d7f71..a7d506c 100755 --- a/share/scripts/curl +++ b/share/scripts/curl @@ -100,6 +100,47 @@ Usage () exit 1 } +Debconf () +{ + # Configure local debconf + mkdir -p /tmp/container-tools + DEBCONF_TMPDIR="$(mktemp -d -p /tmp/container-tools -t $(basename ${0}).XXXX)" + export DEBCONF_TMPDIR + + mkdir -p "${DEBCONF_TMPDIR}/debconf" + +cat > "${DEBCONF_TMPDIR}/debconf.systemrc" << EOF +Config: configdb +Templates: templatedb + +Name: config +Driver: File +Mode: 644 +Reject-Type: password +Filename: ${DEBCONF_TMPDIR}/debconf/config.dat + +Name: passwords +Driver: File +Mode: 600 +Backup: false +Required: false +Accept-Type: password +Filename: ${DEBCONF_TMPDIR}/debconf/passwords.dat + +Name: configdb +Driver: Stack +Stack: config, passwords + +Name: templatedb +Driver: File +Mode: 644 +Filename: ${DEBCONF_TMPDIR}/debconf/templates.dat +EOF + + DEBCONF_SYSTEMRC="${DEBCONF_TMPDIR}/debconf.systemrc" + export DEBCONF_SYSTEMRC +} + Parameters "${@}" if [ -z "${NAME}" ] @@ -126,13 +167,36 @@ then fi ARCHITECTURE="${ARCHITECTURE:-$(dpkg --print-architecture)}" -IMAGE="${IMAGE:-debian-stretch-${ARCHITECTURE}.tar.xz}" SERVER="${SERVER:-https://files.open-infrastructure.net/images/container-tools/current}" PASSWORD="${PASSWORD:-$(dd if=/dev/urandom bs=12 count=1 2> /dev/null | base64)}" VERSION="$(container version)" # Run +if [ -z "${IMAGE}" ] +then + Debconf + + # Run debconf parts + for SCRIPT in /usr/share/container-tools/scripts/curl.d/* + do + if [ -x "${SCRIPT}" ] + then + # FIXME + # debconf -ocontainer-tools "${SCRIPT}" + "${SCRIPT}" + fi + done + + # Read-in configuration from debconf + . "${DEBCONF_TMPDIR}/debconf.default" +fi + +if [ -z "${IMAGE}" ] +then + echo "no image selected, doing nothing." >&2 + exit 1 +fi # FIXME: Show available image directories on server # FIXME: Fetch debconf-choices.txt to show debconf select @@ -202,3 +266,7 @@ case "${CLEAN}" in rm -f "${CACHE}/${IMAGE}" ;; esac + +# remove debconf temporary files +rm --preserve-root --one-file-system -rf "${DEBCONF_TMPDIR}" +rmdir --ignore-fail-on-non-empty /tmp/container-tools 2>&1 || true diff --git a/share/scripts/curl.d/0001-images b/share/scripts/curl.d/0001-images new file mode 100755 index 0000000..b350634 --- /dev/null +++ b/share/scripts/curl.d/0001-images @@ -0,0 +1,85 @@ +#!/bin/sh + +# container-tools - Manage systemd-nspawn containers +# Copyright (C) 2014-2016 Daniel Baumann +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +set -e + +CONFIG="/etc/container-tools/debconf" + +DEBCONF_NOWARNINGS="true" +export DEBCONF_NOWARNINGS + +. /usr/share/debconf/confmodule + +curl + +if [ -n "${PRESEED_FILE}" ] +then + # user specified one or more preseed files through commandline option + db_set cnt-debconf/preseed-file "${PRESEED_FILE}" + db_fset cnt-debconf/preseed-file seen true +elif ls "${CONFIG}"/*.cfg > /dev/null 2>&1 +then + # user has not specified preseed files through commandline option, + # showing debconf selection dialog for global preseed file. + + FILES="$(cd ${CONFIG} && find . -type f -name '*.cfg' -printf '%P\n' | sort)" + PRESEED_FILES="$(for FILE in ${FILES}; do echo -n "${FILE}, "; done | sed -e 's|, $||')" + + if [ -n "${PRESEED_FILES}" ] + then + db_subst cnt-debconf/preseed-files CHOICES "none, custom, , ${PRESEED_FILES}" + + db_settitle cnt-debconf/title + db_input high cnt-debconf/preseed-files || true + db_go + + db_get cnt-debconf/preseed-files + PRESEED_FILE="${RET}" # select + + case "${PRESEED_FILE}" in + none|custom) + ;; + + *) + # user specified preseed file through debconf select + db_set cnt-debconf/preseed-file "${CONFIG}/${PRESEED_FILE}" + db_fset cnt-debconf/preseed-file seen true + ;; + esac + fi +fi + +case "${PRESEED_FILE}" in + none) + ;; + + *) + # ask user for a preseed file + db_settitle cnt-debconf/title + db_input high cnt-debconf/preseed-file || true + db_go + + db_get cnt-debconf/preseed-file + PRESEED_FILE="${RET}" # string (w/ empty) + + echo "PRESEED_FILE=\"${PRESEED_FILE}\"" >> "${DEBCONF_TMPDIR}/debconf.default" + export PRESEED_FILE + ;; +esac + +db_stop diff --git a/share/scripts/curl.d/0001-images.templates b/share/scripts/curl.d/0001-images.templates new file mode 100644 index 0000000..0e899b7 --- /dev/null +++ b/share/scripts/curl.d/0001-images.templates @@ -0,0 +1,9 @@ +Template: cnt-curl/title +Type: title +Description: container-tools + +Template: cnt-curl/images +Type: select +Default: none +Choices: ${CHOICES} +Description: Choose a container image. -- cgit v1.2.3