itwatch.itwess/builder/builder-lib.sh
2012-01-25 23:23:38 +01:00

206 lines
5.9 KiB
Bash

#####################
# Call user entry point
####################
call_entry_point() {
# Entry point
type $1 &>/dev/null
if [ $? == 0 ] ; then
$1
fi
}
#####################
# Read config
####################
builder_read_config() {
# Check temp dir
test -d ${TMP_DIR}
builder_check_error "temp directory not available: $TMP_DIR"
# Source product release configuration
test -f ${PRODUCT_DIR}/release.cfg
builder_check_error "can't read release configuration: ${PRODUCT_DIR}/release.cfg"
. $PRODUCT_DIR/release.cfg
# Source product build configuration
test -f ${PRODUCT_DIR}/build.cfg
builder_check_error "can't read release configuration: ${PRODUCT_DIR}/build.cfg"
. $PRODUCT_DIR/build.cfg
# Source local build configuration (must be done AFTER sourcing the release.cfg)
test -f "$BUILD_LOCAL_CFG"
builder_check_error "can't read local buld configuration: $BUILD_LOCAL_CFG"
echo "Using local build configuration: $BUILD_LOCAL_CFG"
. $BUILD_LOCAL_CFG
# Check variables
if [ -z ${OPSI_REPOS_BASE_DIR} ] || [ ! -d ${OPSI_REPOS_BASE_DIR} ] ; then
echo "configuration error: OPSI_REPOS_BASE_DIR is empty or does not exist: $OPSI_REPOS_BASE_DIR"
exit 2
fi
}
#####################
# Download all dist files from one of the defined URLs.
# and validate the checksum
####################
builder_download_dist_files() {
mkdir -p $DIST_CACHE_DIR
echo "Distribution directory: $DIST_CACHE_DIR"
for f1 in $ICON $FILE1 $FILE2 ; do
basename=`echo $f1 | cut -d ";" -f1`
urls=`echo $f1 | cut -d ";" -f2-`
downloaded=0
# Add private repos to the urls
if [ ! -z ${DIST_PRIVATE_REPOS} ]; then
urls="${DIST_PRIVATE_REPOS}/$basename;$urls"
fi
# check existence of CRC file
if [ ! -e ${basename}.sha1sum ] ; then
echo "You need to create the checksums with: sha1sum ${DIST_CACHE_DIR}/${basename} > ${basename}.sha1sum"
exit 1
fi
echo "Downloading $basename"
# check downloading from the defined URLs
for f2 in `echo $urls | sed -e 's/;/\n/g'` ; do
if [ $downloaded == 1 ]; then continue; fi
echo " Info: Downloding from $f2"
# Check, if the URL is a file URL starting with file://
if [ -f ${DIST_CACHE_DIR}/$basename ] && [ -z ${DIST_FORCE_DOWNLOAD} ]; then
echo " Info: File still cached/downloaded. To force a download, set DIST_FORCE_DOWNLOAD=1"
elif [[ $f2 == file://* ]]; then
fileurl=`echo $f2 | sed "s/^file:\/\///"`
cp $fileurl ${DIST_CACHE_DIR}/$basename 2>/dev/null
else
rm -f ${DIST_CACHE_DIR}/$basename
wget --tries=1 -O ${DIST_CACHE_DIR}/$basename --timeout=5 -q --no-verbose $f2
fi
if [ $? == 0 ] ; then
# testing the checksum of the downloaded files
SHA1SUM=`cat ${basename}.sha1sum | cut -d " " -f1`
CHECKSUM=`sha1sum ${DIST_CACHE_DIR}/$basename | cut -d " " -f1`
if [ "$CHECKSUM" == "$SHA1SUM" ] ; then
downloaded=1
echo " Info: Downloaded successfully"
else
echo " Error: The checksums do not match - try next URL"
fi
else
echo " Warning: Failed to download file - try next URL"
fi
done
echo
# Ups - no URL works
if [ $downloaded != 1 ] ; then
echo " Error: can download the file or checksum wrong (sha1sum ${DIST_CACHE_DIR}/${basename} > ${basename}.sha1sum)"
exit 1;
fi
done
}
#####################
# build opsi package
#####################
builder_package() {
OUT=$(mktemp -d $TMP_DIR/opsi-builder.XXXXXXXXXX) || { echo "Failed to create temp dir"; exit 1; }
# Copy files and convert text files to dos format
mkdir $OUT/$PN
cp -Rv OPSI CLIENT_DATA $OUT/$PN
find $OUT/$PN/CLIENT_DATA -type f | xargs -n1 -iREP sh -c 'file -i $0 | grep "text/plain" && dos2unix $0' REP
# copy binaries
for f1 in $FILE1 $FILE2 ; do
basename=`echo $f1 | cut -d ";" -f1`
echo cp ${DIST_CACHE_DIR}/$basename $OUT/$PN/CLIENT_DATA
done
# converting icon file
ICONFILE=`echo $ICON | cut -d ";" -f1`
convert -colorspace rgb ${DIST_CACHE_DIR}/$ICONFILE -transparent white -background transparent -resize 160x160 \
-size 160x160 xc:transparent +swap -gravity center -composite $OUT/$PN/CLIENT_DATA/$PN.png
builder_check_error "converting image"
# replace variables
echo Building OPSI-Package
sed -e "s!VERSION!$VERSION!g" -e "s!RELEASE!$RELEASE!g" -e "s!PRIORITY!$PRIORITY!g" -e "s!ADVICE!$ADVICE!g" -i $OUT/$PN/OPSI/control
sed -e "s!X86FILE!$X86FILE!g" -i $OUT/$PN/CLIENT_DATA/setup32.ins
# Create changelog based on git - if available
if test -d ".git"; then
git log --date-order --date=short | \
sed -e '/^commit.*$/d' | \
awk '/^Author/ {sub(/\\$/,""); getline t; print $0 t; next}; 1' | \
sed -e 's/^Author: //g' | \
sed -e 's/>Date: \([0-9]*-[0-9]*-[0-9]*\)/>\t\1/g' | \
sed -e 's/^\(.*\) \(\)\t\(.*\)/\3 \1 \2/g' > $OUT/$PN/OPSI/changelog.txt
else
echo "No git repository present."
fi
# building package
echo tempdir: $TMP_DIR
pushd ${TMP_DIR}
rm -f ${PN}_${VERSION}-${RELEASE}.opsi
opsi-makeproductfile -v $OUT/$PN
builder_check_error "Building OPSI-package"
popd
# cleanup
rm -rf $OUT
}
#####################
# build opsi package
#####################
builder_upload() {
# Upload file to repository
mkdir -p ${OPSI_REPOS_PRODUCT_DIR}
cp -av $TMP_DIR/${PN}_${VERSION}-${RELEASE}.opsi ${OPSI_REPOS_PRODUCT_DIR}/${OPSI_REPOS_FILE_PATTERN}
builder_check_error "Can't upload file $TMP_DIR/${PN}_${VERSION}-${RELEASE}.opsi to ${OPSI_REPOS_PRODUCT_DIR}/${OPSI_REPOS_FILE_PATTERN}"
}
###################
# Commiting changes to repos
###################
builder_commit() {
if test -d ".git"; then
echo
# echo "builder_commit() not implemented yet."
fi
}
###################
# Check error
###################
builder_check_error() {
if [ $? == 1 ] ; then
echo "FATAL: $1"
exit 0
fi
}