def create_site_globals_file(self, site_id, tmp_dir, sites=None): # TODO: Cleanup this local import import cmk.gui.watolib.sites # pylint: disable=redefined-outer-name try: os.makedirs(tmp_dir) except OSError as e: if e.errno == errno.EEXIST: pass else: raise if not sites: sites = cmk.gui.watolib.sites.SiteManagementFactory().factory( ).load_sites() site = sites[site_id] site_globals = site.get("globals", {}) site_globals.update({ "wato_enabled": not site.get("disable_wato", True), "userdb_automatic_sync": site.get("user_sync", userdb.user_sync_default_config(site_id)), }) store.save_data_to_file(tmp_dir + "/sitespecific.mk", site_globals)
def save_to(self, path, filename, pretty=False): filepath = "%s/%s" % (path, filename) output = self.get_raw_tree() store.save_data_to_file(filepath, output, pretty=pretty) gzip.open(filepath + ".gz", "w").write(repr(output) + "\n") # Inform Livestatus about the latest inventory update store.save_file("%s/.last" % path, "")
def rename_host_in_multisite(oldname, newname): # State of Multisite --------------------------------------- # Favorites of users and maybe other settings. We simply walk through # all directories rather then through the user database. That way we # are sure that also currently non-existant users are being found and # also only users that really have a profile. users_changed = 0 total_changed = 0 for userid in os.listdir(config.config_dir): if userid[0] == '.': continue if not os.path.isdir(config.config_dir + "/" + userid): continue favpath = config.config_dir + "/" + userid + "/favorites.mk" num_changed = 0 favorites = store.load_data_from_file(favpath, [], lock=True) for nr, entry in enumerate(favorites): if entry == oldname: favorites[nr] = newname num_changed += 1 elif entry.startswith(oldname + ";"): favorites[nr] = newname + ";" + entry.split(";")[1] num_changed += 1 if num_changed: store.save_data_to_file(favpath, favorites) users_changed += 1 total_changed += num_changed store.release_lock(favpath) if users_changed: return ["favorites"] * total_changed return []
def _save_snmpwalk_cache(hostname, fetchoid, rowinfo): path = _snmpwalk_cache_path(hostname, fetchoid) if not os.path.exists(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) console.vverbose(" Saving walk of %s to walk cache %s\n" % (fetchoid, path)) store.save_data_to_file(path, rowinfo, pretty=False)
def write_single_oid_cache(snmp_config): # type: (snmp_utils.SNMPHostConfig) -> None if not _g_single_oid_cache: return cache_dir = cmk.utils.paths.snmp_scan_cache_dir if not os.path.exists(cache_dir): os.makedirs(cache_dir) cache_path = "%s/%s.%s" % (cache_dir, snmp_config.hostname, snmp_config.ipaddress) store.save_data_to_file(cache_path, _g_single_oid_cache, pretty=False)
def _get_login_secret(create_on_demand=False): path = cmk.utils.paths.var_dir + "/wato/automation_secret.mk" secret = store.load_data_from_file(path) if secret is not None: return secret if not create_on_demand: return None secret = cmk.gui.utils.get_random_string(32) store.save_data_to_file(path, secret) return secret
def test_save_data_to_file_not_pretty(tmp_path): path = str(tmp_path / "test") data = { "asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd", "1asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd", "2asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd", "3asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd", "4asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd", "5asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd", } store.save_data_to_file(path, data, pretty=False) assert file(path).read().count("\n") == 1 assert store.load_data_from_file(path) == data
def test_save_data_to_file_pretty(tmp_path, path_type): path = path_type(tmp_path / "test") data = { "asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd", "1asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd", "2asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd", "3asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd", "4asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd", "5asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd", } store.save_data_to_file(path, data) assert open(str(path)).read().count("\n") > 4 assert store.load_data_from_file(path) == data
def test_save_data_to_file_not_pretty(tmpdir): f = tmpdir.join("test") path = "%s" % f data = { "asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd", "1asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd", "2asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd", "3asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd", "4asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd", "5asdasaaaaaaaaaaaaaaaaaaaad": "asbbbbbbbbbbbbbbbbbbd", } store.save_data_to_file(path, data, pretty=False) assert file(path).read().count("\n") == 1 assert store.load_data_from_file(path) == data
def _store_persisted_sections(self, persisted_sections): if not persisted_sections: return file_path = self._persisted_sections_file_path() try: os.makedirs(os.path.dirname(file_path)) except OSError as e: if e.errno == errno.EEXIST: pass else: raise store.save_data_to_file(file_path, persisted_sections, pretty=False) self._logger.debug("Stored persisted sections: %s" % (", ".join(persisted_sections.keys())))
def _save_activation(self): try: os.makedirs(os.path.dirname(self._info_path())) except OSError as e: if e.errno == errno.EEXIST: pass else: raise return store.save_data_to_file( self._info_path(), { "_sites": self._sites, "_activate_until": self._activate_until, "_comment": self._comment, "_activate_foreign": self._activate_foreign, "_activation_id": self._activation_id, "_snapshot_id": self._snapshot_id, "_time_started": self._time_started, })
def _save_state(self): state_path = os.path.join(ActivateChangesManager.activation_tmp_base_dir, self._activation_id, ActivateChangesManager.site_filename(self._site_id)) return store.save_data_to_file( state_path, { "_site_id": self._site_id, "_phase": self._phase, "_state": self._state, "_status_text": self._status_text, "_status_details": self._status_details, "_warnings": self._warnings, "_time_started": self._time_started, "_time_updated": self._time_updated, "_time_ended": self._time_ended, "_expected_duration": self._expected_duration, "_pid": self._pid, })
def save_user_file(name, data, user_id, unlock=False): path = config_dir + "/" + user_id.encode("utf-8") + "/" + name + ".mk" store.mkdir(os.path.dirname(path)) store.save_data_to_file(path, data)
def _save_site_replication_status(site_id, repl_status): store.save_data_to_file(_site_replication_status_path(site_id), repl_status, pretty=False) _cleanup_legacy_replication_status()
def save_acknowledgements(acknowledged_werks): store.save_data_to_file(acknowledgement_path, acknowledged_werks)
def save_gui_messages(messages, user_id=None): if user_id is None: user_id = config.user.id path = config.config_dir + "/" + user_id.encode("utf-8") + '/messages.mk' store.mkdir(os.path.dirname(path)) store.save_data_to_file(path, messages)
def test_save_data_to_file(tmp_path): path = str(tmp_path / "test") store.save_data_to_file(path, [2, 3]) assert store.load_data_from_file(path) == [2, 3]
def test_save_data_to_file(tmpdir): f = tmpdir.join("test") path = "%s" % f store.save_data_to_file(path, [2, 3]) assert store.load_data_from_file(path) == [2, 3]
def _save_acknowledgements(self, acknowledged_werks): store.save_data_to_file(self._ack_path, acknowledged_werks)