From dce843db1279654411cd1be95316871e7ed836db Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Sat, 8 Mar 2014 02:23:06 -0300 Subject: [PATCH] Improved base boxes scripts UI and implement prepare-vagrant-user.sh --- boxes/common/download.sh | 8 +++--- boxes/common/prepare-vagrant-user.sh | 37 ++++++++++++++++++++++++++-- boxes/common/ui.sh | 16 +++++++++--- boxes/mk-ubuntu.sh | 9 ++++--- 4 files changed, 57 insertions(+), 13 deletions(-) diff --git a/boxes/common/download.sh b/boxes/common/download.sh index 9f9dcf0..01e8272 100755 --- a/boxes/common/download.sh +++ b/boxes/common/download.sh @@ -3,17 +3,16 @@ set -e source common/ui.sh -container_exists=$(lxc-ls | grep -q ${CONTAINER}) # If container exists, check if want to continue -if $container_exists; then - if ! $(confirm "The '${CONTAINER}' container already exists, do you want to continue building the box?" 'n'); then +if $(lxc-ls | grep -q ${CONTAINER}); then + if ! $(confirm "The '${CONTAINER}' container already exists, do you want to continue building the box?" 'y'); then log 'Aborting...' exit 1 fi fi # If container exists and wants to continue building the box -if $container_exists; then +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 @@ -30,3 +29,4 @@ lxc-create -n ${CONTAINER} -t download -- \ --dist ${DISTRIBUTION} \ --release ${RELEASE} \ --arch ${ARCH} +log "Container created!" diff --git a/boxes/common/prepare-vagrant-user.sh b/boxes/common/prepare-vagrant-user.sh index c728668..08ec17b 100755 --- a/boxes/common/prepare-vagrant-user.sh +++ b/boxes/common/prepare-vagrant-user.sh @@ -1,6 +1,39 @@ #!/bin/bash set -e -container_name=$1 +source common/ui.sh -echo " Will prepare the vagrant user on '${container_name}'" +info "Preparing vagrant user..." + +# Create vagrant user +if $(grep -q 'vagrant' ${ROOTFS}/etc/shadow); then + log 'Skipping vagrant user creation' +else + debug 'vagrant user does not exist, renaming ubuntu user...' + mv ${ROOTFS}/home/{ubuntu,vagrant} + chroot ${ROOTFS} usermod -l vagrant -d /home/vagrant ubuntu + chroot ${ROOTFS} groupmod -n vagrant ubuntu + echo -n 'vagrant:vagrant' | chroot ${ROOTFS} chpasswd + log 'Renamed ubuntu user to vagrant and changed password.' +fi + +# Configure SSH access +if [ -d ${ROOTFS}/home/vagrant/.ssh ]; then + log 'Skipping vagrant SSH credentials configuration' +else + debug 'SSH key has not been set' + mkdir -p ${ROOTFS}/home/vagrant/.ssh + echo $VAGRANT_KEY > ${ROOTFS}/home/vagrant/.ssh/authorized_keys + chroot ${ROOTFS} chown -R vagrant: /home/vagrant/.ssh + log 'SSH credentials configured for the vagrant user.' +fi + +# Enable passwordless sudo for the vagrant user +if [ -f ${ROOTFS}/etc/sudoers.d/vagrant ]; then + log 'Skipping sudoers file creation.' +else + debug 'Sudoers file was not found' + echo "vagrant ALL=(ALL) NOPASSWD:ALL" > ${ROOTFS}/etc/sudoers.d/vagrant + chmod 0441 ${ROOTFS}/etc/sudoers.d/vagrant + log 'Sudoers file created.' +fi diff --git a/boxes/common/ui.sh b/boxes/common/ui.sh index 2f84194..c5c40f1 100644 --- a/boxes/common/ui.sh +++ b/boxes/common/ui.sh @@ -1,11 +1,15 @@ #!/bin/bash log() { - echo " ${1}" >&2 + echo " [${RELEASE}] ${1}" >&2 } -debug() { - [ ! $DEBUG ] || echo " [DEBUG] ${1}" >&2 +warn() { + echo -e "${WARN_COLOR}==> [${RELEASE}] ${1}${NO_COLOR}" +} + +info() { + echo -e "${OK_COLOR}==> [${RELEASE}] ${1}${NO_COLOR}" } confirm() { @@ -21,7 +25,7 @@ confirm() { default='Yes' fi - echo -n " ${question} [${default_prompt}] " >&2 + echo -e -n "${WARN_COLOR}==> [${RELEASE}] ${question} [${default_prompt}] ${NO_COLOR}" >&2 read answer if [ -z $answer ]; then @@ -35,3 +39,7 @@ confirm() { return 1 fi } + +debug() { + [ ! $DEBUG ] || echo " [${RELEASE}] [DEBUG] ${1}" >&2 +} diff --git a/boxes/mk-ubuntu.sh b/boxes/mk-ubuntu.sh index 62d07a7..c1508c1 100755 --- a/boxes/mk-ubuntu.sh +++ b/boxes/mk-ubuntu.sh @@ -1,6 +1,8 @@ #!/bin/bash set -e +source common/ui.sh + if [ "$(id -u)" != "0" ]; then echo "You should run this script as root (sudo)." exit 1 @@ -16,14 +18,15 @@ export RELEASE=$1 export ARCH=$2 export CONTAINER=$3 export PACKAGE=$4 +export ROOTFS="/var/lib/lxc/${CONTAINER}/rootfs" if [ -f ${PACKAGE} ]; then - echo -e "${WARN_COLOR}==> The box '${PACKAGE}' already exists, skipping...${NO_COLOR}" + warn "The box '${PACKAGE}' already exists, skipping..." echo exit fi -echo -e "${OK_COLOR}==> Building '${RELEASE} (${ARCH})' to '${PACKAGE}'...${NO_COLOR}" +info "Building box to '${PACKAGE}'..." ./common/download.sh ubuntu ${RELEASE} ${ARCH} ${CONTAINER} ./common/prepare-vagrant-user.sh ${CONTAINER} @@ -32,5 +35,5 @@ echo -e "${OK_COLOR}==> Building '${RELEASE} (${ARCH})' to '${PACKAGE}'...${NO_C ./common/package.sh ${CONTAINER} ${PACKAGE} touch $PACKAGE -echo -e "${OK_COLOR}==> Finished building '${RELEASE} (${ARCH})' to '${PACKAGE}'...${NO_COLOR}" +info "Finished building '${PACKAGE}'!" echo