1
0
mirror of https://github.com/altlinux/gpupdate.git synced 2025-03-21 18:50:38 +03:00

NTP applier expanded with simple start mechanism

This commit is contained in:
Игорь Чудов 2020-07-21 18:54:40 +04:00
parent 211568e345
commit 149f929616
Signed by untrusted user: nir
GPG Key ID: 0F3883600CAE7AAC

View File

@ -18,9 +18,14 @@
import logging
import subprocess
from enum import Enum
from applier_frontend import (
applier_frontend
, check_enabled
)
from util.logging import slogm
@ -28,7 +33,7 @@ class NTPServerType(Enum):
NTP = 'NTP'
class ntp_applier:
class ntp_applier(applier_frontend):
__module_name = 'NTPApplier'
__module_experimental = True
__module_enabled = False
@ -42,6 +47,8 @@ class ntp_applier:
__ntp_key_client_enabled = 'Enabled'
__ntp_key_server_enabled = 'Enabled'
__chrony_config = '/etc/chrony.conf'
def __init__(self, storage, sid, username):
self.storage = storage
self.sid = sid
@ -52,12 +59,99 @@ class ntp_applier:
self.ntp_client_enabled = '{}\\{}'.format(self.__ntp_client_branch, self.__ntp_key_client_enabled)
self.ntp_server_enabled = '{}\\{}'.format(self.__ntp_server_branch, self.__ntp_key_server_enabled)
self.__module_enabled = check_enabled(
self.storage
, self.__module_name
, self.__module_experimental
)
def _start_ntpd_server(self, server=None):
start_command = ['systemctl', 'start', 'ntpd']
logging.debug(slogm('Starting ntpd as a server'))
proc = subprocess.Popen(start_command)
proc.wait()
def _stop_ntpd_server(self):
stop_command = ['systemctl', 'stop', 'ntpd']
logging.debug(slogm('Stopping ntpd server'))
proc = subprocess.Popen(stop_command)
proc.wait()
def _start_ntpd_client(self, server=None):
start_command = ['systemctl', 'start', 'ntpd']
logging.debug(slogm('Starting ntpd as a client'))
proc = subprocess.Popen(start_command)
proc.wait()
def _stop_ntpd_client(self):
stop_command = ['systemctl', 'stop', 'ntpd']
logging.debug(slogm('Stopping ntpd client'))
proc = subprocess.Popen(stop_command)
proc.wait()
def _start_chrony_client(self, server=None):
start_command = ['systemctl', 'start', 'chronyd']
chrony_set_server = ['chronyc', 'add', 'server', server]
chrony_disconnect_all = ['chronyc', 'offline']
chrony_connect = ['chronyc', 'online', server]
logging.debug(slogm('Starting Chrony daemon'))
proc = subprocess.Popen(start_command)
proc.wait()
if server:
logging.debug(slogm('Setting reference NTP server to {}'.format(server)))
proc = subprocess.Popen(chrony_disconnect_all)
proc.wait()
proc = subprocess.Popen(chrony_set_server)
proc.wait()
proc = subprocess.Popen(chrony_connect)
proc.wait()
def _stop_chrony_client(self):
stop_command = ['systemctl', 'stop', 'chronyd']
logging.debug(slogm('Stopping Chrony daemon'))
proc = subprocess.Popen(stop_command)
proc.wait()
def run(self):
server_type = self.storage.get_hklm_key(self.ntp_server_type)
server_address = self.storage.get_hklm_key(self.ntp_server_address_key)
ntp_server_enabled = self.storage.get_hklm_key(self.ntp_server_enabled)
ntp_client_enabled = self.storage.get_hklm_key(self.ntp_client_enabled)
if NTPServerType.NTP.value != server_type:
logging.warning(slogm('Unsupported NTP server type: {}'.format(server_type)))
else:
logging.debug(slogm('Configuring NTP server...'))
if '1' == ntp_server_enabled:
self._stop_chrony_client()
self._start_ntpd_server(server_address)
if '1' == ntp_client_enabled:
self._stop_chrony_client()
self._start_ntpd_client(server_address)
elif '0' == ntp_server_enabled:
self._stop_ntpd_server()
if '1' == ntp_client_enabled:
self._stop_ntpd_client()
self._start_chrony_client(server_address)
elif '0' == ntp_client_enabled:
self._stop_ntpd_client()
self._stop_chrony_client()
def apply(self):
if self.__module_enabled: