In some Fedora versions /etc/sysconfig/ntpd has OPTIONS scattered on
multiple line through shell expansion.
Handle simple cases like that.

Simo.

-- 
Simo Sorce * Red Hat, Inc * New York
>From 3f140eff0e264bae3d23b1e8a94f52886ba09469 Mon Sep 17 00:00:00 2001
From: Simo Sorce <sso...@redhat.com>
Date: Wed, 20 Oct 2010 17:03:10 -0400
Subject: [PATCH] Handle cases where ntpd options are scattered on multiple lines

---
 ipaserver/install/ntpinstance.py |   47 +++++++++++++++++++++++++++++++++----
 1 files changed, 42 insertions(+), 5 deletions(-)

diff --git a/ipaserver/install/ntpinstance.py b/ipaserver/install/ntpinstance.py
index 3f7f715..43149fc 100644
--- a/ipaserver/install/ntpinstance.py
+++ b/ipaserver/install/ntpinstance.py
@@ -1,6 +1,7 @@
 # Authors: Karl MacMillan <kmacmil...@redhat.com>
+# Authors: Simo Sorce <sso...@redhat.com>
 #
-# Copyright (C) 2007  Red Hat
+# Copyright (C) 2007-2010  Red Hat
 # see file 'COPYING' for use and warranty information
 #
 # This program is free software; you can redistribute it and/or
@@ -100,27 +101,63 @@ class NTPInstance(service.Service):
         file_changed = False
         found_options = False
         ntpdsysc = []
+        options_line = -1
+        line_num = 0
         fd = open("/etc/sysconfig/ntpd", "r")
         for line in fd:
+            skip_line = False
             sline = line.strip()
             if sline.find("OPTIONS") == 0:
-                found_options = True
                 opts = sline.split("=", 1)
                 if len(opts) != 2:
                     optvals=""
                 else:
                     optvals = opts[1].strip(' "')
+                added_option = False
                 if optvals.find("-x") == -1:
                     optvals += " -x"
                     file_changed = True
+                    added_option = True
                 if optvals.find("-g") == -1:
                     optvals += " -g"
                     file_changed = True
-                if file_changed:
+                    added_option = True
+                if added_option:
                     line = 'OPTIONS="'+optvals+'"\n'
-            ntpdsysc.append(line)
+                if options_line >= 0:
+                    file_changed = True
+                    skip_line = True
+                    #multiple options lines
+                    if optvals.find("$OPTIONS") == -1:
+                        #replaces
+                        ntpdsysc[options_line] = line
+                    else:
+                        #needs merge
+                        try:
+                            oldline = ntpdsysc[options_line].strip()
+                            oldopts = oldline.split("=", 1)
+                        except:
+                            oldopts = []
+                            pass
+                        if len(oldopts) != 2:
+                            #wut? just replace
+                            ntpdsysc[options_line] = line
+                        else:
+                            oldvals = oldopts[1].strip(' "')
+                            newvals = optvals.split()
+                            newvals.remove("$OPTIONS")
+                            for newopt in newvals:
+                                if oldvals.find(newopt) == -1:
+                                    oldvals += " " + newopt
+                            ntpdsysc[options_line] = 'OPTIONS="'+oldvals+'"\n'
+                else:
+                    options_line = line_num
+            if not skip_line:
+                ntpdsysc.append(line)
+                line_num = line_num + 1
+
         fd.close()
-        if not found_options:
+        if options_line == -1:
             ntpdsysc.insert(0, 'OPTIONS="-x -g"\n')
             file_changed = True
 
-- 
1.7.2.3

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to