From e54e87c4339438fc50fe4a3db37e3d23a89e2337 Mon Sep 17 00:00:00 2001 From: Arnaud Patard Date: Wed, 26 Apr 2023 12:00:04 +0200 Subject: [PATCH] Use api to get driver schema file Current code is assuming that the molecule driver are inside a molecule_$driver python resource, which is not the case. For instance, vagrant plugin is in molecule_plugins. As a solution: - extend driver class to provide a schema_file() member, allowing to return a path to the driver.json file, - replace schema_v3 code to use molecule api and get the schema file path thanks to the new schema_file(). Signed-off-by: Arnaud Patard --- src/molecule/driver/base.py | 3 +++ src/molecule/driver/delegated.py | 5 +++++ src/molecule/model/schema_v3.py | 24 ++++++++++-------------- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/molecule/driver/base.py b/src/molecule/driver/base.py index e81cbf6c9d..5ead0bdd7e 100644 --- a/src/molecule/driver/base.py +++ b/src/molecule/driver/base.py @@ -271,6 +271,9 @@ def get_playbook(self, step): return p return None + def schema_file(self): + return None + def modules_dir(self): """Return path to ansible modules included with driver.""" p = os.path.join(self._path, "modules") diff --git a/src/molecule/driver/delegated.py b/src/molecule/driver/delegated.py index cabe28d51c..68c63669db 100644 --- a/src/molecule/driver/delegated.py +++ b/src/molecule/driver/delegated.py @@ -20,9 +20,11 @@ """Delegated Driver Module.""" import logging +import os from molecule import util from molecule.api import Driver +from molecule.data import __file__ as data_module LOG = logging.getLogger(__name__) @@ -253,3 +255,6 @@ def _get_instance_config(self, instance_name): def sanity_checks(self): # Note(decentral1se): Cannot implement driver specifics are unknown pass + + def schema_file(self): + return os.path.join(os.path.dirname(data_module), "driver.json") diff --git a/src/molecule/model/schema_v3.py b/src/molecule/model/schema_v3.py index 5f661bf30f..7d5b41acd1 100644 --- a/src/molecule/model/schema_v3.py +++ b/src/molecule/model/schema_v3.py @@ -19,7 +19,6 @@ # DEALINGS IN THE SOFTWARE. """Schema v3 Validation Module.""" -import importlib.resources as pkg_resources import json import logging import os @@ -27,6 +26,7 @@ from jsonschema import validate as jsonschema_validate from jsonschema.exceptions import ValidationError +from molecule import api from molecule.data import __file__ as data_module LOG = logging.getLogger(__name__) @@ -39,22 +39,18 @@ def validate(c): schema_files = [os.path.dirname(data_module) + "/molecule.json"] driver_name = c["driver"]["name"] + driver_schema_file = None + if driver_name in api.drivers(): + driver_schema_file = api.drivers()[driver_name].schema_file() - if driver_name == "delegated": - driver_schema_file = os.path.dirname(data_module) + "/driver.json" + if driver_schema_file is None: + msg = f"Driver {driver_name} does not provide a schema." + LOG.warning(msg) + elif not os.path.exists(driver_schema_file): + msg = f"Schema {driver_schema_file} for driver {driver_name} not found." + LOG.warning(msg) else: - try: - with pkg_resources.path(f"molecule_{driver_name}", "driver.json") as p: - driver_schema_file = p.as_posix() - except FileNotFoundError: - msg = f"No schema found in {driver_name} driver." - LOG.warning(msg) - except ModuleNotFoundError: - msg = f"{driver_name} driver is not installed." - LOG.warning(msg) - - if driver_schema_file: schema_files.append(driver_schema_file) for schema_file in schema_files: