Skip to content

Commit

Permalink
More cleanup_and_exit refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mlschroe committed Sep 21, 2016
1 parent 619d2aa commit 7cb3657
Show file tree
Hide file tree
Showing 21 changed files with 75 additions and 120 deletions.
14 changes: 9 additions & 5 deletions build
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,7 @@ EOT
}

usage () {
echo "Usage: `basename $0` [--no-init|--clean|--rpms path|--verify|--help] [dir-to-build|recipe-to-build]"
cleanup_and_exit 1
cleanup_and_exit 1 "Usage: `basename $0` [--no-init|--clean|--rpms path|--verify|--help] [dir-to-build|recipe-to-build]"
}

#
Expand All @@ -376,7 +375,13 @@ usage () {
cleanup_and_exit () {
trap EXIT
test -z "$1" && set 0
test -n "$2" && echo "$2"
if test -n "$2" ; then
if "$1" -ne 0 ; then
echo "$2" >&2
else
echo "$2"
fi
fi
if test -z "$BUILD_OPTIONS_PARSED" ; then
# if we haven't parsed the options yet we do
# not know the correct build root. just exit.
Expand Down Expand Up @@ -708,8 +713,7 @@ export HOST

needarg() {
if test -z "$ARG" ; then
echo "$PARAM needs an agrument" >&2
cleanup_and_exit 1
cleanup_and_exit 1 "$PARAM needs an agrument"
fi
}

Expand Down
9 changes: 3 additions & 6 deletions build-pkg
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,14 @@ pkg_runscripts() {

pkg_autodetect_type() {
if test -n "$PREINSTALL_IMAGE" ; then
echo "cannot autodetect build type when using a preinstall image" >&2
cleanup_and_exit 1
cleanup_and_exit 1 "cannot autodetect build type when using a preinstall image"
fi
PSUF=
test -e $BUILD_ROOT/.init_b_cache/rpms/rpm.rpm && PSUF=rpm
test -e $BUILD_ROOT/.init_b_cache/rpms/dpkg.deb && PSUF=deb
test -e $BUILD_ROOT/.init_b_cache/rpms/pacman.arch && PSUF=arch
if test -z "$PSUF" ; then
echo "could not autodetect package type" >&2
cleanup_and_exit 1
cleanup_and_exit 1 "could not autodetect package type"
fi
}

Expand All @@ -86,8 +84,7 @@ pkg_set_type() {
pkg_autodetect_type
;;
*)
echo "unknown package type '$PSUF'" >&2
cleanup_and_exit 1
cleanup_and_exit 1 "unknown package type '$PSUF'"
;;
esac
}
6 changes: 2 additions & 4 deletions build-recipe
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ recipe_set_buildtype() {
*.livebuild) BUILDTYPE=livebuild ;;
esac
if test -z "$BUILDTYPE" ; then
echo "I don't know how to build $RECIPEFILE"
cleanup_and_exit 1
cleanup_and_exit 1 "I don't know how to build $RECIPEFILE"
fi
# we can't query right after vm startup, so we put the BUILDENGINE in the build.data
if test -z "$RUNNING_IN_VM" ; then
Expand Down Expand Up @@ -142,7 +141,6 @@ expand_recipe_directories() {
fi
done
if test -z "$RECIPEFILES" ; then
echo "no recipe files found in $@. exit..."
cleanup_and_exit 1
cleanup_and_exit 1 "no recipe files found in $@. exit..."
fi
}
3 changes: 1 addition & 2 deletions build-recipe-arch
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ recipe_setup_arch() {
recipe_prepare_arch() {
echo "Preparing sources..."
if ! _arch_recipe_makepkg -so "2>&1" ">/dev/null" ; then
echo "failed to prepare sources"
cleanup_and_exit 1
cleanup_and_exit 1 "failed to prepare sources"
fi
}

Expand Down
3 changes: 1 addition & 2 deletions build-recipe-debbuild
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
recipe_setup_debbuild() {
TOPDIR=`chroot $BUILD_ROOT su -c "debbuild --eval '%_topdir'" - $BUILD_USER`
if test -z "$TOPDIR"; then
echo "Error: TOPDIR empty"
cleanup_and_exit 1
cleanup_and_exit 1 "Error: TOPDIR empty"
fi
test "$DO_INIT_TOPDIR" = false || rm -rf "$BUILD_ROOT$TOPDIR"
mkdir -p "$BUILD_ROOT$TOPDIR"
Expand Down
3 changes: 1 addition & 2 deletions build-recipe-debootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ recipe_build_debootstrap() {
echo "running debootstrap..."
if ! "$@" || ! chroot $BUILD_ROOT dpkg --configure -a; then
cat $BUILD_ROOT/debootstrap/debootstrap.log
echo "Failed to setup debootstrap chroot"
cleanup_and_exit 1
cleanup_and_exit 1 "Failed to setup debootstrap chroot"
fi

# adapt passwd
Expand Down
3 changes: 1 addition & 2 deletions build-recipe-dsc
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ recipe_prepare_dsc() {
RELEASEARGS="--release $DEB_RELEASE"
fi
if ! debtransform $CHANGELOGARGS $RELEASEARGS $BUILD_ROOT$TOPDIR/SOURCES $BUILD_ROOT$TOPDIR/SOURCES/$RECIPEFILE $BUILD_ROOT$TOPDIR/SOURCES.DEB ; then
echo "debian transforming failed."
cleanup_and_exit 1
cleanup_and_exit 1 "debian transforming failed."
fi
DEB_SOURCEDIR=$TOPDIR/SOURCES.DEB
for DEB_DSCFILE in $BUILD_ROOT/$DEB_SOURCEDIR/*.dsc ; do : ; done
Expand Down
3 changes: 1 addition & 2 deletions build-recipe-kiwi
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,7 @@ recipe_setup_kiwi() {
cp -lR "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
fi
if test "$?" != 0 ; then
echo "source copy failed"
cleanup_and_exit 1
cleanup_and_exit 1 "source copy failed"
fi
fi

Expand Down
6 changes: 2 additions & 4 deletions build-recipe-livebuild
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ recipe_build_livebuild() {
# Sanity check to not configure archives inside configuration
files=($BUILD_ROOT/$TOPDIR/$LIVEBUILD_ROOT/config/archives/*)
if [ ${#files[@]} -gt 0 ]; then
echo "E: No configuration in config/archives/* allowed"
cleanup_and_exit 1
cleanup_and_exit 1 "E: No configuration in config/archives/* allowed"
fi

# TODO: Add the repository public key
Expand Down Expand Up @@ -226,8 +225,7 @@ recipe_build_livebuild() {

# Fail the build if no build results are found
if [ -z "${build_results}" ] ; then
echo "No live-build result found"
cleanup_and_exit 1
cleanup_and_exit 1 "No live-build result found"
fi

# move created products (and their metadata files) to destination
Expand Down
2 changes: 1 addition & 1 deletion build-recipe-simpleimage
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ recipe_build_simpleimage() {
rm -f $BUILD_ROOT/.build.packages
ln -s ${TOPDIR#/} $BUILD_ROOT/.build.packages
test -d "$SRCDIR" && cd "$SRCDIR"
cleanup_and_exit 0
cleanup_and_exit
}

recipe_resultdirs_simpleimage() {
Expand Down
3 changes: 1 addition & 2 deletions build-recipe-snapcraft
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ recipe_build_snapcraft() {

# Fail the build if no build results are found
if [ -z "${build_results}" ] ; then
echo "No live-build result found"
cleanup_and_exit 1
cleanup_and_exit 1 "No live-build result found"
fi

# move created products (and their metadata files) to destination
Expand Down
9 changes: 3 additions & 6 deletions build-recipe-spec
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
recipe_setup_spec() {
TOPDIR=`chroot $BUILD_ROOT su -c "rpm --eval '%_topdir'" - $BUILD_USER`
if test -z "$TOPDIR"; then
echo "Error: TOPDIR empty"
cleanup_and_exit 1
cleanup_and_exit 1 "Error: TOPDIR empty"
fi
test "$DO_INIT_TOPDIR" = false || rm -rf "$BUILD_ROOT$TOPDIR"
for i in BUILD RPMS/`uname -m` RPMS/i386 RPMS/noarch SOURCES SPECS SRPMS BUILDROOT OTHER ; do
Expand Down Expand Up @@ -167,8 +166,7 @@ recipe_unpack_srcrpm() {
mkdir -p "$MYSRCDIR"
cd $MYSRCDIR || cleanup_and_exit 1
$BUILD_DIR/unrpm -q $SRCDIR/$RECIPEFILE || {
echo "could not unpack $RECIPEFILE."
cleanup_and_exit 1
cleanup_and_exit 1 "could not unpack $RECIPEFILE."
}
for RECIPEFILE in *.spec ; do : ; done
}
Expand All @@ -185,8 +183,7 @@ recipe_check_file_owners() {
fi
done < <(rpm -qp --qf '[%{FILEUSERNAME} %{FILEGROUPNAME} %{FILENAMES}\n]' $RPMS)
if test -n "$BADFILE" ; then
echo "please fix your filelist (e.g. add defattr)"
cleanup_and_exit 1
cleanup_and_exit 1 "please fix your filelist (e.g. add defattr)"
fi
}

Expand Down
11 changes: 4 additions & 7 deletions build-vm
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ vm_parse_options() {
;;
none|chroot) VM_TYPE= ;;
*)
echo "VM '$VM_TYPE' is not supported"
cleanup_and_exit
cleanup_and_exit 1 "VM '$VM_TYPE' is not supported"
;;
esac
shift
Expand Down Expand Up @@ -346,8 +345,7 @@ vm_img_mkfs() {
eval "tunefs=\"\$vm_img_tunefs_${fs}\""

if test -z "$mkfs"; then
echo "filesystem \"$fs\" is not supported"
cleanup_and_exit 3
cleanup_and_exit 3 "filesystem \"$fs\" is not supported"
fi

echo "Creating $fs filesystem on $img"
Expand Down Expand Up @@ -442,7 +440,7 @@ vm_set_personality_syscall() {
sparc*) PERSONALITY_SYSCALL=191 ;;
ia64*) PERSONALITY_SYSCALL=1140 ;;
i?86*|ppc*|aarch64*|arm*|sh4|cris|m68k*|s390*|unicore32|microblaze) PERSONALITY_SYSCALL=136 ;;
*) echo "Unknown architecture personality: '$archname'"; cleanup_and_exit 1 ;;
*) cleanup_and_exit 1 "Unknown architecture personality: '$archname'" ;;
esac
}

Expand Down Expand Up @@ -611,8 +609,7 @@ vm_setup() {
vm_img_create "$VM_SWAP" "$VMDISK_SWAPSIZE"
fi
if test ! -e "$VM_IMAGE" ; then
echo "you need to create $VM_IMAGE first"
cleanup_and_exit 3
cleanup_and_exit 3 "you need to create $VM_IMAGE first"
fi
if test -n "$CLEAN_BUILD" ; then
vm_img_mkfs "$VMDISK_FILESYSTEM" "$VM_IMAGE" || cleanup_and_exit 3
Expand Down
21 changes: 7 additions & 14 deletions build-vm-ec2
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ cloud_volume_detach_ec2() {
vm_verify_options_ec2() {
# verify settings
if test -z "$AWS_ACCESS_KEY" -o -z "$AWS_ACCESS_KEY" ; then
echo "ERROR: No amazon EC2 environment set. Set AWS_ACCESS_KEY and AWS_SECRET_KEY."
cleanup_and_exit 3
cleanup_and_exit 3 "ERROR: No amazon EC2 environment set. Set AWS_ACCESS_KEY and AWS_SECRET_KEY."
fi
. /etc/profile.d/ec2.sh
EC2_INSTANCE_ID=`ec2-instance-id`
Expand All @@ -77,20 +76,16 @@ vm_verify_options_ec2() {
us-gov-west-1) BUILD_EC2_AKI=aki-79a4c05a ;;
esac
if test -z "$BUILD_EC2_AKI" ; then
echo "Unknown Amazon EC2 Zone: $BUILD_EC2_ZONE"
cleanup_and_exit 1
cleanup_and_exit 1 "Unknown Amazon EC2 Zone: $BUILD_EC2_ZONE"
fi
if test -z "$BUILD_EC2_AKI" ; then
echo "ERROR: No image refering to kernel and ramdisk is defined in BUILD_EC2_AKI env."
cleanup_and_exit 3
cleanup_and_exit 3 "ERROR: No image refering to kernel and ramdisk is defined in BUILD_EC2_AKI env."
fi
if test -z "$VM_VOLUME_NAME" ; then
echo "ERROR: No worker root VM volume name specified."
cleanup_and_exit 3
cleanup_and_exit 3 "ERROR: No worker root VM volume name specified."
fi
if test -z "$VM_VOLUME_SWAP" ; then
echo "ERROR: No worker swap VM volume name specified."
cleanup_and_exit 3
cleanup_and_exit 3 "ERROR: No worker swap VM volume name specified."
fi

VM_SWAPDEV=/dev/sdb1 # in the vm
Expand Down Expand Up @@ -154,17 +149,15 @@ vm_cleanup_ec2() {
vm_kill_ec2() {
if ec2-describe-instance-status "$VM_BUILD_INSTANCE" --region "$BUILD_EC2_REGION" >/dev/null 2>&1 ; then
if ec2-terminate-instances "$VM_BUILD_INSTANCE" >/dev/null 2>&1 ; then
echo "could not kill EC2 instance $VM_BUILD_INSTANCE"
cleanup_and_exit 1
cleanup_and_exit 1 "could not kill EC2 instance $VM_BUILD_INSTANCE"
fi
fi
}

vm_startup_ec2() {
EC2_SNAP_root=`ec2-create-snapshot --region "$BUILD_EC2_REGION" "$VM_VOLUME_NAME" | awk '{ print $2 }'`
if test "$EC2_SNAP_root" = "${EC2_SNAP_root#snap-}" ; then
echo "ERROR: Failed to create snapshot for root disk $VM_VOLUME_NAME"
cleanup_and_exit 3
cleanup_and_exit 3 "ERROR: Failed to create snapshot for root disk $VM_VOLUME_NAME"
fi
EC2_SNAP_swap=`ec2-create-snapshot --region "$BUILD_EC2_REGION" "$VM_VOLUME_SWAP" | awk '{ print $2 }'`
if test "$EC2_SNAP_swap" = "${EC2_SNAP_swap#snap-}" ; then
Expand Down
6 changes: 2 additions & 4 deletions build-vm-emulator
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ vm_startup_emulator() {
echo "$@"
if ! "$@"; then
popd
echo "ERROR: The emulator returned with a failure"
cleanup_and_exit 3
cleanup_and_exit 3 "ERROR: The emulator returned with a failure"
fi
popd

Expand All @@ -65,8 +64,7 @@ vm_startup_emulator() {

vm_kill_emulator() {
if ! fuser -k -TERM "$VM_IMAGE" ; then
echo "could not kill build in $VM_IMAGE"
cleanup_and_exit 1
cleanup_and_exit 1 "could not kill build in $VM_IMAGE"
fi
}

Expand Down
21 changes: 5 additions & 16 deletions build-vm-kvm
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,20 @@ kvm_serial_device=
kvm_rng_device=virtio-rng-pci
kvm_options=

function complain()
{
local ex=$1; shift
printf "Error: %s\n" "$@" >&2
cleanup_and_exit $ex
}

kvm_check_ppc970() {
if ! grep -q -E '(kvm_rma_count.*kvm_hpt_count)|(kvm_hpt_count.*kvm_rma_count)' /proc/cmdline ; then
echo "put kvm_rma_count=<VM number> or kvm_hpt_count=<> to your boot options"
cleanup_and_exit 3
cleanup_and_exit 3 "put kvm_rma_count=<VM number> or kvm_hpt_count=<> to your boot options"
fi
}

kvm_check_hugetlb() {
if ! grep -q "$HUGETLBFSPATH" /proc/mounts ; then
echo "hugetlbfs is not mounted to $HUGETLBFSPATH"
cleanup_and_exit 3
cleanup_and_exit 3 "hugetlbfs is not mounted to $HUGETLBFSPATH"
fi
local HUGETLBBLKSIZE=$(stat -f -c "%S" "$HUGETLBFSPATH")
HUGETLBBLKSIZE=$(( ${HUGETLBBLKSIZE:-0} / 1024 ))
if test "$HUGETLBBLKSIZE" -lt 1 -o ! -e "/sys/kernel/mm/hugepages/hugepages-${HUGETLBBLKSIZE}kB" ; then
echo "could not determine hugetlbfs block size"
cleanup_and_exit 3
cleanup_and_exit 3 "could not determine hugetlbfs block size"
fi
local PAGES_FREE=$(cat /sys/kernel/mm/hugepages/hugepages-${HUGETLBBLKSIZE}kB/free_hugepages)
local PAGES_REQ=$(( ${VM_MEMSIZE:-64} * 1024 / $HUGETLBBLKSIZE ))
Expand Down Expand Up @@ -222,7 +212,7 @@ vm_startup_kvm() {
qemu_bin="$kvm_bin"
qemu_args=(-drive file="$VM_IMAGE",format=raw,if=none,id=disk,serial=0,cache=unsafe -device "$kvm_device",drive=disk)
if [ -n "$VM_USER" ] ; then
getent passwd "$VM_USER" > /dev/null || complain 22 "cannot find KVM user '$VM_USER'"
getent passwd "$VM_USER" > /dev/null || cleanup_and_exit 3 "cannot find KVM user '$VM_USER'"
else
# use qemu user by default if available
getent passwd qemu >/dev/null && VM_USER=qemu
Expand Down Expand Up @@ -282,8 +272,7 @@ vm_startup_kvm() {

vm_kill_kvm() {
if ! fuser -k -TERM "$VM_IMAGE" ; then
echo "could not kill build in $VM_IMAGE"
cleanup_and_exit 1
cleanup_and_exit 1 "could not kill build in $VM_IMAGE"
fi
}

Expand Down
Loading

0 comments on commit 7cb3657

Please sign in to comment.