From 9641e58a3dfe6fb223560c3783559d52ca772a62 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sat, 30 Jun 2018 22:59:14 +0000 Subject: [PATCH] Misc cleaning / simplification / cosmetic improvement in build scripts --- init.sh | 1 + scripts/build_deb | 54 +++++++--------------- scripts/common.sh | 48 ++++++++++++++++++++ scripts/ynh-custom-builds | 89 ++++++++++++++++++++---------------- ynh-build | 94 +++++++-------------------------------- 5 files changed, 131 insertions(+), 155 deletions(-) create mode 100644 scripts/common.sh diff --git a/init.sh b/init.sh index 501de0a..10c5888 100755 --- a/init.sh +++ b/init.sh @@ -2,6 +2,7 @@ apt-get install nginx pbuilder reprepro rebuildd gawk sendxmpp -y apt-get install qemu-system-arm debootstrap cdebootstrap qemu-user-static -y apt-get install python-virtualenv python3-pip -y +apt-get install boxes -y # Fix the damn pbuilder-satistydepends (aptitude causes segfault on ARM) cd /usr/lib/pbuilder diff --git a/scripts/build_deb b/scripts/build_deb index 59f5d92..615dac6 100755 --- a/scripts/build_deb +++ b/scripts/build_deb @@ -6,11 +6,13 @@ readonly BRANCH=$2 readonly FOLDER=$3 source /home/vinaigrette/config/config +source /home/vinaigrette/scripts/common.sh + export DEBSIGN_KEYID export DEBFULLNAME export DEBEMAIL -IMG="${PBUILDER_IMAGES}/${DISTRIB}-amd64.tgz" +readonly IMG="${PBUILDER_IMAGES}/${DISTRIB}-amd64.tgz" # ##### # # Usage # @@ -38,7 +40,7 @@ function main() build_sources add_to_reprepro - echo "Build will start soon. See 'rebuildd-job list | tail'" + success "Build will start soon. See 'rebuildd-job list | tail'" sendxmpppy "Added builds for ${PACKAGE}/${VERSION} in branch ${BRANCH} ..." } @@ -48,26 +50,9 @@ function main() function validate_arguments() { - # Distribution - if [[ ! $DISTRIB =~ ^stretch$ ]]; then - echo "Invalid distribution $DISTRIB" - usage - exit 1 - fi - - ## Branch - if [[ ! $BRANCH =~ ^testing|stable|unstable$ ]]; then - echo "Invalid branch $BRANCH" - usage - exit 2 - fi - - # Folder - if [ -z "$FOLDER" ]; then - echo "Need a folder in which to build" - usage - exit 3 - fi + [[ $DISTRIB =~ ^stretch$ ]] || critical "Invalid distribution $DISTRIB" + [[ $BRANCH =~ ^testing|stable|unstable$ ]] || critical "Invalid branch $BRANCH" + [[ ! -z "$FOLDER" ]] || critical "Need a folder in which to build" } function retrieve_package_info() @@ -87,28 +72,19 @@ function retrieve_package_info() function build_sources() { - echo "---------------------------------------------------" - echo "Building source package of ${PACKAGE}_${VERSION}..." - echo "---------------------------------------------------" + boxed "Building source package of ${PACKAGE} ${VERSION} ..." - sudo pbuilder execute --bindmounts ${ROOT_DIR} --basetgz ${IMG} -- ${BUILD_SOURCES} $PKG_DIR + sudo pbuilder execute --bindmounts ${ROOT_DIR} --basetgz ${IMG} -- ${BUILD_SOURCES} $PKG_DIR \ + || critical "An error occured while building source package" - if [ $? -ne 0 ] || [ ! -f ${CHANGES_FILE} ]; then - echo "An error occured while building source package" - exit 1 - fi + [[ -f ${CHANGES_FILE} ]] || critical "An error occured while building source package (no change file available)" } function add_to_reprepro() { - echo "---------------------------------------------------" - echo "Adding ${PACKAGE}_${VERSION} to ${DISTRIB}/${BRANCH}..." - echo "---------------------------------------------------" - $INCLUDE_CHANGES $DISTRIB $BRANCH $CHANGES_FILE - if [ $? -ne 0 ]; then - echo "An error occured while including source package" - exit 1 - fi + boxed "Adding ${PACKAGE}_${VERSION} to ${DISTRIB}/${BRANCH}..." + + $INCLUDE_CHANGES $DISTRIB $BRANCH $CHANGES_FILE || critical "An error occured while including source package" } -main +[[ "$1" =~ ^-h|--help$ ]] && (usage; exit 0) || main diff --git a/scripts/common.sh b/scripts/common.sh new file mode 100644 index 0000000..8692064 --- /dev/null +++ b/scripts/common.sh @@ -0,0 +1,48 @@ +readonly NORMAL=$(printf '\033[0m') +readonly BOLD=$(printf '\033[1m') +readonly faint=$(printf '\033[2m') +readonly UNDERLINE=$(printf '\033[4m') +readonly NEGATIVE=$(printf '\033[7m') +readonly RED=$(printf '\033[31m') +readonly GREEN=$(printf '\033[32m') +readonly ORANGE=$(printf '\033[33m') +readonly BLUE=$(printf '\033[34m') +readonly YELLOW=$(printf '\033[93m') +readonly WHITE=$(printf '\033[39m') + +function success() +{ + local msg=${1} + echo "[${BOLD}${GREEN} OK ${NORMAL}] ${msg}" +} + +function info() +{ + local msg=${1} + echo "[${BOLD}${BLUE}INFO${NORMAL}] ${msg}" +} + +function boxed() +{ + local msg=${1} + echo "$msg" | boxes -d stone +} + +function warn() +{ + local msg=${1} + echo "[${BOLD}${ORANGE}WARN${NORMAL}] ${msg}" 2>&1 +} + +function error() +{ + local msg=${1} + echo "[${BOLD}${RED}FAIL${NORMAL}] ${msg}" 2>&1 +} + +function critical() +{ + local msg=${1} + echo "[${BOLD}${RED}CRIT${NORMAL}] ${msg}" 2>&1 + exit 1 +} diff --git a/scripts/ynh-custom-builds b/scripts/ynh-custom-builds index 0bd443f..0917ddb 100755 --- a/scripts/ynh-custom-builds +++ b/scripts/ynh-custom-builds @@ -1,88 +1,100 @@ #!/bin/bash readonly THISSCRIPT=$0 -readonly BRANCH="testing" +readonly PACKAGE=$1 +readonly BRANCH=$2 readonly DISTRIB="stretch" -readonly DEBIAN_BRANCH="testing" -readonly BUILD_MESSAGE="YunoHost custom build." source /home/vinaigrette/config/config +source /home/vinaigrette/scripts/common.sh + +# ##### # +# Usage # +# ##### # + +function usage() +{ + cat << EOF + +Usage: + $THISSCRIPT + +Arguments: + metronome, rspamd or unscd + stable, testing or unstable + +EOF +} function main() { - build_metronome - build_rspamd - build_unscd + validate_arguments + cd $GIT_REPOS/$PACKAGE + tweak_$PACKAGE + build } -function build_metronome() +# ################# # +# Check user inputs # +# ################# # + +function validate_arguments() { + [[ $PACKAGE =~ ^metronome|rspamd|unscd$ ]] || critical "Invalid package $PACKAGE" + [[ $BRANCH =~ ^testing|stable|unstable$ ]] || critical "Invalid branch $BRANCH" + [[ "$(tty)" != "not a tty" ]] || critical "You aint in a tty (are you in a 'lxc exec' ?) The script can't run because pbuilder won't be happy :|" +} - local PACKAGE="metronome" - - cd $GIT_REPOS/$PACKAGE +# ##################### # +# Build recipe / tweaks # +# ##################### # +function tweak_metronome() +{ # For some reason metronome version must include a revision ("-x") - local VERSION="$(dpkg-parsechangelog -S Version 2>/dev/null)+yunohost-1" - - build $PACKAGE $VERSION + readonly VERSION="$(dpkg-parsechangelog -S Version 2>/dev/null)+yunohost-1" } -function build_unscd() +function tweak_unscd() { - - local PACKAGE="unscd" - - cd $GIT_REPOS/$PACKAGE - - local VERSION="$(dpkg-parsechangelog -S Version 2>/dev/null)+yunohost" - - build $PACKAGE $VERSION + readonly VERSION="$(dpkg-parsechangelog -S Version 2>/dev/null)+yunohost" } -function build_rspamd() +function tweak_rspamd() { - local PACKAGE="rspamd" - - cd $GIT_REPOS/$PACKAGE git checkout master git pull origin master # Get last tag and deduce version number to user... local LASTTAG=$(git tag --list | grep "^1." | sort -n | tail -n1) - local VERSION="${LASTTAG}+yunohost" + readonly VERSION="${LASTTAG}+yunohost" git checkout $LASTTAG - - build $PACKAGE $VERSION } function build() { - local PACKAGE=$1 - local VERSION=$2 - # SVERSION is VERSION without the -1 at the end ... local SVERSION=$(echo $VERSION | tr '-' ' ' | awk '{print $1}') cd $GIT_REPOS/$PACKAGE # Tweak the changelog temporarily - echo "> Setting version in changelog to ${VERSION}" + info "Setting version in changelog to ${VERSION}" rm -f debian/changelog.dch cp debian/changelog debian/changelog.old dch --package "${PACKAGE}" \ --force-bad-version \ -v "${VERSION}" \ - -D "${DEBIAN_BRANCH}" \ + -D "${BRANCH}" \ --force-distribution \ - $BUILD_MESSAGE \ + "YunoHost custom build." \ > /dev/null 2>&1 # Create temporary folder TMP_FOLDER=$(mktemp -d) # Extract git archive a desired tag - echo "> Exporting in $TMP_FOLDER ... " + info "Exporting in $TMP_FOLDER ... " git ls-files | xargs tar -czf archive.tar.gz cat archive.tar.gz | tar -xz -C $TMP_FOLDER # For some reason, these wants archive named @@ -91,15 +103,14 @@ function build() mv archive.tar.gz $TMP_FOLDER/../${PACKAGE}_${SVERSION}.orig.tar.gz # Build Debian package - echo "> Starting build ..." cd $TMP_FOLDER $BUILD_DEB $DISTRIB $BRANCH . # Restore changelog - echo "> Restoring previous changelog" + info "Restoring previous changelog" cd $GIT_REPOS/$PACKAGE cp debian/changelog.old debian/changelog rm debian/changelog.old } -main +[[ "$1" =~ ^-h|--help$ ]] && (usage; exit 0) || main diff --git a/ynh-build b/ynh-build index d00a03b..52a706d 100755 --- a/ynh-build +++ b/ynh-build @@ -8,6 +8,7 @@ readonly VERSION=$4 readonly TAG=$(echo "debian/$VERSION" | tr '~' '-') source /home/vinaigrette/config/config +source /home/vinaigrette/scripts/common.sh # ##### # # Usage # @@ -35,42 +36,17 @@ EOF function validate_arguments() { - - ## Project - if [[ ! $PROJECT =~ ^yunohost|yunohost-admin|moulinette|SSOwat$ ]]; then - echo "Invalid project $PROJECT" - usage - exit 1 - fi - - ## Branch - if [[ ! $BRANCH =~ ^testing|stable|unstable$ ]]; then - echo "Invalid branch $BRANCH" - usage - exit 2 - fi - - # Distribution - if [[ ! $DISTRIB =~ ^stretch$ ]]; then - echo "Invalid distribution $DISTRIB" - usage - exit 3 - fi - - # Version - if [ -z "$VERSION" ]; then - echo "Invalid version $VERSION" - usage - exit 4 - fi + [[ $PROJECT =~ ^yunohost|yunohost-admin|moulinette|SSOwat$ ]] || critical "Invalid project $PROJECT" + [[ $BRANCH =~ ^testing|stable|unstable$ ]] || critical "Invalid branch $BRANCH" + [[ $DISTRIB =~ ^stretch$ ]]; || critical "Invalid distribution $DISTRIB" + [[ ! -z "$VERSION" ]] || critical "Invalid version $VERSION" + [[ "$(tty)" != "not a tty" ]] || critical "You aint in a tty (are you in a 'lxc exec' ?) The script can't run because pbuilder won't be happy :|" } function checkout_tag() { # Update project's repository - echo "" - echo "> Updating $GIT_REPOS/$PROJECT repository ... " - echo "" + boxed "> Updating $GIT_REPOS/$PROJECT repository ... " cd $GIT_REPOS/$PROJECT git fetch --quiet @@ -78,31 +54,18 @@ function checkout_tag() git checkout $DISTRIB-$BRANCH --quiet git reset --hard origin/$DISTRIB-$BRANCH --quiet + # Validate constrains for the version number given (is in changelog + has corresponding tag) + LASTVERSIONCHANGELOG=$(dpkg-parsechangelog -S Version 2>/dev/null) + [[ "$VERSION" == "$LASTVERSIONCHANGELOG" ]] || critical "Version $VERSION is not the last version in changelog" + git rev-parse "$TAG" >/dev/null 2>&1 || critical "Invalid version $VERSION (there's no tag $TAG in the git repo !)" - if [[ "$VERSION" != "$LASTVERSIONCHANGELOG" ]] - then - echo "Version $VERSION is not the last version in changelog" - exit - fi - - # Check if tag really exists - if ! git rev-parse "$TAG" >/dev/null 2>&1; - then - echo "Invalid version $VERSION (there's no tag $TAG in the git repo !)" - exit - fi # Get commit for the tag and for HEAD + TAGCOMMIT=$(git rev-parse "$TAG") HEADCOMMIT=$(git rev-parse "HEAD") - if [[ "$TAGCOMMIT" != "$HEADCOMMIT" ]] - then - echo "Tag $TAG is not the HEAD of the branch :/" - exit - fi - - + [[ "$TAGCOMMIT" == "$HEADCOMMIT" ]] || critical "Tag $TAG is not the HEAD of the branch :/" } function build() @@ -111,15 +74,11 @@ function build() TMP_FOLDER=$(mktemp -d) # Extract git archive a desired tag - echo "" - echo "> Exporting in $TMP_FOLDER ... " - echo "" + info "Exporting in $TMP_FOLDER ... " git archive $TAG --format=tar | tar -x -C $TMP_FOLDER # Build Debian package - echo "###############################" - echo "# Building Debian package ... #" - echo "###############################" + boxed "Building Debian package ... " cd $TMP_FOLDER $BUILD_DEB $DISTRIB $BRANCH . } @@ -127,32 +86,13 @@ function build() function main() { - - # If used asked for help, show usage - if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then - usage - exit 0 - fi - validate_arguments - if [[ "$(tty)" == "not a tty" ]] - then - echo "You aint in a tty (are you in a 'lxc exec' ?)" - echo "The script can't run because pbuilder won't be happy :|" - exit - fi - - # Sum up configuration - echo "########################################################" - echo "# Building $PROJECT $BRANCH release - $VERSION version " - echo "########################################################" + boxed "Building $PROJECT $BRANCH release - $VERSION version" checkout_tag build - } - -main +[[ "$1" =~ ^-h|--help$ ]] && (usage; exit 0) || main