ikev2: add retry logic for session initiation
[vpp.git] / extras / vpp_config / vpplib / VPPUtil.py
index 89e2d74..38c7108 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+from __future__ import print_function
+
 """VPP util library"""
 import logging
 import re
 import subprocess
-import platform
 import requests
 
 from collections import Counter
 
-# VPP_VERSION = '1707'
-# VPP_VERSION = '1710'
-VPP_VERSION = '1810'
+import distro
+
+ubuntu_pkgs = {'release': ['vpp', 'vpp-plugin-core', 'vpp-plugin-dpdk', 'vpp-api-python', 'python3-vpp-api',
+                           'vpp-dbg', 'vpp-dev'],
+               'master': ['vpp', 'vpp-plugin-core', 'vpp-plugin-dpdk', 'vpp-api-python', 'python3-vpp-api',
+                          'vpp-dbg', 'vpp-dev']}
+
+centos_pkgs = {'release': ['vpp', 'vpp-selinux-policy', 'vpp-plugins', 'vpp-api-lua',
+                           'vpp-api-python', 'vpp-debuginfo', 'vpp-devel', 'libvpp0'],
+               'master': ['vpp', 'vpp-selinux-policy', 'vpp-plugins', 'vpp-api-lua',
+                          'vpp-api-python', 'vpp-debuginfo', 'vpp-devel', 'libvpp0']}
 
 
 class VPPUtil(object):
@@ -49,13 +58,19 @@ class VPPUtil(object):
                                stderr=subprocess.PIPE)
 
         with prc.stdout:
-            for line in iter(prc.stdout.readline, b''):
+            lines = prc.stdout.readlines()
+            for line in lines:
+                if type(line) != str:
+                    line = line.decode()
                 logging.info("  {}".format(line.strip('\n')))
                 out += line
 
         with prc.stderr:
-            for line in iter(prc.stderr.readline, b''):
-                logging.warn("  {}".format(line.strip('\n')))
+            lines = prc.stderr.readlines()
+            for line in lines:
+                if type(line) != str:
+                    line = line.decode()
+                logging.warning("  {}".format(line.strip('\n')))
                 err += line
 
         ret = prc.wait()
@@ -81,48 +96,15 @@ class VPPUtil(object):
                 if ret != 0:
                     logging.debug(stderr)
 
-    def _install_vpp_pkg_ubuntu(self, node, pkg):
-        """
-        Install the VPP packages
-
-        :param node: Node dictionary
-        :param pkg: The vpp packages
-        :type node: dict
-        :type pkg: string
-        """
-
-        cmd = 'apt-get -y install {}'.format(pkg)
-        (ret, stdout, stderr) = self.exec_command(cmd)
-        if ret != 0:
-            raise RuntimeError('{} failed on node {} {} {}'.format(
-                cmd, node['host'], stdout, stderr))
-
-    def _install_vpp_pkg_centos(self, node, pkg):
-        """
-        Install the VPP packages
-
-        :param node: Node dictionary
-        :param pkg: The vpp packages
-        :type node: dict
-        :type pkg: string
-        """
-
-        cmd = 'yum -y install {}'.format(pkg)
-        (ret, stdout, stderr) = self.exec_command(cmd)
-        if ret != 0:
-            raise RuntimeError('{} failed on node {} {} {}'.format(
-                cmd, node['host'], stdout, stderr))
-
-    def _install_vpp_ubuntu(self, node, fdio_release=VPP_VERSION,
-                            ubuntu_version='xenial'):
+    def _install_vpp_ubuntu(self, node, branch, ubuntu_version='xenial'):
         """
         Install the VPP packages
 
         :param node: Node dictionary with cpuinfo.
-        :param fdio_release: VPP release number
+        :param branch: VPP branch
         :param ubuntu_version: Ubuntu Version
         :type node: dict
-        :type fdio_release: string
+        :type branch: string
         :type ubuntu_version: string
         """
 
@@ -133,24 +115,17 @@ class VPPUtil(object):
         self._autoconfig_backup_file(sfile)
 
         reps = 'deb [trusted=yes] https://packagecloud.io/fdio/'
-        # When using a stable branch
-        # reps += '{}/ubuntu {} main ./\n'.format(fdio_release, ubuntu_version)
-        # When using release
-        reps += 'release/ubuntu {} main ./\n'.format(ubuntu_version)
-        # When using master
-        # reps += 'master/ubuntu {} main ./\n'.format(ubuntu_version)
-        # When using 1807
-        # reps += '1807/ubuntu {} main ./\n'.format(ubuntu_version)
+        reps += '{}/ubuntu {} main\n'.format(branch, ubuntu_version)
 
         with open(sfile, 'w') as sfd:
             sfd.write(reps)
             sfd.close()
 
         # Add the key
-        key = requests.get('https://packagecloud.io/fdio/{}/gpgkey'.format('release'))
-        # key = requests.get('https://packagecloud.io/fdio/{}/gpgkey'.format('master'))
-        # key = requests.get('https://packagecloud.io/fdio/{}/gpgkey'.format('1807'))
-        cmd = 'echo "{}" | apt-key add -'.format(key.content)
+
+        key = requests.get(
+            'https://packagecloud.io/fdio/{}/gpgkey'.format(branch))
+        cmd = 'echo "{}" | apt-key add -'.format(key.content.decode(key.encoding))
         (ret, stdout, stderr) = self.exec_command(cmd)
         if ret != 0:
             raise RuntimeError('{} failed on node {} {}'.format(
@@ -167,26 +142,25 @@ class VPPUtil(object):
                 node['host'],
                 stderr))
 
-        self._install_vpp_pkg_ubuntu(node, 'vpp-lib')
-        self._install_vpp_pkg_ubuntu(node, 'vpp')
-        self._install_vpp_pkg_ubuntu(node, 'vpp-plugins')
-        self._install_vpp_pkg_ubuntu(node, 'vpp-api-python')
-        self._install_vpp_pkg_ubuntu(node, 'vpp-api-java')
-        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')
+        # Get the package list
+        pkgstr = ''
+        for ps in ubuntu_pkgs[branch]:
+            pkgstr += ps + ' '
 
-    def _install_vpp_centos(self, node, fdio_release=VPP_VERSION,
-                            centos_version='centos7'):
+        cmd = 'apt-get -y install {}'.format(pkgstr)
+        (ret, stdout, stderr) = self.exec_command(cmd)
+        if ret != 0:
+            raise RuntimeError('{} failed on node {} {} {}'.format(
+                cmd, node['host'], stdout, stderr))
+
+    def _install_vpp_centos(self, node, branch):
         """
         Install the VPP packages
 
         :param node: Node dictionary with cpuinfo.
-        :param fdio_release: VPP release number
-        :param centos_version: Ubuntu Version
+        :param branch: The branch name  release or master
         :type node: dict
-        :type fdio_release: string
-        :type centos_version: string
+        :type branch: string
         """
 
         # Be sure the correct system packages are installed
@@ -221,34 +195,32 @@ class VPPUtil(object):
                 node['host'],
                 stderr))
 
-        # Set the branch
-        bname = 'release'
-        # bname = '1810'
-        # bname = 'master'
-
         # Get the file contents
-        reps = '[fdio_{}]\n'.format(bname)
-        reps += 'name=fdio_{}\n'.format(bname)
-        reps += 'baseurl=https://packagecloud.io/fdio/{}/el/7/$basearch\n'.format(bname)
-        reps += 'repo_gpgcheck=1\n'
-        reps += 'gpgcheck=0\n'
-        reps += 'enabled=1\n'
-        reps += 'gpgkey=https://packagecloud.io/fdio/{}/gpgkey\n'.format(bname)
-        reps += 'sslverify=1\n'
-        reps += 'sslcacert=/etc/pki/tls/certs/ca-bundle.crt\n'
-        reps += 'metadata_expire=300\n'
-        reps += '\n'
-        reps += '[fdio_{}-source]\n'.format(bname)
-        reps += 'name=fdio_release-{}\n'.format(bname)
-        reps += 'baseurl=https://packagecloud.io/fdio/{}/el/7/SRPMS\n'.format(bname)
-        reps += 'repo_gpgcheck=1\n'
-        reps += 'gpgcheck=0\n'
-        reps += 'enabled=1\n'
-        reps += 'gpgkey=https://packagecloud.io/fdio/{}/gpgkey\n'.format(bname)
-        reps += 'sslverify =1\n'
-        reps += 'sslcacert=/etc/pki/tls/certs/ca-bundle.crt\n'
-        reps += 'metadata_expire=300\n'
 
+        reps = '\n'.join([
+            '[fdio_{}]'.format(branch),
+            'name=fdio_{}'.format(branch),
+            'baseurl=https://packagecloud.io/fdio/{}/el/7/$basearch'.format(
+                branch),
+            'repo_gpgcheck=1',
+            'gpgcheck=0',
+            'enabled=1',
+            'gpgkey=https://packagecloud.io/fdio/{}/gpgkey'.format(branch),
+            'sslverify=1',
+            'sslcacert=/etc/pki/tls/certs/ca-bundle.crt',
+            'metadata_expire=300\n',
+            '[fdio_{}-source]'.format(branch),
+            'name=fdio_release-{}'.format(branch),
+            'baseurl=https://packagecloud.io/fdio/{}/el/7/SRPMS'.format(
+                branch),
+            'repo_gpgcheck=1',
+            'gpgcheck=0',
+            'enabled=1',
+            'gpgkey=https://packagecloud.io/fdio/{}/gpgkey'.format(branch),
+            'sslverify =1',
+            'sslcacert=/etc/pki/tls/certs/ca-bundle.crt',
+            'metadata_expire=300\n'
+        ])
         with open(sfile, 'w') as sfd:
             sfd.write(reps)
             sfd.close()
@@ -262,7 +234,8 @@ class VPPUtil(object):
                 node['host'],
                 stderr))
 
-        cmd = "yum -q makecache -y --disablerepo='*' --enablerepo='fdio_{}'".format(bname)
+        cmd = "yum -q makecache -y --disablerepo='*' " \
+              "--enablerepo='fdio_{}'".format(branch)
         (ret, stdout, stderr) = self.exec_command(cmd)
         if ret != 0:
             logging.debug('{} failed on node {} {}'.format(
@@ -270,122 +243,81 @@ class VPPUtil(object):
                 node['host'],
                 stderr))
 
-        # Install the packages
-        self._install_vpp_pkg_centos(node, 'vpp-selinux-policy')
-        self._install_vpp_pkg_centos(node, 'vpp-lib')
-        self._install_vpp_pkg_centos(node, 'vpp')
-        self._install_vpp_pkg_centos(node, 'vpp-plugins')
-        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')
+        # Get the package list
+        pkgstr = ''
+        for ps in centos_pkgs[branch]:
+            pkgstr += ps + ' '
 
-    def install_vpp(self, node):
+        cmd = 'yum -y install {}'.format(pkgstr)
+        (ret, stdout, stderr) = self.exec_command(cmd)
+        if ret != 0:
+            raise RuntimeError('{} failed on node {} {} {}'.format(
+                cmd, node['host'], stdout, stderr))
+
+    def install_vpp(self, node, branch):
         """
         Install the VPP packages
 
         :param node: Node dictionary with cpuinfo.
+        :param branch: The branch name
         :type node: dict
+        :type branch: string
+
         """
         distro = self.get_linux_distro()
         logging.info("  {}".format(distro[0]))
         if distro[0] == 'Ubuntu':
             logging.info("Install Ubuntu")
-            self._install_vpp_ubuntu(node)
+            self._install_vpp_ubuntu(node, branch, ubuntu_version=distro[2])
         elif distro[0] == 'CentOS Linux':
             logging.info("Install CentOS")
-            self._install_vpp_centos(node)
+            self._install_vpp_centos(node, branch)
         else:
             logging.info("Install CentOS (default)")
-            self._install_vpp_centos(node)
+            self._install_vpp_centos(node, branch)
         return
 
-    def _uninstall_vpp_pkg_ubuntu(self, node, pkg):
+    def _uninstall_vpp_ubuntu(self, node):
         """
         Uninstall the VPP packages
 
-        :param node: Node dictionary
-        :param pkg: The vpp packages
+        :param node: Node dictionary with cpuinfo.
         :type node: dict
-        :type pkg: string
         """
-        cmd = 'dpkg --purge {}'.format(pkg)
-        (ret, stdout, stderr) = self.exec_command(cmd)
-        if ret != 0:
-            raise RuntimeError('{} failed on node {} {} {}'.format(
-                cmd, node['host'], stdout, stderr))
 
-    def _uninstall_vpp_pkg_centos(self, node, pkg):
-        """
-        Uninstall the VPP packages
+        # get the package list
+        pkgstr = ''
+        pkgs = self.get_installed_vpp_pkgs()
+        for pkg in pkgs:
+            pkgname = pkg['name']
+            pkgstr += pkgname + ' '
 
-        :param node: Node dictionary
-        :param pkg: The vpp packages
-        :type node: dict
-        :type pkg: string
-        """
-        cmd = 'yum -y remove {}'.format(pkg)
+        cmd = 'dpkg --purge {}'.format(pkgstr)
         (ret, stdout, stderr) = self.exec_command(cmd)
         if ret != 0:
             raise RuntimeError('{} failed on node {} {} {}'.format(
                 cmd, node['host'], stdout, stderr))
 
-    def _uninstall_vpp_ubuntu(self, node):
-        """
-        Uninstall the VPP packages
-
-        :param node: Node dictionary with cpuinfo.
-        :type node: dict
-        """
-        pkgs = self.get_installed_vpp_pkgs()
-
-        if len(pkgs) > 0:
-            if 'version' in pkgs[0]:
-                logging.info("Uninstall Ubuntu Packages")
-                self._uninstall_vpp_pkg_ubuntu(node, 'vpp-dbg')
-                self._uninstall_vpp_pkg_ubuntu(node, 'vpp-dev')
-                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')
-                self._uninstall_vpp_pkg_ubuntu(node, 'vpp-plugins')
-                self._uninstall_vpp_pkg_ubuntu(node, 'vpp')
-                self._uninstall_vpp_pkg_ubuntu(node, 'vpp-lib')
-            else:
-                logging.info("Uninstall locally installed Ubuntu Packages")
-                for pkg in pkgs:
-                    self._uninstall_vpp_pkg_ubuntu(node, pkg['name'])
-        else:
-            logging.error("There are no Ubuntu packages installed")
-
     def _uninstall_vpp_centos(self, node):
         """
         Uninstall the VPP packages
 
         :param node: Node dictionary with cpuinfo.
         :type node: dict
-            """
+        """
 
+        pkgstr = ''
         pkgs = self.get_installed_vpp_pkgs()
+        for pkg in pkgs:
+            pkgname = pkg['name']
+            pkgstr += pkgname + ' '
 
-        if len(pkgs) > 0:
-            if 'version' in pkgs[0]:
-                logging.info("Uninstall CentOS Packages")
-                self._install_vpp_pkg_centos(node, 'vpp-debuginfo')
-                self._uninstall_vpp_pkg_centos(node, 'vpp-devel')
-                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')
-                self._uninstall_vpp_pkg_centos(node, 'vpp-lib')
-                self._uninstall_vpp_pkg_centos(node, 'vpp-selinux-policy')
-            else:
-                logging.info("Uninstall locally installed CentOS Packages")
-                for pkg in pkgs:
-                    self._uninstall_vpp_pkg_centos(node, pkg['name'])
-        else:
-            logging.error("There are no CentOS packages installed")
+        logging.info("Uninstalling {}".format(pkgstr))
+        cmd = 'yum -y remove {}'.format(pkgstr)
+        (ret, stdout, stderr) = self.exec_command(cmd)
+        if ret != 0:
+            raise RuntimeError('{} failed on node {} {} {}'.format(
+                cmd, node['host'], stdout, stderr))
 
     def uninstall_vpp(self, node):
         """
@@ -397,7 +329,6 @@ class VPPUtil(object):
 
         # First stop VPP
         self.stop(node)
-
         distro = self.get_linux_distro()
         if distro[0] == 'Ubuntu':
             logging.info("Uninstall Ubuntu")
@@ -444,14 +375,15 @@ class VPPUtil(object):
 
         :param node: VPP node.
         :type node: dict
-        :returns: Dictionary containing a list of VMs and the interfaces that are connected to VPP
+        :returns: Dictionary containing a list of VMs and the interfaces
+                  that are connected to VPP
         :rtype: dictionary
         """
 
         vmdict = {}
 
-        print "Need to implement get vms"
-        
+        print("Need to implement get vms")
+
         return vmdict
 
     @staticmethod
@@ -471,18 +403,18 @@ class VPPUtil(object):
             return interfaces
 
         lines = stdout.split('\n')
-        if len(lines[0]) is not 0:
+        if len(lines[0]) != 0:
             if lines[0].split(' ')[0] == 'FileNotFoundError':
                 return interfaces
 
         name = ''
         for line in lines:
-            if len(line) is 0:
+            if len(line) == 0:
                 continue
 
             # If the first character is not whitespace
             # create a new interface
-            if len(re.findall(r'\s', line[0])) is 0:
+            if len(re.findall(r'\s', line[0])) == 0:
                 spl = line.split()
                 name = spl[0]
                 if name == 'local0':
@@ -513,17 +445,17 @@ class VPPUtil(object):
             return interfaces
 
         lines = stdout.split('\n')
-        if len(lines[0]) is not 0:
+        if len(lines[0]) != 0:
             if lines[0].split(' ')[0] == 'FileNotFoundError':
                 return interfaces
 
         for line in lines:
-            if len(line) is 0:
+            if len(line) == 0:
                 continue
 
             # If the first character is not whitespace
             # create a new interface
-            if len(re.findall(r'\s', line[0])) is 0:
+            if len(re.findall(r'\s', line[0])) == 0:
                 spl = line.split()
                 name = spl[0]
                 interfaces[name] = {}
@@ -543,6 +475,7 @@ class VPPUtil(object):
                 interfaces[name]['carrier'] = spl[1]
 
             # Socket
+            spl = ''
             rfall = re.findall(r'numa \d+', line)
             if rfall:
                 spl = rfall[0].split()
@@ -644,8 +577,10 @@ class VPPUtil(object):
     def get_interfaces_numa_node(node, *iface_keys):
         """Get numa node on which are located most of the interfaces.
 
-        Return numa node with highest count of interfaces provided as arguments.
-        Return 0 if the interface does not have numa_node information available.
+        Return numa node with highest count of interfaces provided as
+        arguments.
+        Return 0 if the interface does not have numa_node information
+        available.
         If all interfaces have unknown location (-1), then return 0.
         If most of interfaces have unknown location (-1), but there are
         some interfaces with known location, then return the second most
@@ -720,8 +655,8 @@ class VPPUtil(object):
         (ret, stdout, stderr) = VPPUtil.exec_command(cmd)
         if ret != 0:
             logging.debug('{} failed on node {} {} {}'.
-                               format(cmd, node['host'],
-                                      stdout, stderr))
+                          format(cmd, node['host'],
+                                 stdout, stderr))
 
     # noinspection RegExpRedundantEscape
     @staticmethod
@@ -771,13 +706,14 @@ class VPPUtil(object):
         :rtype: list
         """
 
-        distro = platform.linux_distribution()
-        if distro[0] == 'Ubuntu' or \
-                        distro[0] == 'CentOS Linux' or \
-                        distro[:7] == 'Red Hat':
-            return distro
+        dist = distro.linux_distribution()
+        if dist[0] == 'Ubuntu' or \
+                dist[0] == 'CentOS Linux' or \
+                dist[:7] == 'Red Hat':
+            return dist
         else:
-            raise RuntimeError('Linux Distribution {} is not supported'.format(distro[0]))
+            raise RuntimeError(
+                'Linux Distribution {} is not supported'.format(dist[0]))
 
     @staticmethod
     def version():
@@ -796,12 +732,12 @@ class VPPUtil(object):
             return version
 
         lines = stdout.split('\n')
-        if len(lines[0]) is not 0:
+        if len(lines[0]) != 0:
             if lines[0].split(' ')[0] == 'FileNotFoundError':
                 return version
 
         for line in lines:
-            if len(line) is 0:
+            if len(line) == 0:
                 continue
             dct = line.split(':')
             version[dct[0]] = dct[1].lstrip(' ')
@@ -829,7 +765,7 @@ class VPPUtil(object):
         bridges = []
         for line in lines:
             if line == 'no bridge-domains in use':
-                print line
+                print(line)
                 return ifaces
             if len(line) == 0:
                 continue
@@ -850,8 +786,8 @@ class VPPUtil(object):
         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] }
+                ifcidx = {'name': iface[0], 'index': line.split()[1]}
                 ifaces.append(ifcidx)
 
-        print stdout
+        print(stdout)
         return ifaces