From 31fcc001e41e3fc35c659db27eabe7a48e7510c1 Mon Sep 17 00:00:00 2001 From: Holger Sielaff Date: Thu, 26 Jun 2025 10:01:07 +0200 Subject: [PATCH] Initial --- build-package | 377 +++++++++++++++++++++ deploy-package | 30 ++ internal_stuff/repo-server/update-apt-repo | 25 ++ internal_stuff/reset-package | 32 ++ internal_stuff/update-build-submodule | 11 + lib.sh | 18 + 6 files changed, 493 insertions(+) create mode 100755 build-package create mode 100755 deploy-package create mode 100644 internal_stuff/repo-server/update-apt-repo create mode 100755 internal_stuff/reset-package create mode 100755 internal_stuff/update-build-submodule create mode 100644 lib.sh diff --git a/build-package b/build-package new file mode 100755 index 0000000..2b49fa9 --- /dev/null +++ b/build-package @@ -0,0 +1,377 @@ +#!/bin/bash +set -e + +cd $(dirname $(realpath $0))/.. + +edit_config_files=() +force=false +no_package_update=false +force_package_update=false +delete_old=false + +erro(){ + echo -e "\e[31m$*\e[0m" +} +info(){ + echo -e "\e[36m$*\e[0m" +} +light(){ + echo -e "\e[37m$*\e[0m" +} + +usage() { + echo + light "Usage: $0 [-pfFUuhD] [-v ] [-c , ...] [-e , ...]" + echo + echo " OPTIONS:" + echo + info " -v " + echo " Forces the " + echo + info " -f Force" + echo " Upload without asking to git-packages" + echo + info " -F Force all" + echo " Like '-f -U' Do package-update and upload without asking to git-packages" + echo " Also deletes ALL previous Version" + echo + info " -D Delete previous" + echo " Deletes ALL previous Version" + echo + info " -U Package Update" + echo " Force Package Update Process without asking" + echo + info " -u No Package Update" + echo " Disables asking for Update and ommits" + echo + info " -e " + echo " These files may be edited before ./bin/package-update" + echo " Multiple are possible" + erro " If ./bin/package-update.d/configs exists, the files in this file are edited (too)" + echo + info " -h Help" + echo " Print this help and exit" +} + +while getopts "uUDhfFv:e:" opt; do + case ${opt} in + u) + no_package_update=true + ;; + U) + force_package_update=true + ;; + h) + usage + exit 0 + ;; + e) + edit_config_files+=("$OPTARG") + ;; + f) + force=true + ;; + F) + force=true + force_package_update=true + delete_old=true + ;; + v) + version="$OPTARG" + ;; + D) + delete_old=true + ;; + \?) + usage + exit 1 + ;; + esac +done + +$no_package_update && $force_package_update && ( force_package_update=false; no_package_update=false ) + + + +[[ ! -f apt.conf.sh ]] && echo "No apt.conf.sh in $(pwd) - aborting" && exit 1 +[[ ! -d .git ]] && echo "No .git - aborting" && exit 1 + +if [[ ! -z "$(git status -s)" ]]; then + echo -e "\e[3;31mCommitte bitte erst die Änderungen\e[0m" + git status -s + exit 1 +fi + +git fetch --tags +git pull --all + + + +. apt.conf.sh + +# from apt.conf.sh: +[[ -z "$PACKAGE_NAME" ]] && echo "Must have 'PACKAGE_NAME=...' in apt.conf" && exit 2 +[[ -z "$INSTALLDIR" ]] && echo "Must have 'INSTALLDIR=...' in apt.conf" && exit 2 +[[ ! "$INSTALLDIR" =~ ^/ ]] && echo "$INSTALLDIR be absolute" && exit 2 +[[ -z "$DESCRIPTION" ]] && echo "Must have 'DESCRIPTION=...' in apt.conf" && exit 2 + +[[ -f version-affix.txt ]] && PACKAGE_NAME+="-$(cat version-affix.txt)" + + +REPO_NAME='stable' +MAIN_VERSION="" +temp_changelog="/tmp/changelog-$PACKAGE_NAME.txt" +temp_packageupdate="/tmp/packageupdate-$PACKAGE_NAME.log" + +[[ -f main-version.txt ]] && MAIN_VERSION=$(cat main-version.txt) + +branch=$(git branch -q | grep '*' | sed 's/\* *//') + +# Das erlaubt uns zB ein Odoo 18.0 zu packen (18.0 ist dann main) +if [[ "$branch" != 'main' ]] && [[ "$branch" != "$MAIN_VERSION" ]]; then + echo -e "\e[3;31mDu bist nicht auf \e[1;29mmain \e[3;31m - nach \e[1;29mtesting \e[3;31m deployen?" + read -p "Bitte eingeben [N,y]: " ok + [[ "$ok" =~ y|Y ]] || echo "Steige aus" && exit 0 + REPO_NAME='testing' + echo "Die Funktion gibt es noch nicht - es kann nur main oder MAIN_VERSION gepackt werden :(" && exit +fi + +# we have to do the update process here because it may change the main-version.txt +PACKAGE_UPDATE=$force_package_update +if $no_package_update; then + PACKAGE_UPDATE=false +elif ! $force_package_update; then + if [[ -f bin/package-update ]]; then + read -p "Soll ein package-update gemacht werden? [N,y]: " _PU + [[ "$_PU" =~ y|Y ]] && PACKAGE_UPDATE=true + fi +fi + +if $PACKAGE_UPDATE; then + if [[ -f bin/package-update.d/configs ]]; then + info "Edit configfiles in package-update.d/configs" + while read cfgf; do + [[ ! -f "$cfgf" ]] && erro "$cfgf does not exist" && continue + read -p "Edit $cfgf? [N,y]: " _edok + [[ "$_edok" =~ y|Y ]] || continue + vi $cfgf + git add $cfgf + while [[ -z "$commit_msg" ]]; do + read -p $(info "Commit Message: ") commit_msg + done + git commit -m "$commit_msg" + done < bin/package-update.d/configs + fi + if [[ ! -z "$edit_config_files" ]]; then + info "Edit configfiles from params (-e)" + for cfgf in "${edit_config_files[@]}"; do + [[ ! -f "$cfgf" ]] && erro "$cfgf does not exist" && continue + vi $cfgf + git add $cfgf + while [[ -z "$commit_msg" ]]; do + read -p $(info "Commit Message: ") commit_msg + done + git commit -m "$commit_msg" + done + fi + + ./bin/package-update 2>&1 | tee -a $temp_packageupdate + + if [[ "${PIPESTATUS[0]}" != "0" ]]; then + read -p "Das ging irgendwie schief (s. Ausgabe) - trotzdem packen? [N,y]: " proceed + [[ "$proceed" =~ y|Y ]] || error "Steige aus" && exit 16 + fi +fi + + +ARCHITECTURE="all" +get_version() { + [[ ! -z "$version" ]] && echo -n "" && return 0 + git tag | grep -Pe '^build-' | sort -V | tail -1 | sed 's/^build-//' +} +VERSION=$(get_version) + +if [[ -z $VERSION ]]; then + VERSION=${version-0.0.1} + [[ ! -z "$MAIN_VERSION" ]] && VERSION="${MAIN_VERSION}-${VERSION}" + update=0 +else + # get "real" version without main version + _update=1 + if [[ "$VERSION" =~ - ]]; then + _MAIN_VERSION=$(echo $VERSION | perl -pe 's/^([^\-]+)-.*/\1/') + VERSION=$(echo $VERSION | perl -pe 's/^[^\-]+-(\d+\.\d+\.\d+).*/\1/') + if [[ ! -z "$MAIN_VERSION" ]]; then + _HIGHER_VERSION=$((echo "$_MAIN_VERSION" && echo "$MAIN_VERSION") | sort -V | tail -1) + [[ "$_HIGHER_VERSION" != "$MAIN_VERSION" ]] && echo "Main version aus file ist kleiner als die aus den tags ($MAIN_VERSION < $_MAIN_VERSION)" && exit 2 + if [[ "$_MAIN_VERSION" != "$MAIN_VERSION" ]]; then + # Upgrade Main Version + VERSION="${MAIN_VERSION}-0.0.1" + _update=0 + fi + fi + fi + update=$_update +fi + +if [ $update -eq 1 ]; then + echo "Welche Art von Update?" + echo "1) Major (x.0.0) - Für große Änderungen" + echo "2) Minor (0.x.0) - Für neue Features" + echo "3) Patch (0.0.x) - Für Bugfixes" + read -p "Wähle (1-3): " update_type + + IFS='.' read -r -a version_parts <<< "$VERSION" + major="${version_parts[0]}" + minor="${version_parts[1]}" + patch="${version_parts[2]}" + + [[ -z "$update_type" ]] && update_type=3 + + case $update_type in + 1) # major + major=$((major + 1)) + minor=0 + patch=0 + ;; + 2) # minor + minor=$((minor + 1)) + patch=0 + ;; + 3) # patch + patch=$((patch + 1)) + ;; + esac + + NEW_VERSION="${major}.${minor}.${patch}" + [[ -z $MAIN_VERSION ]] || NEW_VERSION="${MAIN_VERSION}-${NEW_VERSION}" +else + NEW_VERSION=$VERSION +fi + +relevant=() + +while IFS= read -r line; do + if [[ "$line" =~ build- ]]; then + echo -e "\e[1;31m$line\e[0m" 1>&2 + break + fi + relevant+=("$line") # Füge die relevante Zeile zum Array hinzu +done < <(git log --oneline --decorate) + +echo "$PACKAGE_NAME ($NEW_VERSION) stable; urgency=medium" > $temp_changelog +echo "----------------------------------------------------------------------" >> $temp_changelog +for line in "${relevant[@]}"; do + echo -e "* $line\n" >> $temp_changelog +done +echo >> $temp_changelog +if grep -qi 'extracting archive' $temp_packageupdate ; then + grep -i "extracting archive" $temp_packageupdate | sed 's/extracting archive *//' >> $temp_changelog + echo >> $temp_changelog +fi +echo >> $temp_changelog +echo "-- $(git config user.name) <$(git config user.email)> $(date -R)" >> $temp_changelog +echo "----------------------------------------------------------------------" >> $temp_changelog + +cat debian/changelog >> $temp_changelog + +BUILD_DIR="build" +rm -rf $BUILDDIR +DEBIAN_DIR="${BUILD_DIR}/DEBIAN" +INSTALL_DIR="${BUILD_DIR}${INSTALLDIR}" + +mkdir -p "${DEBIAN_DIR}" +mkdir -p "${INSTALL_DIR}" + +CONTROLFILE="${DEBIAN_DIR}/control" + +cat > $CONTROLFILE << EOF +Package: ${PACKAGE_NAME} +Version: ${NEW_VERSION} +Section: web +Priority: optional +Architecture: ${ARCHITECTURE} +Pre-Depends: $DEPENDENCIES +EOF +if [[ ! -z "$SUGGESTS" ]]; then + echo "Suggests: $SUGGESTS" >> $CONTROLFILE +fi +cat >> $CONTROLFILE << EOF +Maintainer: $(git config user.name) <$(git config user.email)> +Description: $DESCRIPTION +EOF + + +for inst_script in preinst conffiles; do + [[ ! -f debscripts/$inst_script ]] && echo "No $inst_script - continue" 1>&2 && continue + cp debscripts/$inst_script $DEBIAN_DIR + chmod 755 $DEBIAN_DIR/$inst_script +done +postinst=debscripts/postinst +POSTINST=$DEBIAN_DIR/postinst + +echo "Create postinst" +echo '#!/bin/bash' > $POSTINST +find src/ -type f -path '*/etc/sudoers.d/*' -exec bash -c 'fn={};echo chown -v root:root ${fn/src/}' \; >> $POSTINST +find src/ -type f -path '*/etc/sudoers.d/*' -exec bash -c 'fn={};echo chmod -v 440 ${fn/src/}' \; >> $POSTINST +find src/ -type f -path '*/etc/cron.*/*' -exec bash -c 'fn={};echo chmod -v 700 ${fn/src/};' \; >> $POSTINST + +if [[ -f $postinst ]]; then + if head -n 1 $postinst | grep -q 'bash'; then + echo "Extend postinst" + cat $postinst >> $POSTINST + else + echo "no bash script - taking original" + cat $postinst > $POSTINST + fi +fi +chmod 755 $POSTINST + +[[ -f debscripts/before-copy ]] && . debscripts/before-copy + +if [[ -f debscripts/src-to-builddir ]]; then + . debscripts/src-to-builddir +else + rsync -av --exclude .gitignore src/ $INSTALL_DIR +fi + +# find $INSTALL_DIR -path '*/etc/sudoers.d*' -type f -exec bash -c "sudo chown root:root {};sudo chmod 400 {};" \; + +[[ -f debscripts/after-copy ]] && . debscripts/after-copy + +cp $temp_changelog "${DEBIAN_DIR}/changelog" +gzip -9 -n "${DEBIAN_DIR}/changelog" + +distfile="dists/$REPO_NAME/main/binary-amd64/${PACKAGE_NAME}_${NEW_VERSION}_${ARCHITECTURE}.deb" +mkdir -p "dists/$REPO_NAME/main/binary-amd64" + +[[ -f debscripts/before-build ]] && . debscripts/before-build + +dpkg-deb --build "${BUILD_DIR}" $distfile +# ME=$(whoami) +# sudo chown -R $ME:$ME $distfile $BUILD_DIR + +[[ -f debscripts/after-build ]] && . debscripts/after-build + +echo "Paket wurde erstellt: $distfile" + +rm -rf "${BUILD_DIR}" + +if [[ -z $DEBBUILD_ASSUME_YES ]] && ! $force; then + read -p "Auf git-packages hochladen? [Y,n]: " upload + [[ "$upload" =~ n|N ]] && exit 0 +fi + +[[ -f debscripts/before-deploy ]] && . debscripts/before-deploy + +. $(dirname $0)/deploy-package + +[[ -f debscripts/after-deploy ]] && . debscripts/after-deploy + +echo "$NEW_VERSION" > version.txt +mv $temp_changelog debian/changelog +git add version.txt debian/changelog +git commit -m "Deploy $NEW_VERSION" +git tag -a "build-$NEW_VERSION" -m "Deploy builded $NEW_VERSION" +git push +git push --tags diff --git a/deploy-package b/deploy-package new file mode 100755 index 0000000..e558549 --- /dev/null +++ b/deploy-package @@ -0,0 +1,30 @@ +#!/bin/bash +cd $(dirname $(realpath $0))/.. + +[[ ! -f apt.conf.sh ]] && echo "No apt.conf.sh in $(pwd) - aborting" && exit 1 + +. apt.conf.sh + +[[ -z $REPO_NAME ]] && REPO_NAME='stable' + +basepath=$DEBIAN_BASEPATH +dpath=dists/$REPO_NAME/main/binary-amd64/ + +# in case this is unset - false just stays, since false != -z (???) +[[ -z "$delete_old" ]] && delete_old=false + +if ! $delete_old; then + read -p "$(echo -e "\e[1;31mSollen alle alten Versionen gelöscht werden? [N,y]: \e[0m")" del_old + [[ "$del_old" =~ y|Y ]] && delete_old=true +fi + +if $delete_old; then + _delcmd="find $basepath -name '${PACKAGE_NAME}_*.deb' -delete" + echo "sudo -uuser ssh $DEBHOST $_delcmd" + sudo -uuser ssh $DEBHOST $_delcmd +fi + +sudo -uuser rsync -av $dpath $DEBHOST:$basepath/pool/main/ +find dists -name '*.deb' -delete +sudo -uuser ssh $DEBHOST bash $basepath/update-apt-repo + diff --git a/internal_stuff/repo-server/update-apt-repo b/internal_stuff/repo-server/update-apt-repo new file mode 100644 index 0000000..49e130d --- /dev/null +++ b/internal_stuff/repo-server/update-apt-repo @@ -0,0 +1,25 @@ +#!/bin/bash +cd $(dirname $(realpath $0)) + +mkdir -p dists/stable/main/binary-all +mkdir -p dists/stable/main/binary-amd64 + +SIGNKEY=$1 +[[ -z "$SIGNKEY" ]] && SIGNKEY=$SIGN_KEY +[[ -z "$SIGNKEY" ]] && echo "No SIGNKEY (or SIGN_KEY in apt.conf.sh)" && exit 1 + + +# Packages Dateien für die Architekturen erstellen +dpkg-scanpackages --multiversion pool/main/. /dev/null > dists/stable/main/binary-all/Packages +gzip -k -f dists/stable/main/binary-all/Packages + +# Release Datei erstellen +apt-ftparchive release \ + -c=conf/distributions.stable \ + dists/stable > dists/stable/Release + +# Release signieren +gpg --export --armor $SIGNKEY > repos.gpg.key +rm -f dists/stable/Release.gpg dists/stable/InRelease +gpg --default-key $SIGNKEY -abs -o dists/stable/Release.gpg dists/stable/Release +gpg --default-key $SIGNKEY --clearsign -o dists/stable/InRelease dists/stable/Release diff --git a/internal_stuff/reset-package b/internal_stuff/reset-package new file mode 100755 index 0000000..b4219a1 --- /dev/null +++ b/internal_stuff/reset-package @@ -0,0 +1,32 @@ +#!/bin/bash + +cd $(dirname $0)/.. +. apt.conf.sh + +echo "$*" | grep -qi main && version="1.0.0" +echo "$*" | grep -qi minor && version="0.1.0" +echo "$*" | grep -qi patch && version="0.0.1" + +[[ -z "$version" ]] && read -p "Auf welche Version? [0.0.1]: " version +[[ ! "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] && echo "Falsche Version, nehme 0.0.1" && version="0.0.1" + + +for i in $(git tag | grep "build-"); do + git tag -d "$i" + git push origin :$i +done + +git submodule update --remote build-scripts || echo +git commit -am 'Updated Build scripts' || echo + +ssh user@$DEBHOST find /var/www/$DEBHOST/debian -name "*${PACKAGE_NAME}_*.deb" -delete 2> /dev/null +if [[ -f ./bin/package-update ]] && echo "$*" | grep -qv -- '--force'; then + read -p "Package Update? [N,y]: " pu + [[ "$pu" =~ y|Y ]] && ./bin/package-update +fi +git rm -f version.txt || echo -n +> debian/changelog +git commit -am 'reset build' +DEBBUILD_ASSUME_YES=y ./bin/build-package -v $version -m Initial + + diff --git a/internal_stuff/update-build-submodule b/internal_stuff/update-build-submodule new file mode 100755 index 0000000..65f063d --- /dev/null +++ b/internal_stuff/update-build-submodule @@ -0,0 +1,11 @@ +#!/bin/bash +[[ ! -f $i/apt.conf.sh ]] && echo "No apt.conf.sh" && exit 1 +cd $i; +echo "Update build-scripts submodule in $i" +git submodule update --remote build-scripts > /dev/null 2>&1; +if [[ ! -z "$(git status -s)" ]]; then + git add build-scripts + git commit -m 'updated buildscript submodule' +else + echo "Unchanged" +fi diff --git a/lib.sh b/lib.sh new file mode 100644 index 0000000..a809fc9 --- /dev/null +++ b/lib.sh @@ -0,0 +1,18 @@ +check_apt() { + [[ ! -f apt.conf.sh ]] && echo "No apt.conf.sh - cd to package root directory" && exit 128 +} + +is_yes(){ + [[ "$1" =~ y|Y|j|J ]] && return true + return false +} + +is_no(){ + [[ "$1" =~ n|N ]] && return true + return false +} + +actual_branch() { + git rev-parse --abbrev-ref HEAD +} +