[node-patches] Change in ovirt-node[master]: utils: Add aug.set_many and fixes

fabiand at fedoraproject.org fabiand at fedoraproject.org
Tue Dec 11 20:09:39 UTC 2012


Fabian Deutsch has uploaded a new change for review.

Change subject: utils: Add aug.set_many and fixes
......................................................................

utils: Add aug.set_many and fixes

Change-Id: I0b65aa345e3092f2a75e272d3a5747bbd323f4c3
Signed-off-by: Fabian Deutsch <fabiand at fedoraproject.org>
---
M scripts/tui/src/ovirt/node/config/network.py
M scripts/tui/src/ovirt/node/model.py
M scripts/tui/src/ovirt/node/ui/__init__.py
M scripts/tui/src/ovirt/node/ui/builder.py
M scripts/tui/src/ovirt/node/ui/tui.py
M scripts/tui/src/ovirt/node/ui/widgets.py
M scripts/tui/src/ovirt/node/utils/__init__.py
M scripts/tui/src/ovirt/node/utils/fs.py
M scripts/tui/src/ovirt/node/utils/network.py
M scripts/tui/src/ovirt/node/valid.py
10 files changed, 87 insertions(+), 40 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/27/9927/1

diff --git a/scripts/tui/src/ovirt/node/config/network.py b/scripts/tui/src/ovirt/node/config/network.py
index 110d460..c86e906 100644
--- a/scripts/tui/src/ovirt/node/config/network.py
+++ b/scripts/tui/src/ovirt/node/config/network.py
@@ -108,7 +108,9 @@
 def nameservers(new_servers=None):
     """Get or set DNS servers
 
-    >>> len(nameservers()) > 0
+    >>> import ovirt.node.utils.process as p
+    >>> _, stdout = p.pipe("egrep '^nameserver' /etc/resolv.conf | wc -l")
+    >>> len(nameservers()) == int(stdout)
     True
     """
     augpath = "/files/etc/resolv.conf/nameserver"
@@ -117,9 +119,6 @@
 
 def timeservers(new_servers=None):
     """Get or set TIME servers
-
-    >>> len(nameservers()) > 0
-    True
     """
     augpath = "/files/etc/ntp.conf/server"
     return _aug_get_or_set(augpath, new_servers)
diff --git a/scripts/tui/src/ovirt/node/model.py b/scripts/tui/src/ovirt/node/model.py
index 68229fe..75bc74b 100644
--- a/scripts/tui/src/ovirt/node/model.py
+++ b/scripts/tui/src/ovirt/node/model.py
@@ -29,14 +29,27 @@
 
 LOGGER = logging.getLogger(__name__)
 
+OVIRT_NODE_DEFAULTS_FILENAME = "/etc/defaults/ovirt"
 
-def parse_defaults():
+
+def defaults(new_dict=None, filename=OVIRT_NODE_DEFAULTS_FILENAME):
     """Reads /etc/defaults/ovirt
 
+    Args:
+        new_dict: New values to be used for setting the defaults
     Returns:
         A dict
     """
-    pass
+    aug = ovirt.node.utils.AugeasWrapper()
+    basepath = "/files/%s/" % filename.strip("/")
+    if new_dict:
+        # If values are given, update the file
+        aug.set_many(new_dict, basepath + "OVIRT_")
+
+    # Retrieve all entries of the default file and return their values
+    paths = aug.match(basepath + "*")
+    return aug.get_many(paths)
+
 
 def configure_networking(iface, bootproto, ipaddr=None, netmask=None, gw=None,
                          vlanid=None):
@@ -46,23 +59,19 @@
         - OVIRT_VLAN
         - OVIRT_IPV6
     """
-    if bootproto == "dhcp":
-        pass
-    elif bootproto == "static":
-        pass
+    config = {
+        "BOOTIF": iface,
+        "BOOTPROTO": bootproto,
+        "IP_ADDRESS": ipaddr,
+        "IP_NETMASK": netmask,
+        "IP_GATEWAY": gw,
+        "VLAN": vlanid
+    }
+    defaults(config)
+    # FIXME also remove keys with None value?
 
 
-def disable_networking():
-    """Unsets
-        - OVIRT_BOOTIF
-        - OVIRT_IP_ADDRESS, OVIRT_IP_NETMASK, OVIRT_IP_GATEWAY
-        - OVIRT_VLAN
-        - OVIRT_IPV6
-    """
-    pass
-
-
-def nameservers(servers):
+def configure_nameservers(servers):
     """Sets OVIRT_DNS
 
     1. Parse nameservers from defaults
@@ -73,7 +82,7 @@
     Args:
         servers: List of servers (str)
     """
-    ovirt_config = parse_defaults()
+    ovirt_config = defaults()
     if "OVIRT_DNS" not in ovirt_config:
         LOGGER.debug("No DNS server entry in default config")
         return
diff --git a/scripts/tui/src/ovirt/node/ui/__init__.py b/scripts/tui/src/ovirt/node/ui/__init__.py
index 7168ea2..8eeed93 100644
--- a/scripts/tui/src/ovirt/node/ui/__init__.py
+++ b/scripts/tui/src/ovirt/node/ui/__init__.py
@@ -336,7 +336,6 @@
 
         self.footer = None
 
-
     def register_plugin(self, title, plugin):
         """Register a plugin to be shown in the UI
         """
diff --git a/scripts/tui/src/ovirt/node/ui/builder.py b/scripts/tui/src/ovirt/node/ui/builder.py
index 4645f93..d2d2446 100644
--- a/scripts/tui/src/ovirt/node/ui/builder.py
+++ b/scripts/tui/src/ovirt/node/ui/builder.py
@@ -255,6 +255,7 @@
     widget = ovirt.node.ui.widgets.TableWidget(item.label, item.header,
                                                children,
                                                item.height, item.enabled())
+
     def on_change_cb(w, d=None):
         plugin._on_ui_change({path: w._key})
         item.select(w._key)
diff --git a/scripts/tui/src/ovirt/node/ui/tui.py b/scripts/tui/src/ovirt/node/ui/tui.py
index 9c0c3ea..d120d28 100644
--- a/scripts/tui/src/ovirt/node/ui/tui.py
+++ b/scripts/tui/src/ovirt/node/ui/tui.py
@@ -260,7 +260,7 @@
             if k == None:
                 continue
             colors = [e or default[idx] for idx, e in enumerate(v)]
-            rest = default[ len(colors): ]
+            rest = default[len(colors):]
             palette.append(tuple([k] + colors + rest))
         return palette
 
diff --git a/scripts/tui/src/ovirt/node/ui/widgets.py b/scripts/tui/src/ovirt/node/ui/widgets.py
index a21d6c8..3eada51 100644
--- a/scripts/tui/src/ovirt/node/ui/widgets.py
+++ b/scripts/tui/src/ovirt/node/ui/widgets.py
@@ -309,7 +309,6 @@
         return self._selectable
 
 
-
 class PasswordEntry(Entry):
     def __init__(self, label, align_vertical=False):
         super(PasswordEntry, self).__init__(label, mask="*",
diff --git a/scripts/tui/src/ovirt/node/utils/__init__.py b/scripts/tui/src/ovirt/node/utils/__init__.py
index f8c1728..e99e546 100644
--- a/scripts/tui/src/ovirt/node/utils/__init__.py
+++ b/scripts/tui/src/ovirt/node/utils/__init__.py
@@ -24,7 +24,6 @@
 
 import logging
 import hashlib
-import re
 import augeas as _augeas
 
 LOGGER = logging.getLogger(__name__)
@@ -43,13 +42,15 @@
             v = v.strip("'\"")
         return v
 
-    def set(self, p, v):
+    def set(self, p, v, do_save=True):
         self._aug.set(p, v)
-        self.save()
+        if do_save:
+            self.save()
 
-    def remove(self, p):
+    def remove(self, p, do_save=True):
         self._aug.remove(p)
-        self.save()
+        if do_save:
+            self.save()
 
     def save(self):
         return self._aug.save()
@@ -57,6 +58,44 @@
     def match(self, p):
         return self._aug.match(p)
 
+    def set_many(self, new_dict, basepath=""):
+        """Set's many augpaths at once
+
+        Args:
+            new_dict: A dict with a mapping (path, value)
+            basepath: An optional prefix for each path of new_dict
+        """
+        for key, value in new_dict.items():
+            path = basepath + key
+            self.set(path, value)
+        return self.save()
+
+    def remove_many(self, paths, basepath=None):
+        """Removes many keys at once
+
+        Args:
+            paths: The paths to be removed
+            basepath: An optional prefix for each path of new_dict
+        """
+        for key in paths:
+            path = basepath + key
+            self.remove(path, False)
+        return self.save()
+
+    def get_many(self, paths, strip_basepath=""):
+        """Get all values for all the paths
+
+        Args:
+            paths: Paths from which to fetch the values
+            strip_basepath: Prefix to be stripped from all paths
+        """
+        values = {}
+        for path in paths:
+            if strip_basepath:
+                path = path[len(basepath):]
+            values[path] = self.get(path)
+        return values
+
 
 def checksum(filename, algo="md5"):
     """Calculcate the checksum for a file.
diff --git a/scripts/tui/src/ovirt/node/utils/fs.py b/scripts/tui/src/ovirt/node/utils/fs.py
index 89822e1..7c8977b 100644
--- a/scripts/tui/src/ovirt/node/utils/fs.py
+++ b/scripts/tui/src/ovirt/node/utils/fs.py
@@ -25,8 +25,8 @@
 import logging
 import shutil
 import os
-import ovirt.node.utils
 from ovirt.node.utils import checksum, is_bind_mount
+from ovirt.node.utils.process import system
 
 LOGGER = logging.getLogger(__name__)
 
@@ -151,17 +151,17 @@
 
         else:
             # skip if file does not exist
-            logger.warn("Skipping, file '%s' does not exist" % filename)
+            LOGGER.warn("Skipping, file '%s' does not exist" % filename)
             continue
 
         # At this poitn we know that we want to persist the file.
 
         # skip if already bind-mounted
         if is_bind_mount(filename):
-            logger.warn("%s is already persisted" % filename)
+            LOGGER.warn("%s is already persisted" % filename)
         else:
             dirname = os.path.dirname(filename)
-            system("mkdir -p %s" %s persist_path(dirname))
+            system("mkdir -p %s" % persist_path(dirname))
             persist_filename = persist_path(filename)
             if system("cp -a %s %s" % (filename, persist_filename)):
                 if not system("mount -n --bind %s %s" % (persist_filename,
@@ -169,15 +169,13 @@
                     LOGGER.error("Failed to persist: " + filename)
                     persist_failed = True
                 else:
-                    logger.info("Persisted: $s" % filename)
+                    LOGGER.info("Persisted: $s" % filename)
 
         with open(persist_path("files"), "r") as files:
             if filename not in files.read().split("\n"):
                 # register in /config/files used by rc.sysinit
-            system_closefds("echo "+filename+" >> /config/files")
-            logger.info("Successfully persisted (reg): %s" % filename)
-
-
+                system("echo " + filename + " >> /config/files")
+                LOGGER.info("Successfully persisted (reg): %s" % filename)
     return not persist_failed
 
 
diff --git a/scripts/tui/src/ovirt/node/utils/network.py b/scripts/tui/src/ovirt/node/utils/network.py
index b3d62fa..7614774 100644
--- a/scripts/tui/src/ovirt/node/utils/network.py
+++ b/scripts/tui/src/ovirt/node/utils/network.py
@@ -181,7 +181,8 @@
             n.startswith("tun") or \
             n.startswith("wlan") or \
             (filter_vlans and ("." in n)))
-    valid_props = lambda i, p: (filter_bridges and (p["type"] != "bridge"))
+# FIXME!!!
+#    valid_props = lambda i, p: (filter_bridges and (p["type"] != "bridge"))
 
     relevant_ifaces = [iface for iface in all_ifaces() if valid_name(iface)]
 #    relevant_ifaces = {iface: iface_information(iface) for iface \
diff --git a/scripts/tui/src/ovirt/node/valid.py b/scripts/tui/src/ovirt/node/valid.py
index 535e41a..2706cce 100644
--- a/scripts/tui/src/ovirt/node/valid.py
+++ b/scripts/tui/src/ovirt/node/valid.py
@@ -98,6 +98,8 @@
     def validate(self, value):
         if type(self.pattern) in [str, unicode]:
             self.pattern = (self.pattern, )
+        if type(value) in [bool, int]:
+            value = str(value)
         return re.compile(*self.pattern).search(value) != None
 
 


--
To view, visit http://gerrit.ovirt.org/9927
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0b65aa345e3092f2a75e272d3a5747bbd323f4c3
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-node
Gerrit-Branch: master
Gerrit-Owner: Fabian Deutsch <fabiand at fedoraproject.org>



More information about the node-patches mailing list