--- /dev/null
+#!/bin/bash
+
+ROOT_PATH="/home/chroots"
+UNION_OVERLAY_DIR="/var/tmpfs"
+DISTROS=
+SUDO=
+
+VERSION="0.3.0"
+
+DEBIAN_DISTROS="wheezy jessie stretch"
+UBUNTU_DISTROS="trusty vivid wily xenial"
+
+DEBIAN_MIRROR="http://ftp-stud.hs-esslingen.de/debian"
+UBUNTU_MIRROR="http://de.archive.ubuntu.com/ubuntu"
+
+#------------------------------------------------------------------------------
+debug="n"
+verbose="n"
+use_color=false
+
+RED=""
+YELLOW=""
+GREEN=""
+BLUE=""
+NORMAL=""
+
+MY_BASE=$( basename $0 )
+MY_DIR=$( dirname $0 )
+
+#------------------------------------------------------------------------------
+RM() {
+ if [[ "${verbose}" = "y" ]] ; then
+ ${SUDO} rm -v "$@"
+ else
+ ${SUDO} rm "$@"
+ fi
+}
+
+#------------------------------------------------------------------------------
+MKDIR() {
+ if [[ "${verbose}" = "y" ]] ; then
+ ${SUDO} mkdir -v "$@"
+ else
+ ${SUDO} mkdir "$@"
+ fi
+}
+
+#------------------------------------------------------------------------------
+set_colors() {
+
+ local safe_term=${TERM//[^[:alnum:]]/?} # sanitize TERM
+ local match_lhs=""
+ use_color=false
+ [[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
+ [[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
+ [[ -z ${match_lhs} ]] \
+ && type -P dircolors >/dev/null \
+ && match_lhs=$(dircolors --print-database)
+ [[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color=true
+
+ # console colors:
+ if ${use_color}; then
+ RED="\033[38;5;196m"
+ YELLOW="\033[38;5;226m"
+ GREEN="\033[38;5;46m"
+ BLUE="\033[38;5;27m"
+ NORMAL="\033[39m"
+ fi
+
+}
+
+CHROOT_INI_DEBIAN=$( cat <<-END
+ [{{chroot}}]
+ type=directory
+ description={{vendor}} {{distro}} amd64 autobuilder
+ directory={{path}}
+ groups=root,sbuild
+ root-groups=root,sbuild
+ profile=sbuild
+ source-groups=root,sbuild
+ source-root-groups=root,sbuild
+ union-type=aufs
+ union-overlay-directory=${UNION_OVERLAY_DIR}
+ END
+)
+
+CHROOT_INI_UBUNTU=$( cat <<-END
+ [{{chroot}}]
+ type=directory
+ description={{vendor}} {{distro}} amd64 autobuilder
+ directory={{path}}
+ groups=root,sbuild
+ root-groups=root,sbuild
+ profile=sbuild
+ source-groups=root,sbuild
+ source-root-groups=root,sbuild
+ union-type=overlayfs
+ union-overlay-directory=${UNION_OVERLAY_DIR}
+ END
+)
+
+SRC_LIST_DEBIAN=$( cat <<-END
+ deb http://repo.pb.local/debian {{distro}} main non-free contrib
+ deb http://repo.pb.local/debian {{distro}}-updates main non-free contrib
+ deb http://repo.pb.local/debian-security {{distro}}/updates main non-free contrib
+ END
+)
+
+PB_SRC_LIST_DEBIAN=$( cat <<-END
+ deb http://repo.pb.local/pb-debian {{distro}} main non-free contrib
+ deb http://repo.pb.local/pb-debian {{distro}}-proposed main non-free contrib
+ END
+)
+
+PB_SRC_LIST_UBUNTU=$( cat <<-END
+ deb http://repo.pb.local/pb-ubuntu {{distro}} main restricted
+ deb http://repo.pb.local/pb-ubuntu {{distro}}-proposed main restricted
+ END
+)
+
+PROXY_FILE_CONTENT=$( cat <<-END
+ Acquire {
+ http {
+ Proxy "http://proxy:3128/";
+ Proxy::repo.pb.local "DIRECT";
+ };
+ };
+ END
+)
+
+#------------------------------------------------------------------------------
+usage() {
+ cat <<EOF
+Usage: ${MY_BASE} [-d] [-v] [-R PATH] [DISTRO ...]
+ ${MY_BASE} [-h|--help]
+ ${MY_BASE} [-V|--version]
+
+ Generates a new schroot images
+
+ Options:
+ -R|--root PATH Root path of generated images (default: '${ROOT_PATH}')
+ -d|--debug Debug output (bash -x)
+ -v|--verbose Set verbosity on.
+ -h|--help Show this output and exit
+ -V|--version Prints out version number of the script and exit
+EOF
+}
+
+
+#------------------------------------------------------------------------------
+do_cmd_line_opts() {
+
+ set +e
+ local temp=$( getopt -o R:dvhV \
+ --long root:,debug,verbose,help,version \
+ -n "${MY_BASE}" -- "$@" )
+ if [[ $? != 0 ]] ; then
+ echo "" >&2
+ usage >&2
+ exit 1
+ fi
+ set -e
+
+ # Note the quotes around `$TEMP': they are essential!
+ eval set -- "$temp"
+
+ while true ; do
+ case "$1" in
+ -R|--root)
+ ROOT_PATH="$2"
+ shift 2
+ ;;
+ -d|--debug)
+ debug="y"
+ shift
+ ;;
+ -v|--verbose)
+ verbose="y"
+ shift
+ ;;
+ -h|--help)
+ usage
+ exit 0
+ ;;
+ -V|--version)
+ echo "${MY_BASE} version: ${VERSION}"
+ exit 0
+ ;;
+ --) shift
+ break
+ ;;
+ *) echo "Internal parse error, wrong parameter $1" >&2
+ exit 1
+ ;;
+ esac
+ done
+
+ if [[ "${debug}" = "y" ]] ; then
+ set -x
+ fi
+
+ if [[ $( id -u ) != "0" ]] ; then
+ SUDO="sudo"
+ fi
+
+ if [[ "$#" -gt "0" ]] ; then
+ DISTROS="$*"
+ fi
+
+ if [[ -n "${DISTROS}" ]] ; then
+ local target_distro=
+ local distro=
+ local found="n"
+ for target_distro in ${DISTROS} ; do
+ for distro in ${DEBIAN_DISTROS} ; do
+ if [[ "${distro}" = "${target_distro}" ]] ; then
+ found="y"
+ break
+ fi
+ done
+ if [[ "${found}" = "y" ]] ; then
+ continue
+ fi
+ for distro in ${UBUNTU_DISTROS} ; do
+ if [[ "${distro}" = "${target_distro}" ]] ; then
+ found="y"
+ break
+ fi
+ done
+ if [[ "${found}" = "y" ]] ; then
+ continue
+ fi
+ echo "Distribution '${target_distro}' not found."
+ exit 1
+ done
+ fi
+
+ if [[ ! -d "${ROOT_PATH}" ]] ; then
+ echo "Root directory '${ROOT_PATH}' not found." >&2
+ exit 1
+ fi
+
+}
+
+#------------------------------------------------------------------------------
+do_debian() {
+
+ local do_distros=""
+ local distro
+ local target_distro
+ if [[ -n "${DISTROS}" ]] ; then
+ for target_distro in ${DISTROS} ; do
+ for distro in ${DEBIAN_DISTROS} ; do
+ if [[ "${distro}" = "${target_distro}" ]] ; then
+ if [[ -n "${do_distros}" ]] ; then
+ do_distros="${do_distros} "
+ fi
+ do_distros="${do_distros}${distro}"
+ fi
+ done
+ done
+ else
+ do_distros="${DEBIAN_DISTROS}"
+ fi
+
+ if [[ -z "${do_distros}" ]] ; then
+ return
+ fi
+
+ for distro in ${do_distros} ; do
+
+ local chroot="pb-${distro}"
+ local path="${ROOT_PATH}/${chroot}"
+ local ini_file="/etc/schroot/chroot.d/${chroot}"
+ local sources_list="${path}/etc/apt/sources.list"
+ local pb_sources_list="${path}/etc/apt/sources.list.d/profitbricks.list"
+
+ echo
+ echo "--------------------------------------------------------------------------"
+ echo -e "[$(date)]: Performing ${GREEN}Debian ${distro}${NORMAL} ..."
+
+ if [[ ! -d "${path}" ]] ; then
+ ${SUDO} sbuild-createchroot --components=main,non-free,contrib "${distro}" "${path}" "${DEBIAN_MIRROR}" || continue
+ RM -f "/etc/schroot/chroot.d/${distro}-amd64-sbuild-"*
+ echo
+ fi
+
+ echo "Updating ${ini_file} ..."
+ echo "${CHROOT_INI_DEBIAN}" | \
+ sed -e "s/{{chroot}}/${chroot}/g" \
+ -e "s|{{path}}|${path}|g" \
+ -e "s/{{distro}}/${distro}/g" \
+ -e "s/{{vendor}}/Debian/g" | \
+ ${SUDO} tee "${ini_file}" >/dev/null
+
+# echo "Updating ${sources_list} ..."
+# echo "${SRC_LIST_DEBIAN}" | \
+# sed -e "s/{{distro}}/${distro}/g" | \
+# ${SUDO} tee "${sources_list}" >/dev/null
+
+# echo "Updating ${pb_sources_list} ..."
+# echo "${PB_SRC_LIST_DEBIAN}" | \
+# sed -e "s/{{distro}}/${distro}/g" | \
+# ${SUDO} tee "${pb_sources_list}" >/dev/null
+
+# echo
+# echo "Checking profitbricks keyring ..."
+# if schroot -d / -u root -c source:${chroot} -- dpkg -s profitbricks-keyring >/dev/null 2>&1 ; then
+# :
+# else
+# echo "Adding profitbricks keyring ..."
+# schroot -d / -u root -c source:${chroot} -- apt-get update
+# schroot -d / -u root -c source:${chroot} -- apt-get -y --allow-unauthenticated install profitbricks-keyring
+# fi
+
+ done
+
+}
+
+#------------------------------------------------------------------------------
+do_ubuntu() {
+
+ local do_distros=""
+ local distro
+ local target_distro
+ if [[ -n "${DISTROS}" ]] ; then
+ for target_distro in ${DISTROS} ; do
+ for distro in ${UBUNTU_DISTROS} ; do
+ if [[ "${distro}" = "${target_distro}" ]] ; then
+ if [[ -n "${do_distros}" ]] ; then
+ do_distros="${do_distros} "
+ fi
+ do_distros="${do_distros}${distro}"
+ fi
+ done
+ done
+ else
+ do_distros="${UBUNTU_DISTROS}"
+ fi
+
+ if [[ -z "${do_distros}" ]] ; then
+ return
+ fi
+
+ for distro in ${do_distros} ; do
+
+ local chroot="pb-${distro}"
+ local path="${ROOT_PATH}/${chroot}"
+ local ini_file="/etc/schroot/chroot.d/${chroot}"
+ local sources_list="${path}/etc/apt/sources.list"
+ local pb_sources_list="${path}/etc/apt/sources.list.d/profitbricks.list"
+ local proxy_file="${path}/etc/apt/apt.conf.d/80proxy"
+
+ echo
+ echo "--------------------------------------------------------------------------"
+ echo -e "[$(date)]: Performing ${GREEN}Ubuntu ${distro}${NORMAL} ..."
+
+ if [[ ! -d "${path}" ]] ; then
+ ${SUDO} sbuild-createchroot --components=main,universe "${distro}" "${path}" "${UBUNTU_MIRROR}" || continue
+ RM -f "/etc/schroot/chroot.d/${distro}-amd64-sbuild-"*
+ echo
+ fi
+
+ echo "Updating ${ini_file} ..."
+ echo "${CHROOT_INI_DEBIAN}" | \
+ sed -e "s/{{chroot}}/${chroot}/g" \
+ -e "s|{{path}}|${path}|g" \
+ -e "s/{{distro}}/${distro}/g" \
+ -e "s/{{vendor}}/Debian/g" | \
+ ${SUDO} tee "${ini_file}" >/dev/null
+
+# echo "Updating ${pb_sources_list} ..."
+# echo "${PB_SRC_LIST_UBUNTU}" | \
+# sed -e "s/{{distro}}/${distro}/g" | \
+# ${SUDO} tee "${pb_sources_list}" >/dev/null
+
+# echo "Updating ${proxy_file} ..."
+# echo "${PROXY_FILE_CONTENT}" | ${SUDO} tee "${proxy_file}" >/dev/null
+
+# echo
+# echo "Checking profitbricks keyring ..."
+# if schroot -d / -u root -c source:${chroot} -- dpkg -s profitbricks-keyring >/dev/null 2>&1 ; then
+# :
+# else
+# echo "Adding profitbricks keyring ..."
+# schroot -d / -u root -c source:${chroot} -- apt-get update
+## schroot -d / -u root -c source:${chroot} -- apt-get -y --allow-unauthenticated install profitbricks-keyring
+# fi
+
+ done
+
+}
+
+#------------------------------------------------------------------------------
+set_colors
+do_cmd_line_opts "$@"
+
+
+if [[ ! -d "${UNION_OVERLAY_DIR}" ]] ; then
+ echo "Creating '${UNION_OVERLAY_DIR}' ..."
+ MKDIR -p "${UNION_OVERLAY_DIR}" || exit $?
+fi
+
+do_debian
+do_ubuntu
+
+echo
+echo "--------------------------------------------------------------------------"
+echo -e "[$(date)]: ${GREEN}Finished${NORMAL}."
+
+
+# vim: ts=4 et softtabstop=4