boxes: Improved output logging

This commit is contained in:
Fabio Rehm 2014-03-09 19:34:59 -03:00
parent 74cf06b148
commit ff3baf1cd4
8 changed files with 67 additions and 31 deletions

1
boxes/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/log

View file

@ -2,6 +2,7 @@
set -e set -e
source common/ui.sh source common/ui.sh
source common/utils.sh
# If container exists, check if want to continue # If container exists, check if want to continue
if $(lxc-ls | grep -q ${CONTAINER}); then if $(lxc-ls | grep -q ${CONTAINER}); then
@ -15,8 +16,8 @@ fi
if $(lxc-ls | grep -q ${CONTAINER}); then if $(lxc-ls | grep -q ${CONTAINER}); then
if $(confirm "Do you want to rebuild the '${CONTAINER}' container?" 'n'); then if $(confirm "Do you want to rebuild the '${CONTAINER}' container?" 'n'); then
log "Destroying container ${CONTAINER}..." log "Destroying container ${CONTAINER}..."
lxc-stop -n ${CONTAINER} &>/dev/null || true utils.lxc.stop
lxc-destroy -n ${CONTAINER} utils.lxc.destroy
else else
log "Reusing existing container..." log "Reusing existing container..."
exit 0 exit 0
@ -26,14 +27,14 @@ fi
# If we got to this point, we need to create the container # If we got to this point, we need to create the container
log "Creating container..." log "Creating container..."
if [ $RELEASE = 'raring' ]; then if [ $RELEASE = 'raring' ]; then
lxc-create -n ${CONTAINER} -t ubuntu -- \ utils.lxc.create -t ubuntu -- \
--release ${RELEASE} \ --release ${RELEASE} \
--arch ${ARCH} --arch ${ARCH}
else else
lxc-create -n ${CONTAINER} -t download -- \ utils.lxc.create -t download -- \
--dist ${DISTRIBUTION} \ --dist ${DISTRIBUTION} \
--release ${RELEASE} \ --release ${RELEASE} \
--arch ${ARCH} --arch ${ARCH}
fi fi
log "Container created!" log "Container created!"

View file

@ -16,10 +16,10 @@ if [ -f ${WORKING_DIR}/rootfs.tar.gz ]; then
fi fi
log "Compressing container's rootfs" log "Compressing container's rootfs"
pushd $(dirname ${ROOTFS}) &>/dev/null pushd $(dirname ${ROOTFS}) &>${LOG}
tar --numeric-owner --anchored --exclude=./rootfs/dev/log -czf \ tar --numeric-owner --anchored --exclude=./rootfs/dev/log -czf \
${WORKING_DIR}/rootfs.tar.gz ./rootfs/* ${WORKING_DIR}/rootfs.tar.gz ./rootfs/*
popd &>/dev/null popd &>${LOG}
# Prepare package contents # Prepare package contents
log 'Preparing box package contents' log 'Preparing box package contents'

View file

@ -6,14 +6,17 @@ export ERROR_COLOR='\033[31;01m'
export WARN_COLOR='\033[33;01m' export WARN_COLOR='\033[33;01m'
log() { log() {
echo " [${RELEASE}] ${1}" >${LOG}
echo " [${RELEASE}] ${1}" >&2 echo " [${RELEASE}] ${1}" >&2
} }
warn() { warn() {
echo "==> [${RELEASE}] [WARN] ${1}" >${LOG}
echo -e "${WARN_COLOR}==> [${RELEASE}] ${1}${NO_COLOR}" echo -e "${WARN_COLOR}==> [${RELEASE}] ${1}${NO_COLOR}"
} }
info() { info() {
echo "==> [${RELEASE}] [INFO] ${1}" >${LOG}
echo -e "${OK_COLOR}==> [${RELEASE}] ${1}${NO_COLOR}" echo -e "${OK_COLOR}==> [${RELEASE}] ${1}${NO_COLOR}"
} }

28
boxes/common/utils.sh Normal file
View file

@ -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}
}

View file

@ -2,14 +2,15 @@
set -e set -e
source common/ui.sh source common/ui.sh
source common/utils.sh
debug 'Bringing container up' debug 'Bringing container up'
lxc-start -d -n ${CONTAINER} &>/dev/null || true utils.lxc.start
info "Cleaning up '${CONTAINER}'..." info "Cleaning up '${CONTAINER}'..."
log 'Removing temporary files...' log 'Removing temporary files...'
lxc-attach -n ${CONTAINER} -- rm -rf /tmp/* rm -rf ${ROOTFS}/tmp/*
log 'Removing downloaded packages...' log 'Removing downloaded packages...'
lxc-attach -n ${CONTAINER} -- apt-get clean utils.lxc.attach apt-get clean

View file

@ -2,26 +2,27 @@
set -e set -e
source common/ui.sh source common/ui.sh
source common/utils.sh
info 'Installing extra packages and upgrading' info 'Installing extra packages and upgrading'
debug 'Bringing container up' 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 # Sleep for a bit so that the container can get an IP
log 'Sleeping for 5 seconds...'
sleep 5 sleep 5
# TODO: Support for setting this from outside # TODO: Support for setting this from outside
UBUNTU_PACKAGES=(vim curl wget man-db bash-completion python-software-properties software-properties-common) UBUNTU_PACKAGES=(vim curl wget man-db bash-completion python-software-properties software-properties-common)
utils.lxc.attach apt-get update
lxc-attach -n ${CONTAINER} -- apt-get update utils.lxc.attach apt-get install ${UBUNTU_PACKAGES[*]} -y --force-yes
lxc-attach -n ${CONTAINER} -- apt-get install ${UBUNTU_PACKAGES[*]} -y --force-yes utils.lxc.attach apt-get upgrade -y --force-yes
lxc-attach -n ${CONTAINER} -- apt-get upgrade -y --force-yes
# TODO: SEPARATE FILE! # TODO: SEPARATE FILE!
# Ensure locales are properly set, based on http://askubuntu.com/a/238063 # Ensure locales are properly set, based on http://askubuntu.com/a/238063
lxc-attach -n ${CONTAINER} -- locale-gen en_US.UTF-8 utils.lxc.attach locale-gen en_US.UTF-8
lxc-attach -n ${CONTAINER} -- dpkg-reconfigure locales utils.lxc.attach dpkg-reconfigure locales
CHEF=${CHEF:-0} CHEF=${CHEF:-0}
PUPPET=${PUPPET:-0} PUPPET=${PUPPET:-0}
@ -38,7 +39,7 @@ if [ $CHEF = 1 ]; then
curl -L https://www.opscode.com/chef/install.sh -k | sudo bash curl -L https://www.opscode.com/chef/install.sh -k | sudo bash
EOF EOF
chmod +x ${ROOTFS}/tmp/install-chef.sh chmod +x ${ROOTFS}/tmp/install-chef.sh
lxc-attach -n ${CONTAINER} -- /tmp/install-chef.sh utils.lxc.attach /tmp/install-chef.sh
fi fi
else else
log "Skipping Chef installation" log "Skipping Chef installation"
@ -52,9 +53,9 @@ if [ $PUPPET = 1 ]; then
else else
log "Installing Puppet" log "Installing Puppet"
wget http://apt.puppetlabs.com/puppetlabs-release-stable.deb -O "${ROOTFS}/tmp/puppetlabs-release-stable.deb" 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" utils.lxc.attach dpkg -i "/tmp/puppetlabs-release-stable.deb"
lxc-attach -n ${CONTAINER} -- apt-get update utils.lxc.attach apt-get update
lxc-attach -n ${CONTAINER} -- apt-get install puppet -y --force-yes utils.lxc.attach apt-get install puppet -y --force-yes
fi fi
else else
log "Skipping Puppet installation" log "Skipping Puppet installation"
@ -63,12 +64,10 @@ fi
if [ $SALT = 1 ]; then if [ $SALT = 1 ]; then
if $(lxc-attach -n ${CONTAINER} -- which salt-minion &>/dev/null); then if $(lxc-attach -n ${CONTAINER} -- which salt-minion &>/dev/null); then
log "Salt has been installed on container, skipping" 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 else
lxc-attach -n ${CONTAINER} -- apt-add-repository -y ppa:saltstack/salt utils.lxc.attach apt-add-repository -y ppa:saltstack/salt
lxc-attach -n ${CONTAINER} -- apt-get update utils.lxc.attach apt-get update
chroot ${ROOTFS} apt-get install salt-minion -y --force-yes utils.lxc.attach apt-get install salt-minion -y --force-yes
fi fi
else else
log "Skipping Salt installation" log "Skipping Salt installation"
@ -77,6 +76,8 @@ fi
if [ $BABUSHKA = 1 ]; then if [ $BABUSHKA = 1 ]; then
if $(lxc-attach -n ${CONTAINER} -- which babushka &>/dev/null); then if $(lxc-attach -n ${CONTAINER} -- which babushka &>/dev/null); then
log "Babushka has been installed on container, skipping" 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 else
log "Installing Babushka" log "Installing Babushka"
cat > $ROOTFS/tmp/install-babushka.sh << EOF cat > $ROOTFS/tmp/install-babushka.sh << EOF
@ -84,7 +85,7 @@ if [ $BABUSHKA = 1 ]; then
curl https://babushka.me/up | sudo bash curl https://babushka.me/up | sudo bash
EOF EOF
chmod +x $ROOTFS/tmp/install-babushka.sh chmod +x $ROOTFS/tmp/install-babushka.sh
lxc-attach -n ${CONTAINER} -- /tmp/install-babushka.sh utils.lxc.attach /tmp/install-babushka.sh
fi fi
else else
log "Skipping Babushka installation" log "Skipping Babushka installation"

View file

@ -16,6 +16,7 @@ export PACKAGE=$4
export ROOTFS="/var/lib/lxc/${CONTAINER}/rootfs" export ROOTFS="/var/lib/lxc/${CONTAINER}/rootfs"
export WORKING_DIR="/tmp/${CONTAINER}" export WORKING_DIR="/tmp/${CONTAINER}"
export NOW=$(date -u) export NOW=$(date -u)
export LOG=$(readlink -f .)/log/${CONTAINER}.log
if [ -f ${PACKAGE} ]; then if [ -f ${PACKAGE} ]; then
warn "The box '${PACKAGE}' already exists, skipping..." warn "The box '${PACKAGE}' already exists, skipping..."