buildiso: add error handling #174

This commit is contained in:
udeved 2016-02-25 06:40:39 +01:00
parent 9b4638a960
commit 5b5a4694d6
8 changed files with 72 additions and 78 deletions

View file

@ -235,6 +235,7 @@ Usage: buildiso [options]
-x Build images only -x Build images only
-z Generate iso only -z Generate iso only
Requires pre built images (-x) Requires pre built images (-x)
-l Log the build process
-v Verbose output, show profies detail (-q) -v Verbose output, show profies detail (-q)
-q Query settings and pretend build -q Query settings and pretend build
-h This help -h This help

View file

@ -20,8 +20,7 @@ SYSCONFDIR='@sysconfdir@'
import ${LIBDIR}/util.sh import ${LIBDIR}/util.sh
show_profile(){ show_profile(){
eval_edition "$1" prepare_profile "$1"
load_profile "${run_dir}/${edition}/$1"
if ${verbose};then if ${verbose};then
msg2 "work_dir: %s" "${work_dir}" msg2 "work_dir: %s" "${work_dir}"
msg2 "iso_dir: %s" "${iso_dir}" msg2 "iso_dir: %s" "${iso_dir}"
@ -190,4 +189,4 @@ check_requirements
${pretend} && display_settings && exit 1 ${pretend} && display_settings && exit 1
run make_profile "${buildset_iso}" run build "${buildset_iso}"

View file

@ -20,7 +20,6 @@ import ${LIBDIR}/util.sh
import ${LIBDIR}/util-publish.sh import ${LIBDIR}/util-publish.sh
show_profile(){ show_profile(){
eval_edition "$1"
info "Iso: [$1]" info "Iso: [$1]"
prepare_transfer "$1" prepare_transfer "$1"
msg2 "src_dir: ${src_dir}" msg2 "src_dir: ${src_dir}"

View file

@ -100,7 +100,6 @@ configure_accountsservice(){
load_desktop_map(){ load_desktop_map(){
local _space="s| ||g" _clean=':a;N;$!ba;s/\n/ /g' _com_rm="s|#.*||g" \ local _space="s| ||g" _clean=':a;N;$!ba;s/\n/ /g' _com_rm="s|#.*||g" \
file=${DATADIR}/desktop.map file=${DATADIR}/desktop.map
info "Loading [%s] ..." "$file"
local desktop_map=$(sed "$_com_rm" "$file" \ local desktop_map=$(sed "$_com_rm" "$file" \
| sed "$_space" \ | sed "$_space" \
| sed "$_clean") | sed "$_clean")

View file

@ -18,26 +18,25 @@ error_function() {
error "A failure occurred in %s()." "$1" error "A failure occurred in %s()." "$1"
plain "Aborting..." plain "Aborting..."
fi fi
for mp in ${work_dir}/{root,${custom},live,mhwd,boot}-image;do
umount_image "$mp"
done
exit 2 exit 2
} }
# $1: function # $1: function
run_log(){ run_log(){
local func="$1" local func="$1"
if ${is_log};then local tmpfile=/tmp/$(gen_iso_fn).$func.log logfile=${iso_dir}/$func.log
local tmpfile=/tmp/$(gen_iso_fn).$func.log logfile=${iso_dir}/$func.log logpipe=$(mktemp -u "/tmp/logpipe.XXXXXXXX")
logpipe=$(mktemp -u "/tmp/logpipe.XXXXXXXX") mkfifo "$logpipe"
mkfifo "$logpipe" tee "$tmpfile" < "$logpipe" &
tee "$tmpfile" < "$logpipe" & local teepid=$!
local teepid=$! $func &> "$logpipe"
$func &> "$logpipe" wait $teepid
wait $teepid rm "$logpipe"
rm "$logpipe" cat $tmpfile | perl -pe 's/\e\[?.*?[\@-~]//g' > $logfile
cat $tmpfile | perl -pe 's/\e\[?.*?[\@-~]//g' > $logfile rm "$tmpfile"
rm "$tmpfile"
else
"$func"
fi
} }
run_safe() { run_safe() {
@ -46,7 +45,13 @@ run_safe() {
set -E set -E
restoretrap=$(trap -p ERR) restoretrap=$(trap -p ERR)
trap 'error_function $func' ERR trap 'error_function $func' ERR
run_log "$func"
if ${is_log};then
run_log "$func"
else
"$func"
fi
eval $restoretrap eval $restoretrap
set +E set +E
set +e set +e

View file

@ -42,9 +42,9 @@ squash_image_dir() {
msg2 "Creating SquashFS image. This may take some time..." msg2 "Creating SquashFS image. This may take some time..."
local used_kernel=$(echo ${kernel} | cut -c 6) local used_kernel=$(echo ${kernel} | cut -c 6)
if [[ "${1##*/}" == "mhwd-image" && ${used_kernel} -ge "4" ]]; then if [[ "${1##*/}" == "mhwd-image" && ${used_kernel} -ge "4" ]]; then
mksquashfs "${1}" "${sq_img}" -noappend -comp lz4 || die "Exit ..." mksquashfs "${1}" "${sq_img}" -noappend -comp lz4
else else
mksquashfs "${1}" "${sq_img}" -noappend -comp ${iso_compression} ${highcomp} || die "Exit ..." mksquashfs "${1}" "${sq_img}" -noappend -comp ${iso_compression} ${highcomp}
fi fi
show_elapsed_time "${FUNCNAME}" "${timer_start}" show_elapsed_time "${FUNCNAME}" "${timer_start}"
} }
@ -93,7 +93,7 @@ make_iso() {
msg "Making bootable image" msg "Making bootable image"
# Sanity checks # Sanity checks
[[ ! -d "${work_dir}/iso" ]] && die "[%s/iso] doesn't exist. What did you do?!" "${work_dir}" [[ ! -d "${work_dir}/iso" ]] && return 1
if [[ -f "${iso_dir}/${iso_file}" ]]; then if [[ -f "${iso_dir}/${iso_file}" ]]; then
msg2 "Removing existing bootable image..." msg2 "Removing existing bootable image..."
rm -rf "${iso_dir}/${iso_file}" rm -rf "${iso_dir}/${iso_file}"
@ -134,10 +134,7 @@ make_image_root() {
local path="${work_dir}/root-image" local path="${work_dir}/root-image"
mkdir -p ${path} mkdir -p ${path}
if ! chroot_create "${path}" "${packages}"; then chroot_create "${path}" "${packages}"
umount_image "${path}"
die "Exit %s" "${FUNCNAME}"
fi
pacman -Qr "${path}" > "${path}/root-image-pkgs.txt" pacman -Qr "${path}" > "${path}/root-image-pkgs.txt"
copy_overlay "${profile_dir}/root-overlay" "${path}" copy_overlay "${profile_dir}/root-overlay" "${path}"
@ -157,10 +154,7 @@ make_image_custom() {
mount_root_image "${path}" mount_root_image "${path}"
if ! chroot_create "${path}" "${packages}"; then chroot_create "${path}" "${packages}"
umount_image "${path}"
die "Exit %s" "${FUNCNAME}"
fi
pacman -Qr "${path}" > "${path}/${custom}-image-pkgs.txt" pacman -Qr "${path}" > "${path}/${custom}-image-pkgs.txt"
cp "${path}/${custom}-image-pkgs.txt" ${iso_dir}/$(gen_iso_fn)-pkgs.txt cp "${path}/${custom}-image-pkgs.txt" ${iso_dir}/$(gen_iso_fn)-pkgs.txt
@ -188,10 +182,7 @@ make_image_live() {
mount_root_image "${path}" mount_root_image "${path}"
fi fi
if ! chroot_create "${path}" "${packages}"; then chroot_create "${path}" "${packages}"
umount_image "${path}"
die "Exit %s" "${FUNCNAME}"
fi
pacman -Qr "${path}" > "${path}/live-image-pkgs.txt" pacman -Qr "${path}" > "${path}/live-image-pkgs.txt"
copy_overlay "${profile_dir}/live-overlay" "${path}" copy_overlay "${profile_dir}/live-overlay" "${path}"
@ -223,10 +214,7 @@ make_image_mhwd() {
${is_custom_pac_conf} && clean_pacman_conf "${path}" ${is_custom_pac_conf} && clean_pacman_conf "${path}"
if ! download_to_cache "${path}" "${packages}"; then download_to_cache "${path}" "${packages}"
umount_image "${path}"
die "Exit %s" "${FUNCNAME}"
fi
copy_cache_mhwd "${work_dir}/mhwd-image" copy_cache_mhwd "${work_dir}/mhwd-image"
@ -266,12 +254,9 @@ make_image_boot() {
mount_root_image "${path}" mount_root_image "${path}"
fi fi
copy_initcpio "${profile_dir}" "${path}" || die "Failed to copy initcpio." copy_initcpio "${profile_dir}" "${path}"
if ! gen_boot_image "${path}"; then gen_boot_image "${path}"
umount_image "${path}"
die "Exit %s" "${FUNCNAME}"
fi
mv ${path}/boot/${iso_name}.img ${path_iso}/${arch}/${iso_name}.img mv ${path}/boot/${iso_name}.img ${path_iso}/${arch}/${iso_name}.img
[[ -f ${path}/boot/intel-ucode.img ]] && copy_ucode "${path}" "${path_iso}" [[ -f ${path}/boot/intel-ucode.img ]] && copy_ucode "${path}" "${path_iso}"
@ -570,41 +555,20 @@ check_profile_vars(){
fi fi
} }
# $1: profile
load_profile(){
profile_dir=$1
local prof=${1##*/}
info "Profile: [%s]" "$prof"
check_profile_sanity "${profile_dir}"
load_profile_config "${profile_dir}/profile.conf" || die "%s is not a valid profile!" "${profile_dir}"
check_profile_vars
iso_file=$(gen_iso_fn).iso
check_custom_pacman_conf "${profile_dir}"
mkchroot_args+=(-C ${pacman_conf} -S ${mirrors_conf} -B "${build_mirror}/${branch}" -K)
work_dir=${chroots_iso}/$prof/${arch}
iso_dir="${cache_dir_iso}/${edition}/$prof/${dist_release}/${arch}"
prepare_dir "${iso_dir}"
}
sign_iso(){ sign_iso(){
su ${OWNER} -c "signfile ${iso_dir}/$1" su ${OWNER} -c "signfile ${iso_dir}/$1"
} }
compress_images(){ compress_images(){
local timer=$(get_timer) local timer=$(get_timer)
make_iso run_safe "make_iso"
make_checksum "${iso_file}" make_checksum "${iso_file}"
${sign} && sign_iso "${iso_file}" ${sign} && sign_iso "${iso_file}"
chown -R "${OWNER}:users" "${iso_dir}" chown -R "${OWNER}:users" "${iso_dir}"
show_elapsed_time "${FUNCNAME}" "${timer_start}" show_elapsed_time "${FUNCNAME}" "${timer}"
} }
build_images(){ prepare_images(){
local timer=$(get_timer) local timer=$(get_timer)
load_pkgs "${profile_dir}/Packages-Root" load_pkgs "${profile_dir}/Packages-Root"
run_safe "make_image_root" run_safe "make_image_root"
@ -627,13 +591,10 @@ build_images(){
fi fi
run_safe "make_isolinux" run_safe "make_isolinux"
run_safe "make_isomounts" run_safe "make_isomounts"
show_elapsed_time "${FUNCNAME}" "${timer_start}" show_elapsed_time "${FUNCNAME}" "${timer}"
} }
make_profile(){ make_profile(){
eval_edition "$1"
load_profile "${run_dir}/${edition}/$1"
msg "Start building [%s]" "$1" msg "Start building [%s]" "$1"
import ${LIBDIR}/util-iso-${iso_fs}.sh import ${LIBDIR}/util-iso-${iso_fs}.sh
${clean_first} && chroot_clean "${work_dir}" ${clean_first} && chroot_clean "${work_dir}"
@ -643,14 +604,45 @@ make_profile(){
exit 1 exit 1
fi fi
if ${images_only}; then if ${images_only}; then
build_images prepare_images
warning "Continue compress: buildiso -p %s -zc ..." "$1" warning "Continue compress: buildiso -p %s -zc ..." "$1"
exit 1 exit 1
else else
build_images prepare_images
compress_images compress_images
fi fi
unset_profile unset_profile
msg "Finished building [%s]" "$1" msg "Finished building [%s]" "$1"
show_elapsed_time "${FUNCNAME}" "${timer_start}" show_elapsed_time "${FUNCNAME}" "${timer_start}"
} }
# $1: profile
load_profile(){
profile_dir=$1
local prof=${1##*/}
info "Profile: [%s]" "$prof"
check_profile_sanity "${profile_dir}"
load_profile_config "${profile_dir}/profile.conf" || die "%s is not a valid profile!" "${profile_dir}"
check_profile_vars
iso_file=$(gen_iso_fn).iso
check_custom_pacman_conf "${profile_dir}"
mkchroot_args+=(-C ${pacman_conf} -S ${mirrors_conf} -B "${build_mirror}/${branch}" -K)
work_dir=${chroots_iso}/$prof/${arch}
iso_dir="${cache_dir_iso}/${edition}/$prof/${dist_release}/${arch}"
prepare_dir "${iso_dir}"
}
prepare_profile(){
edition=$(get_edition $1)
load_profile "${run_dir}/${edition}/$1"
}
build(){
prepare_profile "$1"
make_profile "$1"
}

View file

@ -24,6 +24,7 @@ create_subtree(){
} }
prepare_transfer(){ prepare_transfer(){
edition=$(get_edition $1)
remote_dir="${edition}/$1/${dist_release}/${arch}" remote_dir="${edition}/$1/${dist_release}/${arch}"
src_dir="${run_dir}/${remote_dir}" src_dir="${run_dir}/${remote_dir}"
} }
@ -54,7 +55,6 @@ create_torrent(){
} }
sync_dir(){ sync_dir(){
eval_edition "$1"
prepare_transfer "$1" prepare_transfer "$1"
${torrent_create} && create_torrent "$1" ${torrent_create} && create_torrent "$1"
${remote_create} && create_subtree "$1" ${remote_create} && create_subtree "$1"

View file

@ -28,7 +28,6 @@ list_sets(){
echo $prof echo $prof
} }
# $1: sets_dir # $1: sets_dir
# $2: buildset # $2: buildset
eval_buildset(){ eval_buildset(){
@ -39,11 +38,11 @@ eval_buildset(){
${is_buildset} && read_set $1/$2 ${is_buildset} && read_set $1/$2
} }
eval_edition(){ get_edition(){
local result=$(find ${run_dir} -maxdepth 2 -name "$1") path local result=$(find ${run_dir} -maxdepth 2 -name "$1") path
[[ -z $result ]] && die "%s is not a valid profile or buildset!" "$1" [[ -z $result ]] && die "%s is not a valid profile or buildset!" "$1"
path=${result%/*} path=${result%/*}
edition=${path##*/} echo ${path##*/}
} }
get_timer(){ get_timer(){