Skip to content

Commit

Permalink
Use api to get driver schema file (#3898)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
apatard authored Apr 28, 2023
1 parent 7cfcf4c commit 400a3b7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/molecule/driver/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 5 additions & 0 deletions src/molecule/driver/delegated.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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")
24 changes: 10 additions & 14 deletions src/molecule/model/schema_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
# DEALINGS IN THE SOFTWARE.
"""Schema v3 Validation Module."""

import importlib.resources as pkg_resources
import json
import logging
import os

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__)
Expand All @@ -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:
Expand Down

0 comments on commit 400a3b7

Please sign in to comment.