From 5ce9c4b6778c49001a5ca670cd1b6da3739d1aa3 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 6 Feb 2024 09:06:49 +0100 Subject: [PATCH] Finishing simulation mode of bin/update-nextcloud --- bin/update-nextcloud | 45 ++++++++++++++++++--------------- lib/functions.rc | 59 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 82 insertions(+), 22 deletions(-) diff --git a/bin/update-nextcloud b/bin/update-nextcloud index 5e716af..ff1f3c7 100755 --- a/bin/update-nextcloud +++ b/bin/update-nextcloud @@ -172,9 +172,9 @@ get_old_version() { if [[ -d "${NEXTCLOUD_DIR_NEW}" ]] ; then warn "The directory '${YELLOW}${NEXTCLOUD_DIR_NEW}${NORMAL}' is already existing." - if yes_or_no "Continue [y/n]? " ; then + if yes_or_no "Continue with removing '${CYAN}${NEXTCLOUD_DIR_NEW}${NORMAL}' [y/n]? " ; then info "Removing existing '${YELLOW}${NEXTCLOUD_DIR_NEW}${NORMAL}' ..." - RM -r "${NEXTCLOUD_DIR_NEW}" + rm -rf "${NEXTCLOUD_DIR_NEW}" else info "Give it up." exit 1 @@ -297,6 +297,11 @@ unpack_archive() { info "Unpacking '${CYAN}${archive}${NORMAL}' into '${CYAN}${TEMP_DIR}${NORMAL}' ..." + if [[ -d "${NEXTCLOUD_DIR_NEW}" ]] ; then + error "Directory '${RED}${NEXTCLOUD_DIR_NEW}${NORMAL}' is already existing." + exit 7 + fi + debug "Activating trap ..." trap cleanup_tmp_dir INT TERM EXIT ABRT @@ -305,15 +310,11 @@ unpack_archive() { if [[ "${VERBOSE}" && "${VERBOSE}" -gt '1' ]] ; then cmd="tar xfvj \"${archive}\"" fi - if [[ "${SIMULATE}" ]] ; then - info "Simulate exec: ${cmd}" - else - debug "Exec: ${cmd}" - eval ${cmd} - fi + debug "Exec: ${cmd}" + eval ${cmd} info "Moving '${CYAN}${TEMP_DIR}/nextcloud${NORMAL}' to '${CYAN}${NEXTCLOUD_DIR_NEW}${NORMAL}' ..." - mv_no_verbose "${TEMP_DIR}/nextcloud" "${NEXTCLOUD_DIR_NEW}" + mv_no_verbose_force "${TEMP_DIR}/nextcloud" "${NEXTCLOUD_DIR_NEW}" cd debug "Deactivating trap ..." @@ -325,19 +326,17 @@ unpack_archive() { #------------------------------------------------------------------------------ adjust_permissions() { - cd "${NEXTCLOUD_DIR_NEW}" + if [[ -d "${NEXTCLOUD_DIR_NEW}" ]] ; then + cd "${NEXTCLOUD_DIR_NEW}" + fi - info "Adjusting permissions if '${CYAN}${NEXTCLOUD_DIR_NEW}${NORMAL}' ..." + info "Adjusting permissions of '${CYAN}${NEXTCLOUD_DIR_NEW}${NORMAL}' ..." info "Setting ownership to ${WWW_USER}:${WWW_GROUP} ..." - chown_no_verbose --recursive "${WWW_USER}:${WWW_GROUP}" . + chown_no_verbose_force --recursive "${WWW_USER}:${WWW_GROUP}" "${NEXTCLOUD_DIR_NEW}" info "Setting file mode to '${FILE_PERMS}' ..." - if [[ -z "${SIMULATE}" ]] ; then - find ./ -type f -print0 | xargs --null chmod "${FILE_PERMS}" - fi + find "${NEXTCLOUD_DIR_NEW}" -type f -print0 | xargs --null chmod "${FILE_PERMS}" info "Setting dir mode to '${DIR_PERMS}' ..." - if [[ -z "${SIMULATE}" ]] ; then - find ./ -type d -print0 | xargs --null chmod "${DIR_PERMS}" - fi + find "${NEXTCLOUD_DIR_NEW}" -type d -print0 | xargs --null chmod "${DIR_PERMS}" } @@ -409,8 +408,8 @@ copy_config() { local new_cfg_file="${NEXTCLOUD_DIR_NEW}/config/config.php" info "Copying configuration ..." - CP "${cur_cfg_file}" "${new_cfg_file}" - CHOWN "${WWW_USER}:${WWW_GROUP}" "${new_cfg_file}" + cp_force "${cur_cfg_file}" "${new_cfg_file}" + chown_force "${WWW_USER}:${WWW_GROUP}" "${new_cfg_file}" } @@ -450,6 +449,7 @@ main() { get_options "$@" umask 0022 + debug "Verbose level is '${CYAN}${VERBOSE}${NORMAL}'." get_old_version do_backup @@ -465,6 +465,11 @@ main() { start_service "${SERVICE_NAME}" release_maintenance + if [[ -d "${NEXTCLOUD_DIR_NEW}" ]] ; then + info "Purging ''${CYAN}${NEXTCLOUD_DIR_NEW}${NORMAL}' ..." + rm -rf "${NEXTCLOUD_DIR_NEW}" + fi + } main "$@" diff --git a/lib/functions.rc b/lib/functions.rc index 210b451..2543926 100644 --- a/lib/functions.rc +++ b/lib/functions.rc @@ -10,7 +10,7 @@ BLUE="" CYAN="" NORMAL="" -VERSION="0.5.1" +VERSION="0.5.2" STD_SHORT_OPTIONS="sdvqhV" STD_LONG_OPTIONS="simulate,debug,verbose,quiet,nocolor,help,version" @@ -439,6 +439,20 @@ CP() { } +#------------------------------------------------------------------------------ +cp_force() { + local cmd="cp $*" + if [[ "${VERBOSE}" ]] ; then + cmd="cp --verbose $*" + fi + debug "Executing: ${cmd}" + if [[ -z "${VERBOSE}" ]] ; then + cp "$@" + else + cp --verbose "$@" + fi +} + #------------------------------------------------------------------------------ cp_no_verbose() { if [[ "${SIMULATE}" ]] ; then @@ -479,6 +493,12 @@ mv_no_verbose() { mv "$@" } +#------------------------------------------------------------------------------ +mv_no_verbose_force() { + debug "Executing: mv $*" + mv "$@" +} + #------------------------------------------------------------------------------ CHOWN() { @@ -499,6 +519,20 @@ CHOWN() { } +#------------------------------------------------------------------------------ +chown_force() { + local cmd="chown $*" + if [[ "${VERBOSE}" ]] ; then + cmd="chown --verbose $*" + fi + debug "Executing: ${cmd}" + if [[ -z "${VERBOSE}" ]] ; then + chown "$@" + else + chown --verbose "$@" + fi +} + #------------------------------------------------------------------------------ chown_no_verbose() { if [[ "${SIMULATE}" ]] ; then @@ -509,6 +543,12 @@ chown_no_verbose() { chown "$@" } +#------------------------------------------------------------------------------ +chown_no_verbose_force() { + debug "Executing: chown $*" + chown "$@" +} + #------------------------------------------------------------------------------ CHMOD() { @@ -529,6 +569,20 @@ CHMOD() { } +#------------------------------------------------------------------------------ +chmod_force() { + local cmd="chmod $*" + if [[ "${VERBOSE}" ]] ; then + cmd="chmod --verbose $*" + fi + debug "Executing: ${cmd}" + if [[ -z "${VERBOSE}" ]] ; then + chmod "$@" + else + chmod --verbose "$@" + fi +} + #------------------------------------------------------------------------------ chmod_no_verbose() { if [[ "${SIMULATE}" ]] ; then @@ -659,7 +713,8 @@ yes_or_no() { local answer= debug "Trying to get an answer with a timeout of ${timeout} seconds." - if read -p "${msg}" -t "${timeout}" answer ; then + echo -e -n "${msg}" + if read -t "${timeout}" answer ; then debug "Got an answer: '${answer}'" else exit 1 -- 2.39.5