def _save_folder(self, folder): store.mkdir(folder.get_root_dir()) has_content = False content = "" for varname, ruleset in sorted(self._rulesets.items(), key=lambda x: x[0]): if varname not in rulespec_registry: continue # don't save unknown rulesets if ruleset.is_empty_in_folder(folder): continue # don't save empty rule sets has_content = True content += ruleset.to_config(folder) rules_file_path = folder.rules_file_path() try: # Remove rules files if it has no content. This prevents needless reads if not has_content: if os.path.exists(rules_file_path): os.unlink(rules_file_path) # Do not keep empty rules.mk files return # Adding this instead of the full path makes it easy to move config # files around. The real FOLDER_PATH will be added dynamically while # loading the file in cmk.base.config content = content.replace("'%s'" % _FOLDER_PATH_MACRO, "'/%s/' % FOLDER_PATH") store.save_mk_file(rules_file_path, content, add_header=not active_config.wato_use_git) finally: if may_use_redis(): get_wato_redis_client().folder_updated(folder.filesystem_path())
def get_fake_setup_redis_client(monkeypatch, all_folders, redis_answers: List): monkeypatch.setattr(hosts_and_folders, "may_use_redis", lambda: True) mock_redis_client = MockRedisClient(redis_answers) monkeypatch.setattr(hosts_and_folders._RedisHelper, "_cache_integrity_ok", lambda x: True) redis_helper = hosts_and_folders.get_wato_redis_client() monkeypatch.setattr(redis_helper, "_client", mock_redis_client) monkeypatch.setattr(redis_helper, "_folder_paths", [f"{x}/" for x in all_folders.keys()]) monkeypatch.setattr( redis_helper, "_folder_metadata", { f"{x}/": hosts_and_folders.FolderMetaData(f"{x}/", "nix", "nix", []) for x in all_folders.keys() }, ) yield mock_redis_client monkeypatch.setattr(hosts_and_folders, "may_use_redis", lambda: False) # I have no idea if this is actually working.. monkeypatch.undo()
def _load_rulesets_via_redis( self, folder: CREFolder, only_varname: Optional[RulesetName] = None ) -> None: # Search relevant folders with rules.mk files # Note: The sort order of the folders does not matter here # self._load_folder_rulesets ultimately puts each folder into a dict # and groups/sorts them later on with a different mechanism all_folders = get_wato_redis_client().recursive_subfolders_for_path( f"{folder.path()}/".lstrip("/") ) root_dir = wato_root_dir()[:-1] relevant_folders = [] for folder_path in all_folders: if os.path.exists(f"{root_dir}/{folder_path}rules.mk"): relevant_folders.append(folder_path) for folder_path_with_slash in relevant_folders: stripped_folder = folder_path_with_slash.strip("/") self._load_folder_rulesets(Folder.folder(stripped_folder), only_varname)