Update the vpp config utility
[vpp.git] / extras / vpp_config / vpplib / VPPUtil.py
index f042e80..ade905e 100644 (file)
@@ -20,7 +20,8 @@ import platform
 from collections import Counter
 
 # VPP_VERSION = '1707'
-VPP_VERSION = '1710'
+# VPP_VERSION = '1710'
+VPP_VERSION = '1804'
 
 
 class VPPUtil(object):
@@ -140,8 +141,12 @@ class VPPUtil(object):
                 stderr))
 
         reps = 'deb [trusted=yes] https://nexus.fd.io/content/'
-        reps += 'repositories/fd.io.stable.{}.ubuntu.{}.main/ ./\n' \
-            .format(fdio_release, ubuntu_version)
+        # When using a stable branch
+        # reps += 'repositories/fd.io.stable.{}.ubuntu.{}.main/ ./\n'.format(fdio_release, ubuntu_version)
+        # When using release
+        reps += 'repositories/fd.io.ubuntu.{}.main/ ./\n'.format(ubuntu_version)
+        # When using master
+        # reps += 'repositories/fd.io.master.ubuntu.{}.main/ ./\n'.format(ubuntu_version)
 
         cmd = 'echo "{0}" | sudo tee {1}'.format(reps, sfile)
         (ret, stdout, stderr) = self.exec_command(cmd)
@@ -170,6 +175,9 @@ class VPPUtil(object):
         self._install_vpp_pkg_ubuntu(node, 'vpp-api-lua')
         self._install_vpp_pkg_ubuntu(node, 'vpp-dev')
         self._install_vpp_pkg_ubuntu(node, 'vpp-dbg')
+        self._install_vpp_pkg_ubuntu(node, 'vpp-nsh-plugin')
+        self._install_vpp_pkg_ubuntu(node, 'vpp-nsh-plugin-dbg')
+        self._install_vpp_pkg_ubuntu(node, 'vpp-nsh-plugin-dev')
 
     def _install_vpp_centos(self, node, fdio_release=VPP_VERSION,
                             centos_version='centos7'):
@@ -199,10 +207,19 @@ class VPPUtil(object):
                 node['host'],
                 stderr))
 
-        reps = '[fdio-stable-{}]\n'.format(fdio_release)
-        reps += 'name=fd.io stable/{} branch latest merge\n'.format(fdio_release)
-        reps += 'baseurl=https://nexus.fd.io/content/repositories/fd.io.stable.{}.{}/\n'.\
-            format(fdio_release, centos_version)
+        # Latest
+        # reps = '[fdio-master]\n'
+        # reps += 'name=fd.io master branch latest merge\n'
+        # reps += 'baseurl=https://nexus.fd.io/content/repositories/fd.io.master.{}/\n'.format(centos_version)
+        # reps = '[fdio-stable-{}]\n'.format(fdio_release)
+        # reps += 'name=fd.io stable/{} branch latest merge\n'.format(fdio_release)
+        # When using stable
+        # reps += 'baseurl=https://nexus.fd.io/content/repositories/fd.io.stable.{}.{}/\n'.\
+        #     format(fdio_release, centos_version)
+        # When using release
+        reps = '[fdio-release]\n'
+        reps += 'name=fd.io release branch latest merge\n'
+        reps += 'baseurl=https://nexus.fd.io/content/repositories/fd.io.{}/\n'.format(centos_version)
         reps += 'enabled=1\n'
         reps += 'gpgcheck=0'
 
@@ -219,12 +236,14 @@ class VPPUtil(object):
         self._install_vpp_pkg_centos(node, 'vpp-lib')
         self._install_vpp_pkg_centos(node, 'vpp')
         self._install_vpp_pkg_centos(node, 'vpp-plugins')
-        # jadfix Check with Ole
-        # self._install_vpp_pkg_centos(node, 'vpp-dpdk-devel')
         self._install_vpp_pkg_centos(node, 'vpp-api-python')
         self._install_vpp_pkg_centos(node, 'vpp-api-java')
         self._install_vpp_pkg_centos(node, 'vpp-api-lua')
         self._install_vpp_pkg_centos(node, 'vpp-devel')
+        self._install_vpp_pkg_centos(node, 'vpp-debuginfo')
+        self._install_vpp_pkg_centos(node, 'vpp-nsh-plugin')
+        self._install_vpp_pkg_centos(node, 'vpp-nsh-plugin-devel')
+        self._install_vpp_pkg_centos(node, 'vpp-selinux-policy')
 
     def install_vpp(self, node):
         """
@@ -234,13 +253,17 @@ class VPPUtil(object):
         :type node: dict
         """
         distro = self.get_linux_distro()
+        logging.info("  {}".format(distro[0]))
         if distro[0] == 'Ubuntu':
+            logging.info("Install Ubuntu")
             self._install_vpp_ubuntu(node)
         elif distro[0] == 'CentOS Linux':
             logging.info("Install CentOS")
             self._install_vpp_centos(node)
         else:
-            return
+            logging.info("Install CentOS (default)")
+            self._install_vpp_centos(node)
+        return
 
     def _uninstall_vpp_pkg_ubuntu(self, node, pkg):
         """
@@ -284,6 +307,9 @@ class VPPUtil(object):
         if len(pkgs) > 0:
             if 'version' in pkgs[0]:
                 logging.info("Uninstall Ubuntu Packages")
+                self._uninstall_vpp_pkg_ubuntu(node, 'vpp-nsh-plugin-dev')
+                self._uninstall_vpp_pkg_ubuntu(node, 'vpp-nsh-plugin-dbg')
+                self._uninstall_vpp_pkg_ubuntu(node, 'vpp-nsh-plugin')
                 self._uninstall_vpp_pkg_ubuntu(node, 'vpp-api-python')
                 self._uninstall_vpp_pkg_ubuntu(node, 'vpp-api-java')
                 self._uninstall_vpp_pkg_ubuntu(node, 'vpp-api-lua')
@@ -314,11 +340,14 @@ class VPPUtil(object):
         if len(pkgs) > 0:
             if 'version' in pkgs[0]:
                 logging.info("Uninstall CentOS Packages")
+                self._uninstall_vpp_pkg_centos(node, 'vpp-selinux-policy')
+                self._uninstall_vpp_pkg_centos(node, 'vpp-nsh-plugin-devel')
+                self._uninstall_vpp_pkg_centos(node, 'vpp-nsh-plugin')
+                self._uninstall_vpp_pkg_centos(node, 'vpp-debuginfo')
                 self._uninstall_vpp_pkg_centos(node, 'vpp-api-python')
                 self._uninstall_vpp_pkg_centos(node, 'vpp-api-java')
                 self._uninstall_vpp_pkg_centos(node, 'vpp-api-lua')
                 self._uninstall_vpp_pkg_centos(node, 'vpp-plugins')
-                self._uninstall_vpp_pkg_centos(node, 'vpp-dpdk-devel')
                 self._uninstall_vpp_pkg_centos(node, 'vpp-devel')
                 self._uninstall_vpp_pkg_centos(node, 'vpp')
                 self._uninstall_vpp_pkg_centos(node, 'vpp-lib')
@@ -336,13 +365,20 @@ class VPPUtil(object):
         :param node: Node dictionary with cpuinfo.
         :type node: dict
         """
+
+        # First stop VPP
+        self.stop(node)
+
         distro = self.get_linux_distro()
         if distro[0] == 'Ubuntu':
+            logging.info("Uninstall Ubuntu")
             self._uninstall_vpp_ubuntu(node)
         elif distro[0] == 'CentOS Linux':
             logging.info("Uninstall CentOS")
             self._uninstall_vpp_centos(node)
         else:
+            logging.info("Uninstall CentOS (Default)")
+            self._uninstall_vpp_centos(node)
             return
 
     def show_vpp_settings(self, *additional_cmds):
@@ -372,6 +408,23 @@ class VPPUtil(object):
                 for _, value in def_setting_tb_displayed.items():
                     self.exec_command('vppctl sh {}'.format(value))
 
+    @staticmethod
+    def get_vms(node):
+        """
+        Get a list of VMs that are connected to VPP interfaces
+
+        :param node: VPP node.
+        :type node: dict
+        :returns: Dictionary containing a list of VMs and the interfaces that are connected to VPP
+        :rtype: dictionary
+        """
+
+        vmdict = {}
+
+        print "Need to implement get vms"
+        
+        return vmdict
+
     @staticmethod
     def get_int_ip(node):
         """
@@ -393,6 +446,7 @@ class VPPUtil(object):
             if lines[0].split(' ')[0] == 'FileNotFoundError':
                 return interfaces
 
+        name = ''
         for line in lines:
             if len(line) is 0:
                 continue
@@ -546,6 +600,7 @@ class VPPUtil(object):
         elif distro[0] == 'CentOS Linux':
             pkgs = self._get_installed_vpp_pkgs_centos()
         else:
+            pkgs = self._get_installed_vpp_pkgs_centos()
             return []
 
         return pkgs
@@ -582,6 +637,23 @@ class VPPUtil(object):
 
         return 0
 
+    @staticmethod
+    def restart(node):
+        """
+
+        Starts vpp for a given node
+
+        :param node: VPP node.
+        :type node: dict
+        """
+
+        cmd = 'service vpp restart'
+        (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
+        if ret != 0:
+            raise RuntimeError('{} failed on node {} {} {}'.
+                               format(cmd, node['host'],
+                                      stdout, stderr))
+
     @staticmethod
     def start(node):
         """
@@ -616,6 +688,7 @@ class VPPUtil(object):
                                format(cmd, node['host'],
                                       stdout, stderr))
 
+    # noinspection RegExpRedundantEscape
     @staticmethod
     def status(node):
         """
@@ -666,7 +739,7 @@ class VPPUtil(object):
         distro = platform.linux_distribution()
         if distro[0] == 'Ubuntu' or \
                         distro[0] == 'CentOS Linux' or \
-                        distro[:26] == 'Linux Distribution Red Hat':
+                        distro[:7] == 'Red Hat':
             return distro
         else:
             raise RuntimeError('Linux Distribution {} is not supported'.format(distro[0]))
@@ -699,3 +772,51 @@ class VPPUtil(object):
             version[dct[0]] = dct[1].lstrip(' ')
 
         return version
+
+    @staticmethod
+    def show_bridge(node):
+        """
+        Shows the current bridge configuration
+
+        :param node: VPP node.
+        :type node: dict
+        :returns: A list of interfaces
+        """
+
+        ifaces = []
+        cmd = 'vppctl show bridge'
+        (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
+        if ret != 0:
+            raise RuntimeError('{} failed on node {} {} {}'.
+                               format(cmd, node['host'],
+                                      stdout, stderr))
+        lines = stdout.split('\r\n')
+        bridges = []
+        for line in lines:
+            if line == 'no bridge-domains in use':
+                print line
+                return ifaces
+            if len(line) == 0:
+                continue
+
+            lspl = line.lstrip(' ').split()
+            if lspl[0] != 'BD-ID':
+                bridges.append(lspl[0])
+
+        for bridge in bridges:
+            cmd = 'vppctl show bridge {} detail'.format(bridge)
+            (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
+            if ret != 0:
+                raise RuntimeError('{} failed on node {} {} {}'.
+                                   format(cmd, node['host'],
+                                          stdout, stderr))
+
+        lines = stdout.split('\r\n')
+        for line in lines:
+            iface = re.findall(r'[a-zA-z]+\d+/\d+/\d+', line)
+            if len(iface):
+                ifcidx ={'name': iface[0], 'index': line.split()[1] }
+                ifaces.append(ifcidx)
+
+        print stdout
+        return ifaces