[Kimchi-devel] [PATCH 3/7] Host's software update: Update backend.

Aline Manera alinefm at linux.vnet.ibm.com
Thu Feb 13 14:07:29 UTC 2014


From: Paulo Vital <pvital at linux.vnet.ibm.com>

Update model and mockmodel to support backend opertions.
Add new file implementing backend operations, with four new classes:

1) SoftwareUpdate (object): Class to represent and operate with OS software
update system in Kimchi's perspective. It's agnostic to host's package management
system, and can execute all operations necessary: get all packages to update,
get information about one package and execute the update. This class will load
in runtime the necessary classes to work with the host's package management:
YumUpdate for YUM systems based, AptUpdate for APT systems based and ZypperUpdate
for Zypper systems based.

2) YumUpdate (object): Class to represent and operate with YUM. Loaded only on
those systems that supports YUM, it's responsible to connect and collect
information of the packages to be updated. Also it's responsible to execute the
update of the system.

3) AptUpdate (object): Class to represent and operate with APT. Loaded only on
those systems that supports APT, it's responsible to connect and collect
information of the packages to be updated. Also it's responsible to execute the
update of the system.

4) ZypperUpdate (object): Class to represent and operate with Zypper. Loaded only
on those systems that supports Zypper, it's responsible to connect and collect
information of the packages to be updated. Also it's responsible to execute the
update of the system.

Signed-off-by: Paulo Vital <pvital at linux.vnet.ibm.com>
Signed-off-by: Ramon Medeiros <ramonn at linux.vnet.ibm.com>
Signed-off-by: Aline Manera <alinefm at br.ibm.com>
---
 src/kimchi/i18n.py       |    5 +
 src/kimchi/mockmodel.py  |   51 ++++++++++
 src/kimchi/model/host.py |   45 ++++++++-
 src/kimchi/swupdate.py   |  247 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 347 insertions(+), 1 deletion(-)
 create mode 100644 src/kimchi/swupdate.py

diff --git a/src/kimchi/i18n.py b/src/kimchi/i18n.py
index 03d1052..d0a269e 100644
--- a/src/kimchi/i18n.py
+++ b/src/kimchi/i18n.py
@@ -176,6 +176,11 @@ messages = {
     "KCHHOST0001E": _("Unable to shutdown host machine as there are running virtual machines"),
     "KCHHOST0002E": _("Unable to reboot host machine as there are running virtual machines"),
 
+    "KCHPKGUPD0001E": _("No packages marked for update"),
+    "KCHPKGUPD0002E": _("Package %(name)s is not marked to be updated."),
+    "KCHPKGUPD0003E": _("Error while getting packages marked to be updated. Details: %(err)s"),
+    "KCHPKGUPD0004E": _("There is no compatible package manager for this system."),
+
     "KCHOBJST0001E": _("Unable to find %(item)s in datastore"),
 
     "KCHUTILS0001E": _("Invalid URI %(uri)s"),
diff --git a/src/kimchi/mockmodel.py b/src/kimchi/mockmodel.py
index be0ae4b..73ae970 100644
--- a/src/kimchi/mockmodel.py
+++ b/src/kimchi/mockmodel.py
@@ -75,6 +75,7 @@ class MockModel(object):
         self._mock_storagepools = {'default': MockStoragePool('default')}
         self._mock_networks = {'default': MockNetwork('default')}
         self._mock_interfaces = self.dummy_interfaces()
+        self._mock_swupdate = MockSoftwareUpdate()
         self.next_taskid = 1
         self.storagepool_activate('default')
 
@@ -685,6 +686,16 @@ class MockModel(object):
                 'display_proxy_port':
                 kconfig.get('display', 'display_proxy_port')}
 
+    def packagesupdate_get_list(self):
+        return self._mock_swupdate.getUpdates()
+
+    def packageupdate_lookup(self, pkg_name):
+        return self._mock_swupdate.getUpdate(pkg_name)
+
+    def packagesupdate_update(self, args=None):
+        task_id = self.add_task('', self._mock_swupdate.doUpdate, None)
+        return self.task_lookup(task_id)
+
 
 class MockVMTemplate(VMTemplate):
     def __init__(self, args, mockmodel_inst=None):
@@ -848,6 +859,46 @@ class MockVMScreenshot(VMScreenshot):
         image.save(thumbnail)
 
 
+class MockSoftwareUpdate(object):
+    def __init__(self):
+        self._packages = {
+            'udevmountd': {'repository': 'openSUSE-13.1-Update',
+                           'version': '0.81.5-14.1',
+                           'arch': 'x86_64',
+                           'package_name': 'udevmountd'},
+            'sysconfig-network': {'repository': 'openSUSE-13.1-Extras',
+                                  'version': '0.81.5-14.1',
+                                  'arch': 'x86_64',
+                                  'package_name': 'sysconfig-network'},
+            'libzypp': {'repository': 'openSUSE-13.1-Update',
+                        'version': '13.9.0-10.1',
+                        'arch': 'noarch',
+                        'package_name': 'libzypp'}}
+        self._num2update = 3
+
+    def getUpdates(self):
+        return self._packages.keys()
+
+    def getUpdate(self, name):
+        if name not in self._packages.keys():
+            raise NotFoundError('KCHPKGUPD0002E', {'name': name})
+        return self._packages[name]
+
+    def getNumOfUpdates(self):
+        return self._num2update
+
+    def doUpdate(self, cb, params):
+        msgs = []
+        for pkg in self._packages.keys():
+            msgs.append("Updating package %s" % pkg)
+            cb('\n'.join(msgs))
+            time.sleep(1)
+
+        time.sleep(2)
+        msgs.append("All packages updated")
+        cb('\n'.join(msgs), True)
+
+
 def get_mock_environment():
     model = MockModel()
     for i in xrange(5):
diff --git a/src/kimchi/model/host.py b/src/kimchi/model/host.py
index 80f93db..b078b7b 100644
--- a/src/kimchi/model/host.py
+++ b/src/kimchi/model/host.py
@@ -32,8 +32,10 @@ from kimchi import disks
 from kimchi import netinfo
 from kimchi.basemodel import Singleton
 from kimchi.exception import NotFoundError, OperationFailed
+from kimchi.model.tasks import TaskModel
 from kimchi.model.vms import DOM_STATE_MAP
-from kimchi.utils import kimchi_log
+from kimchi.swupdate import SoftwareUpdate
+from kimchi.utils import add_task, kimchi_log
 
 
 HOST_STATS_INTERVAL = 1
@@ -201,3 +203,44 @@ class PartitionModel(object):
             raise NotFoundError("KCHPART0001E", {'name': name})
 
         return disks.get_partition_details(name)
+
+
+class PackagesUpdateModel(object):
+    def __init__(self, **kargs):
+
+        self.objstore = kargs['objstore']
+        self.task = TaskModel(**kargs)
+
+    def get_list(self):
+        return self.host_swupdate.getUpdates()
+
+    def update(self, **kargs):
+        try:
+            swupdate = SoftwareUpdate()
+        except Exception:
+            raise OperationFailed('KCHPKGUPD0004E')
+
+        try:
+            pkgs = swupdate.getNumOfUpdates()
+        except OperationFailed, e:
+            raise e
+
+        if pkgs == 0:
+            raise OperationFailed('KCHPKGUPD0001E')
+
+        kimchi_log.debug('Host is going to be updated.')
+        taskid = add_task('', swupdate.doUpdate, self.objstore, None)
+        return self.task.lookup(taskid)
+
+
+class PackageUpdateModel(object):
+    def __init__(self, **kargs):
+        pass
+
+    def lookup(self, name):
+        try:
+            swupdate = SoftwareUpdate()
+        except Exception:
+            raise OperationFailed('KCHPKGUPD0004E')
+
+        return swupdate.getUpdate(name)
diff --git a/src/kimchi/swupdate.py b/src/kimchi/swupdate.py
new file mode 100644
index 0000000..70855dc
--- /dev/null
+++ b/src/kimchi/swupdate.py
@@ -0,0 +1,247 @@
+#
+# Project Kimchi
+#
+# Copyright IBM, Corp. 2014
+#
+# Authors:
+#  Paulo Vital <pvital at linux.vnet.ibm.com>
+#  Ramon Medeiros <ramonn at linux.vnet.ibm.com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library 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
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+
+import platform
+import subprocess
+import time
+
+from kimchi.basemodel import Singleton
+from kimchi.exception import NotFoundError, OperationFailed
+from kimchi.utils import kimchi_log, run_command
+
+YUM_DISTROS = ['fedora', 'red hat enterprise linux',
+               'red hat enterprise linux server']
+APT_DISTROS = ['debian', 'ubuntu']
+ZYPPER_DISTROS = ['opensuse ', 'suse linux enterprise server ']
+
+
+class SoftwareUpdate(object):
+    __metaclass__ = Singleton
+
+    """
+    Class to represent and operate with OS software update.
+    """
+    def __init__(self):
+        # This stores all packages to be updated for Kimchi perspective. It's a
+        # dictionary of dictionaries, in the format {'package_name': package},
+        # where:
+        # package = {'package_name': <string>, 'version': <string>,
+        #           'arch': <string>, 'repository': <string>
+        #           }
+        self._packages = {}
+
+        # This stores the number of packages to update
+        self._num2update = 0
+
+        # Get the distro of host machine and creates an object related to
+        # correct package management system
+        self._distro = platform.linux_distribution()[0].lower()
+        if (self._distro in YUM_DISTROS):
+            kimchi_log.info("Loading YumUpdate features.")
+            self._pkg_mnger = YumUpdate()
+        elif (self._distro in APT_DISTROS):
+            kimchi_log.info("Loading AptUpdate features.")
+            self._pkg_mnger = AptUpdate()
+        elif (self._distro in ZYPPER_DISTROS):
+            kimchi_log.info("Loading ZypperUpdate features.")
+            self._pkg_mnger = ZypperUpdate()
+        else:
+            raise Exception("There is no compatible package manager for "
+                            "this system.")
+
+    def _scanUpdates(self):
+        """
+        Update self._packages with packages to be updated.
+        """
+        self._packages = {}
+        self._num2update = 0
+
+        # Call system pkg_mnger to get the packages as list of dictionaries.
+        for pkg in self._pkg_mnger.getPackagesList():
+
+            # Check if already exist a package in self._packages
+            pkg_id = pkg.get('package_name')
+            if pkg_id in self._packages.keys():
+                # package already listed to update. do nothing
+                continue
+
+            # Update the self._packages and self._num2update
+            self._packages[pkg_id] = pkg
+            self._num2update = self._num2update + 1
+
+    def getUpdates(self):
+        """
+        Return the self._packages.
+        """
+        self._scanUpdates()
+        return self._packages
+
+    def getUpdate(self, name):
+        """
+        Return a dictionary with all info from a given package name.
+        """
+        if not name in self._packages.keys():
+            raise NotFoundError('KCHPKGUPD0002E', {'name': name})
+
+        return self._packages[name]
+
+    def getNumOfUpdates(self):
+        """
+        Return the number of packages to be updated.
+        """
+        self._scanUpdates()
+        return self._num2update
+
+    def doUpdate(self, cb, params):
+        """
+        Execute the update
+        """
+        cmd = self._pkg_mnger.update_cmd
+        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
+                                stderr=subprocess.PIPE)
+        msgs = []
+        while proc.poll() is None:
+            msgs.append(proc.stdout.read())
+            cb('\n'.join(msgs))
+            time.sleep(0.5)
+
+        retcode = proc.poll()
+        if retcode == 0:
+            return cb('\n'.join(msgs), True)
+
+        msgs.append(proc.stderr.read())
+        return cb('\n'.join(msgs), False)
+
+
+class YumUpdate(object):
+    """
+    Class to represent and operate with YUM software update system.
+    It's loaded only on those systems listed at YUM_DISTROS and loads necessary
+    modules in runtime.
+    """
+    def __init__(self):
+        self._pkgs = {}
+        self._yb = getattr(__import__('yum'), 'YumBase')()
+        self.update_cmd = ["yum", "-y", "update"]
+
+    def _refreshUpdateList(self):
+        """
+        Update the list of packages to be updated in the system.
+        """
+        self._pkgs = self._yb.doPackageLists('updates')
+
+    def getPackagesList(self):
+        """
+        Return a list of package's dictionaries. Each dictionary contains the
+        information about a package, in the format:
+        package = {'package_name': <string>, 'version': <string>,
+                   'arch': <string>, 'repository': <string>}
+        """
+        self._refreshUpdateList()
+        pkg_list = []
+        for pkg in self._pkgs:
+            package = {'package_name': pkg.name,
+                       'version': "%s-%s" % (pkg.version, pkg.release),
+                       'arch': pkg.arch, 'repository': pkg.ui_from_repo}
+            pkg_list.append(package)
+        return pkg_list
+
+
+class AptUpdate(object):
+    """
+    Class to represent and operate with APT software update system.
+    It's loaded only on those systems listed at APT_DISTROS and loads necessary
+    modules in runtime.
+    """
+    def __init__(self):
+        self._pkgs = {}
+        self._apt_cache = getattr(__import__('apt'), 'Cache')()
+        self.update_cmd = ['apt-get', 'upgrade', '-y']
+
+    def _refreshUpdateList(self):
+        """
+        Update the list of packages to be updated in the system.
+        """
+        self._apt_cache.update()
+        self._apt_cache.upgrade()
+        self._pkgs = self._apt_cache.get_changes()
+
+    def getPackagesList(self):
+        """
+        Return a list of package's dictionaries. Each dictionary contains the
+        information about a package, in the format
+        package = {'package_name': <string>, 'version': <string>,
+                   'arch': <string>, 'repository': <string>}
+        """
+        self._refreshUpdateList()
+        pkg_list = []
+        for pkg in self._pkgs:
+            package = {'package_name': pkg.shortname,
+                       'version': pkg.candidate.version,
+                       'arch': pkg.architecture(),
+                       'repository': pkg.candidate.origins[0].label}
+            pkg_list.append(package)
+        return pkg_list
+
+
+class ZypperUpdate(object):
+    """
+    Class to represent and operate with Zypper software update system.
+    It's loaded only on those systems listed at ZYPPER_DISTROS and loads
+    necessary modules in runtime.
+    """
+    def __init__(self):
+        self._pkgs = {}
+        self.update_cmd = ["zypper", "--non-interactive", "update",
+                           "--auto-agree-with-licenses"]
+
+    def _refreshUpdateList(self):
+        """
+        Update the list of packages to be updated in the system.
+        """
+        self._pkgs = {}
+        cmd = ["zypper", "list-updates"]
+        (stdout, stderr, returncode) = run_command(cmd)
+
+        if len(stderr) > 0:
+            raise OperationFailed('KCHPKGUPD0003E', {'err': stderr})
+
+        for line in stdout.split('\n'):
+            if line.find('v |') >= 0:
+                info = line.split(' | ')
+                package = {'package_name': info[2], 'version': info[4],
+                           'arch': info[5], 'repository': info[1]}
+                self._pkgs[info[2]] = package
+
+    def getPackagesList(self):
+        """
+        Return a list of package's dictionaries. Each dictionary contains the
+        information about a package, in the format
+        package = {'package_name': <string>, 'version': <string>,
+                   'arch': <string>, 'repository': <string>}
+        """
+        self._refreshUpdateList()
+        pkg_list = []
+        for pkg in self._pkgs:
+            pkg_list.append(pkg)
+        return pkg_list
-- 
1.7.10.4




More information about the Kimchi-devel mailing list