From 02fac0f3d14c3f2a123f559faffdd3d5fd9cdf55 Mon Sep 17 00:00:00 2001
From: DT Netsolution GmbH <daniel.schwager@dtnet.de>
Date: Mon, 6 Feb 2012 14:20:56 +0100
Subject: [PATCH] Fix: fix autopurging bug (probs mit sorting ..) New: Added
 new RELEASE-incrementing algorithm dependent on the VERSION.      Refer to
 opsi-builder.cfg:STATUS_INTEGRATION_RELEASE="func:inc1"

---
 conf/opsi-builder.cfg  | 11 ++++++-----
 lib/builder-targets.sh | 40 +++++++++++++++++++++++++++++-----------
 lib/builder-utils.sh   | 33 +++++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+), 16 deletions(-)

diff --git a/conf/opsi-builder.cfg b/conf/opsi-builder.cfg
index be054a1..bda97bd 100644
--- a/conf/opsi-builder.cfg
+++ b/conf/opsi-builder.cfg
@@ -24,9 +24,10 @@ STATUS="integration"
 # STATUS_INTEGRATION_RELEASE - automatically created release number,
 # if STATUS is set to "integration". Valid values: anything, default value: "`date +%Y%m%d%H%M`"
 # Samples:
-#       "`date +%Y%m%d%H%M`"
-#       "$BUILD_NUMBER"         - jenkins https://wiki.jenkins-ci.org/display/JENKINS/Building+a+software+project#Buildingasoftwareproject-JenkinsSetEnvironmentVariables
-STATUS_INTEGRATION_RELEASE="`date +%Y%m%d%H%M`"
+#       "func:inc1"		- build-in function "inc1": search the opsi-repository for the latested release and increment 1
+#       "`date +%Y%m%d%H%M`"	- command output, e.g. the date format YYMMDD-HHMM
+#       "$BUILD_NUMBER"         - Environment variables, jenkins https://wiki.jenkins-ci.org/display/JENKINS/Building+a+software+project#Buildingasoftwareproject-JenkinsSetEnvironmentVariables
+STATUS_INTEGRATION_RELEASE="func:inc1"
 
 # CHECKSUM_AUTOCREATE - Every downloaded file is checked using SHA1 algorithm 
 # against a checksum file, if this file ist not available, it will be created 
@@ -51,7 +52,7 @@ CREATOR_EMAIL="your-email@domain.de"
 # OPSI repository settings
 #################################
 # Basedirectory storing OPSI-packages after building
-OPSI_REPOS_BASE_DIR=$HOME/.opsi-repository
+OPSI_REPOS_BASE_DIR=$HOME/opsi-repository
 
 # Directory- and filename pattern
 OPSI_REPOS_PRODUCT_DIR=${OPSI_REPOS_BASE_DIR}/${STATUS}/${VENDOR}/${PN}/${VERSION}-${CREATOR_TAG}${RELEASE}
@@ -74,7 +75,7 @@ OPSI_REPOS_FILE_PATTERN=${PN}_${VERSION}-${CREATOR_TAG}${RELEASE}
 #            You must be member of the unix group pcpatch&opsiadmin
 OPSI_REPOS_UPLOAD_OPSI=true
 OPSI_REPOS_UPLOAD_OPSI_ZSYNC=false
-OPSI_REPOS_UPLOAD_SOURCE=true
+OPSI_REPOS_UPLOAD_SOURCE=false
 OPSI_REPOS_UPLOAD_BIN=false
 OPSI_REPOS_OPSIMANAGER_INSTALL=false
 
diff --git a/lib/builder-targets.sh b/lib/builder-targets.sh
index 1f7ec31..4b0e04e 100644
--- a/lib/builder-targets.sh
+++ b/lib/builder-targets.sh
@@ -45,8 +45,13 @@ builder_config() {
     # change some variable from the builder-product.cfg dynamically:
     # autogenerate release number, if we are in status "integration"
     if [ "$STATUS" = "integration" ] ; then
-	# OPSI/control:RELEASE is limited to max 16 chars - take care in regards to the CREATOR_TAG
-	RELEASE="${STATUS_INTEGRATION_RELEASE}"
+	if [ "${STATUS_INTEGRATION_RELEASE}" = "func:inc1" ] ; then
+	    . ${config}
+	    calc_release
+	else
+	    # OPSI/control:RELEASE is limited to max 16 chars - take care in regards to the CREATOR_TAG
+	    RELEASE="${STATUS_INTEGRATION_RELEASE}"
+	fi
     fi
 
     # Read configurationfile
@@ -310,6 +315,8 @@ EOF
    # Purge old product versions - defined by limit OPSI_REPOS_PURGE_LIMIT
    if [ "${OPSI_REPOS_PURGE}" = "true" ]  && [ ! -z "${OPSI_REPOS_PURGE_LIMIT}" ] && [  "${OPSI_REPOS_PURGE_LIMIT}" > 0 ] ; then
        echo "Autopurging enabled"
+
+       # determinte max version to delete
        local limit
        eval "`echo limit=\\$\\{OPSI_REPOS_PURGE_LIMIT_${PN}\\}`"
        if [ -z "$limit" ] || [ ! `expr $limit + 1 2> /dev/null` ]  ; then
@@ -317,10 +324,21 @@ EOF
        fi
        echo "  Purging, max. number of versions: $limit"
 
-       # Find all revision files
+       # Find all revision files and sort them
        local file_list=${OUTPUT_DIR}/product-file-list.txt
-       find ${OPSI_REPOS_BASE_DIR} -name "${PN}-${VERSION}-${CREATOR_TAG}*.cfg" -exec echo {} \; | sort > ${file_list}
-       for cfg_file in `tail -${limit} ${file_list} | ${CMD_comm} -13 - ${file_list}` ; do
+       local file_sort_list=${OUTPUT_DIR}/product-file-sort-list.txt
+       rm -f ${file_list}
+       for cfg_file in `find ${OPSI_REPOS_BASE_DIR} -name "${PN}-${VERSION}-${CREATOR_TAG}*.cfg" -print ` ; do   
+	   . ${cfg_file}
+	   printf "%08d;$cfg_file\n" $REV_RELEASE >> ${file_list}
+       done
+       sort -n ${file_list}  > ${file_sort_list}
+
+       # Delete the oldest files
+       log_debug "base list for calculate purge:"
+       for cfg_sort_file in `tail -${limit} ${file_sort_list} | ${CMD_comm} -13 - ${file_sort_list}` ; do
+
+	   local cfg_file=`echo $cfg_sort_file | cut -f 2 -d ";"`
 	   dir_base=`dirname ${cfg_file}`
 	   . ${cfg_file}
 
@@ -330,12 +348,12 @@ EOF
 	   # Paranoid ... check the files to delete first
 	   if [ ! -z "${dir_base}" ] && [ -d "${OPSI_REPOS_BASE_DIR}" ] ; then
 	       rm -f ${product_file}.* ${cfg_file}
-	   fi
-	   
-	   # remove directory - if it's empty
-	   if [ $(ls -1A ${dir_base} | wc -l) -eq 0 ]; then
-	       rmdir ${dir_base}
-	   fi
+
+  	       # remove directory - if it's empty
+	       if [ $(ls -1A ${dir_base} | wc -l) -eq 0 ]; then
+		   rmdir ${dir_base}
+	       fi
+	   fi	   
        done
    fi
 }
diff --git a/lib/builder-utils.sh b/lib/builder-utils.sh
index 9c8f3b9..1fb28f3 100644
--- a/lib/builder-utils.sh
+++ b/lib/builder-utils.sh
@@ -224,3 +224,36 @@ create_winst_varfile() {
 
     echo >>$var_file
 }
+
+#####################
+# void calc_release() 
+#
+# Description:
+#   Calculate new release based on
+#   the latest one found in the repository.
+# 
+# $RELEASE is set to the calculated release.
+# 
+####################
+function calc_release() {
+
+       # Find all revision files and sort them
+       local file_list=`mktemp /tmp/opsi-builder.calc_release.XXXXXXXXXXX`
+       for cfg_file in `find ${OPSI_REPOS_BASE_DIR} -name "${PN}-${VERSION}-${CREATOR_TAG}*.cfg" -print ` ; do   
+	   . ${cfg_file}
+	   printf "%08d;$cfg_file\n" $REV_RELEASE >> ${file_list}
+       done
+       local oldest_cfg=`sort -n ${file_list} | cut -f 2 -d ";" | tail -1`
+       rm -f ${file_list}
+
+       if [ ! -f "${oldest_cfg}" ] ; then
+	   echo "Warning: no cfg-file found for this product. RELEASE will be set to 1"
+	   RELEASE=1
+       else      
+	   log_debug "calc_release() oldest_cfg: ${oldest_cfg}"
+	   . ${oldest_cfg}
+	   log_debug "  latest release: $REV_RELEASE"
+	   RELEASE=`expr ${REV_RELEASE} + 1 2> /dev/null`
+	   builder_check_error "Cannot incrememnt REV_RELEASE from file ${oldest_cfg}"
+       fi
+}