Skip to content

Commit

Permalink
Remove dependency on sh library (#2917)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Oct 27, 2020
1 parent ef80ba4 commit 1cefe5c
Show file tree
Hide file tree
Showing 25 changed files with 327 additions and 352 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ jobs:
- tox_env: lint
- tox_env: docs
- tox_env: py36
PREFIX: PYTEST_REQPASS=433
PREFIX: PYTEST_REQPASS=427
- tox_env: py37
PREFIX: PYTEST_REQPASS=433
PREFIX: PYTEST_REQPASS=427
- tox_env: py38
PREFIX: PYTEST_REQPASS=433
PREFIX: PYTEST_REQPASS=427
- tox_env: py39
PREFIX: PYTEST_REQPASS=433
PREFIX: PYTEST_REQPASS=427
- tox_env: py36-devel
PREFIX: PYTEST_REQPASS=433
PREFIX: PYTEST_REQPASS=427
- tox_env: py39-devel
PREFIX: PYTEST_REQPASS=433
PREFIX: PYTEST_REQPASS=427
- tox_env: packaging
- tox_env: dockerfile
- tox_env: build-containers
Expand Down
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ repos:
additional_dependencies:
- packaging
- rich
- subprocess-tee
- repo: https://github.com/pre-commit/mirrors-pylint
rev: v2.6.0
hooks:
- id: pylint
additional_dependencies:
- ansible-base
- rich
- subprocess-tee
- testinfra
6 changes: 3 additions & 3 deletions .yamllint
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---
# Based on ansible-lint config
extends: default

ignore: |
*{{cookiecutter**

lib/molecule/cookiecutter/scenario/driver/delegated/{{cookiecutter.molecule_directory}}
lib/molecule/cookiecutter/molecule/{{cookiecutter.role_name}}
lib/molecule/cookiecutter/scenario/verifier/ansible/{{cookiecutter.molecule_directory}}
rules:
braces:
max-spaces-inside: 1
Expand Down
4 changes: 4 additions & 0 deletions lib/molecule/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@
RC_SUCCESS = 0
RC_TIMEOUT = 3
RC_SETUP_ERROR = 4 # Broken setup, like missing Ansible
RC_UNKNOWN_ERROR = (
5 # Unexpected errors for which we do not have more specific codes, yet
)


MOLECULE_HEADER = "# Molecule managed"
11 changes: 5 additions & 6 deletions lib/molecule/dependency/ansible_galaxy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import copy
import os

import sh

from molecule import logger, util
from molecule.dependency import base

Expand Down Expand Up @@ -106,10 +104,11 @@ def bake(self):
options = self.options
verbose_flag = util.verbose_flag(options)

self._sh_command = getattr(sh, self.command)
self._sh_command = self._sh_command.bake(*self.COMMANDS)
self._sh_command = self._sh_command.bake(
options, *verbose_flag, _env=self.env, _out=LOG.out, _err=LOG.error
self._sh_command = util.BakedCommand(
cmd=[self.command, *self.COMMANDS, *util.dict2args(options), *verbose_flag],
env=self.env,
stdout=LOG.out,
stderr=LOG.error,
)

def execute(self):
Expand Down
12 changes: 6 additions & 6 deletions lib/molecule/dependency/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
import os
import time

import sh

from molecule import util
from molecule import constants, util
from molecule.logger import get_logger

LOG = get_logger(__name__)
Expand Down Expand Up @@ -54,11 +52,12 @@ def execute_with_retries(self):
exception = None

try:
# print(555, self._sh_command)
util.run_command(self._sh_command, debug=self._config.debug)
msg = "Dependency completed successfully."
LOG.success(msg)
return
except sh.ErrorReturnCode:
except Exception:
pass

for counter in range(1, (self.RETRY + 1)):
Expand All @@ -75,10 +74,11 @@ def execute_with_retries(self):
msg = "Dependency completed successfully."
LOG.success(msg)
return
except sh.ErrorReturnCode as _exception:
except Exception as _exception:
exception = _exception

util.sysexit(exception.exit_code)
LOG.error(str(exception), self._sh_command)
util.sysexit(getattr(exception, "exit_code", constants.RC_UNKNOWN_ERROR))

@abc.abstractmethod
def execute(self): # pragma: no cover
Expand Down
22 changes: 4 additions & 18 deletions lib/molecule/dependency/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@
# DEALINGS IN THE SOFTWARE.
"""Shell Dependency Module."""

import shlex

import sh

from molecule import logger
from molecule.dependency import base
from molecule.util import BakedCommand

LOG = logger.get_logger(__name__)

Expand Down Expand Up @@ -86,20 +83,9 @@ def command(self):
def default_options(self):
return {}

def bake(self):
"""
Bake a ``shell`` command so it's ready to execute and returns None.
:return: None
"""
command_list = shlex.split(self.command)
command, args = command_list[0], command_list[1:]

self._sh_command = getattr(sh, command)
# Reconstruct command with remaining args.
self._sh_command = self._sh_command.bake(
args, _env=self.env, _out=LOG.out, _err=LOG.error
)
def bake(self) -> None:
"""Bake a ``shell`` command so it's ready to execute."""
self._sh_command = BakedCommand(cmd=self.command, env=self.env)

def execute(self):
if not self.enabled:
Expand Down
46 changes: 24 additions & 22 deletions lib/molecule/provisioner/ansible_playbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
# DEALINGS IN THE SOFTWARE.
"""Ansible-Playbook Provisioner Module."""

import sh

from molecule import logger, util

LOG = logger.get_logger(__name__)
Expand Down Expand Up @@ -69,23 +67,28 @@ def bake(self):
if options.get("become"):
del options["become"]

self._ansible_command = sh.ansible_playbook.bake(
options,
self._playbook,
*verbose_flag,
_cwd=self._config.scenario.directory,
_env=self._env,
_out=self._out,
_err=self._err
)

ansible_args = list(self._config.provisioner.ansible_args) + list(
self._config.ansible_args
)

if ansible_args:
if self._config.action not in ["create", "destroy"]:
self._ansible_command = self._ansible_command.bake(ansible_args)
# if ansible_args:
# if self._config.action not in ["create", "destroy"]:
# # inserts ansible_args at index 1
# self._ansible_command.cmd.extend(ansible_args)

self._ansible_command = util.BakedCommand(
cmd=[
"ansible-playbook",
*util.dict2args(options),
*util.bool2args(verbose_flag),
*ansible_args,
self._playbook, # must always go last
],
cwd=self._config.scenario.directory,
env=self._env,
stdout=self._out,
stderr=self._err,
)

def execute(self):
"""
Expand All @@ -100,13 +103,12 @@ def execute(self):
LOG.warning("Skipping, %s action has no playbook." % self._config.action)
return

try:
self._config.driver.sanity_checks()
cmd = util.run_command(self._ansible_command, debug=self._config.debug)
return cmd.stdout.decode("utf-8")
except sh.ErrorReturnCode as e:
out = e.stdout.decode("utf-8")
util.sysexit_with_message(str(out), e.exit_code)
self._config.driver.sanity_checks()
result = util.run_command(self._ansible_command, debug=self._config.debug)
if result.returncode != 0:
util.sysexit_with_message(result.stdout, result.returncode)

return result.stdout

def add_cli_arg(self, name, value):
"""
Expand Down
11 changes: 6 additions & 5 deletions lib/molecule/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@

@pytest.helpers.register
def run_command(cmd, env=os.environ, log=True):
if log:
cmd = _rebake_command(cmd, env)
if cmd.__class__.__name__ == "Command":
if log:
cmd = _rebake_command(cmd, env)

# Never let sh truncate exceptions in testing
cmd = cmd.bake(_truncate_exc=False)
# Never let sh truncate exceptions in testing
cmd = cmd.bake(_truncate_exc=False)

return util.run_command(cmd)
return util.run_command(cmd, env=env)


def _rebake_command(cmd, env, out=LOG.out, err=LOG.error):
Expand Down
Loading

0 comments on commit 1cefe5c

Please sign in to comment.