def get_servers_from_config(conf_file_path=NTP_CONFIG_FILE): """Get NTP servers from a configuration file. Goes through the chronyd's configuration file looking for lines starting with 'server'. :param conf_file_path: a path to the chronyd's configuration file :return: servers found in the chronyd's configuration :rtype: a list of TimeSourceData instances """ servers = [] try: with open(conf_file_path, "r") as conf_file: for line in conf_file: match = SRV_LINE_REGEXP.match(line) if not match: continue server = TimeSourceData() server.type = match.group(1).upper() server.hostname = match.group(2) server.options = ["iburst"] servers.append(server) except IOError as ioerr: msg = "Cannot open config file {} for reading ({})." raise NTPconfigError(msg.format(conf_file_path, ioerr.strerror)) return servers
def on_add_button_clicked(self, *args): """Handler for Add button. Tries to add a new server for editing, or reuse an existing server that was not edited after adding. """ # check if there is any unedited server # exactly zero or one such server can exist, at last position only if not self._servers[-1].hostname == SERVER_STARTING_STRING: # no unedited leftover, so make a new server with a reasonable guess about the defaults server = TimeSourceData() server.type = TIME_SOURCE_SERVER server.hostname = SERVER_STARTING_STRING server.options = ["iburst"] # add the (still invalid) server self._servers.append(server) self._states.check_status(server) self._add_row(server) # select the correct row - it is always the last one num = len(self._serversStore) path = Gtk.TreePath(num - 1) itr = self._serversStore.get_iter(path) selection = self._serversView.get_selection() selection.select_iter(itr) self._serversView.grab_focus() # start editing the newly added server hostname # it is already selected so just "press" the edit button self.on_edit_button_clicked(*args)
def _add_ntp_server(self, server_hostname): for server in self._servers: if server.hostname == server_hostname: return server = TimeSourceData() server.type = TIME_SOURCE_SERVER server.hostname = server_hostname server.options = ["iburst"] self._servers.append(server) self._states.check_status(server)
def _get_test_sources(self): """Get a list of sources""" server = TimeSourceData() server.type = TIME_SOURCE_SERVER server.hostname = "unique.ntp.server" server.options = ["iburst"] pool = TimeSourceData() pool.type = TIME_SOURCE_POOL pool.hostname = "another.unique.server" return [server, pool]
def initialize(self): self.initialize_start() # We get the initial NTP servers (if any): # - from kickstart when running inside of Anaconda # during the installation # - from config files when running in Initial Setup # after the installation if constants.ANACONDA_ENVIRON in flags.environs: self._ntp_servers = TimeSourceData.from_structure_list( self._timezone_module.TimeSources) elif constants.FIRSTBOOT_ENVIRON in flags.environs: self._ntp_servers = ntp.get_servers_from_config() else: log.error( "tui time spoke: unsupported environment configuration %s," "can't decide where to get initial NTP servers", flags.environs) # check if the newly added NTP servers work fine for server in self._ntp_servers: self._ntp_servers_states.check_status(server) # we assume that the NTP spoke is initialized enough even if some NTP # server check threads might still be running self.initialize_done()
def get_servers_from_config(conf_file_path=NTP_CONFIG_FILE): """Get NTP servers from a configuration file. Goes through the chronyd's configuration file looking for lines starting with 'server'. :param conf_file_path: a path to the chronyd's configuration file :return: servers found in the chronyd's configuration :rtype: a list of TimeSourceData instances """ servers = [] try: with open(conf_file_path, "r") as conf_file: for line in conf_file: match = SRV_LINE_REGEXP.match(line) if not match: continue server = TimeSourceData() server.type = match.group(1).upper() server.hostname = match.group(2) server.options = [] words = match.group(3).lower().split() skip_argument = False for i in range(len(words)): if skip_argument: skip_argument = False continue if words[i] in SRV_NOARG_OPTIONS: server.options.append(words[i]) elif words[i] in SRV_ARG_OPTIONS and i + 1 < len(words): server.options.append(' '.join(words[i:i + 2])) skip_argument = True else: log.debug("Unknown NTP server option %s", words[i]) servers.append(server) except IOError as ioerr: msg = "Cannot open config file {} for reading ({})." raise NTPconfigError(msg.format(conf_file_path, ioerr.strerror)) return servers
def TimeSources(self, sources: List[Structure]): """Set the time sources. :param sources: a list of time sources :type sources: a list of structures of the type TimeSourceData """ self.implementation.set_time_sources( TimeSourceData.from_structure_list(sources))
def TimeSources(self) -> List[Structure]: """A list of time sources. :return: a list of time source data :rtype: a list of structures of the type TimeSourceData """ return TimeSourceData.to_structure_list( self.implementation.time_sources)
def test_install_with_tasks_configured(self, publisher): """Test install tasks - module in configured state.""" self.timezone_interface.SetIsUTC(True) self.timezone_interface.SetTimezone("Asia/Tokyo") self.timezone_interface.SetNTPEnabled(False) # --nontp and --ntpservers are mutually exclusive in kicstart but # there is no such enforcement in the module so for testing this is ok server = TimeSourceData() server.type = TIME_SOURCE_SERVER server.hostname = "clock1.example.com" server.options = ["iburst"] pool = TimeSourceData() pool.type = TIME_SOURCE_POOL pool.hostname = "clock2.example.com" self.timezone_interface.SetTimeSources( TimeSourceData.to_structure_list([server, pool]) ) task_classes = [ ConfigureHardwareClockTask, ConfigureTimezoneTask, ConfigureNTPTask, ] task_paths = self.timezone_interface.InstallWithTasks() task_objs = check_task_creation_list(task_paths, publisher, task_classes) # ConfigureHardwareClockTask obj = task_objs[0] assert obj.implementation._is_utc is True # ConfigureTimezoneTask obj = task_objs[1] assert obj.implementation._timezone == "Asia/Tokyo" assert obj.implementation._is_utc is True # ConfigureNTPTask obj = task_objs[2] assert obj.implementation._ntp_enabled is False assert len(obj.implementation._ntp_servers) == 2 assert compare_data(obj.implementation._ntp_servers[0], server) assert compare_data(obj.implementation._ntp_servers[1], pool)
def on_entry_activated(self, entry, *args): # Check that the input check has passed if self._serverCheck.check_status != InputCheck.CHECK_OK: return server = TimeSourceData() if self._poolCheckButton.get_active(): server.type = TIME_SOURCE_POOL else: server.type = TIME_SOURCE_SERVER server.hostname = entry.get_text() server.options = ["iburst"] if self._ntsCheckButton.get_active(): server.options.append("nts") self._servers.append(server) self._states.check_status(server) self._add_row(server) entry.set_text("") self._poolCheckButton.set_active(False) self._ntsCheckButton.set_active(False)
def _set_ntp_servers_from_dhcp(): """Set NTP servers of timezone module from dhcp if not set by kickstart.""" # FIXME - do it only if they will be applied (the guard at the end of the function) timezone_proxy = TIMEZONE.get_proxy() ntp_servers = get_ntp_servers_from_dhcp(get_nm_client()) log.info("got %d NTP servers from DHCP", len(ntp_servers)) hostnames = [] for server_address in ntp_servers: try: hostname = socket.gethostbyaddr(server_address)[0] except socket.error: # getting hostname failed, just use the address returned from DHCP log.debug("getting NTP server host name failed for address: %s", server_address) hostname = server_address hostnames.append(hostname) # check if some NTP servers were specified from kickstart if not timezone_proxy.TimeSources and conf.target.is_hardware: # no NTP servers were specified, add those from DHCP servers = [] for hostname in hostnames: server = TimeSourceData() server.type = TIME_SOURCE_SERVER server.hostname = hostname server.options = ["iburst"] servers.append(server) timezone_proxy.SetTimeSources( TimeSourceData.to_structure_list(servers))
def refresh(self): self._shown = True # update the displayed time self._update_datetime_timer = Timer() self._update_datetime_timer.timeout_sec(1, self._update_datetime) self._start_updating_timer = None kickstart_timezone = self._timezone_module.Timezone if is_valid_timezone(kickstart_timezone): self._tzmap.set_timezone(kickstart_timezone) time.tzset() self._update_datetime() # update the ntp configuration self._ntp_servers = TimeSourceData.from_structure_list( self._timezone_module.TimeSources) if not self._ntp_servers: try: self._ntp_servers = ntp.get_servers_from_config() except ntp.NTPconfigError: log.warning("Failed to load NTP servers configuration") self._ntp_servers_states = NTPServerStatusCache() self._ntp_servers_states.changed.connect( self._update_ntp_server_warning) has_active_network = self._network_module.Connected if not has_active_network: self._show_no_network_warning() else: self.clear_info() for server in self._ntp_servers: self._ntp_servers_states.check_status(server) if conf.system.can_set_time_synchronization: ntp_working = has_active_network and is_service_running( NTP_SERVICE) else: ntp_working = self._timezone_module.NTPEnabled self._ntpSwitch.set_active(ntp_working)
def start_chronyd(): """Start the NTP daemon chronyd. Set up NTP servers and start NTP daemon if not requested otherwise. """ if not conf.system.can_set_time_synchronization: log.debug("Skip the time synchronization.") return timezone_proxy = TIMEZONE.get_proxy() enabled = timezone_proxy.NTPEnabled servers = TimeSourceData.from_structure_list(timezone_proxy.TimeSources) if servers: ntp.save_servers_to_config(servers) if enabled: util.start_service("chronyd")
def on_ntp_config_clicked(self, *args): servers = copy.deepcopy(self._ntp_servers) states = self._ntp_servers_states dialog = NTPConfigDialog(self.data, servers, states) dialog.refresh() with self.main_window.enlightbox(dialog.window): response = dialog.run() if response == 1: self._timezone_module.SetTimeSources( TimeSourceData.to_structure_list(servers)) self._ntp_servers = servers working_server = self._get_working_server() if working_server is None: self._show_no_ntp_server_warning() else: self.clear_info()
def on_ntp_config_clicked(self, *args): servers = copy.deepcopy(self._ntp_servers) states = self._ntp_servers_states # Temporarily disconnect the update callback. states.changed.disconnect(self._update_ntp_server_warning) dialog = NTPConfigDialog(self.data, servers, states) dialog.refresh() with self.main_window.enlightbox(dialog.window): response = dialog.run() # Connect the update callback again. states.changed.connect(self._update_ntp_server_warning) if response == 1: self._timezone_module.TimeSources = \ TimeSourceData.to_structure_list(servers) self._ntp_servers = servers self._update_ntp_server_warning()
def apply(self): # update the NTP server list in kickstart self._timezone_module.SetTimeSources( TimeSourceData.to_structure_list(self._ntp_servers))
def process_kickstart(self, data): """Process the kickstart data.""" self.set_timezone(data.timezone.timezone) self.set_is_utc(data.timezone.isUtc) self.set_ntp_enabled(not data.timezone.nontp) sources = [] for hostname in data.timezone.ntpservers: source = TimeSourceData() source.type = TIME_SOURCE_SERVER source.hostname = hostname source.options = ["iburst"] sources.append(source) for source_data in data.timesource.dataList(): if source_data.ntp_disable: self.set_ntp_enabled(False) continue source = TimeSourceData() source.options = ["iburst"] if source_data.ntp_server: source.type = TIME_SOURCE_SERVER source.hostname = source_data.ntp_server elif source_data.ntp_pool: source.type = TIME_SOURCE_POOL source.hostname = source_data.ntp_pool else: KickstartParseError(_("Invalid time source."), lineno=source_data.lineno) if source_data.nts: source.options.append("nts") sources.append(source) self.set_time_sources(sources)