diff --git a/src/warden-server/sh/uninstall.sh b/src/warden-server/sh/uninstall.sh index 762b57d27d922eac80afef41fa67af4395bc1f0b..8641b9939d3041f5a217e286d8963a18a7b8610b 100755 --- a/src/warden-server/sh/uninstall.sh +++ b/src/warden-server/sh/uninstall.sh @@ -4,23 +4,25 @@ # # Copyright (C) 2011-2013 Cesnet z.s.p.o # +# Use of this source is governed by a BSD-style license, see LICENSE file. -VERSION="2.1" + +VERSION="2.2" #------------------------------------------------------------------------------- -# FUNCTIONS +# Script functions #------------------------------------------------------------------------------- usage() { - echo "Usage: `basename $0` [-d <directory>] [-hV]" - echo "-d <directory> uninstallation directory (default: /opt)" + echo "Usage: ${0##*/} -d <directory> [-s <directory>] [-hV]" + echo "-d <directory> uninstallation directory of Warden server" + echo "-s <directory> directory for symlinks to Warden server control scripts (optional)" echo "-h print this help" echo "-V print script version number and exit" echo - echo "Example: # ./`basename $0` -d /opt" + echo "Example: $ ${0##*/} -d /opt/warden-server -s /usr/local/bin" echo - echo "Note: You must be root for running this script." - echo " For more information about uninstallation process, see README file (section Uninstallation)." + echo "For more information about uninstallation process, see README file (section Uninstallation)." echo exit 0 } @@ -28,7 +30,7 @@ usage() version() { - echo "`basename ${0}` - current version is $VERSION" + echo "${0##*/} - current version is $VERSION" exit 0 } @@ -36,8 +38,9 @@ version() err() { echo "FAILED!" - cat $err - rm -rf $err $backup_dir + cat "$err" + rm -f "$err" + rm -rf "$backup" echo echo "Uninstallation of $package_version package FAILED!!!" exit 1 @@ -47,55 +50,43 @@ err() errClean() { echo "FAILED!" - echo " -> Reverting changes of warden server package ... OK" - rm -rf ${server_path}/* > /dev/null 2>&1 # delete new version - cp -R ${backup_dir}/* $server_path # copy old backuped server - for file in `ls -1 $bin` - do - ln -s ${bin}/$file ${local_bin}/$file # create symlinks to /usr/local/bin - done - cat $err - rm -rf $err $backup_dir + echo " -> Reverting changes of warden server package ... " + rm -rf "${basedir}/"* > /dev/null 2>&1 + cp -R "${backup}/"* "$basedir" + if [[ ! -z "$symbin" ]] && [[ -d "$symbin" ]] && [[ -w "$symbin" ]]; then + for file in "${bin}/"* + do + ln -s "${bin}/${file##*/}" "${symbin}/${file##*/}" + done + fi + cat "$err" + rm -f "$err" + rm -rf "$backup" echo echo "Uninstallation of $package_version package FAILED!!!" exit 1 } -rootChck() -{ - if [ $UID -ne 0 ]; then - echo "You must be root for running this script!" - exit 1 - fi -} - - paramsChck() { - if [ -z $prefix ]; then + if [ -z "$basedir" ]; then echo "Parameter -d <directory> is not set!" exit 1 fi } -obtainPackageVersion() -{ - if [ -f $old_package_version_file ]; then - package_version=`cat $old_package_version_file` - else - package_version="unknown" - fi -} - +#------------------------------------------------------------------------------- +# Uninstallation functions +#------------------------------------------------------------------------------- wardenDirChck() { echo -n "Checking Warden server directory ... " - if [ ! -d $server_path ]; then + if [[ ! -d "$basedir" ]] && [[ ! -w "$basedir" ]]; then echo "FAILED!" - ls $server_path + echo "No version of Warden server is not installed!" exit 1 else echo "OK" @@ -103,11 +94,24 @@ wardenDirChck() } -backup() +getPackageVersion() { - echo -n "Backing-up Warden server directory ... " - mkdir $backup_dir - if cp -R ${server_path}/* $backup_dir 2> $err; then + echo -n "Checking Warden server version ... " + old_package_version_file=$(find "$basedir" -name package_version) + if [ -f "$old_package_version_file" ]; then + package_version=$(<"$old_package_version_file") + else + package_version="unknown" + fi + echo "OK" +} + + +createBackup() +{ + echo -n "Creating Warden server backup ... " + [[ -d "$backup" ]] && rm -rf "$backup" || mkdir "$backup" + if cp -R "${basedir}/"* "$backup" 2> "$err"; then echo "OK" else err @@ -115,87 +119,89 @@ backup() } -deleteSymlinks() +uninstallWardenServer() { - echo -n "Deleting symlinks from /usr/local/bin ..." - for file in `ls -1 $bin` + echo -n "Uninstalling $package_version package ... " + cp "${doc}/UNINSTALL" "$uninstall" + manifest=$(find "$basedir" -name MANIFEST) || err + for file in $(<"$manifest") do - rm -rf ${local_bin}/$file 2> /dev/null + directory="${file%/*}" + rm -f "${basedir}/$file" + rmdir --ignore-fail-on-non-empty "${basedir}/$directory" done + rm -f "${basedir}/uninstall.sh" + rmdir --ignore-fail-on-non-empty "$basedir" echo "OK" } -uninstallWardenServer() +deleteSymlinks() { - echo -n "Uninstalling $package_version package ... " - cp ${doc}/UNINSTALL $uninstall_file - if rm -rf $server_path 2> $err; then - echo "OK" - else - errClean - fi + echo -n "Deleting symlinks from $symbin ..." + for file in "$symbin/"* + do + rm -f "${symbin}/${file##*/}" 2> /dev/null + done + echo "OK" } - #------------------------------------------------------------------------------- # MAIN #------------------------------------------------------------------------------- - # read input -while getopts "d:Vh" options; do - case $options in - d ) prefix=$OPTARG;; +while getopts "d:s:Vh" options; do + case "$options" in + d ) basedir="$OPTARG";; + s ) symbin="$OPTARG";; h ) usage;; V ) version;; * ) usage;; esac done -# root test -rootChck +# remove last char (slash) from name of directories +[[ "$basedir" == */ ]] && basedir="${basedir%?}" # params test paramsChck -# create variables -[[ $prefix == */ ]] && prefix="${prefix%?}" # remove last char (slash) from prefix -dirname=`dirname $0` -server_path="${prefix}/warden-server" -bin="${server_path}/bin" -local_bin="/usr/local/bin" -etc="${server_path}/etc" -doc="${server_path}/doc" -uninstall_file="/tmp/UNINSTALL.warden" -old_package_version_file="${etc}/package_version" +# set variables +bin="${basedir}/bin" +etc="${basedir}/etc" +doc="${basedir}/doc" err="/tmp/warden-err" -backup_dir="/tmp/warden-backup" +uninstall="/tmp/UNINSTALL.warden" +backup="/tmp/warden-backup" -# obtain version of installed warden-server package -obtainPackageVersion echo echo "------------------------- Uninstallation process --------------------------------" -# check if $prefix/warden-server directory exist +# check Warden server directory if exists wardenDirChck -# make backup of currently installed warden-server package -backup +# obtain version of existing Warden server (if exists) +getPackageVersion -# delete symbolic links -deleteSymlinks +# make backup of currently installed warden-server package +createBackup -# do uninstallation +# uninstall Warden server uninstallWardenServer +# delete symlinks from $symbin (if -s option is set) +[[ ! -z "$symbin" ]] && [[ -d "$symbin" ]] && [[ -w "$symbin" ]] && deleteSymlinks + echo -echo "Please follow post-uninstallation steps in $uninstall_file!" +echo "Please follow post-uninstallation steps in $uninstall!" echo -echo "Uninstallation of $package_version package was SUCCESSFUL!" +echo "Uninstallation of $package_version package was SUCCESSFUL!!!" echo + # cleanup section -rm -rf $err $backup_dir +rm -r "$err" +rm -rf "$backup" exit 0