From 8139bf5b509265f84d233abe621204092853a088 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Thu, 1 Feb 2024 11:35:24 +0100 Subject: [PATCH] Reworking bin/update-nextcloud for simulation stuff --- bin/update-nextcloud | 74 ++++++++++++++++++++------------------- lib/functions.rc | 82 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 119 insertions(+), 37 deletions(-) diff --git a/bin/update-nextcloud b/bin/update-nextcloud index 98216c5..5e716af 100755 --- a/bin/update-nextcloud +++ b/bin/update-nextcloud @@ -204,6 +204,10 @@ do_backup() { info "Backing up current Nextcloud directory to '${GREEN}${backup_file}${NORMAL}' ..." + if [[ "${SIMULATE}" ]] ; then + return + fi + if [[ ! -d "${BACKUP_DIR}" ]] ; then info "Creating '${CYAN}${BACKUP_DIR}${NORMAL}' ..." MKDIR -p "${BACKUP_DIR}" @@ -278,6 +282,9 @@ cleanup_tmp_dir() { cd debug "Removing temporary directory '${CYAN}${TEMP_DIR}${NORMAL}' ..." + if [[ "${SIMULATE}" ]] ; then + return + fi purge -r "${TEMP_DIR}" } @@ -288,19 +295,25 @@ unpack_archive() { TEMP_DIR=$( mktemp -d "${TEMP_DIR_PARENT}/nextcloud-unpack-XXXXXXXXXX" ) local archive="${DOWNLOAD_DIR}/${ARTIFACT}" + info "Unpacking '${CYAN}${archive}${NORMAL}' into '${CYAN}${TEMP_DIR}${NORMAL}' ..." + debug "Activating trap ..." trap cleanup_tmp_dir INT TERM EXIT ABRT cd "${TEMP_DIR}" - info "Unpacking '${GREEN}${archive}${NORMAL}' ..." + local cmd="tar xfj \"${archive}\"" if [[ "${VERBOSE}" && "${VERBOSE}" -gt '1' ]] ; then - tar xfvj "${archive}" + cmd="tar xfvj \"${archive}\"" + fi + if [[ "${SIMULATE}" ]] ; then + info "Simulate exec: ${cmd}" else - tar xfj "${archive}" + debug "Exec: ${cmd}" + eval ${cmd} fi info "Moving '${CYAN}${TEMP_DIR}/nextcloud${NORMAL}' to '${CYAN}${NEXTCLOUD_DIR_NEW}${NORMAL}' ..." - mv "${TEMP_DIR}/nextcloud" "${NEXTCLOUD_DIR_NEW}" + mv_no_verbose "${TEMP_DIR}/nextcloud" "${NEXTCLOUD_DIR_NEW}" cd debug "Deactivating trap ..." @@ -316,11 +329,15 @@ adjust_permissions() { info "Adjusting permissions if '${CYAN}${NEXTCLOUD_DIR_NEW}${NORMAL}' ..." info "Setting ownership to ${WWW_USER}:${WWW_GROUP} ..." - chown -R "${WWW_USER}:${WWW_GROUP}" . + chown_no_verbose --recursive "${WWW_USER}:${WWW_GROUP}" . info "Setting file mode to '${FILE_PERMS}' ..." - find ./ -type f -print0 | xargs --null chmod "${FILE_PERMS}" + if [[ -z "${SIMULATE}" ]] ; then + find ./ -type f -print0 | xargs --null chmod "${FILE_PERMS}" + fi info "Setting dir mode to '${DIR_PERMS}' ..." - find ./ -type d -print0 | xargs --null chmod "${DIR_PERMS}" + if [[ -z "${SIMULATE}" ]] ; then + find ./ -type d -print0 | xargs --null chmod "${DIR_PERMS}" + fi } @@ -347,7 +364,9 @@ sync_apps() { for app in ${app_sync_list} ; do if [[ -d "${cur_apps_dir}/${app}" ]] ; then debug "Syncing app '${CYAN}${app}${NORMAL}' ..." - rsync -a "${cur_apps_dir}/${app}" "${new_apps_dir}/" + if [[ -z "${SIMULATE}" ]] ; then + rsync -a "${cur_apps_dir}/${app}" "${new_apps_dir}/" + fi fi done @@ -362,10 +381,15 @@ sync_apps() { occ() { debug "Executing '${CYAN}php occ $*${NORMAL}' ..." - ( - cd "${NEXTCLOUD_DIR}" - sudo -u "${WWW_USER}" php occ "$@" - ) + if [[ "${SIMULATE}" ]] ; then + info "Simulate executing: sudo -u \"${WWW_USER}\" php occ $*" + else + debug "Executing: sudo -u \"${WWW_USER}\" php occ $*" + ( + cd "${NEXTCLOUD_DIR}" + sudo -u "${WWW_USER}" php occ "$@" + ) + fi } @@ -390,17 +414,6 @@ copy_config() { } -#------------------------------------------------------------------------------ -stop_service() { - - info "Stopping '${CYAN}${SERVICE_NAME}${NORMAL}' ..." - systemctl stop "${SERVICE_NAME}" - if [[ "${VERBOSE}" ]] ; then - systemctl status "${SERVICE_NAME}" || true - fi - -} - #------------------------------------------------------------------------------ switch_nc_dirs() { @@ -424,17 +437,6 @@ upgrade_nc() { } -#------------------------------------------------------------------------------ -start_service() { - - info "Starting '${CYAN}${SERVICE_NAME}${NORMAL}' ..." - systemctl start "${SERVICE_NAME}" - if [[ "${VERBOSE}" ]] ; then - systemctl status "${SERVICE_NAME}" || true - fi - -} - #------------------------------------------------------------------------------ release_maintenance() { @@ -457,10 +459,10 @@ main() { sync_apps set_maintenance copy_config - stop_service + stop_service "${SERVICE_NAME}" switch_nc_dirs upgrade_nc - start_service + start_service "${SERVICE_NAME}" release_maintenance } diff --git a/lib/functions.rc b/lib/functions.rc index b432a5e..210b451 100644 --- a/lib/functions.rc +++ b/lib/functions.rc @@ -10,7 +10,7 @@ BLUE="" CYAN="" NORMAL="" -VERSION="0.5.0" +VERSION="0.5.1" STD_SHORT_OPTIONS="sdvqhV" STD_LONG_OPTIONS="simulate,debug,verbose,quiet,nocolor,help,version" @@ -439,6 +439,16 @@ CP() { } +#------------------------------------------------------------------------------ +cp_no_verbose() { + if [[ "${SIMULATE}" ]] ; then + info "Executing: cp $*" + return + fi + debug "Executing: cp $*" + cp "$@" +} + #------------------------------------------------------------------------------ MV() { @@ -459,6 +469,16 @@ MV() { } +#------------------------------------------------------------------------------ +mv_no_verbose() { + if [[ "${SIMULATE}" ]] ; then + info "Executing: mv $*" + return + fi + debug "Executing: mv $*" + mv "$@" +} + #------------------------------------------------------------------------------ CHOWN() { @@ -479,6 +499,16 @@ CHOWN() { } +#------------------------------------------------------------------------------ +chown_no_verbose() { + if [[ "${SIMULATE}" ]] ; then + info "Executing: chown $*" + return + fi + debug "Executing: chown $*" + chown "$@" +} + #------------------------------------------------------------------------------ CHMOD() { @@ -499,6 +529,16 @@ CHMOD() { } +#------------------------------------------------------------------------------ +chmod_no_verbose() { + if [[ "${SIMULATE}" ]] ; then + info "Executing: chmod $*" + return + fi + debug "Executing: chmod $*" + chmod "$@" +} + #------------------------------------------------------------------------------ LN() { @@ -519,6 +559,46 @@ LN() { } +#------------------------------------------------------------------------------ +stop_service() { + + local service="$1" + + info "Stopping '${CYAN}${service}${NORMAL}' ..." + local cmd="systemctl stop \"${service}\"" + if [[ "${SIMULATE}" ]] ; then + debug "Simulating exec: ${cmd}" + else + debug "Exec: ${cmd}" + eval ${cmd} + fi + + if [[ "${VERBOSE}" ]] ; then + systemctl status "${service}" || true + fi + +} + +#------------------------------------------------------------------------------ +start_service() { + + local service="$1" + + info "Starting '${CYAN}${service}${NORMAL}' ..." + local cmd="systemctl start \"${service}\"" + if [[ "${SIMULATE}" ]] ; then + debug "Simulating exec: ${cmd}" + else + debug "Exec: ${cmd}" + eval ${cmd} + fi + + if [[ "${VERBOSE}" ]] ; then + systemctl status "${service}" || true + fi + +} + #------------------------------------------------------------------------------ set_locale() { -- 2.39.5