[PATCH 1/7] Clean up ifcfg file handling.
Vratislav Podzimek
vpodzime at redhat.com
Fri Sep 13 14:22:22 UTC 2013
On Fri, 2013-09-13 at 15:32 +0200, Radek Vykydal wrote:
> - Remove obsolete NetworkDevice class, use just IfcfgFile instead.
> - More robust lookup of ifcfg files of devices - based on values
> instead of relying on filename.
> ---
> pyanaconda/installclasses/fedora.py | 14 +-
> pyanaconda/network.py | 269 +++++++++++++++++-------------------
> pyanaconda/nm.py | 38 +++++
> pyanaconda/simpleconfig.py | 34 -----
> pyanaconda/ui/gui/spokes/network.py | 29 ++--
> pyanaconda/ui/tui/spokes/network.py | 12 +-
> 6 files changed, 187 insertions(+), 209 deletions(-)
>
> diff --git a/pyanaconda/installclasses/fedora.py b/pyanaconda/installclasses/fedora.py
> index 2977211..c433d49 100644
> --- a/pyanaconda/installclasses/fedora.py
> +++ b/pyanaconda/installclasses/fedora.py
> @@ -63,15 +63,15 @@ class InstallClass(BaseInstallClass):
> except ValueError:
> continue
> if link_up:
> - dev = network.NetworkDevice(ROOT_PATH + network.netscriptsDir, devName)
> - try:
> - dev.loadIfcfgFile()
> - except IOError:
> + ifcfg_path = network.find_ifcfg_file_of_device(devName, root_path=ROOT_PATH)
> + if not ifcfg_path:
> continue
> - dev.set(('ONBOOT', 'yes'))
> - dev.writeIfcfgFile()
> + ifcfg = network.IfcfgFile(ifcfg_path)
> + ifcfg.read()
> + ifcfg.set(('ONBOOT', 'yes'))
> + ifcfg.write()
> for nd in ksdata.network.network:
> - if nd.device == dev.iface:
> + if nd.device == devName:
> nd.onboot = True
> break
> break
> diff --git a/pyanaconda/network.py b/pyanaconda/network.py
> index d21e9b0..9c526ad 100644
> --- a/pyanaconda/network.py
> +++ b/pyanaconda/network.py
> @@ -35,7 +35,7 @@ import re
> import dbus
> import IPy
>
> -from simpleconfig import IfcfgFile
> +from simpleconfig import SimpleConfigFile
> from blivet.devices import FcoeDiskDevice, iScsiDiskDevice
> import blivet.arch
>
> @@ -209,81 +209,43 @@ def _ifcfg_files(directory):
> if name.startswith("ifcfg-"):
> if name == "ifcfg-lo":
> continue
> - rv.append(name)
> + rv.append(os.path.join(directory,name))
> return rv
>
> def logIfcfgFiles(message=""):
> ifcfglog.debug("content of files (%s):", message)
> - for name in _ifcfg_files(netscriptsDir):
> - path = os.path.join(netscriptsDir, name)
> + for path in _ifcfg_files(netscriptsDir):
> with open(path, "r") as f:
> content = f.read()
> ifcfglog.debug("%s:\n%s", path, content)
>
> -class NetworkDevice(IfcfgFile):
> -
> - def __init__(self, directory, iface):
> - IfcfgFile.__init__(self, directory, iface)
> - if iface.startswith('ctc'):
> - self.info["TYPE"] = "CTC"
> - self.wepkey = ""
> +class IfcfgFile(SimpleConfigFile):
> + def __init__(self, filename):
> + SimpleConfigFile.__init__(self, always_quote=True, filename=filename)
> self._dirty = False
>
> - def clear(self):
> - IfcfgFile.clear(self)
> - if self.iface.startswith('ctc'):
> - self.info["TYPE"] = "CTC"
> - self.wepkey = ""
> -
> - def __str__(self):
> - s = ""
> - keys = self.info.keys()
> - if blivet.arch.isS390() and ("HWADDR" in keys):
> - keys.remove("HWADDR")
> - # make sure we include autoneg in the ethtool line
> - if 'ETHTOOL_OPTS' in keys:
> - eopts = self.get('ETHTOOL_OPTS')
> - if "autoneg" not in eopts:
> - self.set(('ETHTOOL_OPTS', "autoneg off %s" % eopts))
> -
> - for key in keys:
> - if self.info[key] is not None:
> - s = s + key + '="' + self.info[key] + '"\n'
> -
> - return s
> -
> - def loadIfcfgFile(self):
> - ifcfglog.debug("loadIfcfFile %s", self.path)
> -
> - self.clear()
> - IfcfgFile.read(self)
> + def read(self):
> + self.reset()
> + ifcfglog.debug("IfcfFile.read %s", self.filename)
> + SimpleConfigFile.read(self)
> self._dirty = False
>
> - def writeIfcfgFile(self):
> - # Write out the file only if there is a key whose
> - # value has been changed since last load of ifcfg file.
> - ifcfglog.debug("writeIfcfgFile %s to %s%s", self.iface, self.path,
> - ("" if self._dirty else " not needed"))
> - if self._dirty:
> - ifcfglog.debug("old %s:\n%s", self.path, self.fileContent())
> - ifcfglog.debug("writing NetworkDevice %s:\n%s", self.iface, self.__str__())
> - IfcfgFile.write(self)
> + def write(self, filename=None):
> + if self._dirty or filename:
> + # ifcfg-rh is using inotify IN_CLOSE_WRITE event so we don't use
> + # temporary file for new configuration
> + ifcfglog.debug("IfcfgFile.write %s:\n%s", self.filename, self.__str__())
> + SimpleConfigFile.write(self, filename, use_tmp=False)
> self._dirty = False
>
> - # We can't read the file right now racing with ifcfg-rh update
> - #ifcfglog.debug("%s:\n%s" % (device.path, device.fileContent()))
> -
> def set(self, *args):
> - # If we are changing value of a key set _dirty flag
> - # informing that ifcfg file needs to be synced.
> - s = " ".join("%s=%s" % key_val for key_val in args)
> - ifcfglog.debug("NetworkDevice %s set: %s", self.iface, s)
> for (key, data) in args:
> if self.get(key) != data:
> break
> else:
> return
> - IfcfgFile.set(self, *args)
> + ifcfglog.debug("IfcfgFile.set %s: %s", self.filename, args)
> + SimpleConfigFile.set(self, *args)
> self._dirty = True
>
> def unset(self, *args):
> @@ -293,19 +255,8 @@ class NetworkDevice(IfcfgFile):
> break
> else:
> return
> - IfcfgFile.unset(self, *args)
> -
> - @property
> - def keyfilePath(self):
> - return os.path.join(self.dir, "keys-%s" % self.iface)
> -
> - def fileContent(self):
> - if not os.path.exists(self.path):
> - return ""
> - f = open(self.path, 'r')
> - content = f.read()
> - f.close()
> - return content
> + ifcfglog.debug("IfcfgFile.unset %s: %s", self.filename, args)
> + SimpleConfigFile.unset(self, *args)
>
> def dumpMissingDefaultIfcfgs():
> """
> @@ -331,7 +282,7 @@ def dumpMissingDefaultIfcfgs():
> con_uuid = nm.nm_device_setting_value(devname, "connection", "uuid")
> except nm.DeviceSettingsNotFoundError:
> continue
> - if get_ifcfg_name([("UUID", con_uuid)], root_path=""):
> + if find_ifcfg_file([("UUID", con_uuid)], root_path=""):
> continue
>
> try:
> @@ -348,6 +299,7 @@ def dumpMissingDefaultIfcfgs():
> # get a kernel cmdline string for dracut needed for access to storage host
> def dracutSetupArgs(networkStorageDevice):
>
> + import pdb; pdb.set_trace()
Maybe this could go away before pushing. :)
--
Vratislav Podzimek
Anaconda Rider | Red Hat, Inc. | Brno - Czech Republic
More information about the anaconda-patches
mailing list