build-install-package.sh 5.58 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 32
# This script builds the installation package. Exit status:
#
# 0 - Success
# 1 - Error: empty INEP
# 2 - Error: some files are corrupt
# 3 - Error while trying to copy the agent files
# 4 - Error while trying to copy the installation bootstrap
# 5 - Error while trying to compress the agent files

# ------------------------------------------------------------------------------
# This function checks the integrity of one file
33 34 35 36 37 38 39 40 41
check_file_integrity(){
    FILE="$*"
    FILEREAL="$(echo ${FILE} | sed "s#^${MD5DIRESC}##g")"

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

42
AGENT_FILES="./agent-files"
43
BOOTSTRAP="bootstrap-install"
44
INEP=$1
45 46
MD5DIR="${AGENT_FILES}/SEED2-run/.md5sum"
MD5DIRESC="$(echo ${MD5DIR} | sed "s/\./\\\./g")"
47

48 49 50 51 52 53 54 55 56 57 58 59
# Ensure proxy variables are empty
PHOST=
PPORT=
PUID=
PPASSWD=

# If the parameter isn't quotation marks, add its value to the respective proxy
# information variable
test "$2" = '""' || PHOST=$2
test "$3" = '""' || PPORT=$3
test "$4" = '""' || PUID=$4
test "$5" = '""' || PPASSWD=$5
60

61 62
# Check if $INEP is empty
if test -z "${INEP}"; then
63
    printf "ERROR (1): Empty INEP.\n"
64 65
    exit 1
fi
66

67 68 69 70 71 72
# -----------------------------------------------------------------------------
# Check all MD5 files
printf "Checking SEED2-run files integrity... "

for FILE in $(find "${MD5DIR}" -type f); do
    if ! check_file_integrity "${FILE}"; then
73
        printf "ERROR (2): File(s) corrupted. Please, re-create integrity or %b"
74
               "rebuild the agent files.\n"
75
        exit 2
76 77 78 79
    fi
done

printf "done.\n"
80

81 82
# -----------------------------------------------------------------------------
# Start the tree creation process, removing any possible garbage before
83
# creating the package specific temporary directory (using the INEP)
84 85 86
printf "Creating tree... "

rm -rf "/tmp/${INEP}"
87 88
mkdir -p "/tmp/${INEP}"

89 90 91
# Copy the agent files to the temporary directory where the installation package
# will be generated
if ! cp -r "${AGENT_FILES}/SEED2-run" "/tmp/${INEP}/SEED2"; then
92 93
    printf "ERROR (3): Failed to copy agent files.\n"
    exit 3
94 95
fi

96
# Create client conf directory and the inep file with the school INEP
97 98
mkdir -p "/tmp/${INEP}/SEED2/client/conf"
echo "${INEP}" > "/tmp/${INEP}/SEED2/client/conf/inep"
99 100

# Create the md5 files of inep and inep backup
101 102 103 104 105 106 107 108
FILE="/tmp/${INEP}/SEED2/client/conf/inep"
md5sum "${FILE}" |cut -d' ' -f1 > \
    "/tmp/${INEP}/SEED2/.md5sum/client/conf/inep"
cp -a "${FILE}" "/tmp/${INEP}/SEED2/.backup/client/conf/inep"
FILE="/tmp/${INEP}/SEED2/.backup/client/conf/inep"
md5sum "${FILE}" |cut -d' ' -f1 > \
    "/tmp/${INEP}/SEED2/.md5sum/.backup/client/conf/inep"

109 110
# Create the proxy file with all necessary proxy information, and its md5
echo "phost=$PHOST" > "/tmp/${INEP}/SEED2/client/conf/proxy"
111 112 113 114 115 116 117
echo "pport=$PPORT" >> "/tmp/${INEP}/SEED2/client/conf/proxy"
echo "puid=$PUID" >> "/tmp/${INEP}/SEED2/client/conf/proxy"
echo "ppasswd=$PPASSWD" >> "/tmp/${INEP}/SEED2/client/conf/proxy"
FILE="/tmp/${INEP}/SEED2/client/conf/proxy"
md5sum "${FILE}" |cut -d' ' -f1 > \
    "/tmp/${INEP}/SEED2/.md5sum/client/conf/proxy"

118
# Create the backup proxy file and its md5
119 120 121 122 123
cp -a "${FILE}" "/tmp/${INEP}/SEED2/.backup/client/conf/proxy"
FILE="/tmp/${INEP}/SEED2/.backup/client/conf/proxy"
md5sum "${FILE}" |cut -d' ' -f1 > \
    "/tmp/${INEP}/SEED2/.md5sum/.backup/client/conf/proxy"

124 125
# Copy the bootstrap-install to the temporary directory
if ! cp -r "${AGENT_FILES}/${BOOTSTRAP}" "/tmp/${INEP}"; then
126
    printf "ERROR (4): Failed to copy ${BOOTSTRAP} to the %b" \
127
           "temporary directory.\n"
128
    exit 4
129
fi
130 131

printf "done.\n"
132
# End of tree creation process
133

134
# -----------------------------------------------------------------------------
135
# The tree is ready, we need to compress it now (tar+gz)
136
printf "Packaging... "
137 138

cd "/tmp/${INEP}"
139
if ! tar -czf SEED2.tgz SEED2; then
140 141
    printf "ERROR (5): Failed to compress the agent package.\n"
    exit 5
142
fi
143 144 145

# Create the tar md5 and compute the bootstrap lines
MD5_CHECK=$(md5sum SEED2.tgz | awk '{print $1}')
146
LINENUM=$(( $(wc -l "${BOOTSTRAP}" | cut -d' ' -f1) + 1 ))
147 148

# Edit the bootstrap and start the real package
149 150
sed "s/BOOTSTRAP_LINES=/BOOTSTRAP_LINES=${LINENUM}/g; \
     s/MD5_CHECK=/MD5_CHECK=\"${MD5_CHECK}\"/g" \
151
    "${BOOTSTRAP}" > agent_${INEP}.run
152 153 154 155 156

# Insert the tar on the package and finish
cat "SEED2.tgz" >> "agent_${INEP}.run"
mv "agent_${INEP}.run" "/tmp"

157 158
printf "done.\n"
# End of packaging step
159

160
# -----------------------------------------------------------------------------
161
# Remove temporary files
162 163
printf "Removing temporary files... "

164
cd ..
165
rm -rf "${INEP}" 2> /dev/null
166

167
printf "done.\n"
168
# End of temporary files removal
169

170
printf "\nAll done.\n"