From 00a4664d736ac3ea0397db49b3de1d9a2441793d Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Sun, 22 Apr 2018 21:59:45 -0400 Subject: [PATCH] Add gentoo dist and remove ${LOG}. It's a bit of a useless mechanism and, in fact a hindrance. Why not just let the user redirect stdout? --- Makefile | 4 +++ common/package.sh | 14 ++++++++-- common/prepare-vagrant-user.sh | 14 +++++----- common/ui.sh | 5 +--- common/utils.sh | 20 +++++++++++---- conf/gentoo | 5 ++++ gentoo/clean.sh | 14 ++++++++++ gentoo/install-packages.sh | 41 +++++++++++++++++++++++++++++ mk-gentoo.sh | 47 ++++++++++++++++++++++++++++++++++ 9 files changed, 146 insertions(+), 18 deletions(-) create mode 100644 conf/gentoo create mode 100644 gentoo/clean.sh create mode 100755 gentoo/install-packages.sh create mode 100755 mk-gentoo.sh diff --git a/Makefile b/Makefile index 6b36823..e05b596 100644 --- a/Makefile +++ b/Makefile @@ -46,6 +46,10 @@ $(FEDORA_BOXES): @sudo chmod +rw $(PACKAGE) @sudo chown ${USER}: $(PACKAGE) +.PHONY: gentoo +gentoo: + @sudo -E ./mk-gentoo.sh + acceptance: CONTAINER = "vagrant-base-acceptance-$(ARCH)" acceptance: PACKAGE = "output/${TODAY}/vagrant-lxc-acceptance-$(ARCH).box" acceptance: diff --git a/common/package.sh b/common/package.sh index 16cf4c5..6eff6f1 100755 --- a/common/package.sh +++ b/common/package.sh @@ -3,6 +3,13 @@ set -e source common/ui.sh +ROOTFS="/var/lib/lxc/${CONTAINER}/rootfs" +WORKING_DIR="/tmp/${CONTAINER}" + +debug "Creating ${WORKING_DIR}" +mkdir -p ${WORKING_DIR} +mkdir -p $(dirname ${PACKAGE}) + # TODO: Create file with build date / time on container info "Packaging '${CONTAINER}' to '${PACKAGE}'..." @@ -16,10 +23,10 @@ if [ -f ${WORKING_DIR}/rootfs.tar.gz ]; then fi log "Compressing container's rootfs" -pushd $(dirname ${ROOTFS}) &>>${LOG} +pushd $(dirname ${ROOTFS}) tar --numeric-owner --anchored --exclude=./rootfs/dev/log -czf \ ${WORKING_DIR}/rootfs.tar.gz ./rootfs/* -popd &>>${LOG} +popd # Prepare package contents log 'Preparing box package contents' @@ -35,3 +42,6 @@ sed -i "s//${NOW}/" ${WORKING_DIR}/metadata.json log 'Packaging box' TARBALL=$(readlink -f ${PACKAGE}) (cd ${WORKING_DIR} && tar -czf $TARBALL ./*) + +chmod +rw ${PACKAGE} +chown ${USER}: ${PACKAGE} diff --git a/common/prepare-vagrant-user.sh b/common/prepare-vagrant-user.sh index f45238a..af95d5a 100755 --- a/common/prepare-vagrant-user.sh +++ b/common/prepare-vagrant-user.sh @@ -3,7 +3,8 @@ set -e source common/ui.sh -export VAGRANT_KEY="ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key" +ROOTFS="/var/lib/lxc/${CONTAINER}/rootfs" +VAGRANT_KEY="ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key" info "Preparing vagrant user..." @@ -13,13 +14,13 @@ if $(grep -q 'vagrant' ${ROOTFS}/etc/shadow); then elif $(grep -q 'ubuntu' ${ROOTFS}/etc/shadow); then debug 'vagrant user does not exist, renaming ubuntu user...' mv ${ROOTFS}/home/{ubuntu,vagrant} - chroot ${ROOTFS} usermod -l vagrant -d /home/vagrant ubuntu &>> ${LOG} - chroot ${ROOTFS} groupmod -n vagrant ubuntu &>> ${LOG} + 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.' elif [ ${DISTRIBUTION} = 'centos' -o ${DISTRIBUTION} = 'fedora' ]; then debug 'Creating vagrant user...' - chroot ${ROOTFS} useradd --create-home -s /bin/bash -u 1000 vagrant &>> ${LOG} + chroot ${ROOTFS} useradd --create-home -s /bin/bash -u 1000 vagrant echo -n 'vagrant:vagrant' | chroot ${ROOTFS} chpasswd sed -i 's/^Defaults\s\+requiretty/# Defaults requiretty/' $ROOTFS/etc/sudoers if [ ${RELEASE} -eq 6 ]; then @@ -30,13 +31,12 @@ elif [ ${DISTRIBUTION} = 'centos' -o ${DISTRIBUTION} = 'fedora' ]; then fi else debug 'Creating vagrant user...' - chroot ${ROOTFS} useradd --create-home -s /bin/bash vagrant &>> ${LOG} - chroot ${ROOTFS} adduser vagrant sudo &>> ${LOG} + chroot ${ROOTFS} useradd --create-home -s /bin/bash vagrant echo -n 'vagrant:vagrant' | chroot ${ROOTFS} chpasswd fi # Configure SSH access -if [ -d ${ROOTFS}/home/vagrant/.ssh ]; then +if [ -d ${ROOTFS}/home/vagrant/.ssh/authorized_keys ]; then log 'Skipping vagrant SSH credentials configuration' else debug 'SSH key has not been set' diff --git a/common/ui.sh b/common/ui.sh index 244c1b0..9963d2c 100644 --- a/common/ui.sh +++ b/common/ui.sh @@ -7,19 +7,16 @@ export WARN_COLOR='\033[33;01m' log() { LOG_PREFIX="${DISTRIBUTION}-${RELEASE}" - echo " [${LOG_PREFIX}] ${1}" >>${LOG} - echo " [${LOG_PREFIX}] ${1}" >&2 + echo " [${LOG_PREFIX}] ${1}" } warn() { LOG_PREFIX="${DISTRIBUTION}-${RELEASE}" - echo "==> [${LOG_PREFIX}] [WARN] ${1}" >>${LOG} echo -e "${WARN_COLOR}==> [${LOG_PREFIX}] ${1}${NO_COLOR}" } info() { LOG_PREFIX="${DISTRIBUTION}-${RELEASE}" - echo "==> [${LOG_PREFIX}] [INFO] ${1}" >>${LOG} echo -e "${OK_COLOR}==> [${LOG_PREFIX}] ${1}${NO_COLOR}" } diff --git a/common/utils.sh b/common/utils.sh index ef25f46..9dd520a 100644 --- a/common/utils.sh +++ b/common/utils.sh @@ -3,21 +3,31 @@ utils.lxc.attach() { cmd="$@" log "Running [${cmd}] inside '${CONTAINER}' container..." - (lxc-attach -n ${CONTAINER} -- $cmd) &>> ${LOG} + lxc-attach -n ${CONTAINER} -- $cmd +} + +utils.lxc.pipetofile() { + lxc-attach -n ${CONTAINER} -- /bin/bash -c "tee $1 > /dev/null" +} + +utils.lxc.runscript() { + log "Running $1 inside '${CONTAINER}'..." + cat $1 | utils.lxc.pipetofile /script.sh + utils.lxc.attach /bin/bash /script.sh } utils.lxc.start() { - lxc-start -d -n ${CONTAINER} &>> ${LOG} || true + lxc-start -d -n ${CONTAINER} || true } utils.lxc.stop() { - lxc-stop -n ${CONTAINER} &>> ${LOG} || true + lxc-stop -n ${CONTAINER} || true } utils.lxc.destroy() { - lxc-destroy -n ${CONTAINER} &>> ${LOG} + lxc-destroy -n ${CONTAINER} } utils.lxc.create() { - lxc-create -n ${CONTAINER} "$@" &>> ${LOG} + lxc-create -n ${CONTAINER} "$@" } diff --git a/conf/gentoo b/conf/gentoo new file mode 100644 index 0000000..f57e8cc --- /dev/null +++ b/conf/gentoo @@ -0,0 +1,5 @@ +lxc.mount.auto = cgroup:mixed proc:mixed sys:mixed + +# Default console settings +lxc.tty = 4 +lxc.pts = 1024 diff --git a/gentoo/clean.sh b/gentoo/clean.sh new file mode 100644 index 0000000..1763a7c --- /dev/null +++ b/gentoo/clean.sh @@ -0,0 +1,14 @@ +#!/bin/bash +set -e +source /etc/profile + +echo "Cleaning up" + +rm /script.sh + +echo 'Removing temporary files...' +rm -rf /tmp/* + +echo 'cleaning up distfiles' +rm -f /usr/portage/distfiles/* + diff --git a/gentoo/install-packages.sh b/gentoo/install-packages.sh new file mode 100755 index 0000000..438ebb7 --- /dev/null +++ b/gentoo/install-packages.sh @@ -0,0 +1,41 @@ +#!/bin/bash +set -e + +source /etc/profile + +echo 'Installing packages and upgrading' + +PACKAGES=(net-misc/curl wget man-db openssh ca-certificates sudo) + +echo "Installing additional packages: ${ADDPACKAGES}" +PACKAGES+=" ${ADDPACKAGES}" + +ANSIBLE=${ANSIBLE:-0} +if [[ $ANSIBLE = 1 ]]; then + PACKAGES+=' ansible' +fi + +CHEF=${CHEF:-0} +if [[ $CHEF = 1 ]]; then + echo "Chef installation isn't supported on Gentoo" + exit 1 +fi + +PUPPET=${PUPPET:-0} +if [[ $PUPPET = 1 ]]; then + PACKAGES+=' puppet eix' +fi + +SALT=${SALT:-0} +if [[ $SALT = 1 ]]; then + PACKAGES+=' salt' +fi + +# trying to set capabilities on an unprivileged container fails. +echo "*/* -filecaps" > /etc/portage/package.use/vagrant_overrides + +emerge --sync +emerge --noreplace ${PACKAGES[*]} +emerge -uND @world + +rc-config add sshd default diff --git a/mk-gentoo.sh b/mk-gentoo.sh new file mode 100755 index 0000000..49642b0 --- /dev/null +++ b/mk-gentoo.sh @@ -0,0 +1,47 @@ +#!/bin/bash +set -e + +source common/ui.sh +source common/utils.sh + +if [ "$(id -u)" != "0" ]; then + echo "You should run this script as root (sudo)." + exit 1 +fi + +TODAY=$(date -u +"%Y-%m-%d") +export DISTRIBUTION=gentoo +export RELEASE=current +export ARCH=$(uname -m | sed -e "s/68/38/" | sed -e "s/x86_64/amd64/") +export CONTAINER="vagrant-base-${DISTRIBUTION}-${ARCH}" +export PACKAGE="output/${TODAY}/${CONTAINER}.box" +export NOW=$(date -u) + +echo '############################################' +echo "# Beginning build at $(date)" + +if [ -f ${PACKAGE} ]; then + warn "The box '${PACKAGE}' already exists, skipping..." + echo + exit +fi + +info "Building box to '${PACKAGE}'..." + +./common/download.sh +utils.lxc.start + +SECS=15 +log "Sleeping for $SECS seconds..." +sleep $SECS + +utils.lxc.runscript gentoo/install-packages.sh +./common/prepare-vagrant-user.sh +utils.lxc.runscript gentoo/clean.sh +utils.lxc.stop + +./common/package.sh + +info "Finished building '${PACKAGE}'!" +log "Run \`lxc-destroy -n ${CONTAINER}\` or \`make clean\` to remove the container that was created along the way" +echo