Esempio n. 1
0
    def list_all_backups(self):
        # Grab our backup path from the DB
        backup_list = Backups.get()
        backup_data = model_to_dict(backup_list)

        # List all MC Servers
        servers = multi.list_servers()
        backup_files = []

        # Iterate over all the servers
        for server in servers:
            server_id = server['id']

            # Create path
            path = os.path.join(backup_data['storage_location'], server_id)

            # Search and concat
            file_names, relative_files = helper.list_backups(path)
            backup_files = backup_data + relative_files
        return backup_files
Esempio n. 2
0
    def watch_for_commands(self):
        while True:
            command_instance = Remote.select().where(Remote.id == 1).exists()
            if command_instance:
                entry = Remote.get_by_id(1)
                command_data = model_to_dict(entry)
                command = command_data['command']
                server_id = command_data['server_id']
                source = command_data['command_source']

                server_data = MC_settings.get_by_id(server_id)
                server_name = server_data.server_name

                logger.info(
                    "Remote Command \"%s\" found for server \"%s\" from source %s. Executing!",
                    command, server_name, source)

                self.handle_command(command, server_id)
                self.clear_all_commands()

            time.sleep(1)
    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
    def get_mc_process_stats(self):

        world_data = self.get_world_info()
        server_settings = MC_settings.get(self.server_id)
        server_settings_dict = model_to_dict(server_settings)

        if self.check_running():
            p = psutil.Process(self.PID)

            # call it first so we can be more accurate per the docs
            # https://giamptest.readthedocs.io/en/latest/#psutil.Process.cpu_percent

            dummy = p.cpu_percent()
            real_cpu = round(
                p.cpu_percent(interval=0.5) / psutil.cpu_count(), 2)

            # this is a faster way of getting data for a process
            with p.oneshot():
                server_stats = {
                    'server_start_time':
                    self.get_start_time(),
                    'server_running':
                    self.check_running(),
                    'cpu_usage':
                    real_cpu,
                    'memory_usage':
                    helper.human_readable_file_size(p.memory_info()[0]),
                    'world_name':
                    world_data['world_name'],
                    'world_size':
                    world_data['world_size'],
                    'server_ip':
                    server_settings_dict['server_ip'],
                    'server_port':
                    server_settings_dict['server_port']
                }
        else:
            server_stats = {
                'server_start_time': "Not Started",
                'server_running': False,
                'cpu_usage': 0,
                'memory_usage': "0 MB",
                'world_name': world_data['world_name'],
                'world_size': world_data['world_size'],
                'server_ip': server_settings_dict['server_ip'],
                'server_port': server_settings_dict['server_port']
            }

        # are we pingable?
        try:
            server_ping = self.ping_server()
        except:
            server_ping = False
            pass

        if server_ping:
            online_stats = json.loads(server_ping.players)
            server_stats.update({'online': online_stats.get('online', 0)})
            server_stats.update({'max': online_stats.get('max', 0)})
            server_stats.update({'players': online_stats.get('players', 0)})
            server_stats.update(
                {'server_description': server_ping.description})
            server_stats.update({'server_version': server_ping.version})

        else:
            server_stats.update({'online': 0})
            server_stats.update({'max': 0})
            server_stats.update({'players': []})
            server_stats.update({'server_description': "Unable to connect"})
            server_stats.update({'server_version': "Unable to connect"})

        return server_stats