[node-patches] Change in ovirt-node[master]: tui: Add hostname configuration

fabiand at fedoraproject.org fabiand at fedoraproject.org
Fri Dec 14 08:33:35 UTC 2012


Fabian Deutsch has uploaded a new change for review.

Change subject: tui: Add hostname configuration
......................................................................

tui: Add hostname configuration

Previously it was not possible to set the hostname, this is now
possible.
Additional changes are:
- Traceback is shown in the log sin the case that an exception ovvurred
  during a transaction in the transaction dialog
- Better TUI colors for low-color terminals
- Hint what to do when there are unsaved fields in a page

Change-Id: I450075f33480d8cd9ce86f488fa6c48a566898a8
Signed-off-by: Fabian Deutsch <fabiand at fedoraproject.org>
---
M scripts/tui/src/ovirt/node/config/defaults.py
M scripts/tui/src/ovirt/node/setup/network_page.py
M scripts/tui/src/ovirt/node/ui/__init__.py
M scripts/tui/src/ovirt/node/ui/tui.py
M scripts/tui/src/ovirt/node/utils/system.py
5 files changed, 101 insertions(+), 26 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/55/10055/1

diff --git a/scripts/tui/src/ovirt/node/config/defaults.py b/scripts/tui/src/ovirt/node/config/defaults.py
index d22bb65..f265036 100644
--- a/scripts/tui/src/ovirt/node/config/defaults.py
+++ b/scripts/tui/src/ovirt/node/config/defaults.py
@@ -329,6 +329,54 @@
         return tx
 
 
+class Hostname(NodeConfigFileSection):
+    """Configure hostname
+    >>> fn = "/tmp/cfg_dummy"
+    >>> cfgfile = ConfigFile(fn, SimpleProvider)
+    >>> hostname = "host.example.com"
+    >>> n = Hostname(cfgfile)
+    >>> n.update(hostname)
+    >>> n.retrieve()
+    {'hostname': 'host.example.com'}
+    """
+    keys = ("OVIRT_HOSTNAME",)
+
+    @NodeConfigFileSection.map_and_update_defaults_decorator
+    def update(self, hostname):
+        (valid.Empty() | valid.FQDNOrIPAddress())(hostname)
+
+    def transaction(self):
+        cfg = self.retrieve()
+        hostname = cfg["hostname"]
+
+        class UpdateHostname(utils.Transaction.Element):
+            title = "Setting hostname"
+
+            def __init__(self, hostname):
+                self.hostname = hostname
+
+            def commit(self):
+                from ovirtnode import network as onet, ovirtfunctions
+                network = onet.Network()
+                system = utils.system.Hostname()
+
+                if self.hostname:
+                    network.remove_non_localhost()
+                    network.add_localhost_alias(self.hostname)
+                else:
+                    network.remove_non_localhost()
+                    self.hostname = "localhost.localdomain"
+
+                system.hostname(self.hostname)
+
+                ovirtfunctions.ovirt_store_config("/etc/sysconfig/network")
+                ovirtfunctions.ovirt_store_config("/etc/hosts")
+
+        tx = utils.Transaction("Configuring hostname")
+        tx.append(UpdateHostname(hostname))
+        return tx
+
+
 class Nameservers(NodeConfigFileSection):
     """Configure nameservers
     >>> fn = "/tmp/cfg_dummy"
@@ -458,7 +506,7 @@
     def update(self, servers):
         assert type(servers) is list
         servers = filter(lambda i: i.strip() not in ["", None], servers)
-        map(valid.IPv4Address(), servers)
+        map(valid.FQDNOrIPAddress(), servers)
         return {
                 "OVIRT_NTP": ",".join(servers) or None
                 }
@@ -481,6 +529,7 @@
                 import ovirtnode.network as onet
                 net = onet.Network()
                 net.configure_ntp()
+                net.save_ntp_configuration()
 
         tx = utils.Transaction("Configuring timeservers")
         tx.append(ConfigureTimeservers())
diff --git a/scripts/tui/src/ovirt/node/setup/network_page.py b/scripts/tui/src/ovirt/node/setup/network_page.py
index 240ccdf..fccb2d7 100644
--- a/scripts/tui/src/ovirt/node/setup/network_page.py
+++ b/scripts/tui/src/ovirt/node/setup/network_page.py
@@ -31,13 +31,6 @@
     """This is the network page
     """
 
-    _model = {
-        "hostname": "localhost.example.com",
-        "dns[0]": "192.168.122.1",
-        "dns[1]": "",
-        "ntp[0]": "fedora.pool.ntp.org",
-        "ntp[1]": "",
-    }
     _widgets = None
 
     def __init__(self, app):
@@ -58,29 +51,43 @@
     def rank(self):
         return 10
 
+    _model_extra = {}
     def model(self):
+        model = {
+            "hostname": "",
+            "dns[0]": "",
+            "dns[1]": "",
+            "ntp[0]": "",
+            "ntp[1]": "",
+        }
+
+        model["hostname"] = defaults.Hostname().retrieve()["hostname"] or \
+                            utils.system.Hostname().hostname()
+
         # Pull name-/timeservers from config files (not defaults)
         nameservers = defaults.Nameservers().retrieve()["servers"]
         if nameservers:
             for idx, nameserver in enumerate(nameservers):
-                self._model["dns[%d]" % idx] = nameserver
+                model["dns[%d]" % idx] = nameserver
 
         timeservers = defaults.Timeservers().retrieve()["servers"]
         if timeservers:
             for idx, timeserver in enumerate(timeservers):
-                self._model["ntp[%d]" % idx] = timeserver
+                model["ntp[%d]" % idx] = timeserver
 
-        return self._model
+        model.update(self._model_extra)
+
+        return model
 
     def validators(self):
         ip_or_empty = valid.IPAddress() | valid.Empty()
         fqdn_ip_or_empty = valid.FQDNOrIPAddress() | valid.Empty()
 
         return {
-                "hostname": valid.FQDNOrIPAddress(),
-                "dns[0]": valid.IPAddress(),
+                "hostname": fqdn_ip_or_empty,
+                "dns[0]": ip_or_empty,
                 "dns[1]": ip_or_empty,
-                "ntp[0]": valid.FQDNOrIPAddress(),
+                "ntp[0]": fqdn_ip_or_empty,
                 "ntp[1]": fqdn_ip_or_empty,
 
                 "dialog.nic.ipv4.address": valid.IPv4Address() | valid.Empty(),
@@ -132,7 +139,6 @@
                 justify(nic["hwaddr"], 17)
                 ])
             node_nics.append((name, description))
-        self._model["nics"] = first_nic
         return node_nics
 
     def _build_dialog(self, path, txt, widgets):
@@ -165,7 +171,7 @@
                                                (routes.default(),) +
                                                (nic.vlanid(),))
 
-        self._model.update({
+        self._model_extra.update({
             "dialog.nic.iface": live["name"],
             "dialog.nic.driver": live["driver"],
             "dialog.nic.protocol": live["bootproto"] or "N/A",
@@ -181,7 +187,7 @@
             "dialog.nic.vlanid": vlanid,
         })
 
-        self.logger.debug("model: %s" % self._model)
+        self.logger.debug("model: %s" % self.model())
 
         padd = lambda l: l.ljust(14)
         dialog = self._build_dialog("dialog.nic", "NIC Details: %s" % iface, [
@@ -255,7 +261,7 @@
     def on_merge(self, effective_changes):
         self.logger.info("Saving network stuff")
         changes = self.pending_changes(False)
-        effective_model = dict(self._model)
+        effective_model = dict(self.model())
         effective_model.update(effective_changes)
         self.logger.info("Effective model %s" % effective_model)
         self.logger.info("Effective changes %s" % effective_changes)
@@ -302,6 +308,14 @@
             model.update(timeservers)
             txs += model.transaction()
 
+        hostname_keys = ["hostname"]
+        if e_changes_h.any_key_in_change(hostname_keys):
+            value = e_model_h.get_key_values(hostname_keys)
+            self.logger.info("Setting new hostname: %s" % value)
+            model = defaults.Hostname()
+            model.update(*value)
+            txs += model.transaction()
+
         # For the NIC details dialog:
         if e_changes_h.any_key_in_change(self._nic_details_group):
             # If any networking related key was changed, reconfigure networking
@@ -319,7 +333,7 @@
     def _configure_nic(self, bootproto, ipaddr, netmask, gateway, vlanid):
         vlanid = vlanid or None
         model = defaults.Network()
-        iface = self._model["dialog.nic.iface"]
+        iface = self._model_extra["dialog.nic.iface"]
         if bootproto == "none":
             self.logger.debug("Configuring no networking")
             name = iface + "-DISABLED"
diff --git a/scripts/tui/src/ovirt/node/ui/__init__.py b/scripts/tui/src/ovirt/node/ui/__init__.py
index edc40e7..3f915fb 100644
--- a/scripts/tui/src/ovirt/node/ui/__init__.py
+++ b/scripts/tui/src/ovirt/node/ui/__init__.py
@@ -19,6 +19,7 @@
 # MA  02110-1301, USA.  A copy of the GNU General Public License is
 # also available at http://www.gnu.org/copyleft/gpl.html.
 from ovirt.node import base
+import traceback
 
 """
 This contains abstract UI Elements
@@ -388,4 +389,5 @@
             self.add_update(e.message)
             self.logger.warning("'%s' on transaction '%s': %s - %s" %
                                 (type(e), self.transaction, e, e.message))
+            self.logger.debug(str(traceback.format_exc()))
         self._close_button.enabled(True)
diff --git a/scripts/tui/src/ovirt/node/ui/tui.py b/scripts/tui/src/ovirt/node/ui/tui.py
index b86a1d0..909f6d4 100644
--- a/scripts/tui/src/ovirt/node/ui/tui.py
+++ b/scripts/tui/src/ovirt/node/ui/tui.py
@@ -55,9 +55,9 @@
     footer = u"Press ctrl+c to quit"
 
     element_styles = {
-        "text": "dark gray",
-        "label": "black",
-        "disabled": "white",
+        "text": "black",
+        "label": "dark gray",
+        "disabled": "dark gray",
         "background": "light gray",
         "invalid": "dark red",
     }
@@ -213,10 +213,10 @@
                                                                      widget))
                         msg += "- %s\n" % (field.strip(":"))
                 if msg:
-                    self.__display_as_dialog(urwid.Filler(urwid.Text(
-                                "The following fields have changed:\n%s" %
-                                msg)),
-                                "Pending changes")
+                    txt = "The following fields have changed:\n%s" % msg
+                    txt += "\n\nPlease save or reset the page."
+                    self.__display_as_dialog(urwid.Filler(ui.widgets.Label(
+                                txt)), "Pending changes")
                     has_outstanding_changes = True
         return has_outstanding_changes
 
diff --git a/scripts/tui/src/ovirt/node/utils/system.py b/scripts/tui/src/ovirt/node/utils/system.py
index 89cdd9e..91e504b 100644
--- a/scripts/tui/src/ovirt/node/utils/system.py
+++ b/scripts/tui/src/ovirt/node/utils/system.py
@@ -54,3 +54,13 @@
         self.PRODUCT_SHORT = augg("PRODUCT_SHORT") or "oVirt"
         self.VERSION = augg("VERSION")
         self.RELEASE = augg("RELEASE")
+
+
+class Hostname(base.Base):
+    def hostname(self, hostname=None):
+        aug = utils.AugeasWrapper()
+        augpath = "/files/etc/sysconfig/network/HOSTNAME"
+        if hostname:
+            aug.set(augpath, hostname)
+            utils.process.system("hostname %s" % hostname)
+        return aug.get(augpath)
\ No newline at end of file


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I450075f33480d8cd9ce86f488fa6c48a566898a8
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