def update_server_jar(self, with_console=True):

        self.reload_settings()

        self.updating = True

        logger.info("Starting Jar Update Process")

        if with_console:
            console.info("Starting Jar Update Process")

        backup_dir = os.path.join(self.settings.server_path,
                                  'crafty_jar_backups')
        backup_jar_name = os.path.join(backup_dir, 'old_server.jar')
        current_jar = os.path.join(self.settings.server_path,
                                   self.settings.server_jar)
        was_running = False

        if self.check_running():
            was_running = True
            logger.info("Server was running, stopping server for jar update")

            if with_console:
                console.info(
                    "Server was running, stopping server for jar update")

            self.stop_threaded_server()

        # make sure the backup directory exists
        helper.ensure_dir_exists(backup_dir)

        # remove the old_server.jar
        if helper.check_file_exists(backup_jar_name):
            logger.info("Removing old backup jar %s", backup_jar_name)

            if with_console:
                console.info(
                    "Removing old backup jar {}".format(backup_jar_name))

            os.remove(backup_jar_name)

        logger.info("Starting Server Jar Download")

        if with_console:
            console.info("Starting Server Jar Download")

        # backup the server jar file
        logger.info("Backing up Current Jar")
        helper.copy_file(current_jar, backup_jar_name)

        # download the new server jar file
        download_complete = helper.download_file(self.settings.jar_url,
                                                 current_jar)

        if download_complete:
            logger.info("Server Jar Download Complete")

            if with_console:
                console.info("Server Jar Download Complete")
        else:
            if with_console:
                console.info("Server Jar Had An Error")

        if was_running:
            logger.info(
                "Server was running, starting server backup after update")

            if with_console:
                console.info(
                    "Server was running, starting server backup after update")

            self.run_threaded_server()

        self.updating = False
        console.info(
            "Server Jar Update Completed - press enter to get the prompt back")
    def revert_updated_server_jar(self, with_console=True):

        self.reload_settings()

        self.updating = True

        logger.info("Starting Jar Revert Process")

        if with_console:
            console.info("Starting Jar Revert Process")

        backup_dir = os.path.join(self.settings.server_path,
                                  'crafty_jar_backups')
        backup_jar_name = os.path.join(backup_dir, 'old_server.jar')
        current_jar = os.path.join(self.settings.server_path,
                                   self.settings.server_jar)
        was_running = False

        # verify we have a backup
        if not helper.check_file_exists(backup_jar_name):
            logger.critical("Can't find server.jar backup! - can't continue")
            console.critical("Can't find server.jar backup! - can't continue")
            self.updating = False
            return False

        if self.check_running():
            was_running = True
            logger.info("Server was running, stopping server for jar revert")

            if with_console:
                console.info(
                    "Server was running, stopping server for jar revert")

            self.stop_threaded_server()

        # make sure the backup directory exists
        helper.ensure_dir_exists(backup_dir)

        # remove the current_server.jar
        if helper.check_file_exists(backup_jar_name):
            logger.info("Removing current server jar %s", backup_jar_name)

            if with_console:
                console.info(
                    "Removing current server jar: {}".format(backup_jar_name))

            os.remove(current_jar)

        logger.info("Copying old jar back")

        if with_console:
            console.info("Copying old jar back")

        helper.copy_file(backup_jar_name, current_jar)

        if was_running:
            logger.info(
                "Server was running, starting server backup after update")

            if with_console:
                console.info(
                    "Server was running, starting server backup after update")

            self.run_threaded_server()

        self.updating = False
        console.info(
            "Server Jar Revert Completed - press enter to get the prompt back")
    def backup_server(self, announce=True):

        # backup path is saved in the db
        # Load initial backup config
        backup_list = Backups.get_by_id(self.server_id)
        backup_data = model_to_dict(backup_list)

        logger.debug("Using default path defined in database")
        backup_folder = "{}-{}".format(self.server_id, self.name)
        backup_path = os.path.join(backup_data['storage_location'],
                                   backup_folder)
        helper.ensure_dir_exists(backup_path)

        logger.info('Starting Backup Process')

        logger.info('Checking Backup Path Exists')

        if helper.check_directory_exist(backup_path):

            # if server is running
            if announce:
                if self.check_running():
                    self.send_command(
                        "say [Crafty Controller] Starting Backup of Server")

            try:
                # make sure we have a backup for this date
                backup_filename = "{}.zip".format(
                    datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
                backup_full_path = os.path.join(backup_path, backup_filename)

                logger.info("Backing up server directory to %s",
                            backup_filename)
                logger.debug("Full path is %s", backup_full_path)

                backup_dirs = json.loads(backup_data['directories'])

                helper.zippath(backup_dirs, backup_full_path,
                               ['crafty_backups'])

                logger.info("Backup Completed")

                if announce:
                    if self.check_running():
                        self.send_command(
                            "say [Crafty Controller] Backup Complete")

            except Exception as e:
                logger.exception(
                    "Unable to create backups! Traceback:".format(e))

                if announce:
                    if self.check_running():
                        self.send_command(
                            'say [Crafty Controller] Unable to create backups - check the logs'
                        )

            # remove any extra backups
            max_backups = backup_data['max_backups']
            logger.info("Checking for backups older than %s days", max_backups)
            helper.del_files_older_than_x_days(max_backups, backup_path)

        else:
            logger.error("Unable to find or create backup path!")
            return False