[PATCH 0/5] Host's repositories management support

WIP: This V1 supports only YUM. APT will come in V2 due to finish tests. This patch set provides support to host's repositories management operations. At this point, an agnostic class is providing support to backend and REST API operations. In addition, YUM (for RHEL, Fedora, SLES and OpenSuse) and APT (for Debian and Ubuntu) specific classes are provided to support the operation os each software update system. To test the backend execute the following commands: $ cd tests $ sudo ./run_tests.sh test_model.ModelTests.test_repository_create $ sudo ./run_tests.sh test_model.ModelTests.test_repository_update $ sudo ./run_tests.sh test_model.ModelTests.test_repository_disable_enable To test the REST API, execute the following commands (all them are agnostic of the host's distro): 1) Get list of all repositories enabled in the host: $ curl -u <USER> -H 'Content-type: application/json' -H 'Accept: application/json' http://localhost:8000/host/repositories/ -X GET 2) Create a new repository: $ curl -u <USER> -H 'Content-type: application/json' -H 'Accept: application/json' http://localhost:8000/host/repositories/ -X POST -d' { "repo_id": "fedora-fake", "baseurl": "http://www.fedora.org", "gpgkey": "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-fake-19" } ' 3) Get information from a specific repository: $ curl -u <USER> -H 'Content-type: application/json' -H 'Accept: application/json' http://localhost:8000/host/repositories/fedora-fake 4) Update a specific repository: $ curl -u <USER> -H 'Content-type: application/json' -H 'Accept: application/json' http://localhost:8000/host/repositories/fedora-fake -X PUT -d' { "repo_id":"fedora-fake", "repo_name":"Fedora 19 FAKEs", "baseurl": "http://www.fedora.org/downloads" } ' 5) Disable a specific repository: $ curl -u <USER> -H 'Content-type: application/json' -H 'Accept: application/json' http://localhost:8000/host/repositories/fedora-fake/disable -X POST -d '' 6) Enable a specific repository: $ curl -u <USER> -H 'Content-type: application/json' -H 'Accept: application/json' http://localhost:8000/host/repositories/fedora-fake/enable -X POST -d '' 7) Delete a specific repository: $ curl -u <USER> -H 'Content-type: application/json' -H 'Accept: application/json' http://localhost:8000/host/repositories/fedora-fake -X DELETE Paulo Vital (5): Host's repositories management: Update API.md Host's repositories management: Update REST API Host's repositories management: Update backend. Host's repositories management: Update Makefile Host's repositories management: Update test-cases. Makefile.am | 1 + docs/API.md | 80 +++++++++ src/kimchi/API.json | 63 +++++++ src/kimchi/Makefile.am | 1 + src/kimchi/control/host.py | 21 +++ src/kimchi/mockmodel.py | 34 ++++ src/kimchi/model/host.py | 50 +++++- src/kimchi/repositories.py | 418 +++++++++++++++++++++++++++++++++++++++++++++ tests/test_model.py | 99 +++++++++++ tests/test_rest.py | 32 ++++ 10 files changed, 798 insertions(+), 1 deletion(-) create mode 100644 src/kimchi/repositories.py -- 1.8.3.1

Define get and create API for repositories collection. Define get, update, enable and disable API for repository resource. Signed-off-by: Paulo Vital <pvital@linux.vnet.ibm.com> --- docs/API.md | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/docs/API.md b/docs/API.md index 580728c..52bafae 100644 --- a/docs/API.md +++ b/docs/API.md @@ -741,3 +741,83 @@ 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 Repositories + +**URI:** /host/repositories + +**Methods:** + +* **GET**: Retrieve a summarized list of all repositories available +* **POST**: Add a new repository + * repo_id *(optional)*: Unique repository name for each repository, +one word. + * baseurl: URL to the repodata directory when "is_mirror" is false. +Otherwise, it can be URL to the mirror system for YUM. Can be an +http://, ftp:// or file:// URL. + * is_mirror *(optional)*: Set the given URI of baseurl as a mirror +list, instead of use baseurl in repository configuration. + * url_args *(optional)*: Arguments to be passed to baseurl, like the +list of APT repositories provided by the same baseurl. + * gpgkey *(optional)*: URL pointing to the ASCII-armored GPG key +file for the repository. This option is used if yum needs a public key +to verify a package and the required key hasn't been imported into the +RPM database. + +### Resource: Repository + +**URI:** /host/repositories/*:repo-id* + +**Methods:** + +* **GET**: Retrieve the full description of a Repository + * repo_id: Unique repository name for each repository, one word. + * repo_name: Human-readable string describing the repository. + * baseurl: URL to the repodata directory when "is_mirror" is false. +Otherwise, it can be URL to the mirror system for YUM. Can be an +http://, ftp:// or file:// URL. + * url_args *(optional)*: Arguments to be passed to baseurl, like the +list of APT repositories provided by the same baseurl. + * enabled: Indicates if repository should be included +as a package source: + * false: Do not include the repository. + * true: Include the repository. + * gpgcheck *(optional)*: Indicates if a GPG signature check on the +packages gotten from repository should be performed: + * false: Do not check GPG signature + * true: Check GPG signature + * gpgkey *(optional)*: URL pointing to the ASCII-armored GPG key +file for the repository. This option is used if yum needs a public key +to verify a package and the required key hasn't been imported into the +RPM database. +* **DELETE**: Remove the Repository +* **POST**: *See Repository Actions* +* **PUT**: update the parameters of existing Repository + * repo_id *(otional)*: Unique repository name for each repository, +one word. + * repo_name *(otional)*: Human-readable string describing the +repository. + * baseurl *(optional)*: URL to the repodata directory when +"is_mirror" is false. Otherwise, it can be URL to the mirror system for +YUM. Can be an http://, ftp:// or file:// URL. + * is_mirror *(optional)*: Set the given URI of baseurl as a mirror +list, instead of use baseurl in repository configuration. + * url_args *(optional)*: Arguments to be passed to baseurl, like the +list of APT repositories provided by the same baseurl. + * enabled *(optional)*: Indicates if repository should be included +as a package source: + * false: Do not include the repository. + * true: Include the repository. + * gpgcheck *(optional)*: Indicates if a GPG signature check on the +packages gotten from repository should be performed: + * false: Do not check GPG signature + * true: Check GPG signature + * gpgkey *(optional)*: URL pointing to the ASCII-armored GPG key +file for the repository. This option is used if yum needs a public key +to verify a package and the required key hasn't been imported into the +RPM database. + +**Actions (POST):** + +* enable: Enable the Repository as package source +* disable: Disable the Repository as package source -- 1.8.3.1

On 02/10/2014 11:46 AM, Paulo Vital wrote:
Define get and create API for repositories collection. Define get, update, enable and disable API for repository resource.
Signed-off-by: Paulo Vital <pvital@linux.vnet.ibm.com> --- docs/API.md | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+)
diff --git a/docs/API.md b/docs/API.md index 580728c..52bafae 100644 --- a/docs/API.md +++ b/docs/API.md @@ -741,3 +741,83 @@ 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 Repositories + +**URI:** /host/repositories + +**Methods:** + +* **GET**: Retrieve a summarized list of all repositories available +* **POST**: Add a new repository + * repo_id *(optional)*: Unique repository name for each repository, +one word. + * baseurl: URL to the repodata directory when "is_mirror" is false. +Otherwise, it can be URL to the mirror system for YUM. Can be an +http://, ftp:// or file:// URL. + * is_mirror *(optional)*: Set the given URI of baseurl as a mirror +list, instead of use baseurl in repository configuration. + * url_args *(optional)*: Arguments to be passed to baseurl, like the +list of APT repositories provided by the same baseurl. + * gpgkey *(optional)*: URL pointing to the ASCII-armored GPG key +file for the repository. This option is used if yum needs a public key +to verify a package and the required key hasn't been imported into the +RPM database. + +### Resource: Repository + +**URI:** /host/repositories/*:repo-id* + +**Methods:** + +* **GET**: Retrieve the full description of a Repository + * repo_id: Unique repository name for each repository, one word. + * repo_name: Human-readable string describing the repository. + * baseurl: URL to the repodata directory when "is_mirror" is false. +Otherwise, it can be URL to the mirror system for YUM. Can be an +http://, ftp:// or file:// URL. + * url_args *(optional)*: Arguments to be passed to baseurl, like the +list of APT repositories provided by the same baseurl. + * enabled: Indicates if repository should be included +as a package source: + * false: Do not include the repository. + * true: Include the repository. + * gpgcheck *(optional)*: Indicates if a GPG signature check on the +packages gotten from repository should be performed: + * false: Do not check GPG signature + * true: Check GPG signature + * gpgkey *(optional)*: URL pointing to the ASCII-armored GPG key +file for the repository. This option is used if yum needs a public key +to verify a package and the required key hasn't been imported into the +RPM database. +* **DELETE**: Remove the Repository +* **POST**: *See Repository Actions* +* **PUT**: update the parameters of existing Repository + * repo_id *(otional)*: Unique repository name for each repository, +one word. + * repo_name *(otional)*: Human-readable string describing the +repository. + * baseurl *(optional)*: URL to the repodata directory when +"is_mirror" is false. Otherwise, it can be URL to the mirror system for +YUM. Can be an http://, ftp:// or file:// URL. + * is_mirror *(optional)*: Set the given URI of baseurl as a mirror +list, instead of use baseurl in repository configuration. + * url_args *(optional)*: Arguments to be passed to baseurl, like the +list of APT repositories provided by the same baseurl.
+ * enabled *(optional)*: Indicates if repository should be included +as a package source: + * false: Do not include the repository. + * true: Include the repository.
The enable parameter is update by the actions enable/disable and not through a PUT operation
+ * gpgcheck *(optional)*: Indicates if a GPG signature check on the +packages gotten from repository should be performed: + * false: Do not check GPG signature + * true: Check GPG signature + * gpgkey *(optional)*: URL pointing to the ASCII-armored GPG key +file for the repository. This option is used if yum needs a public key +to verify a package and the required key hasn't been imported into the +RPM database. + +**Actions (POST):** + +* enable: Enable the Repository as package source +* disable: Disable the Repository as package source

Define Repositories collection and Repository resource according to API.md Update API.json Activate auth support to new collection Signed-off-by: Paulo Vital <pvital@linux.vnet.ibm.com> --- src/kimchi/API.json | 63 ++++++++++++++++++++++++++++++++++++++++++++++ src/kimchi/control/host.py | 21 ++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/src/kimchi/API.json b/src/kimchi/API.json index 08c77c5..db9216c 100644 --- a/src/kimchi/API.json +++ b/src/kimchi/API.json @@ -359,6 +359,69 @@ "graphics": { "$ref": "#/kimchitype/graphics" } }, "additionalProperties": false + }, + "repository_create": { + "type": "object", + "properties": { + "repo_id": { + "description": "Unique repository name for each repository, one word.", + "type": "string" + }, + "baseurl": { + "description": "URL to the directory where the repodata directory of a repository is located. Can be an http://, ftp:// or file:// URL.", + "type": "string", + "required": true + }, + "is_mirror": { + "description": "Set the given URI of baseurl as a mirror list", + "type": "boolean" + }, + "url_args": { + "description": "Arguments to be passed to baseurl, like the list of APT repositories provided by the same baseurl.", + "type": "string" + }, + "gpgkey": { + "description": "URL pointing to the ASCII-armored GPG key file for the repository.", + "type": "string" + } + } + }, + "repository_update": { + "type": "object", + "properties": { + "repo_id": { + "description": "Unique repository name for each repository, one word.", + "type": "string" + }, + "repo_name": { + "description": "Human-readable string describing the repository.", + "type": "string" + }, + "baseurl": { + "description": "URL to the directory where the repodata directory of a repository is located. Can be an http://, ftp:// or file:// URL.", + "type": "string" + }, + "is_mirror": { + "description": "Set the given URI of baseurl as a mirror list", + "type": "boolean" + }, + "url_args": { + "description": "Arguments to be passed to baseurl, like the list of APT repositories provided by the same baseurl.", + "type": "string" + }, + "enabled": { + "description": "Indicates if repository should be included as a package source, or not.", + "type": "boolean" + }, + "gpgcheck": { + "description": "Indicates if a GPG signature check on the packages gotten from repository should be performed.", + "type": "boolean" + }, + "gpgkey": { + "description": "URL pointing to the ASCII-armored GPG key file for the repository.", + "type": "string" + } + } } } } diff --git a/src/kimchi/control/host.py b/src/kimchi/control/host.py index 053c822..12c998e 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.repositories = Repositories(self.model) @property def data(self): @@ -61,3 +62,23 @@ class Partition(Resource): @property def data(self): return self.info + + +class Repositories(Collection): + def __init__(self, model): + super(Repositories, self).__init__(model) + self.resource = Repository + + +class Repository(Resource): + def __init__(self, model, id): + super(Repository, self).__init__(model, id) + self.update_params = ["repo_id", "repo_name", "baseurl", "mirrors", + "url_args", "enabled", "gpgcheck", "gpgkey"] + self.uri_fmt = "/host/repositories/%s" + self.enable = self.generate_action_handler('enable') + self.disable = self.generate_action_handler('disable') + + @property + def data(self): + return self.info -- 1.8.3.1

On 02/10/2014 11:46 AM, Paulo Vital wrote:
Define Repositories collection and Repository resource according to API.md Update API.json Activate auth support to new collection
Signed-off-by: Paulo Vital <pvital@linux.vnet.ibm.com> --- src/kimchi/API.json | 63 ++++++++++++++++++++++++++++++++++++++++++++++ src/kimchi/control/host.py | 21 ++++++++++++++++ 2 files changed, 84 insertions(+)
diff --git a/src/kimchi/API.json b/src/kimchi/API.json index 08c77c5..db9216c 100644 --- a/src/kimchi/API.json +++ b/src/kimchi/API.json @@ -359,6 +359,69 @@ "graphics": { "$ref": "#/kimchitype/graphics" } }, "additionalProperties": false + }, + "repository_create": { + "type": "object", + "properties": { + "repo_id": { + "description": "Unique repository name for each repository, one word.", + "type": "string" + }, + "baseurl": { + "description": "URL to the directory where the repodata directory of a repository is located. Can be an http://, ftp:// or file:// URL.", + "type": "string", + "required": true + }, + "is_mirror": { + "description": "Set the given URI of baseurl as a mirror list", + "type": "boolean" + }, + "url_args": { + "description": "Arguments to be passed to baseurl, like the list of APT repositories provided by the same baseurl.", + "type": "string" + }, + "gpgkey": { + "description": "URL pointing to the ASCII-armored GPG key file for the repository.", + "type": "string" + } + } + }, + "repository_update": { + "type": "object", + "properties": { + "repo_id": { + "description": "Unique repository name for each repository, one word.", + "type": "string" + }, + "repo_name": { + "description": "Human-readable string describing the repository.", + "type": "string" + }, + "baseurl": { + "description": "URL to the directory where the repodata directory of a repository is located. Can be an http://, ftp:// or file:// URL.", + "type": "string" + }, + "is_mirror": { + "description": "Set the given URI of baseurl as a mirror list", + "type": "boolean" + }, + "url_args": { + "description": "Arguments to be passed to baseurl, like the list of APT repositories provided by the same baseurl.", + "type": "string" + },
+ "enabled": { + "description": "Indicates if repository should be included as a package source, or not.", + "type": "boolean" + },
It will be update through actions enable/disable instead of PUT method.
+ "gpgcheck": { + "description": "Indicates if a GPG signature check on the packages gotten from repository should be performed.", + "type": "boolean" + }, + "gpgkey": { + "description": "URL pointing to the ASCII-armored GPG key file for the repository.", + "type": "string" + } + } } } } diff --git a/src/kimchi/control/host.py b/src/kimchi/control/host.py index 053c822..12c998e 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.repositories = Repositories(self.model)
@property def data(self): @@ -61,3 +62,23 @@ class Partition(Resource): @property def data(self): return self.info + + +class Repositories(Collection): + def __init__(self, model): + super(Repositories, self).__init__(model) + self.resource = Repository + + +class Repository(Resource): + def __init__(self, model, id): + super(Repository, self).__init__(model, id) + self.update_params = ["repo_id", "repo_name", "baseurl", "mirrors", + "url_args", "enabled", "gpgcheck", "gpgkey"] + self.uri_fmt = "/host/repositories/%s" + self.enable = self.generate_action_handler('enable') + self.disable = self.generate_action_handler('disable') + + @property + def data(self): + return self.info

Update model and mockmodel to support backend opertions. Add new file implementing backend operations. There are two new classes: 1) Repositories (object): Class to represent and operate with repositories information in Kimchi's perspective. It's agnostic to host;s package management system, and can execute all operations necessary: add repository, get all repositories list, get information about one repository, update a repository, enable and disable a repository and remove a repository. This class will load in runtime the necessary classes to work with the host's package management: YumRepo for YUM systems based and AptRepo for APT systems based (support for this last one will be submited in a close future); 2) YumRepo (object): Class to represent and operate with YUM repositories. Loaded only on those systems that supports YUM, it's responsible to connect, collect and provide information of YUM repositories in the system. Also it's responsible to create/delete the files in disk to maintain the repositories in system after disconnection. Signed-off-by: Paulo Vital <pvital@linux.vnet.ibm.com> --- src/kimchi/mockmodel.py | 34 ++++ src/kimchi/model/host.py | 50 +++++- src/kimchi/repositories.py | 418 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 501 insertions(+), 1 deletion(-) create mode 100644 src/kimchi/repositories.py diff --git a/src/kimchi/mockmodel.py b/src/kimchi/mockmodel.py index 4e276eb..1ae97a2 100644 --- a/src/kimchi/mockmodel.py +++ b/src/kimchi/mockmodel.py @@ -51,6 +51,7 @@ from kimchi.model.storagepools import ISO_POOL_NAME, STORAGE_SOURCES from kimchi.model.utils import get_vm_name from kimchi.model.vms import VM_STATIC_UPDATE_PARAMS from kimchi.objectstore import ObjectStore +from kimchi.repositories import Repositories from kimchi.screenshot import VMScreenshot from kimchi.utils import template_name_from_uri, pool_name_from_uri from kimchi.vmtemplate import VMTemplate @@ -77,6 +78,7 @@ class MockModel(object): self._mock_interfaces = self.dummy_interfaces() self.next_taskid = 1 self.storagepool_activate('default') + self.host_repositories = Repositories() def _static_vm_update(self, dom, params): state = dom.info['state'] @@ -665,6 +667,38 @@ class MockModel(object): 'display_proxy_port': kconfig.get('display', 'display_proxy_port')} + def repositories_get_list(self): + return [repo_id for repo_id in + self.host_repositories.getRepositories().keys()] + + def repositories_create(self, params): + repo_id = params.get('repo_id') + if repo_id in self.repositories_get_list(): + raise InvalidOperation("Repository %s already exists." % repo_id) + self.host_repositories.addRepository(params) + return repo_id + + def repository_lookup(self, repo_id): + return self.host_repositories.getRepository(repo_id) + + def repository_delete(self, repo_id): + return self.host_repositories.removeRepository(repo_id) + + def repository_enable(self, repo_id): + if not self.host_repositories.enableRepository(repo_id): + raise OperationFailed("Could not enable repository %s." % repo_id) + + def repository_disable(self, repo_id): + if not self.host_repositories.disableRepository(repo_id): + raise OperationFailed("Could not disable repository %s." % repo_id) + + def repository_update(self, repo_id, params): + try: + self.host_repositories.updateRepository(repo_id, params) + except: + raise OperationFailed("Could not update repository %s." % repo_id) + return repo_id + class MockVMTemplate(VMTemplate): def __init__(self, args, mockmodel_inst=None): diff --git a/src/kimchi/model/host.py b/src/kimchi/model/host.py index a3d9e38..9b8b250 100644 --- a/src/kimchi/model/host.py +++ b/src/kimchi/model/host.py @@ -31,8 +31,9 @@ from cherrypy.process.plugins import BackgroundTask from kimchi import disks from kimchi import netinfo from kimchi.basemodel import Singleton -from kimchi.exception import NotFoundError, OperationFailed +from kimchi.exception import InvalidOperation, NotFoundError, OperationFailed from kimchi.model.vms import DOM_STATE_MAP +from kimchi.repositories import Repositories from kimchi.utils import kimchi_log @@ -199,3 +200,50 @@ class PartitionModel(object): raise NotFoundError("Partition %s not found in the host" % name) return disks.get_partition_details(name) + + +class RepositoriesModel(object): + __metaclass__ = Singleton + + def __init__(self, **kargs): + self.host_repositories = Repositories() + + def get_list(self): + return [repo_id for repo_id in + self.host_repositories.getRepositories().keys()] + + def create(self, params): + repo_id = params.get('repo_id') + if repo_id in self.get_list(): + raise InvalidOperation("Repository %s already exists." % repo_id) + self.host_repositories.addRepository(params) + return repo_id + + def get_repositories(self): + return self.host_repositories + + +class RepositoryModel(object): + def __init__(self, **kargs): + self._repositories = RepositoriesModel().get_repositories() + + def lookup(self, repo_id): + return self._repositories.getRepository(repo_id) + + def enable(self, repo_id): + if not self._repositories.enableRepository(repo_id): + raise OperationFailed("Could not enable repository %s." % repo_id) + + def disable(self, repo_id): + if not self._repositories.disableRepository(repo_id): + raise OperationFailed("Could not disable repository %s." % repo_id) + + def update(self, repo_id, params): + try: + self._repositories.updateRepository(repo_id, params) + except: + raise OperationFailed("Could not update repository %s." % repo_id) + return repo_id + + def delete(self, repo_id): + return self._repositories.removeRepository(repo_id) diff --git a/src/kimchi/repositories.py b/src/kimchi/repositories.py new file mode 100644 index 0000000..f2b5105 --- /dev/null +++ b/src/kimchi/repositories.py @@ -0,0 +1,418 @@ +# +# Project Kimchi +# +# Copyright IBM, Corp. 2014 +# +# Authors: +# Paulo Vital <pvital@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 os +import platform + +from kimchi.exception import InvalidParameter, OperationFailed, NotFoundError + +YUM_DISTROS = ['fedora', 'red hat enterprise linux', + 'red hat enterprise linux server', 'opensuse ', + 'suse linux enterprise server '] +APT_DISTROS = ['debian', 'ubuntu'] + + +class Repositories (object): + """ + Class to represent and operate with repositories information. + """ + def __init__(self): + # This stores all repositories for Kimchi perspective. It's a + # dictionary of dictionaries, in the format {<repo_id>: {repo}}, + # where: + # repo = {'repo_id': <string>, 'repo_name': <string>, + # 'baseurl': ([<string>], None), 'url_args': ([<string>, None), + # 'enabled': True/False, 'gpgcheck': True/False, + # 'gpgkey': ([<string>], None), + # 'mirrors': ([<string>], None) + # } + self._repo_storage = {} + + self._distro = platform.linux_distribution()[0].lower() + if (self._distro in YUM_DISTROS): + self._pkg_mnger = YumRepo() + elif (self._distro in APT_DISTROS): + self._pkg_mnger = AptRepo() + else: + self._pkg_mnger = None + + if self._pkg_mnger: + # update the self._repo_storage with system's repositories + self._scanSystemRepositories() + + def _scanSystemRepositories(self): + """ + Update repositories._repo_storage with system's (host) repositories. + """ + # Call system pkg_mnger to get the repositories as list of dict. + for repo in self._pkg_mnger.getRepositoriesList(): + self.addRepository(repo) + + def addRepository(self, params={}): + """ + Add and enable a new repository into repositories._repo_storage. + """ + if not params: + raise InvalidParameter("No parameters to add repository.") + + # Check if already exist a repository with the same repo_id. + repo_id = params.get('repo_id') + if repo_id in self.getRepositories(): + raise OperationFailed("Repository %s already exists." % repo_id) + + # Create and enable the repository + repo = {'repo_id': repo_id, + 'repo_name': params.get('repo_name', repo_id), + 'baseurl': params.get('baseurl', None), + 'url_args': params.get('url_args', None), + 'enabled': True, + 'gpgkey': params.get('gpgkey', None), + 'mirrors': params.get('mirrors', None)} + + if repo['gpgkey'] != None: + repo['gpgcheck'] = True + else: + repo['gpgcheck'] = False + + self._repo_storage[repo_id] = repo + + # Check in self._pkg_mnger if the repository already exists there + if not repo_id in [irepo['repo_id'] for irepo in + self._pkg_mnger.getRepositoriesList()]: + self._pkg_mnger.addRepo(repo) + + def getRepositories(self): + """ + Return a dictionary with all Kimchi's repositories. Each element uses + the format {<repo_id>: {repo}}, where repo is a dictionary in the + repositories.Repositories() format. + """ + return self._repo_storage + + def getRepository(self, repo_id): + """ + Return a dictionary with all info from a given repository ID. + """ + if not repo_id in self._repo_storage.keys(): + raise NotFoundError("Repository %s does not exists." % repo_id) + + repo = self._repo_storage[repo_id] + if (isinstance(repo['baseurl'], list)) and (len(repo['baseurl']) > 0): + repo['baseurl'] = repo['baseurl'][0] + if isinstance(repo['mirrors'], list) and (len(repo['mirrors']) > 0): + repo['mirrors'] = repo['mirrors'][0] + + return repo + + def getRepositoryFromPkgMnger(self, repo_id): + """ + Return a dictionary with all info from a given repository ID. + All info come from self._pkg_mnger.getRepo(). + """ + return self._pkg_mnger.getRepo(repo_id) + + def enabledRepositories(self): + """ + Return a list with enabled repositories IDs. + """ + enabled_repos = [] + for repo_id in self._repo_storage.keys(): + if self._repo_storage[repo_id]['enabled']: + enabled_repos.append(repo_id) + return enabled_repos + + def enableRepository(self, repo_id): + """ + Enable a repository. + """ + # Check if repo_id is already enabled + if repo_id in self.enabledRepositories(): + raise NotFoundError("There is no disabled repository called %s." % + repo_id) + + try: + repo = self.getRepository(repo_id) + repo['enabled'] = True + self.updateRepository(repo_id, repo) + self._pkg_mnger.enableRepo(repo_id) + return True + except: + raise OperationFailed("Could not enable repository %s" % repo_id) + + def disableRepository(self, repo_id): + """ + Disable a given repository. + """ + # Check if repo_id is already disabled + if not repo_id in self.enabledRepositories(): + raise NotFoundError("There is no enabled repository called %s." % + repo_id) + + try: + repo = self.getRepository(repo_id) + repo['enabled'] = False + self.updateRepository(repo_id, repo) + self._pkg_mnger.disableRepo(repo_id) + return True + except: + raise OperationFailed("Could not disable repository %s" % repo_id) + + def updateRepository(self, repo_id, new_repo={}): + """ + Update the information of a given repository. + The input is the repo_id of the repository to be updated and a dict + with the information to be updated. + """ + if (len(new_repo) == 0): + raise InvalidParameter("No parameters to update repository.") + + repo = self._repo_storage[repo_id] + repo.update(new_repo) + + del self._repo_storage[repo_id] + self._repo_storage[repo_id] = repo + self._pkg_mnger.updateRepo(repo_id, self._repo_storage[repo_id]) + + def removeRepository(self, repo_id): + """ + Remove a given repository + """ + if not repo_id in self._repo_storage.keys(): + raise NotFoundError("There is no repository called %s." % repo_id) + + del self._repo_storage[repo_id] + self._pkg_mnger.removeRepo(repo_id) + return True + + +class YumRepo (object): + """ + Class to represent and operate with YUM repositories. + It's loaded only on those systems listed at YUM_DISTROS and loads necessary + modules in runtime. + """ + def __init__(self): + self._yb = getattr(__import__('yum'), 'YumBase')() + self._repos = self._yb.repos + self._conf = self._yb.conf + self._enabled_repos = self._repos.listEnabled() + + def getRepositoriesList(self): + """ + Return a list of dictionaries in the repositories.Repositories() format + """ + repo_list = [] + for repo in self.enabledRepos(): + irepo = {} + irepo['repo_id'] = repo.id + irepo['repo_name'] = repo.name + irepo['baseurl'] = repo.baseurl + irepo['mirrors'] = repo.mirrorlist + irepo['url_args'] = None, + irepo['enabled'] = repo.enabled + irepo['gpgcheck'] = repo.gpgcheck + irepo['gpgkey'] = repo.gpgkey + repo_list.append(irepo) + return repo_list + + def addRepo(self, repo={}): + """ + Add a given repository in repositories.Repositories() format to YumBase + """ + if len(repo) == 0: + raise InvalidParameter("No parameters to add repository.") + + # At least one base url, or one mirror, must be given. + # baseurls must be a list of strings specifying the urls + # mirrorlist must be a list of strings specifying a list of mirrors + # Here we creates the lists, or set as None + if not repo['baseurl']: + baseurl = [] + else: + baseurl = [repo['baseurl']] + + if not repo['mirrors']: + mirrors = None + else: + mirrors = repo['mirrors'] + + self._yb.add_enable_repo(repo['repo_id'], baseurl, mirrors, + name=repo['repo_name'], + gpgcheck=repo['gpgcheck'], + gpgkey=[repo['gpgkey']]) + + repo['baseurl'] = baseurl + repo['mirrors'] = mirrors + # write a repo file in the system with repo{} information. + self._write2disk(repo) + + def getRepo(self, repo_id): + """ + Return a dictionary in the repositories.Repositories() of the given + repository ID format with the information of a YumRepository object. + """ + try: + repo = self._repos.getRepo(repo_id) + irepo = {} + irepo['repo_id'] = repo.id + irepo['repo_name'] = repo.name + irepo['baseurl'] = repo.baseurl + irepo['mirrors'] = repo.mirrorlist + irepo['url_args'] = None, + irepo['enabled'] = repo.enabled + irepo['gpgcheck'] = repo.gpgcheck + irepo['gpgkey'] = repo.gpgkey + return irepo + except: + raise OperationFailed("Repository %s does not exists." % repo_id) + + def enabledRepos(self): + """ + Return a list with enabled YUM repositories IDs + """ + return self._enabled_repos + + def isRepoEnable(self, repo_id): + """ + Return if a given repository ID is enabled or not + """ + for repo in self.enabledRepos(): + if repo_id == repo.id: + return True + return False + + def enableRepo(self, repo_id): + """ + Enable a given repository + """ + try: + self._repos.getRepo(repo_id).enable() + self._repos.doSetup() + return True + except: + raise OperationFailed("Could not enable repository %s." % repo_id) + + def disableRepo(self, repo_id): + """ + Disable a given repository + """ + try: + self._repos.getRepo(repo_id).disable() + self._repos.doSetup() + return True + except: + raise OperationFailed("Could not disable repository %s." % repo_id) + + def updateRepo(self, repo_id, repo={}): + """ + Update a given repository in repositories.Repositories() format + """ + if len(repo) == 0: + raise InvalidParameter("No parameters to update repository.") + + self._repos.delete(repo_id) + self.addRepo(repo) + + def removeRepo(self, repo_id): + """ + Remove a given repository + """ + self._repos.delete(repo_id) + self._removefromdisk(repo_id) + + def _write2disk(self, repo={}): + """ + Write repository info into disk. + """ + # Get a list with all reposdir configured in system's YUM. + conf_dir = self._conf.reposdir + if not conf_dir: + raise NotFoundError("There is no YUM configuration directory.") + + # Generate the content to be wrote. + repo_content = '[%s]\n' % repo['repo_id'] + repo_content = repo_content + 'name=%s\n' % repo['repo_name'] + + if not repo['baseurl']: + if isinstance(repo['mirrors'], list): + link = repo['mirrors'][0] + else: + link = repo['mirrors'] + repo_content = repo_content + 'mirrorlist=%s\n' % link + else: + if isinstance(repo['baseurl'], list): + link = repo['baseurl'][0] + else: + link = repo['baseurl'] + repo_content = repo_content + 'baseurl=%s\n' % link + + if repo['enabled']: + repo_content = repo_content + 'enabled=1\n' + else: + repo_content = repo_content + 'enabled=0\n' + + if repo['gpgcheck']: + repo_content = repo_content + 'gpgcheck=1\n' + else: + repo_content = repo_content + 'gpgcheck=0\n' + + if repo['gpgkey']: + if isinstance(repo['gpgkey'], list): + link = repo['gpgkey'][0] + else: + link = repo['gpgkey'] + repo_content = repo_content + 'gpgckey=%s\n' % link + + # Scan for the confdirs and write the file in the first available + # directory in the system. YUM will scan each confdir for repo files + # and load it contents, so we can write in the first available dir. + for dir in conf_dir: + if os.path.isdir(dir): + repo_file = dir + '/%s.repo' % repo['repo_id'] + if os.path.isfile(repo_file): + os.remove(repo_file) + + try: + with open(repo_file, 'w') as fd: + fd.write(repo_content) + fd.close() + except: + raise OperationFailed("Could not write repo file %s" % + repo_file) + break + return True + + def _removefromdisk(self, repo_id): + """ + Delete the repo file from disk of a given repository + """ + conf_dir = self._conf.reposdir + if not conf_dir: + raise NotFoundError("There is no YUM configuration directory.") + + for dir in conf_dir: + if os.path.isdir(dir): + repo_file = dir + '/%s.repo' % repo_id + if os.path.isfile(repo_file): + os.remove(repo_file) + + return True -- 1.8.3.1

On 02/10/2014 11:46 AM, Paulo Vital wrote:
Update model and mockmodel to support backend opertions. Add new file implementing backend operations. There are two new classes:
1) Repositories (object): Class to represent and operate with repositories information in Kimchi's perspective. It's agnostic to host;s package management system, and can execute all operations necessary: add repository, get all repositories list, get information about one repository, update a repository, enable and disable a repository and remove a repository. This class will load in runtime the necessary classes to work with the host's package management: YumRepo for YUM systems based and AptRepo for APT systems based (support for this last one will be submited in a close future);
2) YumRepo (object): Class to represent and operate with YUM repositories. Loaded only on those systems that supports YUM, it's responsible to connect, collect and provide information of YUM repositories in the system. Also it's responsible to create/delete the files in disk to maintain the repositories in system after disconnection.
Signed-off-by: Paulo Vital <pvital@linux.vnet.ibm.com> --- src/kimchi/mockmodel.py | 34 ++++ src/kimchi/model/host.py | 50 +++++- src/kimchi/repositories.py | 418 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 501 insertions(+), 1 deletion(-) create mode 100644 src/kimchi/repositories.py
diff --git a/src/kimchi/mockmodel.py b/src/kimchi/mockmodel.py index 4e276eb..1ae97a2 100644 --- a/src/kimchi/mockmodel.py +++ b/src/kimchi/mockmodel.py @@ -51,6 +51,7 @@ from kimchi.model.storagepools import ISO_POOL_NAME, STORAGE_SOURCES from kimchi.model.utils import get_vm_name from kimchi.model.vms import VM_STATIC_UPDATE_PARAMS from kimchi.objectstore import ObjectStore +from kimchi.repositories import Repositories from kimchi.screenshot import VMScreenshot from kimchi.utils import template_name_from_uri, pool_name_from_uri from kimchi.vmtemplate import VMTemplate @@ -77,6 +78,7 @@ class MockModel(object): self._mock_interfaces = self.dummy_interfaces() self.next_taskid = 1 self.storagepool_activate('default') + self.host_repositories = Repositories()
def _static_vm_update(self, dom, params): state = dom.info['state'] @@ -665,6 +667,38 @@ class MockModel(object): 'display_proxy_port': kconfig.get('display', 'display_proxy_port')}
+ def repositories_get_list(self): + return [repo_id for repo_id in + self.host_repositories.getRepositories().keys()] +
You should return default values here to be able to add tests for this feature. As you did for update feature.
+ def repositories_create(self, params): + repo_id = params.get('repo_id')
If the repo_id was not provided by the user you should create one for him
+ if repo_id in self.repositories_get_list(): + raise InvalidOperation("Repository %s already exists." % repo_id) + self.host_repositories.addRepository(params) + return repo_id + + def repository_lookup(self, repo_id): + return self.host_repositories.getRepository(repo_id) + + def repository_delete(self, repo_id): + return self.host_repositories.removeRepository(repo_id) + + def repository_enable(self, repo_id): + if not self.host_repositories.enableRepository(repo_id): + raise OperationFailed("Could not enable repository %s." % repo_id) + + def repository_disable(self, repo_id): + if not self.host_repositories.disableRepository(repo_id): + raise OperationFailed("Could not disable repository %s." % repo_id) + + def repository_update(self, repo_id, params): + try: + self.host_repositories.updateRepository(repo_id, params) + except: + raise OperationFailed("Could not update repository %s." % repo_id) + return repo_id +
class MockVMTemplate(VMTemplate): def __init__(self, args, mockmodel_inst=None): diff --git a/src/kimchi/model/host.py b/src/kimchi/model/host.py index a3d9e38..9b8b250 100644 --- a/src/kimchi/model/host.py +++ b/src/kimchi/model/host.py @@ -31,8 +31,9 @@ from cherrypy.process.plugins import BackgroundTask from kimchi import disks from kimchi import netinfo from kimchi.basemodel import Singleton -from kimchi.exception import NotFoundError, OperationFailed +from kimchi.exception import InvalidOperation, NotFoundError, OperationFailed from kimchi.model.vms import DOM_STATE_MAP +from kimchi.repositories import Repositories from kimchi.utils import kimchi_log
@@ -199,3 +200,50 @@ class PartitionModel(object): raise NotFoundError("Partition %s not found in the host" % name) return disks.get_partition_details(name) + + +class RepositoriesModel(object): + __metaclass__ = Singleton + + def __init__(self, **kargs): + self.host_repositories = Repositories() + + def get_list(self): + return [repo_id for repo_id in + self.host_repositories.getRepositories().keys()] + + def create(self, params): + repo_id = params.get('repo_id')
Same here. You need to create a repo_id if user does not provide one
+ if repo_id in self.get_list(): + raise InvalidOperation("Repository %s already exists." % repo_id) + self.host_repositories.addRepository(params) + return repo_id + + def get_repositories(self): + return self.host_repositories + + +class RepositoryModel(object): + def __init__(self, **kargs): + self._repositories = RepositoriesModel().get_repositories() + + def lookup(self, repo_id): + return self._repositories.getRepository(repo_id) + + def enable(self, repo_id): + if not self._repositories.enableRepository(repo_id): + raise OperationFailed("Could not enable repository %s." % repo_id) + + def disable(self, repo_id): + if not self._repositories.disableRepository(repo_id): + raise OperationFailed("Could not disable repository %s." % repo_id) + + def update(self, repo_id, params): + try: + self._repositories.updateRepository(repo_id, params) + except: + raise OperationFailed("Could not update repository %s." % repo_id) + return repo_id + + def delete(self, repo_id): + return self._repositories.removeRepository(repo_id) diff --git a/src/kimchi/repositories.py b/src/kimchi/repositories.py new file mode 100644 index 0000000..f2b5105 --- /dev/null +++ b/src/kimchi/repositories.py @@ -0,0 +1,418 @@ +# +# Project Kimchi +# +# Copyright IBM, Corp. 2014 +# +# Authors: +# Paulo Vital <pvital@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 os +import platform + +from kimchi.exception import InvalidParameter, OperationFailed, NotFoundError + +YUM_DISTROS = ['fedora', 'red hat enterprise linux', + 'red hat enterprise linux server', 'opensuse ', + 'suse linux enterprise server '] +APT_DISTROS = ['debian', 'ubuntu'] + + +class Repositories (object): + """ + Class to represent and operate with repositories information. + """ + def __init__(self): + # This stores all repositories for Kimchi perspective. It's a + # dictionary of dictionaries, in the format {<repo_id>: {repo}}, + # where: + # repo = {'repo_id': <string>, 'repo_name': <string>, + # 'baseurl': ([<string>], None), 'url_args': ([<string>, None), + # 'enabled': True/False, 'gpgcheck': True/False, + # 'gpgkey': ([<string>], None), + # 'mirrors': ([<string>], None) + # }
From API.md we have: * repo_id:... * repo_name:... * baseurl:... * url_args: ... * enabled: ... * gpgcheck: ... * gpgkey: ... So you have return the same keys
+ self._repo_storage = {} + + self._distro = platform.linux_distribution()[0].lower() + if (self._distro in YUM_DISTROS): + self._pkg_mnger = YumRepo() + elif (self._distro in APT_DISTROS): + self._pkg_mnger = AptRepo() + else:
+ self._pkg_mnger = None
We need to raise an error is this case.
+ + if self._pkg_mnger: + # update the self._repo_storage with system's repositories + self._scanSystemRepositories() + + def _scanSystemRepositories(self): + """ + Update repositories._repo_storage with system's (host) repositories. + """ + # Call system pkg_mnger to get the repositories as list of dict. + for repo in self._pkg_mnger.getRepositoriesList(): + self.addRepository(repo) + + def addRepository(self, params={}): + """ + Add and enable a new repository into repositories._repo_storage. + """ + if not params: + raise InvalidParameter("No parameters to add repository.")
MissingParameter()
+ + # Check if already exist a repository with the same repo_id. + repo_id = params.get('repo_id') + if repo_id in self.getRepositories(): + raise OperationFailed("Repository %s already exists." % repo_id)
InvalidParameter()
+ + # Create and enable the repository + repo = {'repo_id': repo_id, + 'repo_name': params.get('repo_name', repo_id), + 'baseurl': params.get('baseurl', None), + 'url_args': params.get('url_args', None), + 'enabled': True, + 'gpgkey': params.get('gpgkey', None), + 'mirrors': params.get('mirrors', None)} + + if repo['gpgkey'] != None: + repo['gpgcheck'] = True + else: + repo['gpgcheck'] = False + + self._repo_storage[repo_id] = repo + + # Check in self._pkg_mnger if the repository already exists there + if not repo_id in [irepo['repo_id'] for irepo in + self._pkg_mnger.getRepositoriesList()]: + self._pkg_mnger.addRepo(repo) + + def getRepositories(self): + """ + Return a dictionary with all Kimchi's repositories. Each element uses + the format {<repo_id>: {repo}}, where repo is a dictionary in the + repositories.Repositories() format. + """ + return self._repo_storage + + def getRepository(self, repo_id): + """ + Return a dictionary with all info from a given repository ID. + """ + if not repo_id in self._repo_storage.keys(): + raise NotFoundError("Repository %s does not exists." % repo_id) + + repo = self._repo_storage[repo_id] + if (isinstance(repo['baseurl'], list)) and (len(repo['baseurl']) > 0): + repo['baseurl'] = repo['baseurl'][0] + if isinstance(repo['mirrors'], list) and (len(repo['mirrors']) > 0): + repo['mirrors'] = repo['mirrors'][0] +
From API.md we have: * repo_id:... * repo_name:... * baseurl:... * url_args: ... * enabled: ... * gpgcheck: ... * gpgkey: ... So you have return the same keys
+ return repo + + def getRepositoryFromPkgMnger(self, repo_id): + """ + Return a dictionary with all info from a given repository ID. + All info come from self._pkg_mnger.getRepo(). + """ + return self._pkg_mnger.getRepo(repo_id) + + def enabledRepositories(self): + """ + Return a list with enabled repositories IDs. + """ + enabled_repos = [] + for repo_id in self._repo_storage.keys(): + if self._repo_storage[repo_id]['enabled']: + enabled_repos.append(repo_id) + return enabled_repos + + def enableRepository(self, repo_id): + """ + Enable a repository. + """ + # Check if repo_id is already enabled + if repo_id in self.enabledRepositories(): + raise NotFoundError("There is no disabled repository called %s." % + repo_id) + + try: + repo = self.getRepository(repo_id) + repo['enabled'] = True + self.updateRepository(repo_id, repo) + self._pkg_mnger.enableRepo(repo_id) + return True + except: + raise OperationFailed("Could not enable repository %s" % repo_id) + + def disableRepository(self, repo_id): + """ + Disable a given repository. + """ + # Check if repo_id is already disabled + if not repo_id in self.enabledRepositories(): + raise NotFoundError("There is no enabled repository called %s." % + repo_id) + + try: + repo = self.getRepository(repo_id) + repo['enabled'] = False + self.updateRepository(repo_id, repo) + self._pkg_mnger.disableRepo(repo_id) + return True + except: + raise OperationFailed("Could not disable repository %s" % repo_id) + + def updateRepository(self, repo_id, new_repo={}): + """ + Update the information of a given repository. + The input is the repo_id of the repository to be updated and a dict + with the information to be updated. + """ + if (len(new_repo) == 0): + raise InvalidParameter("No parameters to update repository.") + + repo = self._repo_storage[repo_id] + repo.update(new_repo) + + del self._repo_storage[repo_id] + self._repo_storage[repo_id] = repo + self._pkg_mnger.updateRepo(repo_id, self._repo_storage[repo_id]) + + def removeRepository(self, repo_id): + """ + Remove a given repository + """ + if not repo_id in self._repo_storage.keys(): + raise NotFoundError("There is no repository called %s." % repo_id) + + del self._repo_storage[repo_id] + self._pkg_mnger.removeRepo(repo_id) + return True + + +class YumRepo (object): + """ + Class to represent and operate with YUM repositories. + It's loaded only on those systems listed at YUM_DISTROS and loads necessary + modules in runtime. + """ + def __init__(self): + self._yb = getattr(__import__('yum'), 'YumBase')() + self._repos = self._yb.repos + self._conf = self._yb.conf + self._enabled_repos = self._repos.listEnabled() + + def getRepositoriesList(self): + """ + Return a list of dictionaries in the repositories.Repositories() format + """ + repo_list = [] + for repo in self.enabledRepos(): + irepo = {} + irepo['repo_id'] = repo.id + irepo['repo_name'] = repo.name + irepo['baseurl'] = repo.baseurl + irepo['mirrors'] = repo.mirrorlist + irepo['url_args'] = None, + irepo['enabled'] = repo.enabled + irepo['gpgcheck'] = repo.gpgcheck + irepo['gpgkey'] = repo.gpgkey + repo_list.append(irepo) + return repo_list + + def addRepo(self, repo={}): + """ + Add a given repository in repositories.Repositories() format to YumBase + """ + if len(repo) == 0: + raise InvalidParameter("No parameters to add repository.") + + # At least one base url, or one mirror, must be given. + # baseurls must be a list of strings specifying the urls + # mirrorlist must be a list of strings specifying a list of mirrors + # Here we creates the lists, or set as None + if not repo['baseurl']: + baseurl = []
The baseurl is a required parameter. Must always be there.
+ else: + baseurl = [repo['baseurl']] +
+ if not repo['mirrors']: + mirrors = None + else: + mirrors = repo['mirrors']
From API.md it should be 'is_mirror' that indicates if the 'baseurl' is a mirror or not.
+ + self._yb.add_enable_repo(repo['repo_id'], baseurl, mirrors, + name=repo['repo_name'], + gpgcheck=repo['gpgcheck'], + gpgkey=[repo['gpgkey']]) + + repo['baseurl'] = baseurl + repo['mirrors'] = mirrors + # write a repo file in the system with repo{} information. + self._write2disk(repo) + + def getRepo(self, repo_id): + """ + Return a dictionary in the repositories.Repositories() of the given + repository ID format with the information of a YumRepository object. + """ + try: + repo = self._repos.getRepo(repo_id) + irepo = {} + irepo['repo_id'] = repo.id + irepo['repo_name'] = repo.name + irepo['baseurl'] = repo.baseurl + irepo['mirrors'] = repo.mirrorlist + irepo['url_args'] = None, + irepo['enabled'] = repo.enabled + irepo['gpgcheck'] = repo.gpgcheck + irepo['gpgkey'] = repo.gpgkey + return irepo + except: + raise OperationFailed("Repository %s does not exists." % repo_id) + + def enabledRepos(self): + """ + Return a list with enabled YUM repositories IDs + """ + return self._enabled_repos + + def isRepoEnable(self, repo_id): + """ + Return if a given repository ID is enabled or not + """ + for repo in self.enabledRepos(): + if repo_id == repo.id: + return True + return False + + def enableRepo(self, repo_id): + """ + Enable a given repository + """ + try: + self._repos.getRepo(repo_id).enable() + self._repos.doSetup() + return True + except: + raise OperationFailed("Could not enable repository %s." % repo_id) + + def disableRepo(self, repo_id): + """ + Disable a given repository + """ + try: + self._repos.getRepo(repo_id).disable() + self._repos.doSetup() + return True + except: + raise OperationFailed("Could not disable repository %s." % repo_id) + + def updateRepo(self, repo_id, repo={}): + """ + Update a given repository in repositories.Repositories() format + """ + if len(repo) == 0: + raise InvalidParameter("No parameters to update repository.")
MissingParameter()
+ + self._repos.delete(repo_id) + self.addRepo(repo) + + def removeRepo(self, repo_id): + """ + Remove a given repository + """ + self._repos.delete(repo_id) + self._removefromdisk(repo_id) + + def _write2disk(self, repo={}): + """ + Write repository info into disk. + """ + # Get a list with all reposdir configured in system's YUM. + conf_dir = self._conf.reposdir + if not conf_dir: + raise NotFoundError("There is no YUM configuration directory.") + + # Generate the content to be wrote. + repo_content = '[%s]\n' % repo['repo_id'] + repo_content = repo_content + 'name=%s\n' % repo['repo_name'] + + if not repo['baseurl']: + if isinstance(repo['mirrors'], list): + link = repo['mirrors'][0] + else: + link = repo['mirrors'] + repo_content = repo_content + 'mirrorlist=%s\n' % link + else: + if isinstance(repo['baseurl'], list): + link = repo['baseurl'][0] + else: + link = repo['baseurl'] + repo_content = repo_content + 'baseurl=%s\n' % link + + if repo['enabled']: + repo_content = repo_content + 'enabled=1\n' + else: + repo_content = repo_content + 'enabled=0\n' + + if repo['gpgcheck']: + repo_content = repo_content + 'gpgcheck=1\n' + else: + repo_content = repo_content + 'gpgcheck=0\n' + + if repo['gpgkey']: + if isinstance(repo['gpgkey'], list): + link = repo['gpgkey'][0] + else: + link = repo['gpgkey'] + repo_content = repo_content + 'gpgckey=%s\n' % link + + # Scan for the confdirs and write the file in the first available + # directory in the system. YUM will scan each confdir for repo files + # and load it contents, so we can write in the first available dir. + for dir in conf_dir: + if os.path.isdir(dir): + repo_file = dir + '/%s.repo' % repo['repo_id'] + if os.path.isfile(repo_file): + os.remove(repo_file) + + try: + with open(repo_file, 'w') as fd: + fd.write(repo_content) + fd.close() + except: + raise OperationFailed("Could not write repo file %s" % + repo_file) + break + return True + + def _removefromdisk(self, repo_id): + """ + Delete the repo file from disk of a given repository + """ + conf_dir = self._conf.reposdir + if not conf_dir: + raise NotFoundError("There is no YUM configuration directory.") + + for dir in conf_dir: + if os.path.isdir(dir): + repo_file = dir + '/%s.repo' % repo_id + if os.path.isfile(repo_file): + os.remove(repo_file) + + return True

Update Makefile files to provide repositories support. Signed-off-by: Paulo Vital <pvital@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..3d4fd6e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -54,6 +54,7 @@ PEP8_WHITELIST = \ src/kimchi/iscsi.py \ src/kimchi/isoinfo.py \ src/kimchi/model/*.py \ + src/kimchi/repositories.py \ src/kimchi/rollbackcontext.py \ src/kimchi/root.py \ src/kimchi/server.py \ diff --git a/src/kimchi/Makefile.am b/src/kimchi/Makefile.am index 1653c0c..2608cb0 100644 --- a/src/kimchi/Makefile.am +++ b/src/kimchi/Makefile.am @@ -40,6 +40,7 @@ kimchi_PYTHON = \ networkxml.py \ objectstore.py \ osinfo.py \ + repositories.py \ rollbackcontext.py \ root.py \ scan.py \ -- 1.8.3.1

Reviewed-by: Aline Manera <alinefm@linux.vnet.ibm.com> On 02/10/2014 11:46 AM, Paulo Vital wrote:
Update Makefile files to provide repositories support.
Signed-off-by: Paulo Vital <pvital@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..3d4fd6e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -54,6 +54,7 @@ PEP8_WHITELIST = \ src/kimchi/iscsi.py \ src/kimchi/isoinfo.py \ src/kimchi/model/*.py \ + src/kimchi/repositories.py \ src/kimchi/rollbackcontext.py \ src/kimchi/root.py \ src/kimchi/server.py \ diff --git a/src/kimchi/Makefile.am b/src/kimchi/Makefile.am index 1653c0c..2608cb0 100644 --- a/src/kimchi/Makefile.am +++ b/src/kimchi/Makefile.am @@ -40,6 +40,7 @@ kimchi_PYTHON = \ networkxml.py \ objectstore.py \ osinfo.py \ + repositories.py \ rollbackcontext.py \ root.py \ scan.py \

Added unit tests into test_model.py and test_rest.py Signed-off-by: Paulo Vital <pvital@linux.vnet.ibm.com> --- tests/test_model.py | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/test_rest.py | 32 +++++++++++++++++ 2 files changed, 131 insertions(+) diff --git a/tests/test_model.py b/tests/test_model.py index 4a6d2fe..40c53c0 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -794,6 +794,105 @@ class ModelTests(unittest.TestCase): volumes = inst.storagevolumes_get_list(args['name']) self.assertEquals(len(volumes), 2) + def test_repository_create(self): + inst = model.Model('test:///default', + objstore_loc=self.tmp_store) + + system_host_repos = len(inst.repositories_get_list()) + + test_repos = [{'repo_id': 'fedora-fake', + 'baseurl': 'http://www.fedora.org'}, + {'repo_id': 'fedora-updates-fake', + 'mirrors': 'http://www.fedora.org/updates', + 'gpgkey' : 'file:///tmp/KEY-fedora-updates-fake-19'}] + + for repo in test_repos: + inst.repositories_create(repo) + host_repos = inst.repositories_get_list() + self.assertEquals(system_host_repos+len(test_repos), len(host_repos)) + + for repo in test_repos: + repo_info = inst.repository_lookup(repo.get('repo_id')) + self.assertEquals(repo.get('repo_id'), repo_info.get('repo_id')) + self.assertEquals(repo.get('baseurl', []), + repo_info.get('baseurl')) + self.assertEquals(repo.get('mirrors', None), + repo_info.get('mirrors')) + self.assertEquals(True, repo_info.get('enabled')) + + if 'gpgkey' in repo.keys(): + gpgcheck = True + else: + gpgcheck = False + + self.assertEquals(gpgcheck, repo_info.get('gpgcheck')) + + self.assertRaises(NotFoundError, inst.repository_lookup, 'google') + + # remove files created + for repo in test_repos: + inst.repository_delete(repo['repo_id']) + self.assertRaises(NotFoundError, + inst.repository_lookup, repo['repo_id']) + + def test_repository_update(self): + inst = model.Model('test:///default', + objstore_loc=self.tmp_store) + + system_host_repos = len(inst.repositories_get_list()) + + repo = {'repo_id': 'fedora-fake', + 'repo_name': 'Fedora 19 FAKE', + 'baseurl': 'http://www.fedora.org'} + inst.repositories_create(repo) + + host_repos = inst.repositories_get_list() + self.assertEquals(system_host_repos+1, len(host_repos)) + + new_repo = {'repo_id': 'fedora-fake', + 'repo_name': 'Fedora 19 Update FAKE', + 'baseurl': 'http://www.fedora.org/update'} + + inst.repository_update(repo['repo_id'], new_repo) + repo_info = inst.repository_lookup(new_repo.get('repo_id')) + self.assertEquals(new_repo.get('repo_id'), repo_info.get('repo_id')) + self.assertEquals(new_repo.get('repo_name'), + repo_info.get('repo_name')) + self.assertEquals(new_repo.get('baseurl', None), + repo_info.get('baseurl')) + self.assertEquals(True, repo_info.get('enabled')) + + # remove files creates + inst.repository_delete(repo['repo_id']) + + def test_repository_disable_enable(self): + inst = model.Model('test:///default', + objstore_loc=self.tmp_store) + + system_host_repos = len(inst.repositories_get_list()) + + repo = {'repo_id': 'fedora-fake', + 'repo_name': 'Fedora 19 FAKE', + 'baseurl': 'http://www.fedora.org'} + inst.repositories_create(repo) + + host_repos = inst.repositories_get_list() + self.assertEquals(system_host_repos+1, len(host_repos)) + + repo_info = inst.repository_lookup(repo.get('repo_id')) + self.assertEquals(True, repo_info.get('enabled')) + + inst.repository_disable(repo.get('repo_id')) + repo_info = inst.repository_lookup(repo.get('repo_id')) + self.assertEquals(False, repo_info.get('enabled')) + + inst.repository_enable(repo.get('repo_id')) + repo_info = inst.repository_lookup(repo.get('repo_id')) + self.assertEquals(True, repo_info.get('enabled')) + + # remove files creates + inst.repository_delete(repo['repo_id']) + class BaseModelTests(unittest.TestCase): class FoosModel(object): diff --git a/tests/test_rest.py b/tests/test_rest.py index 0ed293b..cae9e4f 100644 --- a/tests/test_rest.py +++ b/tests/test_rest.py @@ -1321,6 +1321,38 @@ class RestTests(unittest.TestCase): self.assertEquals(1, len(res)) self.assertEquals('test-vm1', res[0]['name']) + def test_repositories(self): + def verify_repo(t, res): + for field in ('repo_id', 'repo_name', 'baseurl', 'mirrors', + 'url_args', 'enabled', 'gpgcheck', 'gpgkey'): + self.assertEquals(t[field], res[field]) + + resp = self.request('/repositories') + self.assertEquals(200, resp.status) + self.assertEquals(0, len(json.loads(resp.read()))) + + # Create a repository + repo = {'repo_id': 'fedora-fake', + 'repo_name': 'Fedora 19 FAKE', + 'baseurl': 'http://www.fedora.org'} + req = json.dumps(repo) + resp = self.request('/repositories', req, 'POST') + self.assertEquals(201, resp.status) + + # Verify the repositorie + res = json.loads(self.request('/repositories/fedora-fake').read()) + verify_template(repo, res) + + # Update the repository + repo['baseurl'] = 'http://www.fedora.org/update' + req = json.dumps(repo) + resp = self.request('/repositories/%s' % repo['repo_id'], req, 'PUT') + self.assertEquals(200, resp.status) + + # Verify the template + res = json.loads(self.request('/repositories/fedora-fake').read()) + verify_template(repo, res) + class HttpsRestTests(RestTests): """ -- 1.8.3.1

On 02/10/2014 11:46 AM, Paulo Vital wrote:
Added unit tests into test_model.py and test_rest.py
Signed-off-by: Paulo Vital <pvital@linux.vnet.ibm.com> --- tests/test_model.py | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/test_rest.py | 32 +++++++++++++++++ 2 files changed, 131 insertions(+)
diff --git a/tests/test_model.py b/tests/test_model.py index 4a6d2fe..40c53c0 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -794,6 +794,105 @@ class ModelTests(unittest.TestCase): volumes = inst.storagevolumes_get_list(args['name']) self.assertEquals(len(volumes), 2)
+ def test_repository_create(self): + inst = model.Model('test:///default', + objstore_loc=self.tmp_store) + + system_host_repos = len(inst.repositories_get_list()) + + test_repos = [{'repo_id': 'fedora-fake', + 'baseurl': 'http://www.fedora.org'}, + {'repo_id': 'fedora-updates-fake', + 'mirrors': 'http://www.fedora.org/updates', + 'gpgkey' : 'file:///tmp/KEY-fedora-updates-fake-19'}] + + for repo in test_repos: + inst.repositories_create(repo) + host_repos = inst.repositories_get_list() + self.assertEquals(system_host_repos+len(test_repos), len(host_repos)) + + for repo in test_repos: + repo_info = inst.repository_lookup(repo.get('repo_id')) + self.assertEquals(repo.get('repo_id'), repo_info.get('repo_id')) + self.assertEquals(repo.get('baseurl', []), + repo_info.get('baseurl')) + self.assertEquals(repo.get('mirrors', None), + repo_info.get('mirrors')) + self.assertEquals(True, repo_info.get('enabled')) + + if 'gpgkey' in repo.keys(): + gpgcheck = True + else: + gpgcheck = False + + self.assertEquals(gpgcheck, repo_info.get('gpgcheck')) + + self.assertRaises(NotFoundError, inst.repository_lookup, 'google') + + # remove files created + for repo in test_repos: + inst.repository_delete(repo['repo_id']) + self.assertRaises(NotFoundError, + inst.repository_lookup, repo['repo_id']) + + def test_repository_update(self): + inst = model.Model('test:///default', + objstore_loc=self.tmp_store) + + system_host_repos = len(inst.repositories_get_list()) + + repo = {'repo_id': 'fedora-fake', + 'repo_name': 'Fedora 19 FAKE', + 'baseurl': 'http://www.fedora.org'} + inst.repositories_create(repo) + + host_repos = inst.repositories_get_list() + self.assertEquals(system_host_repos+1, len(host_repos)) + + new_repo = {'repo_id': 'fedora-fake', + 'repo_name': 'Fedora 19 Update FAKE', + 'baseurl': 'http://www.fedora.org/update'} + + inst.repository_update(repo['repo_id'], new_repo) + repo_info = inst.repository_lookup(new_repo.get('repo_id')) + self.assertEquals(new_repo.get('repo_id'), repo_info.get('repo_id')) + self.assertEquals(new_repo.get('repo_name'), + repo_info.get('repo_name')) + self.assertEquals(new_repo.get('baseurl', None), + repo_info.get('baseurl')) + self.assertEquals(True, repo_info.get('enabled')) + + # remove files creates + inst.repository_delete(repo['repo_id']) + + def test_repository_disable_enable(self): + inst = model.Model('test:///default', + objstore_loc=self.tmp_store) + + system_host_repos = len(inst.repositories_get_list()) + + repo = {'repo_id': 'fedora-fake', + 'repo_name': 'Fedora 19 FAKE', + 'baseurl': 'http://www.fedora.org'} + inst.repositories_create(repo) + + host_repos = inst.repositories_get_list() + self.assertEquals(system_host_repos+1, len(host_repos)) + + repo_info = inst.repository_lookup(repo.get('repo_id')) + self.assertEquals(True, repo_info.get('enabled')) + + inst.repository_disable(repo.get('repo_id')) + repo_info = inst.repository_lookup(repo.get('repo_id')) + self.assertEquals(False, repo_info.get('enabled')) + + inst.repository_enable(repo.get('repo_id')) + repo_info = inst.repository_lookup(repo.get('repo_id')) + self.assertEquals(True, repo_info.get('enabled')) + + # remove files creates + inst.repository_delete(repo['repo_id']) +
class BaseModelTests(unittest.TestCase): class FoosModel(object): diff --git a/tests/test_rest.py b/tests/test_rest.py index 0ed293b..cae9e4f 100644 --- a/tests/test_rest.py +++ b/tests/test_rest.py @@ -1321,6 +1321,38 @@ class RestTests(unittest.TestCase): self.assertEquals(1, len(res)) self.assertEquals('test-vm1', res[0]['name'])
+ def test_repositories(self): + def verify_repo(t, res): + for field in ('repo_id', 'repo_name', 'baseurl', 'mirrors', + 'url_args', 'enabled', 'gpgcheck', 'gpgkey'): + self.assertEquals(t[field], res[field]) + + resp = self.request('/repositories')
It should be /host/repositories, right?
+ self.assertEquals(200, resp.status) + self.assertEquals(0, len(json.loads(resp.read()))) + + # Create a repository + repo = {'repo_id': 'fedora-fake', + 'repo_name': 'Fedora 19 FAKE', + 'baseurl': 'http://www.fedora.org'} + req = json.dumps(repo) + resp = self.request('/repositories', req, 'POST') + self.assertEquals(201, resp.status) + + # Verify the repositorie + res = json.loads(self.request('/repositories/fedora-fake').read()) + verify_template(repo, res) + + # Update the repository + repo['baseurl'] = 'http://www.fedora.org/update' + req = json.dumps(repo) + resp = self.request('/repositories/%s' % repo['repo_id'], req, 'PUT') + self.assertEquals(200, resp.status) + + # Verify the template + res = json.loads(self.request('/repositories/fedora-fake').read()) + verify_template(repo, res) +
class HttpsRestTests(RestTests): """
participants (2)
-
Aline Manera
-
Paulo Vital