Skip to content

Commit 400a3b7

Browse files
authored
Use api to get driver schema file (#3898)
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]>
1 parent 7cfcf4c commit 400a3b7

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

src/molecule/driver/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ def get_playbook(self, step):
271271
return p
272272
return None
273273

274+
def schema_file(self):
275+
return None
276+
274277
def modules_dir(self):
275278
"""Return path to ansible modules included with driver."""
276279
p = os.path.join(self._path, "modules")

src/molecule/driver/delegated.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
"""Delegated Driver Module."""
2121

2222
import logging
23+
import os
2324

2425
from molecule import util
2526
from molecule.api import Driver
27+
from molecule.data import __file__ as data_module
2628

2729
LOG = logging.getLogger(__name__)
2830

@@ -253,3 +255,6 @@ def _get_instance_config(self, instance_name):
253255
def sanity_checks(self):
254256
# Note(decentral1se): Cannot implement driver specifics are unknown
255257
pass
258+
259+
def schema_file(self):
260+
return os.path.join(os.path.dirname(data_module), "driver.json")

src/molecule/model/schema_v3.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
# DEALINGS IN THE SOFTWARE.
2020
"""Schema v3 Validation Module."""
2121

22-
import importlib.resources as pkg_resources
2322
import json
2423
import logging
2524
import os
2625

2726
from jsonschema import validate as jsonschema_validate
2827
from jsonschema.exceptions import ValidationError
2928

29+
from molecule import api
3030
from molecule.data import __file__ as data_module
3131

3232
LOG = logging.getLogger(__name__)
@@ -39,22 +39,18 @@ def validate(c):
3939

4040
schema_files = [os.path.dirname(data_module) + "/molecule.json"]
4141
driver_name = c["driver"]["name"]
42+
4243
driver_schema_file = None
44+
if driver_name in api.drivers():
45+
driver_schema_file = api.drivers()[driver_name].schema_file()
4346

44-
if driver_name == "delegated":
45-
driver_schema_file = os.path.dirname(data_module) + "/driver.json"
47+
if driver_schema_file is None:
48+
msg = f"Driver {driver_name} does not provide a schema."
49+
LOG.warning(msg)
50+
elif not os.path.exists(driver_schema_file):
51+
msg = f"Schema {driver_schema_file} for driver {driver_name} not found."
52+
LOG.warning(msg)
4653
else:
47-
try:
48-
with pkg_resources.path(f"molecule_{driver_name}", "driver.json") as p:
49-
driver_schema_file = p.as_posix()
50-
except FileNotFoundError:
51-
msg = f"No schema found in {driver_name} driver."
52-
LOG.warning(msg)
53-
except ModuleNotFoundError:
54-
msg = f"{driver_name} driver is not installed."
55-
LOG.warning(msg)
56-
57-
if driver_schema_file:
5854
schema_files.append(driver_schema_file)
5955

6056
for schema_file in schema_files:

0 commit comments

Comments
 (0)