Select Git revision
configure.in
update.sh 9.52 KiB
#!/bin/bash
#
# update.sh
#
# Copyright (C) 2011-2015 Cesnet z.s.p.o
#
# Use of this source is governed by a BSD-style license, see LICENSE file.
VERSION="2.2"
#-------------------------------------------------------------------------------
# Script functions
#-------------------------------------------------------------------------------
usage()
{
echo "Usage: ${0##*/} -d <directory> [-hV]"
echo "-d <directory> Warden client to update"
echo "-h print this help"
echo "-V print script version number and exit"
echo
echo "Example: $ ${0##*/} -d /opt/warden-client"
echo
echo "For more information about update process, see README file (section Update)."
echo
exit 0
}
version()
{
echo "${0##*/} - current version is $VERSION"
exit 0
}
err()
{
echo "FAILED!"
echo -n "Error: " && cat "$err"
rm -f "$backup" 2>/dev/null
rm -f "$err"
echo
echo "Update from $old_package_version to $package_version package FAILED!!!"
exit 1
}
errClean()
{
echo "FAILED!"
echo -n "Error: " && cat "$err"
echo " -> Reverting changes of Warden client package ... OK"
rm -r "${basedir}/"* > /dev/null 2>&1
cp -R "${backup}/"* "$basedir"
echo
rm -r "$err" "$backup"
echo
echo "Update from $old_package_version to $package_version package FAILED!!!"
exit 1
}
paramsChck()
{
if [ -z "$basedir" ]; then
echo "Parameter -d <directory> is not set!"
exit 1
fi
}
#-------------------------------------------------------------------------------
# Installation functions
#-------------------------------------------------------------------------------
wardenDirChck()
{
echo -n "Checking Warden client directory ... "
if [[ ! -d "$basedir" ]]; then
echo "FAILED!"
echo
echo "Warden client is not installed!"
exit 1
elif [[ ! -w "$basedir" ]]; then
echo "FAILED!"
echo
echo "You do not have permission to update Warden client!"
exit 1
else
echo "OK"
fi
}
oldPackageChck()
{
echo -n "Checking previous Warden client installation ... "
old_package_version_file=$(find "$basedir" -name package_version)
if [ -f "$old_package_version_file" ]; then
old_package_version=$(<"$old_package_version_file")
if [ "$old_package_version" == "$package_version" ]; then
echo "FAILED!"
echo
echo "Package $package_version has been already installed!"
exit 1
elif [ "$old_package_version" == "warden-client-2.0" ]; then
echo "FAILED!"
echo
echo "First of all you must upgrade to package warden-client-2.1!"
exit 1
fi
else
echo "FAILED!"
echo
echo "Warden client package is not yet installed!"
echo "For installation of Warden client package please use install.sh."
exit 1
fi
echo "OK"
}
perlChck()
{
echo -n "Checking Perl interpreter ... "
if which perl 1> /dev/null; then
echo "OK"
else
echo "FAILED!"
echo
echo "Perl interpreter is not installed!"
exit 1
fi
}
modulesChck()
{
sed '/^use [A-Z]/!d; /Warden/d' $(find "${dirname}" -type f) 2>/dev/null | cut -f 2 -d " " | sed 's/;//' | sort -u | \
while read module;
do
echo -n "Checking $module module ... "
perl -e "use $module" 2> "$err" && echo "OK" || err
done
}
backup()
{
echo -n "Making Warden client backup ... "
mkdir "$backup" 2> "$err" || err
if cp -R "${basedir}/"* "$backup" 2> "$err"; then
echo "OK"
else
err
fi
}
obtainUser()
{
echo -n "Obtaining warden client directory owner ... "
if user=`stat -c %U $package_version` 2> "$err"; then
echo "OK"
else
err
fi
}
updateWardenDir()
{
echo -n "Updating Warden client ... "
for file in $(<"$manifest")
do
dir="$basedir/${file%/*}"
[[ ! -d "$dir" ]] && mkdir -p "$dir"
cp "$file" "${basedir}/$file" 2> "$err" || errClean
done
rm -f "${basedir}/uninstall.sh" 2> /dev/null
ln -s "${sh}/uninstall.sh" "$basedir"
echo "OK"
}
getConfValue() {
perl -e "require \"$1\"; print \"\$$2\";"
}
doTemplate()
{
vars=""
while (($#)); do
vars="'$1'=>'$2', $vars"
shift 2
done
perl -e "my %repl=($vars); foreach my \$l (<>) {\$l =~ s/\$_/\$repl{\$_}/g for keys %repl; print \$l}"
}
removeOldFiles()
{
echo -n "Removing old package files ... "
rm -f "${doc}/example-receiver.pl.txt" 2> /dev/null
rm -f "${doc}/example-sender.pl.txt" 2> /dev/null
rm -f "${lib}/WardenClientConf.pm" 2> /dev/null
echo "OK"
}
createManifest()
{
echo -n "Creating new MANIFEST file ... "
cd "$basedir" && find . -mindepth 2 -type f | sed 's/.\///' | sort > "${doc}/MANIFEST" && cd $OLDPWD 2> "$err" || errClean
echo "OK"
}
changePermissions()
{
echo -n "Changing permissions to updated package ... "
chown -R "$user": "$basedir" 2>"$err" || err_clean
chmod 644 "$old_package_version_file" || err_clean
if chmod 600 "$client_conf"; then
echo "OK"
else
err_clean
fi
}
#-------------------------------------------------------------------------------
# MAIN
#-------------------------------------------------------------------------------
# read input
while getopts "d:Vh" options; do
case $options in
d ) basedir=$OPTARG;;
h ) usage;;
V ) version;;
* ) usage;;
esac
done
# remove last char (slash) from name of directories
[[ "$basedir" == */ ]] && basedir="${basedir%?}"
# params test
paramsChck
# set variables
err="/tmp/warden-err"
backup="/tmp/warden-client-backup"
dirname=${0%/*}
doc="${basedir}/doc"
etc="${basedir}/etc"
sh="${basedir}/sh"
client_conf="${etc}/warden-client.conf"
client_conf_tmpl="${dirname}/etc/warden-client.conf.tmpl"
client_conf_tmpl_update="${dirname}/etc/warden-client.conf.tmpl.update"
package_version=$(<"${dirname}/etc/package_version")
manifest="${dirname}/doc/MANIFEST"
echo "Update of Warden client package to $package_version ..."
echo
echo "------------------------- Dependencies check-in -------------------------"
# check warden client directory
wardenDirChck
# check previous Warden client instalation
oldPackageChck
# Perl interpreter test
perlChck
# Perl modules test
modulesChck || exit 1
echo
echo "------------------------- Update process --------------------------------"
# backup old warden client installation
backup
# obation owner of already installed Warden client
obtainUser
# make warden client directory
updateWardenDir || exit 1
# update client configuration file
echo -n "Checking Warden client configuration file ... "
if [[ -r "$client_conf" ]]; then
echo "OK"
echo -n " -> Updating Warden client configuration file ... "
uri=$(getConfValue "$client_conf" URI)
ssl_key=$(getConfValue "$client_conf" SSL_KEY)
ssl_cert=$(getConfValue "$client_conf" SSL_CERT)
ssl_ca_cert=$(getConfValue "$client_conf" SSL_CA_CERT)
max_rcv_events_limit=$(getConfValue "$client_conf" MAX_RCV_EVENTS_LIMIT)
connection_timeout=$(getConfValue "$client_conf" CONNECTION_TIMEOUT)
log_stderr=$(getConfValue "$client_conf" LOG_STDERR)
log_stderr_verbose=$(getConfValue "$client_conf" LOG_STDERR_VERBOSE)
syslog=$(getConfValue "$client_conf" SYSLOG)
syslog_verbose=$(getConfValue "$client_conf" SYSLOG_VERBOSE)
syslog_facility=$(getConfValue "$client_conf" SYSLOG_FACILITY)
doTemplate \
_URI_ "$uri" \
_KEYFILE_ "$ssl_key" \
_CERTFILE_ "$ssl_cert" \
_CAFILE_ "$ssl_ca_cert" \
_MAXRCVEVENTSLIMIT_ "$max_rcv_events_limit" \
_CONNECTIONTIMEOUT_ "$connection_timeout" \
_LOGSTDERR_ "$log_stderr" \
_LOGSTDERRVERBOSE_ "$log_stderr_verbose" \
_SYSLOG_ "$syslog" \
_SYSLOGVERBOSE_ "$syslog_verbose" \
_SYSLOGFACILITY_ "$syslog_facility" \
< $client_conf_tmpl_update \
> $client_conf && echo "OK" || errClean
else
echo "FAILED!"
echo -n " -> Creating Warden client configuration file ... "
doTemplate \
_KEYFILE_ "" \
_CERTFILE_ "" \
_CAFILE_ "" \
< $client_conf_tmpl \
> $client_conf && echo "OK" || errClean
echo " -> Please specify path to SSL_KEY, SSL_CERT and SSL_CA_CERT!"
fi
# remove old packages files - file list depend on package version
removeOldFiles
# create MANIFEST file
createManifest
# change updated Warden client package permissions
changePermissions
echo
echo "------------------------- Summary ---------------------------------------"
echo "Please check updated/created configuration files:"
echo " - $client_conf"
echo
echo "Warden client directory: $basedir"
echo
echo "Update from $old_package_version to $package_version package was SUCCESSFUL!!!"
echo
# cleanup section
rm -f "$err"
rm -rf "$backup"
exit 0