build-update-package 4.02 KB
Newer Older
1
#!/bin/bash
2
# Copyright (C) 2009-2012 Centro de Computacao Cientifica e Software Livre
3 4 5 6
# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
#
# This file is part of buildpackage
#
7
# buildpackage is free software; you can redistribute it and/or
8 9 10 11 12 13 14 15 16 17 18 19 20 21
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.

22 23 24 25 26 27 28 29 30 31
# This function checks one file integrity
check_file_integrity(){
    FILE="$*"
    FILEREAL="$(echo ${FILE} | sed "s#^${MD5DIRESC}##g")"

    test -f "${AGENTPACKAGEDIR}/${FILEREAL}" || return 1
    (md5sum "${AGENTPACKAGEDIR}/${FILEREAL}" | cut -d' ' -f1 | \
     diff - "${FILE}" &> /dev/null) || return 1
}

32
PREFIX="$(dirname $0)"
33
BOOTSTRAP="${PREFIX}/bootstraps/bootstrap-update"
34 35

# Get the configuration
36
if ! source "${PREFIX}/buildpackage.conf" 2> /dev/null; then
37
    printf "ERROR (1): Failed to load buildpackage.conf.\n"
38 39 40
    exit 1
fi

41 42
# Customizable variables. Please refer to buildpackage.conf to change these
# values
43 44 45 46
: ${AGENTDIR:="agent-files"}
: ${AGENTPACKAGEDIR:="SEED2-run"}
: ${UPDATEDIR:="update-files"}

47
MD5DIR="${AGENTPACKAGEDIR}/.md5sum"
48
MD5DIRESC="$(echo ${MD5DIR} | sed "s/\./\\\./g")"
49 50 51
VERSIONFILE="${AGENTDIR}/${AGENTPACKAGEDIR}/client/conf/version"

printf "====================\n"
52
printf "$0 v0.1\n\n"
53 54
printf "*** Make sure ${AGENTDIR}/${AGENTPACKAGEDIR}\n"
printf "contains the latest package generated by build-agent-files ***\n"
55
printf "====================\n\nStarting to build the package...\n\n"
56

57 58
# Check the readability of version file and get the version
if ! test -r "${VERSIONFILE}"; then
59 60
    printf "ERROR (2): The version file is not readable. Aborting...\n"
    exit 2
61
fi
62
VERSION=$(cat ${VERSIONFILE})
63

64 65 66
# Set the output file name
OUTPUT="${UPDATEDIR}/SEED2-update-${VERSION}.run"

67 68 69 70 71 72 73
# Change to agent directory
cd "${AGENTDIR}"

# -----------------------------------------------------------------------------
# Check all MD5 files
printf "Checking SEED2-run files integrity... "

74
if ! test -d "${MD5DIR}"; then
75
    printf "ERROR (5): There is no MD5 directory. Please, run create %b" \
76 77 78 79
           "integrity.\n"
    exit 5
fi

80 81
for FILE in $(find "${MD5DIR}" -type f); do
    if ! check_file_integrity "${FILE}"; then
82 83
        printf "ERROR (3): File(s) corrupted. Please, re-create integrity %b" \
               "or rebuild the agent files.\n"
84
        exit 3
85 86 87 88 89
    fi
done

printf "done.\n"

90 91 92 93
# -----------------------------------------------------------------------------
# Compress the agent files
printf "Compressing SEED2-run files... "

94
TARFILE=$(tempfile --prefix=SEED2-tar)
95
if ! tar -czf "${TARFILE}" SEED2-run; then
96 97
    printf "ERROR (4): Failed to compress the agent files.\n"
    exit 4
98
fi
99
cd - &> /dev/null
100

101 102 103 104 105 106 107
printf "done.\n"
# End of agent compression

# -----------------------------------------------------------------------------
# Calculate the MD5
printf "Calculating MD5... "

108 109
MD5_CHECK=$(md5sum ${TARFILE} | awk '{print $1}')

110 111 112 113 114 115 116
printf "done.\n"
# End of MD5 calculation

# -----------------------------------------------------------------------------
# Generate the update package
printf "Creating update .run package... "

117 118 119
mkdir -p "${UPDATEDIR}"
LINENUM=$(( $(wc -l "${BOOTSTRAP}" | cut -d' ' -f1) + 1 ))

120 121 122
sed "s/BOOTSTRAP_LINES=/BOOTSTRAP_LINES=${LINENUM}/g; \
     s/FILE=/FILE=\"SEED2-update-${VERSION}.tgz\"/g; \
     s/MD5_CHECK=/MD5_CHECK=\"${MD5_CHECK}\"/g" \
123 124 125 126
     "${BOOTSTRAP}" > "${OUTPUT}"

cat "${TARFILE}" >> "${OUTPUT}"

127 128 129 130
printf "done.\n"
# End of update package generation

# Remove the tar file
131 132
rm -f "${TARFILE}"

133
printf "\nAll done.\n"