[Freeipa-devel] [PATCH] 0016 Setup and restore ntp configuration on the

Alexander Bokovoy abokovoy at redhat.com
Tue Oct 4 11:00:13 UTC 2011


client
Reply-To: 

Hi,

attached patch addresses ticket #1770.

-- 
/ Alexander Bokovoy
-------------- next part --------------
>From 6bb9520e2398a22c0264276171714ea5d201f83a Mon Sep 17 00:00:00 2001
From: Alexander Bokovoy <abokovoy at redhat.com>
Date: Tue, 4 Oct 2011 13:56:12 +0300
Subject: [PATCH] Setup and restore ntp configuration on the client side
 properly

When setting up the client-side NTP configuration, make sure that /etc/ntp/step-tickers
point to IPA NTP server as well.
When restoring the client during ipa-client-install --uninstall, make sure NTP configuration
is fully restored and NTP service is disabled if it was disabled before the installation.

https://fedorahosted.org/freeipa/ticket/1770
---
 ipa-client/ipa-install/ipa-client-install |   19 ++++++++++-
 ipa-client/ipaclient/ntpconf.py           |   52 ++++++++++++++++++++--------
 2 files changed, 55 insertions(+), 16 deletions(-)

diff --git a/ipa-client/ipa-install/ipa-client-install b/ipa-client/ipa-install/ipa-client-install
index 76f7f1913c804053edb8b90979286a0592fa5737..85f94074bfede3106b39e4d603d99d93930def5b 100755
--- a/ipa-client/ipa-install/ipa-client-install
+++ b/ipa-client/ipa-install/ipa-client-install
@@ -331,6 +331,23 @@ def uninstall(options, env, quiet=False):
                     emit_quiet(quiet, "Reboot command failed to exceute. " + str(e))
                     return CLIENT_UNINSTALL_ERROR
 
+    ntp_configured = statestore.has_state('ntp')
+    if ntp_configured:
+        ntp_enabled = statestore.restore_state('ntp', 'enabled')
+        ntp_step_tickers = statestore.restore_state('ntp', 'step-tickers')
+
+        restored = fstore.restore_file("/etc/ntp.conf")
+        restored |= fstore.restore_file("/etc/sysconfig/ntpd")
+        if ntp_step_tickers:
+           restored |= fstore.restore_file("/etc/ntp/step-tickers")
+
+        if not ntp_enabled:
+           ipaservices.knownservices.ntp.stop()
+           ipaservices.knownservices.ntp.disable()
+        else:
+           if restored:
+               ipaservices.knownservices.ntp.restart()
+
     # Remove the IPA configuration file
     try:
         os.remove("/etc/ipa/default.conf")
@@ -1102,7 +1119,7 @@ def install(options, env, fstore, statestore):
             ntp_server = options.ntp_server
         else:
             ntp_server = cli_server
-        ipaclient.ntpconf.config_ntp(ntp_server, fstore)
+        ipaclient.ntpconf.config_ntp(ntp_server, fstore, statestore)
         print "NTP enabled"
 
     print "Client configuration complete."
diff --git a/ipa-client/ipaclient/ntpconf.py b/ipa-client/ipaclient/ntpconf.py
index 3042005f41ea3ed6c8fee739b9cf2b833a8d6d59..f63e5f9795efc38e0843f9e14f51ef286d1ddebc 100644
--- a/ipa-client/ipaclient/ntpconf.py
+++ b/ipa-client/ipaclient/ntpconf.py
@@ -20,6 +20,7 @@
 from ipapython import ipautil
 from ipapython import services as ipaservices
 import shutil
+import os
 
 ntp_conf = """# Permit time synchronization with our time source, but do not
 # permit the source to query or modify the service on this system.
@@ -80,30 +81,51 @@ SYNC_HWCLOCK=yes
 # Additional options for ntpdate
 NTPDATE_OPTIONS=""
 """
+ntp_step_tickers = """# Use IPA-provided NTP server for initial time
+$SERVER
+"""
+def __backup_config(path, fstore = None):
+    if fstore:
+        fstore.backup_file(path)
+    else:
+        shutil.copy(path, "%s.ipasave" % (path))
 
-def config_ntp(server_fqdn, fstore = None):
+def __write_config(path, content):
+    fd = open(path, "w")
+    fd.write(content)
+    fd.close()
+
+def config_ntp(server_fqdn, fstore = None, sysstore = None):
+    path_step_tickers = "/etc/ntp/step-tickers"
+    path_ntp_conf = "/etc/ntp.conf"
+    path_ntp_sysconfig = "/etc/sysconfig/ntpd"
     sub_dict = { }
     sub_dict["SERVER"] = server_fqdn
 
     nc = ipautil.template_str(ntp_conf, sub_dict)
+    config_step_tickers = False
 
-    if fstore:
-        fstore.backup_file("/etc/ntp.conf")
-    else:
-        shutil.copy("/etc/ntp.conf", "/etc/ntp.conf.ipasave")
 
-    fd = open("/etc/ntp.conf", "w")
-    fd.write(nc)
-    fd.close()
+    if os.path.exists(path_step_tickers):
+        config_step_tickers = True
+        ns = ipautil.template_str(ntp_step_tickers, sub_dict)
+        __backup_config(path_step_tickers, fstore)
+        __write_config(path_step_tickers, ns)
+        ipaservices.restore_context(path_step_tickers)
 
-    if fstore:
-        fstore.backup_file("/etc/sysconfig/ntpd")
-    else:
-        shutil.copy("/etc/sysconfig/ntpd", "/etc/sysconfig/ntpd.ipasave")
+    if sysstore:
+        module = 'ntp'
+        sysstore.backup_state(module, "enabled", ipaservices.knownservices.ntp.enabled())
+        if config_step_tickers:
+            sysstore.backup_state(module, "step-tickers", True)
 
-    fd = open("/etc/sysconfig/ntpd", "w")
-    fd.write(ntp_sysconfig)
-    fd.close()
+    __backup_config(path_ntp_conf, fstore)
+    __write_config(path_ntp_conf, nc)
+    ipaservices.restore_context(path_ntp_conf)
+
+    __backup_config(path_ntp_sysconfig)
+    __write_config(path_ntp_sysconfig, ntp_sysconfig)
+    ipaservices.restore_context(path_ntp_sysconfig)
 
     # Set the ntpd to start on boot
     ipaservices.knownservices.ntpd.enable()
-- 
1.7.6.4



More information about the Freeipa-devel mailing list