Hello Ilia Meerovich,

I'd like you to do a code review.  Please visit

    https://gerrit.ovirt.org/55750

to review the following change.

Change subject: Adding dns vdsm hook
......................................................................

Adding dns vdsm hook

dns vdsm hook allows to set dns via custom properties

Change-Id: I1f18f64cf566e15a7b3317b20d5cfad24e75f95e
Signed-off-by: Ilia Meerovich <il...@redhat.com>
---
M configure.ac
A debian/vdsm-hook-dns.install
M vdsm.spec.in
M vdsm_hooks/Makefile.am
A vdsm_hooks/dns/Makefile.am
A vdsm_hooks/dns/README
A vdsm_hooks/dns/dns.py
A vdsm_hooks/dns/sudoers.vdsm_hook_dns
8 files changed, 171 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/50/55750/4

diff --git a/configure.ac b/configure.ac
index 93a0781..a4f6219 100644
--- a/configure.ac
+++ b/configure.ac
@@ -426,6 +426,7 @@
        vdsm_hooks/ethtool_options/Makefile
        vdsm_hooks/extnet/Makefile
        vdsm_hooks/extra_ipv4_addrs/Makefile
+       vdsm_hooks/dns/Makefile
        vdsm_hooks/fakesriov/Makefile
        vdsm_hooks/fakevmstats/Makefile
        vdsm_hooks/faqemu/Makefile
diff --git a/debian/vdsm-hook-dns.install b/debian/vdsm-hook-dns.install
new file mode 100644
index 0000000..2f68e7e
--- /dev/null
+++ b/debian/vdsm-hook-dns.install
@@ -0,0 +1 @@
+usr/libexec/vdsm/hooks/after_network_setup/40_dns
diff --git a/vdsm.spec.in b/vdsm.spec.in
index 76caca5..0801336 100644
--- a/vdsm.spec.in
+++ b/vdsm.spec.in
@@ -413,6 +413,12 @@
 This hook allows the user to set extra ipv4
 addresses for vdsm networks.
 
+%package hook-dns
+Summary:        Set DNS addresses for vdsm networks.
+
+%description hook-dns
+This hook allows the user to set dns addresses for vdsm networks.
+
 %if 0%{?with_vhostmd}
 %package hook-vhostmd
 Summary:        VDSM hook set for interaction with vhostmd
@@ -1415,6 +1421,11 @@
 %defattr(-, root, root, -)
 %{_libexecdir}/%{vdsm_name}/hooks/after_network_setup/40_extra_ipv4_addrs
 
+%files hook-dns
+%defattr(-, root, root, -)
+%{_sysconfdir}/sudoers.d/50_vdsm_hook_dns
+%{_libexecdir}/%{vdsm_name}/hooks/after_network_setup/40_dns
+
 %files hook-diskunmap
 %defattr(-, root, root, -)
 %{_libexecdir}/%{vdsm_name}/hooks/before_vm_start/50_diskunmap
diff --git a/vdsm_hooks/Makefile.am b/vdsm_hooks/Makefile.am
index 900e841..cc23de3 100644
--- a/vdsm_hooks/Makefile.am
+++ b/vdsm_hooks/Makefile.am
@@ -60,6 +60,7 @@
        spiceoptions \
        vmdisk \
        vmfex \
+       dns \
        $(NULL)
 endif
 
diff --git a/vdsm_hooks/dns/Makefile.am b/vdsm_hooks/dns/Makefile.am
new file mode 100644
index 0000000..524d404
--- /dev/null
+++ b/vdsm_hooks/dns/Makefile.am
@@ -0,0 +1,42 @@
+#
+# Copyright 2011 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  
USA
+#
+# Refer to the README and COPYING files for full details of the license
+#
+
+EXTRA_DIST = \
+       dns.py \
+       sudoers.vdsm_hook_dns
+
+install-data-hook:
+       chmod 440 $(DESTDIR)$(sysconfdir)/sudoers.d/50_vdsm_hook_dns
+
+install-data-local: install-data-sudoers
+       $(MKDIR_P) $(DESTDIR)$(vdsmhooksdir)/after_network_setup
+       $(INSTALL_SCRIPT) $(srcdir)/dns.py \
+               $(DESTDIR)$(vdsmhooksdir)/after_network_setup/40_dns
+
+uninstall-local: uninstall-data-sudoers
+       $(RM) $(DESTDIR)$(vdsmhooksdir)/after_network_setup/40_dns
+
+install-data-sudoers:
+       $(MKDIR_P) $(DESTDIR)$(sysconfdir)/sudoers.d
+       $(INSTALL_DATA) $(srcdir)/sudoers.vdsm_hook_dns \
+               $(DESTDIR)$(sysconfdir)/sudoers.d/50_vdsm_hook_dns
+
+uninstall-data-sudoers:
+       $(RM) $(DESTDIR)$(sysconfdir)/sudoers.d/50_vdsm_hook_dns
diff --git a/vdsm_hooks/dns/README b/vdsm_hooks/dns/README
new file mode 100644
index 0000000..54e3c1f
--- /dev/null
+++ b/vdsm_hooks/dns/README
@@ -0,0 +1,17 @@
+set_dns vdsm hook
+=================================
+This hook allows the user to set dns addresses for vdsm networks.
+
+Requirements:
+* oVirt-3.5
+
+Usage:
+Configure the engine to enable setting the ipv4_addrs custom property by doing:
+
+    $ engine-config -s 
"UserDefinedNetworkCustomProperties=DNS=((?:[0-9]{1,3}\.){3}[0-9]{1,3})" \
+      --cver='3.5'
+
+In the oVirt UI edit custom network properties and, for the key 'DNS'
+set DNS addresses in the following format:
+
+    'addr addr1 ... addrN'
diff --git a/vdsm_hooks/dns/dns.py b/vdsm_hooks/dns/dns.py
new file mode 100644
index 0000000..2038ea6
--- /dev/null
+++ b/vdsm_hooks/dns/dns.py
@@ -0,0 +1,97 @@
+#!/usr/bin/env python
+# Copyright 2014 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+#
+# Refer to the README and COPYING files for full details of the license
+#
+import sys
+import hooking
+import traceback
+
+from vdsm import netinfo
+from vdsm import utils
+
+_NMCLI_BINARY = '/usr/bin/nmcli'
+_RESOLV_CONF = '/etc/resolv.conf'
+
+
+def test():
+    dns_list = '1.1.1.1 2.2.2.2 3.3.3.3'
+    commands = ['nmcli connection modify foo_con +ipv4.dns "%s"' % dns_list,
+                'nmcli connection modify foo_con ipv4.dns "%s"' % dns_list,
+                ]
+    print(commands)
+    assert [_generate_nmcli_command(dns_list, 'foo_con', keep_dns_config)
+            for keep_dns_config in [True, False]] == commands
+
+
+def main():
+    """Read dns from the network 'custom' properties and apply them
+    to the network's top device"""
+    setup_nets_config = hooking.read_json()
+    for network, attrs in setup_nets_config['request']['networks'].items():
+        if 'custom' in attrs:
+            _set_dns(network, attrs)
+
+
+def _set_dns(network, attrs):
+    """adding manual DNS entries"""
+    dns_list = attrs['custom'].get('DNS')
+    if dns_list:
+        connection = _connection(network, attrs)
+        nmcli_cmd = _generate_nmcli_command(dns_list, connection)
+        rc, out, err = hooking.execCmd(nmcli_cmd, sudo=True)
+        # pushing to /etc/resolve.conf
+        if rc:
+            sys.stderr.write(
+                'set_dns: error executing command "%s" output: %s error: %s' %
+                (nmcli_cmd, out, err))
+            sys.stderr.write('Trying to modify directly /etc/resolv.conf')
+            data = '\n'.join(
+                ["nameserver %s" % entry for entry in dns_list.split(' ')])
+            data += '\n'
+            with open(_RESOLV_CONF, "a") as f:
+                f.write(data)
+
+
+def _generate_nmcli_command(dns_list, connection,
+                            keep_existing_dns_config=True):
+    """Generating relevant nmcli command"""
+    # TODO: to add ipv6 stuff if needed
+    cmd = [_NMCLI_BINARY, "connection", "modify", connection,
+           "+ipv4.dns" if keep_existing_dns_config else "ipv4.dns",
+           '"%s"' % dns_list]
+    return cmd
+
+
+def _connection(network, attrs):
+    if utils.tobool(attrs.get('bridged')):
+        return network
+    # bridgeless
+    nics, vlan, _, bonding = netinfo.cache.CachingNetInfo(
+        ).getNicsVlanAndBondingForNetwork(network)
+    return vlan or bonding or nics[0]
+
+
+if __name__ == '__main__':
+    try:
+        if '--test' in sys.argv:
+            test()
+        else:
+            main()
+    except:
+        hooking.exit_hook('set dns hook: [unexpected error]: %s\n' %
+                          traceback.format_exc())
diff --git a/vdsm_hooks/dns/sudoers.vdsm_hook_dns 
b/vdsm_hooks/dns/sudoers.vdsm_hook_dns
new file mode 100644
index 0000000..6f1a7a2
--- /dev/null
+++ b/vdsm_hooks/dns/sudoers.vdsm_hook_dns
@@ -0,0 +1 @@
+vdsm  ALL=(ALL) NOPASSWD: /usr/bin/nmcli *


-- 
To view, visit https://gerrit.ovirt.org/55750
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1f18f64cf566e15a7b3317b20d5cfad24e75f95e
Gerrit-PatchSet: 4
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Ilia Meerovich <il...@redhat.com>
Gerrit-Reviewer: Dan Kenigsberg <dan...@redhat.com>
Gerrit-Reviewer: Ilia Meerovich <il...@redhat.com>
Gerrit-Reviewer: Jenkins CI RO
Gerrit-Reviewer: gerrit-hooks <automat...@ovirt.org>
_______________________________________________
vdsm-patches mailing list -- vdsm-patches@lists.fedorahosted.org
To unsubscribe send an email to vdsm-patches-le...@lists.fedorahosted.org

Reply via email to