From ff3baf1cd427542e33b780ab458e06b55878cd91 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Sun, 9 Mar 2014 19:34:59 -0300 Subject: [PATCH] boxes: Improved output logging --- boxes/.gitignore | 1 + boxes/common/download.sh | 19 +++++++++--------- boxes/common/package.sh | 4 ++-- boxes/common/ui.sh | 3 +++ boxes/common/utils.sh | 28 +++++++++++++++++++++++++++ boxes/debian/clean.sh | 7 ++++--- boxes/debian/install-extras.sh | 35 +++++++++++++++++----------------- boxes/mk-ubuntu.sh | 1 + 8 files changed, 67 insertions(+), 31 deletions(-) create mode 100644 boxes/.gitignore create mode 100644 boxes/common/utils.sh diff --git a/boxes/.gitignore b/boxes/.gitignore new file mode 100644 index 0000000..31dbbff --- /dev/null +++ b/boxes/.gitignore @@ -0,0 +1 @@ +/log diff --git a/boxes/common/download.sh b/boxes/common/download.sh index f39d1ba..bbb8c37 100755 --- a/boxes/common/download.sh +++ b/boxes/common/download.sh @@ -2,6 +2,7 @@ set -e source common/ui.sh +source common/utils.sh # If container exists, check if want to continue if $(lxc-ls | grep -q ${CONTAINER}); then @@ -15,8 +16,8 @@ fi if $(lxc-ls | grep -q ${CONTAINER}); then if $(confirm "Do you want to rebuild the '${CONTAINER}' container?" 'n'); then log "Destroying container ${CONTAINER}..." - lxc-stop -n ${CONTAINER} &>/dev/null || true - lxc-destroy -n ${CONTAINER} + utils.lxc.stop + utils.lxc.destroy else log "Reusing existing container..." exit 0 @@ -26,14 +27,14 @@ fi # If we got to this point, we need to create the container log "Creating container..." if [ $RELEASE = 'raring' ]; then - lxc-create -n ${CONTAINER} -t ubuntu -- \ - --release ${RELEASE} \ - --arch ${ARCH} + utils.lxc.create -t ubuntu -- \ + --release ${RELEASE} \ + --arch ${ARCH} else - lxc-create -n ${CONTAINER} -t download -- \ - --dist ${DISTRIBUTION} \ - --release ${RELEASE} \ - --arch ${ARCH} + utils.lxc.create -t download -- \ + --dist ${DISTRIBUTION} \ + --release ${RELEASE} \ + --arch ${ARCH} fi log "Container created!" diff --git a/boxes/common/package.sh b/boxes/common/package.sh index 2ccf320..e223ee4 100755 --- a/boxes/common/package.sh +++ b/boxes/common/package.sh @@ -16,10 +16,10 @@ if [ -f ${WORKING_DIR}/rootfs.tar.gz ]; then fi log "Compressing container's rootfs" -pushd $(dirname ${ROOTFS}) &>/dev/null +pushd $(dirname ${ROOTFS}) &>${LOG} tar --numeric-owner --anchored --exclude=./rootfs/dev/log -czf \ ${WORKING_DIR}/rootfs.tar.gz ./rootfs/* -popd &>/dev/null +popd &>${LOG} # Prepare package contents log 'Preparing box package contents' diff --git a/boxes/common/ui.sh b/boxes/common/ui.sh index 14d9a2c..b389b67 100644 --- a/boxes/common/ui.sh +++ b/boxes/common/ui.sh @@ -6,14 +6,17 @@ export ERROR_COLOR='\033[31;01m' export WARN_COLOR='\033[33;01m' log() { + echo " [${RELEASE}] ${1}" >${LOG} echo " [${RELEASE}] ${1}" >&2 } warn() { + echo "==> [${RELEASE}] [WARN] ${1}" >${LOG} echo -e "${WARN_COLOR}==> [${RELEASE}] ${1}${NO_COLOR}" } info() { + echo "==> [${RELEASE}] [INFO] ${1}" >${LOG} echo -e "${OK_COLOR}==> [${RELEASE}] ${1}${NO_COLOR}" } diff --git a/boxes/common/utils.sh b/boxes/common/utils.sh new file mode 100644 index 0000000..7cc89d8 --- /dev/null +++ b/boxes/common/utils.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +mkdir -p $(dirname $LOG) +rm -f ${LOG} +touch ${LOG} +chmod +rw ${LOG} + +utils.lxc.attach() { + cmd="$@" + log "Running [${cmd}] inside '${CONTAINER}' container..." + (lxc-attach -n ${CONTAINER} -- $cmd) &> ${LOG} +} + +utils.lxc.start() { + lxc-start -d -n ${CONTAINER} &>${LOG} || true +} + +utils.lxc.stop() { + lxc-stop -n ${CONTAINER} &>${LOG} || true +} + +utils.lxc.destroy() { + lxc-destroy -n ${CONTAINER} &>${LOG} +} + +utils.lxc.create() { + lxc-create -n ${CONTAINER} "$@" &>${LOG} +} diff --git a/boxes/debian/clean.sh b/boxes/debian/clean.sh index 36f1302..0bdd2fb 100755 --- a/boxes/debian/clean.sh +++ b/boxes/debian/clean.sh @@ -2,14 +2,15 @@ set -e source common/ui.sh +source common/utils.sh debug 'Bringing container up' -lxc-start -d -n ${CONTAINER} &>/dev/null || true +utils.lxc.start info "Cleaning up '${CONTAINER}'..." log 'Removing temporary files...' -lxc-attach -n ${CONTAINER} -- rm -rf /tmp/* +rm -rf ${ROOTFS}/tmp/* log 'Removing downloaded packages...' -lxc-attach -n ${CONTAINER} -- apt-get clean +utils.lxc.attach apt-get clean diff --git a/boxes/debian/install-extras.sh b/boxes/debian/install-extras.sh index 8cabb09..2bd9b84 100755 --- a/boxes/debian/install-extras.sh +++ b/boxes/debian/install-extras.sh @@ -2,26 +2,27 @@ set -e source common/ui.sh +source common/utils.sh info 'Installing extra packages and upgrading' debug 'Bringing container up' -lxc-start -d -n ${CONTAINER} &>/dev/null || true +utils.lxc.start # Sleep for a bit so that the container can get an IP +log 'Sleeping for 5 seconds...' sleep 5 # TODO: Support for setting this from outside UBUNTU_PACKAGES=(vim curl wget man-db bash-completion python-software-properties software-properties-common) - -lxc-attach -n ${CONTAINER} -- apt-get update -lxc-attach -n ${CONTAINER} -- apt-get install ${UBUNTU_PACKAGES[*]} -y --force-yes -lxc-attach -n ${CONTAINER} -- apt-get upgrade -y --force-yes +utils.lxc.attach apt-get update +utils.lxc.attach apt-get install ${UBUNTU_PACKAGES[*]} -y --force-yes +utils.lxc.attach apt-get upgrade -y --force-yes # TODO: SEPARATE FILE! # Ensure locales are properly set, based on http://askubuntu.com/a/238063 -lxc-attach -n ${CONTAINER} -- locale-gen en_US.UTF-8 -lxc-attach -n ${CONTAINER} -- dpkg-reconfigure locales +utils.lxc.attach locale-gen en_US.UTF-8 +utils.lxc.attach dpkg-reconfigure locales CHEF=${CHEF:-0} PUPPET=${PUPPET:-0} @@ -38,7 +39,7 @@ if [ $CHEF = 1 ]; then curl -L https://www.opscode.com/chef/install.sh -k | sudo bash EOF chmod +x ${ROOTFS}/tmp/install-chef.sh - lxc-attach -n ${CONTAINER} -- /tmp/install-chef.sh + utils.lxc.attach /tmp/install-chef.sh fi else log "Skipping Chef installation" @@ -52,9 +53,9 @@ if [ $PUPPET = 1 ]; then else log "Installing Puppet" wget http://apt.puppetlabs.com/puppetlabs-release-stable.deb -O "${ROOTFS}/tmp/puppetlabs-release-stable.deb" - lxc-attach -n ${CONTAINER} -- dpkg -i "/tmp/puppetlabs-release-stable.deb" - lxc-attach -n ${CONTAINER} -- apt-get update - lxc-attach -n ${CONTAINER} -- apt-get install puppet -y --force-yes + utils.lxc.attach dpkg -i "/tmp/puppetlabs-release-stable.deb" + utils.lxc.attach apt-get update + utils.lxc.attach apt-get install puppet -y --force-yes fi else log "Skipping Puppet installation" @@ -63,12 +64,10 @@ fi if [ $SALT = 1 ]; then if $(lxc-attach -n ${CONTAINER} -- which salt-minion &>/dev/null); then log "Salt has been installed on container, skipping" - elif [ ${RELEASE} = 'raring' ]; then - warn "Salt can't be installed on Ubuntu Raring 13.04, skipping" else - lxc-attach -n ${CONTAINER} -- apt-add-repository -y ppa:saltstack/salt - lxc-attach -n ${CONTAINER} -- apt-get update - chroot ${ROOTFS} apt-get install salt-minion -y --force-yes + utils.lxc.attach apt-add-repository -y ppa:saltstack/salt + utils.lxc.attach apt-get update + utils.lxc.attach apt-get install salt-minion -y --force-yes fi else log "Skipping Salt installation" @@ -77,6 +76,8 @@ fi if [ $BABUSHKA = 1 ]; then if $(lxc-attach -n ${CONTAINER} -- which babushka &>/dev/null); then log "Babushka has been installed on container, skipping" + elif [ ${RELEASE} = 'trusty' ]; then + warn "Babushka can't be installed on Ubuntu Trusty 14.04, skipping" else log "Installing Babushka" cat > $ROOTFS/tmp/install-babushka.sh << EOF @@ -84,7 +85,7 @@ if [ $BABUSHKA = 1 ]; then curl https://babushka.me/up | sudo bash EOF chmod +x $ROOTFS/tmp/install-babushka.sh - lxc-attach -n ${CONTAINER} -- /tmp/install-babushka.sh + utils.lxc.attach /tmp/install-babushka.sh fi else log "Skipping Babushka installation" diff --git a/boxes/mk-ubuntu.sh b/boxes/mk-ubuntu.sh index 45e61d9..b83ebe8 100755 --- a/boxes/mk-ubuntu.sh +++ b/boxes/mk-ubuntu.sh @@ -16,6 +16,7 @@ export PACKAGE=$4 export ROOTFS="/var/lib/lxc/${CONTAINER}/rootfs" export WORKING_DIR="/tmp/${CONTAINER}" export NOW=$(date -u) +export LOG=$(readlink -f .)/log/${CONTAINER}.log if [ -f ${PACKAGE} ]; then warn "The box '${PACKAGE}' already exists, skipping..."