diff --git a/models.py b/models.py index 7840f1d..d9aab5a 100644 --- a/models.py +++ b/models.py @@ -9,6 +9,7 @@ import shutil from colorama import Fore, Style from pathlib import Path from utils import _print +import utils class Container: @@ -198,21 +199,24 @@ class Container: return False # Disable Nextcloud maintenance mode - def __disable_maintenance_mode(self): - try: - disable_maintenance_mode = check_output( - ["docker", "exec", "--user", "www-data", self.app_container, "php", "occ", "maintenance:mode", "--off"]) - chunks = disable_maintenance_mode.decode("utf-8").split('\n') - if 'Maintenance mode disabled' in chunks: - _print(F"Disable Nextcloud maintenance mode: {self.SUCCESS}") - return True - else: + def __disable_maintenance_mode(self) -> bool: + if not utils.keep_maintenance_mode: + try: + disable_maintenance_mode = check_output( + ["docker", "exec", "--user", "www-data", self.app_container, "php", "occ", "maintenance:mode", "--off"]) + chunks = disable_maintenance_mode.decode("utf-8").split('\n') + if 'Maintenance mode disabled' in chunks: + _print(F"Disable Nextcloud maintenance mode: {self.SUCCESS}") + return True + else: + _print(F"Disable Nextcloud maintenance mode: {self.FAILED}") + return False + except: + self.exceptions.update({'__disable_maintenance_mode': traceback.format_exc()}) _print(F"Disable Nextcloud maintenance mode: {self.FAILED}") return False - except: - self.exceptions.update({'__disable_maintenance_mode': traceback.format_exc()}) - _print(F"Disable Nextcloud maintenance mode: {self.FAILED}") - return False + else: + return True # Pull new docker images def __pull_images(self): diff --git a/upgrade.py b/upgrade.py index 5bb27d2..79f13bc 100644 --- a/upgrade.py +++ b/upgrade.py @@ -10,6 +10,7 @@ import utils from utils import _print from models import Container from models import Log +import backup def upgrade(): @@ -35,40 +36,48 @@ def upgrade(): container: Container for container in containers.values(): - # Start backup - _print("----------------------------------------------") - _print(F"Start upgrade for {container.name} at {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") - result = container.upgrade() - if result == 1: - _print(F"{Fore.GREEN}{container.name} upgraded successfully{Style.RESET_ALL}") - upgrade_status = True - elif result == 2: - _print(F"{Fore.GREEN}No upgrades available for {container.name}.{Style.RESET_ALL}") - upgrade_status = True - else: - _print(F"{Fore.RED}Upgrade for {container.name} failed{Style.RESET_ALL}") - for func, traceback in container.exceptions.items(): - _print() - _print(F"{Fore.YELLOW}Exception occurred in method: Container.{func}(){Style.RESET_ALL}") - _print(traceback) - _print() - upgrade_status = False + go_on = True + # Make a backup + if not utils.no_backup: + utils.keep_maintenance_mode = True + go_on = backup.backup() - # Log upgrade - if not utils.no_log and settings_list['log']['logging']: - if upgrade_status: - log.log(F"Upgrade ; {container.name} ; SUCCESS") + if go_on: + # Make the upgrade + utils.keep_maintenance_mode = True if "--maintenance" in sys.argv else False + _print("----------------------------------------------") + _print(F"Start upgrade for {container.name} at {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") + result = container.upgrade() + if result == 1: + _print(F"{Fore.GREEN}{container.name} upgraded successfully{Style.RESET_ALL}") + upgrade_status = True + elif result == 2: + _print(F"{Fore.GREEN}No upgrades available for {container.name}.{Style.RESET_ALL}") + upgrade_status = True else: - log.log(F"Upgrade ; {container.name} ; FAIL") - if len(log.exceptions) > 0: - for func, traceback in log.exceptions.items(): - _print() - _print(F"{Fore.YELLOW}Exception occurred in method: Log.{func}(){Style.RESET_ALL}") - _print(traceback) - _print() + _print(F"{Fore.RED}Upgrade for {container.name} failed{Style.RESET_ALL}") + for func, traceback in container.exceptions.items(): + _print() + _print(F"{Fore.YELLOW}Exception occurred in method: Container.{func}(){Style.RESET_ALL}") + _print(traceback) + _print() + upgrade_status = False + + # Log upgrade + if not utils.no_log and settings_list['log']['logging']: + if upgrade_status: + log.log(F"Upgrade ; {container.name} ; SUCCESS") + else: + log.log(F"Upgrade ; {container.name} ; FAIL") + if len(log.exceptions) > 0: + for func, traceback in log.exceptions.items(): + _print() + _print(F"{Fore.YELLOW}Exception occurred in method: Log.{func}(){Style.RESET_ALL}") + _print(traceback) + _print() return upgrade_status if __name__ == '__main__': - upgrade() \ No newline at end of file + upgrade() diff --git a/utils.py b/utils.py index acf07ee..1a94c26 100644 --- a/utils.py +++ b/utils.py @@ -4,7 +4,10 @@ no_log = False no_cleanup = False all_containers = False no_confirm = False +no_backup = False +# intern Flags +keep_maintenance_mode = False def set_flags(flags=list): @@ -20,6 +23,7 @@ def set_flags(flags=list): all_containers = "--all" in flags no_cleanup = "--nocleanup" in flags no_confirm = "--yes" in flags + no_backup = "--nobackup" in flags def _print(text=None):