[PATCH 0/5 V3] Host's software update support

This patch set provides support to host's software update operations. To test the backend (ONLY mockmodel) execute the following commands: $ cd tests $ sudo ./run_tests.sh test_mockmodel.MockModelTests.test_packages_update To test the REST API, execute the following commands (all them are agnostic of the host's distro): 1) Get list of all packages to be updated in the host: $ curl -H 'Content-type: application/json' -H 'Accept: application/json' http://localhost:8000/host/packagesupdate/ -X GET 2) Update the host system: $ curl -H 'Content-type: application/json' -H 'Accept: application/json' http://localhost:8000/host/packagesupdate/update -X POST -d '' V2 -> V3: * Fixed indent and wrong bullets on docs/API.md * Changed to use Collection and Resource * Removed unnecessary 'exposed' * PEP8 compatibility on mockmodel.py * Removed debug prints from swupdate.py * Fixed indent in swupdate.py * Usage of utils.run_command() to execute Zypper update * Updated swupdate.py and mockmodel.py to make consistent with docs/API.md * Usage of Task element in mockmodel.py * Created test case in test_mockmodel.py V1 -> V2: * rebased to use new model framework * added SLES as Zypper supported distro * PEP8 compatibility * changed import sentences * use of run_command() from utils.py to execute shell commands * usage of Task element to return update status V1: At this point, an agnostic class is providing support to backend and REST API operations. In addition, YUM (for RHEL and Fedora), APT (for Debian and Ubuntu) and ZYPPER (for OpenSuse) specific classes are provided to support the operation os each software update system. There's no test case to check backend once the software update information is volatile to each system/box. Paulo Vital (5): Host's software update: Update API.md Host's software update: Update REST API Host's software update: Update backend. Host's software update: Update Makefile Host's software update: Update test cases. Makefile.am | 1 + docs/API.md | 30 +++++ src/kimchi/Makefile.am | 1 + src/kimchi/control/host.py | 17 +++ src/kimchi/mockmodel.py | 45 ++++++++ src/kimchi/model/host.py | 28 ++++- src/kimchi/swupdate.py | 269 +++++++++++++++++++++++++++++++++++++++++++++ tests/test_mockmodel.py | 11 ++ 8 files changed, 401 insertions(+), 1 deletion(-) create mode 100644 src/kimchi/swupdate.py -- 1.8.3.1

Define get and POST action update API for software update resource. Signed-off-by: Paulo Vital <pvital@linux.vnet.ibm.com> Signed-off-by: Ramon Medeiros <ramonn@linux.vnet.ibm.com> --- docs/API.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/docs/API.md b/docs/API.md index 580728c..eef107a 100644 --- a/docs/API.md +++ b/docs/API.md @@ -741,3 +741,33 @@ Contains the host sample data. * size: The total size of the partition, in bytes * mountpoint: If the partition is mounted, represents the mountpoint. Otherwise blank. + +### Collection: Host Packages Update + +**URI:** /host/packagesupdate + +Contains the information and action of packages update in the host. + +**Methods:** + +* **GET**: Retrieves a list of all packages to be updated in the host: + +* **POST**: *See Software Update Actions* + +**Actions (POST):** + +* update: Start the update of packages in background + +### Resource: Host Package Update + +**URI:** /host/packageupdate/*:name* + +Contains the information for a specific package to be updated. + +**Methods:** + +* **GET**: Retrieves a full description of a package: + * package_name: The name of the package to be updated + * arch: The architecture of the package + * version: The new version of the package + * repository: The repository name from where package will be downloaded -- 1.8.3.1

On 02/12/2014 01:28 AM, Paulo Vital wrote:
Define get and POST action update API for software update resource.
Signed-off-by: Paulo Vital <pvital@linux.vnet.ibm.com> Signed-off-by: Ramon Medeiros <ramonn@linux.vnet.ibm.com> --- docs/API.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
diff --git a/docs/API.md b/docs/API.md index 580728c..eef107a 100644 --- a/docs/API.md +++ b/docs/API.md @@ -741,3 +741,33 @@ Contains the host sample data. * size: The total size of the partition, in bytes * mountpoint: If the partition is mounted, represents the mountpoint. Otherwise blank. + +### Collection: Host Packages Update + +**URI:** /host/packagesupdate + +Contains the information and action of packages update in the host. + +**Methods:** + +* **GET**: Retrieves a list of all packages to be updated in the host: + +* **POST**: *See Software Update Actions* + +**Actions (POST):** + +* update: Start the update of packages in background + +### Resource: Host Package Update + +**URI:** /host/packageupdate/*:name* I got:
"reason":"Nothing matches the given URI", "code":"404 Not Found", when I GET some package. Should be: +**URI:** /host/package*s*update/*:name*
+ +Contains the information for a specific package to be updated. + +**Methods:** + +* **GET**: Retrieves a full description of a package: + * package_name: The name of the package to be updated + * arch: The architecture of the package + * version: The new version of the package + * repository: The repository name from where package will be downloaded

On 02/12/2014 04:04 AM, Hongliang Wang wrote:
On 02/12/2014 01:28 AM, Paulo Vital wrote:
Define get and POST action update API for software update resource.
Signed-off-by: Paulo Vital<pvital@linux.vnet.ibm.com> Signed-off-by: Ramon Medeiros<ramonn@linux.vnet.ibm.com> --- docs/API.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
diff --git a/docs/API.md b/docs/API.md index 580728c..eef107a 100644 --- a/docs/API.md +++ b/docs/API.md @@ -741,3 +741,33 @@ Contains the host sample data. * size: The total size of the partition, in bytes * mountpoint: If the partition is mounted, represents the mountpoint. Otherwise blank. + +### Collection: Host Packages Update + +**URI:** /host/packagesupdate + +Contains the information and action of packages update in the host. + +**Methods:** + +* **GET**: Retrieves a list of all packages to be updated in the host: + +* **POST**: *See Software Update Actions* + +**Actions (POST):** + +* update: Start the update of packages in background + +### Resource: Host Package Update + +**URI:** /host/packageupdate/*:name* I got:
"reason":"Nothing matches the given URI", "code":"404 Not Found",
when I GET some package. Should be: +**URI:** /host/package*s*update/*:name*
Good catch! I passed from my eyes.
+ +Contains the information for a specific package to be updated. + +**Methods:** + +* **GET**: Retrieves a full description of a package: + * package_name: The name of the package to be updated + * arch: The architecture of the package + * version: The new version of the package + * repository: The repository name from where package will be downloaded
_______________________________________________ Kimchi-devel mailing list Kimchi-devel@ovirt.org http://lists.ovirt.org/mailman/listinfo/kimchi-devel

Reviewed-by: Aline Manera <alinefm@linux.vnet.ibm.com> On 02/11/2014 03:28 PM, Paulo Vital wrote:
Define get and POST action update API for software update resource.
Signed-off-by: Paulo Vital <pvital@linux.vnet.ibm.com> Signed-off-by: Ramon Medeiros <ramonn@linux.vnet.ibm.com> --- docs/API.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
diff --git a/docs/API.md b/docs/API.md index 580728c..eef107a 100644 --- a/docs/API.md +++ b/docs/API.md @@ -741,3 +741,33 @@ Contains the host sample data. * size: The total size of the partition, in bytes * mountpoint: If the partition is mounted, represents the mountpoint. Otherwise blank. + +### Collection: Host Packages Update + +**URI:** /host/packagesupdate + +Contains the information and action of packages update in the host. + +**Methods:** + +* **GET**: Retrieves a list of all packages to be updated in the host: + +* **POST**: *See Software Update Actions* + +**Actions (POST):** + +* update: Start the update of packages in background + +### Resource: Host Package Update + +**URI:** /host/packageupdate/*:name* + +Contains the information for a specific package to be updated. + +**Methods:** + +* **GET**: Retrieves a full description of a package: + * package_name: The name of the package to be updated + * arch: The architecture of the package + * version: The new version of the package + * repository: The repository name from where package will be downloaded

Define PackageUpdate resource features according to API.md Activate auth support to new resource Signed-off-by: Paulo Vital <pvital@linux.vnet.ibm.com> Signed-off-by: Ramon Medeiros <ramonn@linux.vnet.ibm.com> --- src/kimchi/control/host.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/kimchi/control/host.py b/src/kimchi/control/host.py index 053c822..e9437c7 100644 --- a/src/kimchi/control/host.py +++ b/src/kimchi/control/host.py @@ -36,6 +36,7 @@ class Host(Resource): self.shutdown = self.generate_action_handler('shutdown') self.stats = HostStats(self.model) self.partitions = Partitions(self.model) + self.packagesupdate = PackagesUpdate(self.model) @property def data(self): @@ -61,3 +62,19 @@ class Partition(Resource): @property def data(self): return self.info + + +class PackagesUpdate(Collection): + def __init__(self, model): + super(PackagesUpdate, self).__init__(model) + self.resource = PackageUpdate + self.update = self.model.packagesupdate_update + + +class PackageUpdate(Resource): + def __init__(self, model, id=None): + super(PackageUpdate, self).__init__(model, id) + + @property + def data(self): + return self.info -- 1.8.3.1

On 02/11/2014 03:28 PM, Paulo Vital wrote:
Define PackageUpdate resource features according to API.md Activate auth support to new resource
Signed-off-by: Paulo Vital <pvital@linux.vnet.ibm.com> Signed-off-by: Ramon Medeiros <ramonn@linux.vnet.ibm.com> --- src/kimchi/control/host.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
diff --git a/src/kimchi/control/host.py b/src/kimchi/control/host.py index 053c822..e9437c7 100644 --- a/src/kimchi/control/host.py +++ b/src/kimchi/control/host.py @@ -36,6 +36,7 @@ class Host(Resource): self.shutdown = self.generate_action_handler('shutdown') self.stats = HostStats(self.model) self.partitions = Partitions(self.model) + self.packagesupdate = PackagesUpdate(self.model)
@property def data(self): @@ -61,3 +62,19 @@ class Partition(Resource): @property def data(self): return self.info + + +class PackagesUpdate(Collection): + def __init__(self, model): + super(PackagesUpdate, self).__init__(model) + self.resource = PackageUpdate
+ self.update = self.model.packagesupdate_update +
The action function needs to catch the exception raised (InvalidOperation, MissingParameter, etc) and raise and cherrypy.HTTPError with the error code and message As generate_action_handler() is only for Resource you need to expose this function as follwing: @cherrypy.expose def update(self): try: self.mode.packagesupdate_update() except OperationFailed, e: raise cherrypy.HTTPError(500, e.message)
+ +class PackageUpdate(Resource): + def __init__(self, model, id=None): + super(PackageUpdate, self).__init__(model, id) + + @property + def data(self): + return self.info

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@linux.vnet.ibm.com> Signed-off-by: Ramon Medeiros <ramonn@linux.vnet.ibm.com> --- src/kimchi/mockmodel.py | 45 ++++++++ src/kimchi/model/host.py | 28 ++++- src/kimchi/swupdate.py | 269 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 341 insertions(+), 1 deletion(-) create mode 100644 src/kimchi/swupdate.py diff --git a/src/kimchi/mockmodel.py b/src/kimchi/mockmodel.py index 4e276eb..09c2c0f 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') @@ -665,6 +666,15 @@ 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): + return self.add_task('', self._mock_swupdate.doUpdate, None) + class MockVMTemplate(VMTemplate): def __init__(self, args, mockmodel_inst=None): @@ -825,6 +835,41 @@ 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 + + def getUpdate(self, name): + if name not in self._packages.keys(): + raise NotFoundError("Package %s is not marked to be updated." % + name) + return self._packages[name] + + def getNumOfUpdates(self): + return self._num2update + + def doUpdate(self): + if self._num2update == 0: + raise OperationFailed("No packages marked for update") + sleep(60) + + 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 a3d9e38..de544e1 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 @@ -199,3 +201,27 @@ class PartitionModel(object): raise NotFoundError("Partition %s not found in the host" % name) return disks.get_partition_details(name) + + +class PackagesUpdateModel(object): + def __init__(self, **kargs): + self.host_swupdate = SoftwareUpdate() + self.objstore = kargs['objstore'] + self.task = TaskModel(**kargs) + + def get_list(self): + return self.host_swupdate.getUpdates() + + def update(self, **kargs): + kimchi_log.info('Host is going to be updated.') + gen_cmd = self.host_swupdate.doUpdate() + taskid = add_task('', gen_cmd, self.objstore, 'packageupdate') + return self.task.lookup(taskid) + + +class PackageUpdateModel(object): + def __init__(self, **kargs): + self.host_swupdate = SoftwareUpdate() + + def lookup(self, name): + return self.host_swupdate.getUpdate(name) diff --git a/src/kimchi/swupdate.py b/src/kimchi/swupdate.py new file mode 100644 index 0000000..f48e0cf --- /dev/null +++ b/src/kimchi/swupdate.py @@ -0,0 +1,269 @@ +# +# Project Kimchi +# +# Copyright IBM, Corp. 2014 +# +# Authors: +# Paulo Vital <pvital@linux.vnet.ibm.com> +# Ramon Medeiros <ramonn@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 + +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: + self._pkg_mnger = None + + if not self._pkg_mnger: + kimchi_log.info("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("Package %s is not marked to update." % name) + + return self._packages[name] + + def getNumOfUpdates(self): + """ + Return the number of packages to be updated. + """ + self._scanUpdates() + return self._num2update + + def doUpdate(self): + """ + Execute the update + """ + if self._num2update == 0: + kimchi_log.info("No packages marked for update") + raise OperationFailed("No packages marked for update") + return self._pkg_mnger.update() + + +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')() + + 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 + + def update(self): + """ + Execute the update of all packages marked to be update. + """ + # FIXME: Due to incompatabilities between cherrypy and yum/sqlite3 + # threading, we need execute the YUM command line to execute the update + cmd = ["yum", "-y", "-d", "0", "-e", "0", "update"] + (stdout, stderr, returncode) = run_command(cmd) + + if len(stderr) > 0: + raise OperationFailed("ERROR when executing command: %s" % stderr) + + +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')() + + 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 + + def update(self): + """ + Execute the update of all packages marked to be update. + """ + try: + self._apt_cache.update() + self._apt_cache.open(None) + self._apt_cache.upgrade() + self._apt_cache.commit() + except Exception, e: + raise OperationFailed("ERROR when executing command: %s" % e) + + +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 = {} + + 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("ERROR when executing command: %s" % 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 + + def update(self): + """ + Execute the update of all packages marked to be update. + """ + cmd = ["zypper", "--non-interactive", "update", + "--auto-agree-with-licenses"] + (stdout, stderr, returncode) = run_command(cmd) + + if len(stderr) > 0: + raise OperationFailed("ERROR when executing command: %s" % stderr) -- 1.8.3.1

On 02/11/2014 03:28 PM, Paulo Vital wrote:
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@linux.vnet.ibm.com> Signed-off-by: Ramon Medeiros <ramonn@linux.vnet.ibm.com> --- src/kimchi/mockmodel.py | 45 ++++++++ src/kimchi/model/host.py | 28 ++++- src/kimchi/swupdate.py | 269 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 341 insertions(+), 1 deletion(-) create mode 100644 src/kimchi/swupdate.py
diff --git a/src/kimchi/mockmodel.py b/src/kimchi/mockmodel.py index 4e276eb..09c2c0f 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')
@@ -665,6 +666,15 @@ 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): + return self.add_task('', self._mock_swupdate.doUpdate, None)
You need to return the whole Task element and not only the task id 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): @@ -825,6 +835,41 @@ 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 + + def getUpdate(self, name): + if name not in self._packages.keys(): + raise NotFoundError("Package %s is not marked to be updated." % + name) + return self._packages[name] + + def getNumOfUpdates(self): + return self._num2update + + def doUpdate(self): + if self._num2update == 0: + raise OperationFailed("No packages marked for update") + sleep(60) + + 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 a3d9e38..de544e1 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 @@ -199,3 +201,27 @@ class PartitionModel(object): raise NotFoundError("Partition %s not found in the host" % name) return disks.get_partition_details(name) + + +class PackagesUpdateModel(object): + def __init__(self, **kargs): + self.host_swupdate = SoftwareUpdate() + self.objstore = kargs['objstore'] + self.task = TaskModel(**kargs) + + def get_list(self): + return self.host_swupdate.getUpdates() + + def update(self, **kargs): + kimchi_log.info('Host is going to be updated.') + gen_cmd = self.host_swupdate.doUpdate() + taskid = add_task('', gen_cmd, self.objstore, 'packageupdate') + return self.task.lookup(taskid) + + +class PackageUpdateModel(object): + def __init__(self, **kargs): + self.host_swupdate = SoftwareUpdate() + + def lookup(self, name): + return self.host_swupdate.getUpdate(name) diff --git a/src/kimchi/swupdate.py b/src/kimchi/swupdate.py new file mode 100644 index 0000000..f48e0cf --- /dev/null +++ b/src/kimchi/swupdate.py @@ -0,0 +1,269 @@ +# +# Project Kimchi +# +# Copyright IBM, Corp. 2014 +# +# Authors: +# Paulo Vital <pvital@linux.vnet.ibm.com> +# Ramon Medeiros <ramonn@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 + +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: + self._pkg_mnger = None + + if not self._pkg_mnger: + kimchi_log.info("There is no compatible package manager for \ + this system.")
You need to raise here instead of justing logging the error. Otherwise all the update process will fail.
+ + 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("Package %s is not marked to update." % name) + + return self._packages[name] + + def getNumOfUpdates(self): + """ + Return the number of packages to be updated. + """ + self._scanUpdates() + return self._num2update + + def doUpdate(self): + """ + Execute the update + """ + if self._num2update == 0: + kimchi_log.info("No packages marked for update") + raise OperationFailed("No packages marked for update") + return self._pkg_mnger.update() + + +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')() + + 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 + + def update(self): + """ + Execute the update of all packages marked to be update. + """ + # FIXME: Due to incompatabilities between cherrypy and yum/sqlite3 + # threading, we need execute the YUM command line to execute the update + cmd = ["yum", "-y", "-d", "0", "-e", "0", "update"] + (stdout, stderr, returncode) = run_command(cmd) + + if len(stderr) > 0: + raise OperationFailed("ERROR when executing command: %s" % stderr) + + +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')() + + 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 + + def update(self): + """ + Execute the update of all packages marked to be update. + """ + try: + self._apt_cache.update() + self._apt_cache.open(None) + self._apt_cache.upgrade() + self._apt_cache.commit() + except Exception, e: + raise OperationFailed("ERROR when executing command: %s" % e) + + +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 = {} + + 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("ERROR when executing command: %s" % 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 + + def update(self): + """ + Execute the update of all packages marked to be update. + """ + cmd = ["zypper", "--non-interactive", "update", + "--auto-agree-with-licenses"] + (stdout, stderr, returncode) = run_command(cmd) + + if len(stderr) > 0: + raise OperationFailed("ERROR when executing command: %s" % stderr)

Update Makefile to provide software update support Signed-off-by: Paulo Vital <pvital@linux.vnet.ibm.com> Signed-off-by: Ramon Medeiros <ramonn@linux.vnet.ibm.com> --- Makefile.am | 1 + src/kimchi/Makefile.am | 1 + 2 files changed, 2 insertions(+) diff --git a/Makefile.am b/Makefile.am index 266f78f..f89dcf2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -57,6 +57,7 @@ PEP8_WHITELIST = \ src/kimchi/rollbackcontext.py \ src/kimchi/root.py \ src/kimchi/server.py \ + src/kimchi/swupdate.py \ src/kimchi/utils.py \ tests/test_config.py.in \ tests/test_mockmodel.py \ diff --git a/src/kimchi/Makefile.am b/src/kimchi/Makefile.am index 1653c0c..1c65bea 100644 --- a/src/kimchi/Makefile.am +++ b/src/kimchi/Makefile.am @@ -46,6 +46,7 @@ kimchi_PYTHON = \ screenshot.py \ server.py \ sslcert.py \ + swupdate.py \ template.py \ utils.py \ vmtemplate.py \ -- 1.8.3.1

Reviewed-by: Aline Manera <alinefm@linux.vnet.ibm.com> On 02/11/2014 03:28 PM, Paulo Vital wrote:
Update Makefile to provide software update support
Signed-off-by: Paulo Vital <pvital@linux.vnet.ibm.com> Signed-off-by: Ramon Medeiros <ramonn@linux.vnet.ibm.com> --- Makefile.am | 1 + src/kimchi/Makefile.am | 1 + 2 files changed, 2 insertions(+)
diff --git a/Makefile.am b/Makefile.am index 266f78f..f89dcf2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -57,6 +57,7 @@ PEP8_WHITELIST = \ src/kimchi/rollbackcontext.py \ src/kimchi/root.py \ src/kimchi/server.py \ + src/kimchi/swupdate.py \ src/kimchi/utils.py \ tests/test_config.py.in \ tests/test_mockmodel.py \ diff --git a/src/kimchi/Makefile.am b/src/kimchi/Makefile.am index 1653c0c..1c65bea 100644 --- a/src/kimchi/Makefile.am +++ b/src/kimchi/Makefile.am @@ -46,6 +46,7 @@ kimchi_PYTHON = \ screenshot.py \ server.py \ sslcert.py \ + swupdate.py \ template.py \ utils.py \ vmtemplate.py \

Added unit tests into test_mockmodel.py Signed-off-by: Paulo Vital <pvital@linux.vnet.ibm.com> --- tests/test_mockmodel.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/test_mockmodel.py b/tests/test_mockmodel.py index 29413a9..10dcd8a 100644 --- a/tests/test_mockmodel.py +++ b/tests/test_mockmodel.py @@ -152,3 +152,14 @@ class MockModelTests(unittest.TestCase): self.assertEquals(stats_keys, set(eval(info['stats']).keys())) self.assertEquals('vnc', info['graphics']['type']) self.assertEquals('0.0.0.0', info['graphics']['listen']) + + def test_packages_update(self): + pkgs = model.packagesupdate_get_list() + for pkg_name in pkgs.keys(): + pkgupdate = model.packageupdate_lookup(pkg_name) + pkg = pkgs[pkg_name] + self.assertEquals(pkg['package_name'], pkgupdate['package_name']) + self.assertEquals(pkg['repository'], pkgupdate['repository']) + self.assertEquals(pkg['arch'], pkgupdate['arch']) + self.assertEquals(pkg['version'], pkgupdate['version']) + task_update = model.packagesupdate_update() -- 1.8.3.1

Please, also add a test for the REST API (test_rest.py) On 02/11/2014 03:28 PM, Paulo Vital wrote:
Added unit tests into test_mockmodel.py
Signed-off-by: Paulo Vital <pvital@linux.vnet.ibm.com> --- tests/test_mockmodel.py | 11 +++++++++++ 1 file changed, 11 insertions(+)
diff --git a/tests/test_mockmodel.py b/tests/test_mockmodel.py index 29413a9..10dcd8a 100644 --- a/tests/test_mockmodel.py +++ b/tests/test_mockmodel.py @@ -152,3 +152,14 @@ class MockModelTests(unittest.TestCase): self.assertEquals(stats_keys, set(eval(info['stats']).keys())) self.assertEquals('vnc', info['graphics']['type']) self.assertEquals('0.0.0.0', info['graphics']['listen']) + + def test_packages_update(self): + pkgs = model.packagesupdate_get_list() + for pkg_name in pkgs.keys(): + pkgupdate = model.packageupdate_lookup(pkg_name) + pkg = pkgs[pkg_name] + self.assertEquals(pkg['package_name'], pkgupdate['package_name']) + self.assertEquals(pkg['repository'], pkgupdate['repository']) + self.assertEquals(pkg['arch'], pkgupdate['arch']) + self.assertEquals(pkg['version'], pkgupdate['version']) + task_update = model.packagesupdate_update()
participants (3)
-
Aline Manera
-
Hongliang Wang
-
Paulo Vital