Skip to content

Commit

Permalink
Allow molecule drivers to require collections
Browse files Browse the repository at this point in the history
This change enables drivers to expose the list of required
collections they need for running.

Molecule will display required collections for each driver as part
of `molecule --version` listing.

During 'dependency' run molecule will check for presence of
required collections and install/upgrade them if needed.
  • Loading branch information
ssbarnea committed Jul 16, 2021
1 parent 142a52c commit af342a2
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Expand Up @@ -54,7 +54,7 @@ repos:
entry: mypy src/
pass_filenames: false
additional_dependencies:
- ansible-lint>=5.0.10
- ansible-lint>=5.1.1
- packaging
- enrich>=1.2.5
- subprocess-tee>=0.2.0
Expand All @@ -63,7 +63,7 @@ repos:
hooks:
- id: pylint
additional_dependencies:
- ansible-lint>=5.0.10
- ansible-lint>=5.1.1
- enrich>=1.2.5
- subprocess-tee>=0.2.0
- testinfra
1 change: 1 addition & 0 deletions src/molecule/dependency/ansible_galaxy/base.py
Expand Up @@ -111,6 +111,7 @@ def bake(self):
)

def execute(self):
super().execute()
if not self.enabled:
msg = "Skipping, dependency is disabled."
LOG.warning(msg)
Expand Down
4 changes: 4 additions & 0 deletions src/molecule/dependency/base.py
Expand Up @@ -25,6 +25,8 @@
import time
from subprocess import CalledProcessError

from ansiblelint.prerun import require_collection

from molecule import util

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -88,6 +90,8 @@ def execute(self): # pragma: no cover
:return: None
"""
for name, version in self._config.driver.required_collections.items():
require_collection(name, version)

@abc.abstractproperty
def default_options(self): # pragma: no cover
Expand Down
1 change: 1 addition & 0 deletions src/molecule/dependency/shell.py
Expand Up @@ -89,6 +89,7 @@ def bake(self) -> None:
self._sh_command = BakedCommand(cmd=self.command, env=self.env)

def execute(self):
super().execute()
if not self.enabled:
msg = "Skipping, dependency is disabled."
LOG.warning(msg)
Expand Down
6 changes: 6 additions & 0 deletions src/molecule/driver/base.py
Expand Up @@ -22,6 +22,7 @@
import abc
import inspect
import os
from typing import Dict

import pkg_resources

Expand Down Expand Up @@ -295,3 +296,8 @@ def reset(self):
by molecule, regardless the scenario name. Molecule will use metadata
like labels or tags to annotate resources allocated by it.
"""

@property
def required_collections(self) -> Dict[str, str]:
"""Return collections dict containing names and versions required."""
return {}
4 changes: 4 additions & 0 deletions src/molecule/shell.py
Expand Up @@ -64,6 +64,10 @@ def print_version(ctx, param, value):
)
for driver in drivers():
msg += f"\n [repr.attrib_name]{str(driver)}[/][dim]:[/][repr.number]{driver.version}[/][dim] from {driver.module}[/]"
if driver.required_collections:
msg += " requiring collections:"
for name, version in driver.required_collections.items():
msg += f" {name}>={version}"
console.print(msg)

ctx.exit()
Expand Down

0 comments on commit af342a2

Please sign in to comment.